基于python3监控服务器状态进行邮件报警
作者:南方小白 时间:2022-05-05 05:25:39
在正式的生产环境中,我们常常会需要监控服务器的状态,以保证公司整个业务的正常运转,常常我们会用到像nagios、zabbix这类工具进行实时监控,那么用python我们怎么进行监控呢?这里我们利用了python3调用psutil和yagmail这两个模块进行监测服务器的内存、磁盘、cpu状态(以及我们监控apache运行状态用到的socket模块)
监控内存、磁盘、cpu
import psutil
def mem():
mem = psutil.virtual_memory()
return mem[2]
def disk():
disk = psutil.disk_usage('/root')
return disk[3]
def cpu():
cpu = psutil.cpu_percent(60)
return cpu
a = mem()
b = disk()
c = cpu()
import yagmail
yag = yagmail.SMTP(user = 'xxx@qq.com',
password = 'cxboscylplifgadd', host =
'smtp.qq.com')
if a > 80:
yag.send(to = 'xxx@qq.com', subject =
'内存报警', contents = '运行内存超过80%请尽快处理',
cc = 'xxx@qq.com')
yag.close()
if b > 70:
yag.send(to = 'xxx@qq.com', subject =
'系统磁盘报警', contents = '系统磁盘超过70%请尽快处理',
cc = 'xxx@qq.com')
yag.close()
if c > 90:
yag.send(to = 'xxx@qq.com', subject =
'cpu报警', contents = 'cpu超过90%请尽快处理',
cc = 'xxx@qq.com')
yag.close()
监控网页服务(报警)
import socket, yagmail
hosts = ['192.168.8.137:80',
'192.168.8.15:88'
]
socket.setdefaulttimeout(5)
for host in hosts:
ip = host.split(':')[0]
port = host.split(':')[1]
server = socket.socket()
res = server.connect_ex((ip, int(port)))# 返回值为0代表ok,
不为0代表失败
if res == 0:
pass
else :
yag = yagmail.SMTP(user = 'xxx@qq.com',
password = 'cxboscylplifgadd', host =
'smtp.qq.com')
yag.send(to = 'xxx@qq.com', subject =
'警告', contents = '%s httpd服务断开请检测' %
ip, cc = 'xx@163.com')
yag.close()
这样我们的监控脚本python脚本就完成了,现在就只需要拿到服务器执行定时任务就行了。现在我们来检测一下:
编辑一下定时任务
我们将写上面两个脚本写入1.py和2.py这两个文件中,为了方便验证我们将磁盘判断值改为30,实际上我们已用到了40%,这样就能直接报警了,两台服务器的Apache端口都为80且均为正常运行状态。
我们等待一分钟。。。。。。。
我们收到两封报警邮件,是不是很方便呢?
来源:https://www.cnblogs.com/lxwei0101/p/11701299.html
标签:python,3,监控,服务器,邮件,报警
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
模拟下拉菜单[兼容IE系列以及火狐]
2009-12-13 10:23:00
![](https://img.aspxhome.com/file/UploadPic/200912/13/menu-37s.gif)
[翻译]标记语言和样式手册 Chapter 9 精简标签
2008-02-01 09:55:00
![](https://img.aspxhome.com/file/UploadPic/20082/1/20082110109651s.jpg)
Python有序查找算法之二分法实例分析
2023-04-09 00:02:37
![](https://img.aspxhome.com/file/2023/8/85378_0s.jpg)
Javascript 闭包[翻译]
2008-09-28 20:59:00
如何让12px以下的文字最佳显示
2008-01-24 18:39:00
对Python3使运行暂停的方法详解
2021-02-05 22:55:21
![](https://img.aspxhome.com/file/2023/8/82738_0s.jpg)
ASP sql:rs.open语句详细说明
2008-03-17 11:30:00
分享SQL Server删除重复行的6个方法
2011-09-30 11:33:05
提高MySQL数据库查询效率的几个技巧
2009-01-19 12:52:00
python自动化测试通过日志3分钟定位bug
2021-09-12 10:11:19
![](https://img.aspxhome.com/file/2023/4/90844_0s.png)
python等间距取值方式
2022-03-15 23:19:26
一个修改Oracle数据库用户密码的小诀窍
2009-09-30 15:29:00
python tkinter库实现气泡屏保和锁屏
2021-09-17 22:37:57
![](https://img.aspxhome.com/file/2023/2/93602_0s.jpg)
服务器安装MySQL教程及注意事项
2008-11-11 12:12:00
PHP Document 代码注释规范
2023-11-14 11:50:54
python opencv画局部放大图实例教程
2023-12-01 09:55:55
![](https://img.aspxhome.com/file/2023/9/93999_0s.jpg)
轻松处理Dreamweaver段落缩进
2007-11-17 07:53:00
python GUI计算器的实现
2021-11-30 01:00:53
![](https://img.aspxhome.com/file/2023/8/87948_0s.png)
python计算n的阶乘的方法代码
2023-08-20 07:33:00
Python函数any()和all()的用法及区别介绍
2021-11-23 04:43:16