首页 > 系统相关 >监控centos7系统CPU,内存,磁盘空间

监控centos7系统CPU,内存,磁盘空间

时间:2023-05-17 17:11:52浏览次数:35  
标签:smtp CPU centos7 磁盘空间 memory usage 使用率 email msg

[cat monitor2.py
#!/usr/bin/env python
#coding=UTF-8

import datetime
import socket
import psutil
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart

# 全局变量
current_time = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") # 当前时间
server_ip = socket.gethostbyname(socket.gethostname()) # 服务器IP

# 监控CPU资源使用率
def check_cpu_usage():
cpu_usage = psutil.cpu_percent(interval=1)
if cpu_usage > 80:
send_email("CPU资源使用率过高", "当前CPU资源使用率为 {}%".format(cpu_usage))

# 监控内存资源使用率
def check_memory_usage():
memory = psutil.virtual_memory()
memory_usage = memory.percent
if memory_usage > 80:
send_email("内存资源使用率过高", "当前内存资源使用率为 {}%".format(memory_usage))

# 监控磁盘资源使用率
def check_disk_usage():
partitions = psutil.disk_partitions()
for partition in partitions:
disk_usage = psutil.disk_usage(partition.mountpoint).percent
if disk_usage > 80:
send_email("磁盘资源使用率过高", "磁盘 {} 的使用率为 {}%".format(partition.mountpoint, disk_usage))

# 发送邮件
def send_email(subject, message):
email_from = "" # 发件人邮箱
#email_to = "" # 收件人邮箱
email_to = [""] # 收件人邮箱
smtp_server = "smtp.exmail.qq.com" # SMTP服务器地址
smtp_port = 587 # SMTP服务器端口号
smtp_username = "" # SMTP服务器用户名
smtp_password = "" # SMTP服务器密码


# 构造邮件内容
message_with_info = """\
当前时间:{}
服务器IP:{}
告警内容:{}
""".format(current_time, server_ip, message)

#subject_with_aliyun = "[阿里云][{}][当前时间:{}][服务器IP:{}][告警内容:{}]".format(subject,current_time,server_ip,message)
subject_with_aliyun = "[阿里云][{}]".format(subject)
#msg = MIMEMultipart()
#msg = MIMEText(message, "plain")
msg = MIMEText(message_with_info, "plain", "utf-8") # 使用utf-8编码
msg["From"] = email_from
#msg["To"] = email_to
msg["To"] = ','.join(email_to)
#msg["Subject"] = subject
msg["Subject"] = subject_with_aliyun


#msg.attach(MIMEText(message, "plain"))

try:
smtp_obj = smtplib.SMTP(smtp_server, smtp_port)
smtp_obj.starttls()
smtp_obj.login(smtp_username, smtp_password)
smtp_obj.sendmail(email_from, email_to, msg.as_string())
smtp_obj.quit()
print("邮件发送成功")
except smtplib.SMTPException as e:
print("邮件发送失败:", e)

# 主程序
if __name__ == "__main__":
check_cpu_usage()
check_memory_usage()
check_disk_usage()

标签:smtp,CPU,centos7,磁盘空间,memory,usage,使用率,email,msg
From: https://www.cnblogs.com/gaoyanbing/p/17409338.html

相关文章

  • 【Python】Centos7安装dirsearch
    一、升级Openssl1.1.1 1、官网下载源码:https://www.openssl.org/2、安装#./config--prefix=/datas/soft/openssl-1.1.1no-zlib#make#makeinstall3、新版配置#ln-s/datas/soft/openssl-1.1.1/include/openssl/usr/include/openssl#ln-s/datas/soft/openss......
  • CPU架构初级概念
    一般常见的CPU架构包括:x86:最常见的桌面和服务器CPU架构,如Intel的x86系列和AMD的x86-64(amd64)架构。ARM:一种广泛应用于移动设备和嵌入式系统的CPU架构,如ARMCortex系列。MIPS:一种用于嵌入式系统和网络设备的CPU架构,曾经在早期的路由器和嵌入式设备中非常常见。Pow......
  • 关于centos7操作系统RX-DRP值每隔30s加1问题
    现象如下图所示:RX-DRP每隔30s,自动+1原因分析CentOS7系列操作系统在处理LLDP等不能直接解析的二层数据帧的过程中,会将ifconfigeth0或者netstat-i等输出信息中的rx_drop值增加。其主要特点为每隔30s(上层交换机LLDP数据帧发送频率)加1、但是采用tcpdump等软件抓包过程的过程中rx......
  • Centos7安装搭建FTP服务器(最简便方法)
    简介:vsftpd是“verysecureFTPdaemon”的缩写,安全性是它的一个最大的特点。vsftpd是一个UNIX类操作系统上运行的服务器的名字,它可以运行在诸如Linux、BSD、Solaris、HP-UNIX等系统上面,是一个完全免费的、开放源代码的ftp服务器软件,支持很多其他的FTP服务器所不支持的......
  • centos7.9安装部署datax和dataxweb
    安装包,里面有mysql、jdk、datax、datax-web的安装包全部下载链接:https://pan.baidu.com/s/1ESNn2Y71vq5SlOnypaUloA提取码:cuto1、安装jdk(jdk1.8)#把jdk上传解压#配置环境变量vim/etc/profule#把JAVA_HOME改成自己的jdk地址#jdkexportJAVA_HOME=/usr/local/java/jdk1.8......
  • CentOS7中使用Git从github上克隆一个仓库
     1.显示git的版本信息(验证是否安装Git)git--version或者yuminfogit 2.查看git的配置gitconfiguser.namegitconfiguser.email如果没有配置git的用户名和用户的邮箱(名字可以任意指定),可以使用一下命令配置。--global的作用是全局有效,也就是对当前的linu......
  • 【转】Linux下判断cpu架构及系统发行版方法
    原文地址:https://zhuanlan.zhihu.com/p/374738476一、判断cpu架构1,使用命令:hostnamectl2,使用命令:arch3,使用lscpu4,使用命令:cat/proc/cpuinfo,可以查到具体指令集二、判断系统是Debian系还是Redhat系大家都知道linux分redhat系和debian系,那么肯定有办法,去判断系统是r......
  • labview yolov5 opencv dnn推理,封装dll, labview调用dll,支持同时加载多个模型并行推
    labviewyolov5opencvdnn推理,封装dll,labview调用dll,支持同时加载多个模型并行推理,可cpu/gpu,可识别视频和图片,只需要替换模型的onnx和nameclass即可ID:212200673924813995......
  • centos7.9部署LNMP
    系统设置创建应用目录mkdir-pv/data/apps系统更新yum-yupdatelimits.confcat>>/etc/security/limits.conf<<EOFrootsoftcoreunlimitedroothardcoreunlimitedrootsoftnproc1000000roothardnproc1000000rootsoftnofile100000roothardnofile......
  • Docker绑定指定CPU核心
    需求使用多个Container并行任务希望可以对CPU不同核心进行负载均衡。实现可以通过cpuset-cpus参数进行资源管理dockerrun--rm-it--cpuset-cpus8-15ubuntu:18.04nproc#8可以看到host后8核被绑定在guest中且总核心数为8参考https://docs.docker.com/config/contain......