用python监控服务器的cpu,磁盘空间,内存,超过邮件报警
作者:小胡要加油 时间:2023-04-22 07:13:38
监控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
来源:https://www.cnblogs.com/hydd/p/14338361.html
标签:python,监控,服务器,邮件报警
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
python必学知识之文件操作(建议收藏)
2021-10-01 16:28:25
![](https://img.aspxhome.com/file/2023/8/66448_0s.png)
加载 Javascript 最佳实践
2011-01-16 18:29:00
![](https://img.aspxhome.com/file/UploadPic/20111/16/03-80s.jpg)
简单了解Python3里的一些新特性
2022-09-22 21:32:23
php 多继承的几种常见实现方法示例
2023-11-22 19:03:19
基于Python实现计算纳什均衡的示例详解
2022-03-03 12:44:27
![](https://img.aspxhome.com/file/2023/7/70057_0s.png)
数字人组件反写[asp组件开发实例1]
2009-06-09 13:10:00
![](https://img.aspxhome.com/file/UploadPic/20096/9/vb-34s.gif)
Sun正式发布MySQL 5.1版 简化数据库应用
2008-12-11 15:15:00
趣用文化元素
2009-09-03 11:53:00
![](https://img.aspxhome.com/file/UploadPic/20099/3/0909-using_culture_elements-01-12s.jpg)
在python中如何建立一个自己的包
2022-12-27 12:42:25
![](https://img.aspxhome.com/file/2023/5/72185_0s.png)
javascript新闻图片轮换类
2009-01-09 12:57:00
使用python flask框架开发图片上传接口的案例详解
2021-12-26 05:54:30
![](https://img.aspxhome.com/file/2023/6/64736_0s.png)
用途相似的标签
2008-05-23 13:11:00
通过Python实现控制手机详解
2021-04-21 12:10:37
![](https://img.aspxhome.com/file/2023/2/69552_0s.png)
Python基础之dict和set的使用详解
2022-01-16 04:15:08
![](https://img.aspxhome.com/file/2023/8/65068_0s.png)
初学者必读:提高SQL执行效率的几点建议
2009-05-07 13:52:00
基于python2.7实现图形密码生成器的实例代码
2021-01-21 15:20:50
MySQL数据库的23个特别注意事项
2010-08-08 14:43:00
使用ASP实现广告代理
2010-05-27 12:15:00
眼未动,心已动【碳酸饮料会】
2009-09-01 19:32:00
![](https://img.aspxhome.com/file/UploadPic/20099/1/t1qlbxxobrxxxxxxxx-455-221-73s.jpg)
django-crontab实现服务端的定时任务的示例代码
2021-02-13 05:15:06