溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Python中怎么創(chuàng)建一個系統(tǒng)監(jiān)控程序

發(fā)布時間:2021-07-28 16:54:36 來源:億速云 閱讀:263 作者:Leah 欄目:編程語言

這期內(nèi)容當(dāng)中小編將會給大家?guī)碛嘘P(guān)Python中怎么創(chuàng)建一個系統(tǒng)監(jiān)控程序,文章內(nèi)容豐富且以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。

psutil(進程和系統(tǒng)實用程序)是一個跨平臺的庫,用于檢索 Python中有關(guān)正在運行的進程和系統(tǒng)利用率(CPU,內(nèi)存,磁盤,網(wǎng)絡(luò),傳感器)的信息。它主要用于系統(tǒng)監(jiān)視,概要分析和限制進程資源以及管理正在運行的進程。

1、安裝psutil

pip3 install psutil    # python2用戶直接使用pip install psutil

2、創(chuàng)建get_system_info.py文件,獲取系統(tǒng)狀態(tài)

 1 import psutil as p

 2

 3

 4 def memory(): # 獲取內(nèi)存信息

 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

# 在此只使用了博主所需的相關(guān)功能,詳細(xì)說明見 https://pypi.org/project/psutil/

3、創(chuàng)建郵件發(fā)送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         <h2>%s</h2>

18         <p>%s</p>

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): # 發(fā)送郵件

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! 無法發(fā)送郵件!!!" + 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="系統(tǒng)狀態(tài)異常", receiver="*******@***.***")

status_d = 0

status_m = 0

while True: # 實時檢測系統(tǒng)狀態(tài),會占用一定的系統(tǒng)資源

    memory = gsi.memory()

    disk, capacity = gsi.disk()

    if memory >= 80 and status_m == 0:

        status_m = 1

        data = "當(dāng)前內(nèi)存占用率為"+str(memory)+"%,建議清理或擴充內(nèi)存!"

        email.send(code=data, title="系統(tǒng)狀態(tài)異常", receiver="*******@***.***")

    elif memory < 80:

        status_m = 0

    if disk >= 80 and status_d == 0:

        status_d = 1

        data = "當(dāng)前硬盤已使用"+str(disk)+"%,剩余"+str(round(capacity/1024/1024/1024, 2))+"GB,請及時擴充!"

                                       if disk >=貨幣符號https://www.gendan5.com/currencyworld.html

        email.send(receiver="*******@***.***", title="系統(tǒng)狀態(tài)異常", code=data)

    elif disk < 80:

        status_d = 0

    if memory >= 90 and status_m == 1:

        status_m = 2

        data = "當(dāng)前內(nèi)存占用率為"+str(memory)+"%,建議清理或擴充內(nèi)存!"

        email.send(code=data, title="系統(tǒng)狀態(tài)異常", receiver="*******@***.***")

    elif memory < 90:

        status_m = 1

    if disk >= 90 and status_d == 1:

        status_d = 2

        data = "當(dāng)前硬盤已使用"+str(disk)+"%,剩余"+str(round(capacity/1024/1024/1024, 2))+"GB,請及時擴充!"

        email.send(receiver="*******@***.***", title="系統(tǒng)狀態(tài)異常", code=data)

    elif disk < 90:

        status_d = 1

上述就是小編為大家分享的Python中怎么創(chuàng)建一個系統(tǒng)監(jiān)控程序了,如果剛好有類似的疑惑,不妨參照上述分析進行理解。如果想知道更多相關(guān)知識,歡迎關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI