时间:2021-04-15 python教程 查看: 801
监控Linux服务器嘛,脚本逻辑基本上是用os.popen模块,然后把获取到的结果通过split切分成一个list,再拿目标list值和我阈值对比,超过就邮件报警;
邮件是通过Linux的mailx发出去的,可自行搜索安装该模块,关键字:“Linux使用mailx发邮件”,脚本如下:
#!/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
#!/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
#!/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,磁盘空间,内存,超过邮件报警的详细内容,更多关于python监控服务器的资料请关注python博客其它相关文章!