监控Linux服务器嘛,脚本逻辑基本上是用os.popen模块,然后把获取到的结果通过split切分成一个list,再拿目标list值和我阈值对比,超过就邮件报警;
邮件是通过Linux的mailx发出去的,可自行搜索安装该模块,关键字:“Linux使用mailx发邮件”,脚本如下:
一、cpu ideal值,不小于20%
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
#!/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%
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
#!/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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
#!/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监控服务器的资料请关注服务器之家其它相关文章!
原文链接:https://www.cnblogs.com/hydd/p/14338361.html