gpt4 book ai didi

用python监控服务器的cpu,磁盘空间,内存,超过邮件报警

转载 作者:qq735679552 更新时间:2022-09-29 22:32:09 27 4
gpt4 key购买 nike

CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.

这篇CFSDN的博客文章用python监控服务器的cpu,磁盘空间,内存,超过邮件报警由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.

  监控Linux服务器嘛,脚本逻辑基本上是用os.popen模块,然后把获取到的结果通过split切分成一个list,再拿目标list值和我阈值对比,超过就邮件报警; 。

  邮件是通过Linux的mailx发出去的,可自行搜索安装该模块,关键字:“Linux使用mailx发邮件”,脚本如下:  。

1、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

2、磁盘空间,不大于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

3、内存利用率,不低于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 。

最后此篇关于用python监控服务器的cpu,磁盘空间,内存,超过邮件报警的文章就讲到这里了,如果你想了解更多关于用python监控服务器的cpu,磁盘空间,内存,超过邮件报警的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com