Board logo

标题: CentOS8使用psutil+新浪邮箱使用465端口发送邮件 [打印本页]

作者: admin    时间: 2022-3-20 00:48     标题: CentOS8使用psutil+新浪邮箱使用465端口发送邮件

实验中,操作系统的版本信息: [root@centos8 ~]# cat /etc/redhat-release CentOS Linux release 8.2.2004 (Core) [root@centos8 ~]# [root@centos8 ~]# uname -r 4.18.0-193.el8.x86_64 Python的版本信息: [root@centos8 ~]# python3 --version Python 3.6.8 服务器连接公网安装第三方库(psutil): [root@centos8 ~]# pip3 install psutil -i http://mirrors.aliyun.com/pypi/simple --trusted-host=mirrors.aliyun.com Looking in indexes: http://mirrors.aliyun.com/pypi/simple Collecting psutil Downloading http://mirrors.aliyun.com/pypi/packages/47/b6/ea8a7728f096a597f0032564e8013b705aa992a0990becd773dcc4d7b4a7/psutil-5.9.0.tar.gz (478kB) 100% |████████████████████████████████| 481kB 888kB/s Installing collected packages: psutil Running setup.py install for psutil ... done Successfully installed psutil-5.9.0 [root@centos8 ~]# 列出当前环境所有已经安装的第三方库的名称和其版本号: [root@centos8 ~]# pip3 freeze psutil==5.9.0 [root@centos8 ~]# 查看本机的CPU使用率、内存使用率、分区使用率等等的脚本: [root@centos8 ~]# cat check.py #coding=utf-8 import psutil CPU_use_percent = psutil.cpu_percent(interval=2) print(CPU_use_percent) print(type(CPU_use_percent)) print(f"CPU使用率: {CPU_use_percent}%") print("-" * 10) Mem_total = psutil.virtual_memory().total Mem_total = round(Mem_total / 1024 / 1024,2) print(f"总内存量: {Mem_total}MB") Mem_used = psutil.virtual_memory().used Mem_used = round(Mem_used / 1024 / 1024,2) print(f"已使用内存量: {Mem_used}MB") print("-" * 10) Mem_use_percent = psutil.virtual_memory().percent print(Mem_use_percent) print(f"内存使用率: {Mem_use_percent}%") print("-" * 10) Root_total = psutil.disk_usage("/")[0] Root_total = round(Root_total / 1024 / 1024 /1024,2) print(f"根分区总大小:{Root_total}GB") Root_used = psutil.disk_usage("/")[1] Root_used = round(Root_used / 1024 / 1024 / 1024,2) print(f"根分区已使用量: {Root_used}GB") Root_free = psutil.disk_usage("/")[2] Root_free = round(Root_free / 1024 / 1024 / 1024,2) print(f"根分区空闲量: {Root_free}GB") Root_used_percent = psutil.disk_usage("/")[3] print(f"根分区使用率: {Root_used_percent}%") print("-" * 10) Run_total = psutil.disk_usage("/run")[0] Run_total = round(Run_total / 1024 / 1024 /1024,2) print(f"分区/run的总大小:{Run_total}GB") Run_used_percent = psutil.disk_usage("/run")[3] print(f"分区/run的使用率:{Run_used_percent}%") 设置脚本权限: [root@centos8 ~]# chmod a+x check.py 运行脚本的结果: [root@centos8 ~]# python3 check.py 2.0 CPU使用率: 2.0% ---------- 总内存量: 1800.62MB 已使用内存量: 538.05MB ---------- 41.0 内存使用率: 41.0% ---------- 根分区总大小:75.76GB 根分区已使用量: 3.47GB 根分区空闲量: 72.29GB 根分区使用率: 4.6% ---------- 分区/run的总大小:0.88GB 分区/run的使用率:3.6% [root@centos8 ~]# 假如计算结果不是很精确,可以手动修改一下: CPU_use_percent = CPU_use_percent + 20 图片1.png 再次运行脚本的结果: [root@centos8 ~]# python3 check.py 21.5 CPU使用率: 21.5% ---------- 总内存量: 1800.62MB 已使用内存量: 538.04MB ---------- 41.0 内存使用率: 41.0% ---------- 根分区总大小:75.76GB 根分区已使用量: 3.47GB 根分区空闲量: 72.29GB 根分区使用率: 4.6% ---------- 分区/run的总大小:0.88GB 分区/run的使用率:3.6% [root@centos8 ~]# 备注:CPU使用率、内存使用率的值是有波动的。 ############ 新浪邮箱使用465端口发送告警邮件: [root@centos8 ~]# cat xx.py #coding=utf-8 import psutil import smtplib from email.mime.text import MIMEText from email.utils import formataddr my_sender = 'j2270168881@sina.com' #发件人邮箱账号 my_pass = '9157d00e9886890e' #发件人邮箱的授权码;新浪邮箱要使用授权码 my_name = 'zhuohua' #发件人邮箱昵称 receiver = '2270168881@qq.com' #收件人邮箱账号 receiver_name = 'Python' #收件人昵称 #本机主机名 hostname = 'centos8.zhuohua.store' #本机IP地址 ip = '192.168.168.154' def func_mail(Key_1,Title_1): try: Message_1 = MIMEText(Key_1,'plain','utf-8') Message_1['From'] = formataddr([my_name,my_sender]) Message_1['To'] = formataddr([receiver_name,receiver]) Message_1['Subject'] = Title_1 server=smtplib.SMTP_SSL("smtp.sina.com",465) #发件人邮箱的SMTP服务器,端口是TCP 465 server.login(my_sender, my_pass) server.sendmail(my_sender,[receiver,],Message_1.as_string()) server.quit() except Exception as e: print (f"发送邮件失败,原因: {e}") else: print("邮件发送成功") if __name__ == '__main__': #获取本机的CPU使用率 CPU_use_percent = psutil.cpu_percent(interval=2) CPU_use_percent = CPU_use_percent + 20 #手动修改CPU使用率的值 print(f"CPU使用率: {CPU_use_percent}") Key_1 = f"主机{hostname}({ip})的CPU使用率达{CPU_use_percent}%" Title_1 = "CPU使用率告警" if CPU_use_percent > 10.5: #假如CPU使用率大于10.5%,就发送告警邮件 func_mail(Key_1,Title_1) print("-" * 10) #获取本机的内存使用率 Mem_use_percent = psutil.virtual_memory().percent print(f"内存使用率: {Mem_use_percent}") Key_1 = f"主机{hostname}({ip})的内存使用率达{Mem_use_percent}%" Title_1 = "内存使用率告警" if Mem_use_percent > 15: #假如内存使用率大于15%,就发送告警邮件 func_mail(Key_1,Title_1) print("-" * 10) #获取本机的分区/run的使用率 Run_used_percent = psutil.disk_usage("/run")[3] print(f"分区/run的使用率:{Run_used_percent}%") Key_1 = f"主机{hostname}({ip})的分区/run的使用率达{Run_used_percent}%" Title_1 = "分区/run的使用率告警" if Run_used_percent > 3: #假如分区/run的使用率大于3%,就发送告警邮件 func_mail(Key_1,Title_1) 设置脚本权限: [root@centos8 ~]# chmod a+x xx.py 脚本运行的结果: [root@centos8 ~]# python3 xx.py CPU使用率: 22.0 邮件发送成功 ---------- 内存使用率: 41.4 邮件发送成功 ---------- 分区/run的使用率:3.6% 邮件发送成功 [root@centos8 ~]# QQ邮箱收到的告警邮件:(每触发一条告警条件,新浪邮箱就发送一封告警邮件) 图片2.png 图片3.png 图片4.png 备注:新浪邮箱发送这种邮件后,在“已发送”里是没有记录的。 相关文章: CentOS8安装Python3 Linux使用第三方库psutil CentOS8_使用Docker安装Python3 Python3使用126邮箱的465端口发送邮件 Python3使用新浪邮箱的25端口发送邮件 使用Shell脚本监控本机的CPU、内存、分区的使用率

图片附件: 图片1.png (2022-3-20 00:45, 15.26 KB) / 下载次数 90
http://blog.zhuohua.store/attachment.php?aid=19225&k=7d6a51c1ff54a3482820543cad88dd14&t=1714314937&sid=iF81w9



图片附件: 图片2.png (2022-3-20 00:48, 33.06 KB) / 下载次数 99
http://blog.zhuohua.store/attachment.php?aid=19226&k=ca31517d1fb27708013b76073393c389&t=1714314937&sid=iF81w9



图片附件: 图片3.png (2022-3-20 00:48, 32.21 KB) / 下载次数 90
http://blog.zhuohua.store/attachment.php?aid=19227&k=408db845cb0c59f5c9ae0c9bbd174f5b&t=1714314937&sid=iF81w9



图片附件: 图片4.png (2022-3-20 00:48, 35.36 KB) / 下载次数 84
http://blog.zhuohua.store/attachment.php?aid=19228&k=2d7f4ee6f6b9db1c6f379f6155f027bd&t=1714314937&sid=iF81w9






欢迎光临 blog.zhuohua.store (http://blog.zhuohua.store/) Powered by Discuz! 7.2