用python监控服务器的cpu,磁盘空间,内存,超过邮件报警
监控Linux服务器嘛,脚本逻辑基本上是用os.popen模块,然后把获取到的结果通过split切分成一个list,再拿目标list值和我阈值对比,超过就邮件报警;
邮件是通过Linux的mailx发出去的,可自行搜索安装该模块,关键字:“Linux使用mailx发邮件”,脚本如下:
一、cpu ideal值,不小于20%
#!/usr/bin/python
# -*- coding: utf-8 -*-
import datetime
import os
f = os.popen('vmstat').readlines()
cpu_ideall = str(f).split()[-3]
if int(cpuideall) > 20:
mail_content = "echo 'ip:IP地址(vmstat)' | mailx -s '[Warning!]CPU ideal below 20%, please check!' 收件邮箱"
os.popen(mail_content)
else:
pass
二、磁盘空间,不大于95%
#!/usr/bin/python
# -*- coding: utf-8 -*-
import datetime
import os
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])
print s
i = 0
while i < len(s):
if int(s[i]) > 95:
mail_content = "echo 'ip:ip地址(df -lh)' | mailx -s '[Warning!]Disk above 95%, please check!' 收件邮件"
os.popen(mail_content)
else:
pass
i = i + 1
三、内存利用率,不低于200
#!/usr/bin/python
# -*- coding: utf-8 -*-
import datetime
import os
f = os.popen('free -m').readlines()
memm = str(f).split()[10]
if int(memm) < 200:
mail_content = "echo 'ip:ip地址(free -m)' | mailx -s '[Warning!]MEM below 200, please check!' 收件邮箱"
os.popen(mail_content)
else:
pass
以上是 用python监控服务器的cpu,磁盘空间,内存,超过邮件报警 的全部内容, 来源链接: utcz.com/z/327696.html