用python监控服务器的cpu,磁盘空间,内存,超过邮件报警
监控Linux服务器嘛,脚本逻辑基本上是用os.popen模块,然后把获取到的结果通过split切分成一个list,再拿目标list值和我阈值对比,超过就邮件报警;
邮件是通过Linux的mailx发出去的,可自行搜索安装该模块,关键字:“Linux使用mailx发邮件”,脚本如下:
一、cpuideal值,不小于20%
#!/usr/bin/python
#-*-coding:utf-8-*-
importdatetime
importos
f=os.popen('vmstat').readlines()
cpu_ideall=str(f).split()[-3]
ifint(cpuideall)>20:
mail_content="echo'ip:IP地址(vmstat)'|mailx-s'[Warning!]CPUidealbelow20%,pleasecheck!'收件邮箱"
os.popen(mail_content)
else:
pass
二、磁盘空间,不大于95%
#!/usr/bin/python
#-*-coding:utf-8-*-
importdatetime
importos
f=os.popen('df-lh').readlines()
s=[]
s.append(str(f).split()[11].split('%')[0])
s.append(str(f).split()[-8].split('%')[0])
s.append(str(f).split()[-2].split('%')[0])
prints
i=0
whilei95:
mail_content="echo'ip:ip地址(df-lh)'|mailx-s'[Warning!]Diskabove95%,pleasecheck!'收件邮件"
os.popen(mail_content)
else:
pass
i=i+1
三、内存利用率,不低于200
#!/usr/bin/python
#-*-coding:utf-8-*-
importdatetime
importos
f=os.popen('free-m').readlines()
memm=str(f).split()[10]
ifint(memm)<200:
mail_content="echo'ip:ip地址(free-m)'|mailx-s'[Warning!]MEMbelow200,pleasecheck!'收件邮箱"
os.popen(mail_content)
else:
pass
以上就是用python监控服务器的cpu,磁盘空间,内存,超过邮件报警的详细内容,更多关于python监控服务器的资料请关注毛票票其它相关文章!