十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
这期内容当中小编将会给大家带来有关Python中怎么创建一个系统监控程序,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。
让客户满意是我们工作的目标,不断超越客户的期望值来自于我们对这个行业的热爱。我们立志把好的技术通过有效、简单的方式提供给客户,将通过不懈努力成为客户在信息化领域值得信任、有价值的长期合作伙伴,公司提供的服务项目有:域名注册、虚拟主机、营销软件、网站建设、博乐网站维护、网站推广。
psutil(进程和系统实用程序)是一个跨平台的库,用于检索 Python中有关正在运行的进程和系统利用率(CPU,内存,磁盘,网络,传感器)的信息。它主要用于系统监视,概要分析和限制进程资源以及管理正在运行的进程。
1、安装psutil
pip3 install psutil # python2用户直接使用pip install psutil
2、创建get_system_info.py文件,获取系统状态
1 import psutil as p
2
3
4 def memory(): # 获取内存信息
5 info = p.virtual_memory()
6 return info[2]
7
8
9 def disk(): # 获取磁盘使用情况
10 info = p.disk_usage('/')
11 return info[-1], info[-2]
12
13
14 def cpu(): # 获取CPU使用率
15 info = p.cpu_percent(1)
16 return info
# 在此只使用了博主所需的相关功能,详细说明见 https://pypi.org/project/psutil/
3、创建邮件发送send_Email.py文件
1 import smtplib
2 from email.header import Header
3 from email.mime.text import MIMEText
4 from email.utils import formataddr
5
6
7 class send_Email():
8 def __init__(self, mail_host, mail_port, mail_user, mail_pass, sender):
9 self.mail_host = mail_host
10 self.mail_port = mail_port
11 self.mail_user = mail_user
12 self.mail_pass = mail_pass
13 self.sender = sender
14
15 def make_mail(self, title, code, receiver): # 生成邮件
16 mail_msg = """
17
18
%s
19 """ % (title, code)
20 self.message = MIMEText(mail_msg, "html", "utf-8")
21 self.message['From'] = formataddr(["XX", self.sender])
22 self.message['To'] = formataddr(["User", receiver])
23 subject = title
24 self.message['Subject'] = Header(subject, "utf-8")
25
26 def send(self, receiver, title, code): # 发送邮件
27 status = 0
28 self.make_mail(title, code, receiver)
29 try:
30 self.smtpObj = smtplib.SMTP_SSL(self.mail_host, self.mail_port)
31 # print(1)
32 self.smtpObj.login(self.mail_user, self.mail_pass)
33 # print(2)
34 self.smtpObj.sendmail(self.sender, [receiver], self.message.as_string())
35 self.smtpObj.quit()
36 status = 1
37 except smtplib.SMTPException as e:
38 print("Error! 无法发送邮件!!!" + e)
39 return status
4、整合代码
from send_Email import send_Email
import get_system_info as gsi
email = send_Email("smtp.163.com", 465, "*******@***.com", "**********", "********@***.com") # 初始化
memory = gsi.memory()
disk, capacity = gsi.disk()
info = str(memory)+" "+str(disk)+" "+str(round(capacity/1024/1024/1024, 2))
email.send(code=info, title="系统状态异常", receiver="*******@***.***")
status_d = 0
status_m = 0
while True: # 实时检测系统状态,会占用一定的系统资源
memory = gsi.memory()
disk, capacity = gsi.disk()
if memory >= 80 and status_m == 0:
status_m = 1
data = "当前内存占用率为"+str(memory)+"%,建议清理或扩充内存!"
email.send(code=data, title="系统状态异常", receiver="*******@***.***")
elif memory < 80:
status_m = 0
if disk >= 80 and status_d == 0:
status_d = 1
data = "当前硬盘已使用"+str(disk)+"%,剩余"+str(round(capacity/1024/1024/1024, 2))+"GB,请及时扩充!"
if disk >=货币符号https://www.gendan5.com/currencyworld.html
email.send(receiver="*******@***.***", title="系统状态异常", code=data)
elif disk < 80:
status_d = 0
if memory >= 90 and status_m == 1:
status_m = 2
data = "当前内存占用率为"+str(memory)+"%,建议清理或扩充内存!"
email.send(code=data, title="系统状态异常", receiver="*******@***.***")
elif memory < 90:
status_m = 1
if disk >= 90 and status_d == 1:
status_d = 2
data = "当前硬盘已使用"+str(disk)+"%,剩余"+str(round(capacity/1024/1024/1024, 2))+"GB,请及时扩充!"
email.send(receiver="*******@***.***", title="系统状态异常", code=data)
elif disk < 90:
status_d = 1
上述就是小编为大家分享的Python中怎么创建一个系统监控程序了,如果刚好有类似的疑惑,不妨参照上述分析进行理解。如果想知道更多相关知识,欢迎关注创新互联行业资讯频道。