由于项目的需要,需要做一个简单监控服务器的CPU利用率、CPU负载、硬盘使用率、内存利用率和服务器的各个端口的开启情况的程序,并把结果通知到监控平台,如果出现异常,监控平台打电话或者发短信通知给具体的运维人员
python版本要求:python3.0 以上
安装 python 的 psutil 包 和 requests 包
pip install psutil pip install requests
Linux系统下运行效果
Windows系统下运行效果
代码实例核心程序
# 获取端口信息
@classmethod
def get_ports(cls, port):
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
result = sock.connect_ex((‘127.0.0.1’,int(port)))
if result != 0:
send_data = cls.g_web_ip+”服务器的”+port+’端口挂了,快去修复哈’
cls.send_msg(send_data)
else:
print(“端口:”+port+”正常”)
# CPU利用率
@classmethod
def get_cpu_used(cls):
if (sysstr == “Linux”):
f = os.popen(“top -bi -n 1| awk ‘{print $4}'”).read().split(‘\n’)[2]
float_cpu_used = float(f)
float_g_cpu_used = float(cls.g_cpu_used.split(“%”)[0])
print(“CPU利用率:”,f,”%”)
if float(float_cpu_used) > float(float_g_cpu_used):
cls.send_msg(cls.g_web_ip+”服务器的CPU利用率超过”+cls.g_cpu_used+”了,快去看看咋回事!”)
else:
print(sysstr + ” CPU Adoption rate Cannot read.”)
printL()
# CPU平均负载
@classmethod
def aver_load(cls):
if (sysstr == “Linux”):
f = os.popen(“uptime | sed ‘s/,//g’ | awk ‘{print $8,$9,$10}'”)
str_aver_load = f.read().strip().split(“:”)[1].strip()
print(“CPU平均负载:”,str_aver_load)
if float(str_aver_load) > float(cls.g_aver_load):
cls.send_msg(cls.g_web_ip+”服务器的CPU平均负载超过”+cls.g_aver_load+”了,快去看看咋回事!”)
else:
print(sysstr + ” CPU Load average Cannot read.”)
printL()
#获取硬盘使用率
@classmethod
def get_disk_used(cls):
if (sysstr == “Linux”):
disk_val = os.popen(“df -h | head -2 | tail -1 |awk ‘{print $5}'”).read().strip()
int_disk_val = int(disk_val.split(“%”)[0])
int_g_disk_val = int(cls.g_disk_used.split(“%”)[0])
print(“硬盘使用率:”,disk_val)
if int_disk_val > int_g_disk_val:
cls.send_msg(cls.g_web_ip+”服务器的硬盘使用率超过”+cls.g_disk_used+”了,快去看看咋回事!”)
else:
print(sysstr + ” hard disk Cannot read.”)
printL()
# 获取内存使用率
@classmethod
def get_mem_used(cls):
if (sysstr == “Linux”):
f = os.popen(“free -m |grep Mem |awk ‘{print $3/$2}'”)
str_men = f.read().strip()
print(“内存使用率:”,str_men)
if float(str_men) > float(cls.g_mem_used):
cls.send_msg(cls.g_web_ip+”服务器的内存使用率超过”+cls.g_mem_used+”了,快去看看咋回事!”)
else:
print(sysstr + ” RAM Cannot read.”)
printL()
#调用报警函数
@classmethod
def send_msg(cls, content):
cls.send_http(content)
# 调用http接口
@classmethod
def send_http(cls,content):
printL()
print(“send_http:”,type(content),content)
url_total = cls.g_php_url + “?msg=” + content
print(“url_total:”,url_total)
rp = requests.get(url_total)
print(“rp:”,rp.text)
printL()
# 发微信预警消息
@classmethod
def send_wx_alarm(cls,content):
post_url = cls.g_wx_url
for id in cls.g_wx_id:
try:
post_data = ‘{“operSys”:”MCS”,”content”:”服务器监控告警:%s\n%s”,”phones”:”%s”}’%(cls.g_web_ip, content, id)
print(post_data)
# data = urllib.parse.urlencode(post_data)
# data = data.encode(‘utf-8’)
req = requests.get(url=post_url,data=post_data)
print(“send_wx_alarm req:”,req,type(req))
result = json.loads(req.text())
print(result)
except Exception as e:
print(“send_wx_alarm:”,e)
# 发邮件预警消息
@classmethod
def send_email_alarm(cls,content):
post_url = cls.g_email_url
for id in cls.g_email_id:
try:
post_data = ‘{“subject”:”%s服务器监控告警”,”email”:”%s”,”bccEmail”:””,”operSys”:”LOG”,”content”:”%s”}’%(cls.g_web_ip, id, content)
print(post_data)
# data = urllib.parse.urlencode(post_data)
# data = data.encode(‘utf-8’)
req = requests.get(url=post_url,data=post_data)
print(“send_email_alarm req:”,req,type(req))
# req.add_header(“Content-Type”, “application/x-www-form-urlencoded;charset=utf-8”)
result = json.loads(req.text())
print(result)
except Exception as e:
print(“send_email_alarm:”,e)
实例代码配置文件
# 本机IP地址(这里之所以不自动获取是因为有些机器只有内网)
web_ip=***
# 检测的端口
monitor_ports=3306, 8088, 6004 ,6379
# CPU利用率
cpu_used=100%
# CPU平均负载
aver_load=1
# 内存使用率
mem_used=0.8
# 磁盘使用率
disk_used=80%
# 通知地址
php_url=http://***:**/TaskMonitor/action
# 微信地址
wecaht_url=http://***:**/wechat/sendWeChat
# 微信ID
wecaht_id=123456,13123
# email地址
email_url=http://***:**/email/sendEmail
# 邮件邮箱
email_id=test@mucfc.com,11223344@qq.com
启动方式
nohup python3 monitor.py > monitor.log 2>&1 &
注:需要定期清理 monitor.log 文件
本文主要实例了Python3监控windows,linux系统的CPU、硬盘、内存使用率和各个端口的开启情况详细代码实例,更多关于Python3监控实例与技巧请查看下面的相关链接