Python脚本——监控系统脚本
监测系统的CPU、内存、硬盘、网卡和服务端口信息,并发送邮件信息
- Linux和Windows都可以使用
- 使用psutil模块获取检测数据
- 字典services:定义端口数据
- 方法send_mail:定义了邮件信息
# 运行输出信息
------------------------------
服务状态:
nginx: Off!
php: Off!
mysql: Off!
redis: Off!
zabbix: Off!
------------------------------
CPU使用率: 23.00%
------------------------------
内存使用率: 32.80%
内存总容量: 16227M
已使用内存: 5317M
------------------------------
磁盘使用率: 54.30%
磁盘总容量: 376Gb
已使用磁盘: 204Gb
------------------------------
网卡发送流量: 113Mb
网卡接收流量: 2184Mb
------------------------------
import psutil
import smtplib
from email.mime.text import MIMEText
from email.header import Header
services = {
'nginx': 80,
'php': 9000,
'mysql': 3306,
'redis': 6379,
'zabbix': 10050,
}
def cpu_info():
cpu = '%.2f%%' % psutil.cpu_percent(1)
return cpu
def mem_info():
mem = psutil.virtual_memory()
mem_per = '%.2f%%' % mem[2]
mem_total = str(int(mem[0] / 1024 / 1024)) + 'M'
mem_used = str(int(mem[3] / 1024 / 1024)) + 'M'
info = {
'mem_per': mem_per,
'mem_total': mem_total,
'mem_used': mem_used
}
return info
def disk_info():
all_disk = psutil.disk_usage('/')
disk_per = '%.2f%%' % all_disk[3]
disk_total = str(int(all_disk[0] / 1024 / 1024 / 1024)) + 'Gb'
disk_used = str(int(all_disk[1] / 1024 / 1024 / 1024)) + 'Gb'
info = {
'disk_per': disk_per,
'disk_total': disk_total,
'disk_used': disk_used,
}
return info
def network_info():
network = psutil.net_io_counters()
network_sent = str(int(network[0] / 1024 / 1024)) + 'Mb'
network_resv = str(int(network[1] / 1024 / 1024)) + 'Mb'
info = {
'network_sent': network_sent,
'network_resv': network_resv
}
return info
def port_check():
info = dict.fromkeys(services.keys(), 'Off!')
for i in services.items():
for j in psutil.net_connections():
if j.status == 'LISTEN' and j.laddr.port == i[1]:
info[i[0]] = 'ok'
return info
def send_mail(message):
a = ['xxxxxx@qq.com', ]
sender = 'xxxxxx@qq.com'
for i in a:
receiver = i
subject = '报警'
username = 'xxxxxx@qq.com'
password = 'xxxxxxxxxxxxx'
msg = MIMEText(message, 'plain', 'utf-8')
msg['Subject'] = Header(subject, 'utf-8')
msg['From'] = sender
msg['To'] = receiver
smtp = smtplib.SMTP_SSL('smtp.qq.com')
smtp.ehlo('smtp.qq.com')
smtp.login(username, password)
smtp.sendmail(sender, receiver, msg.as_string())
smtp.quit()
def main():
cpu = cpu_info()
mem = mem_info()
disk = disk_info()
network = network_info()
port = port_check()
port_result = ' ------------------------------\n 服务状态:\n'
for each in port.items():
port_result += f'\t{each[0]}:\t\t{each[1]}\n'
info = port_result + f'''
------------------------------
CPU使用率:\t\t{cpu}
------------------------------
内存使用率:\t{mem.get('mem_per')}
内存总容量:\t{mem.get('mem_total')}
已使用内存:\t{mem.get('mem_used')}
------------------------------
磁盘使用率:\t{disk.get('disk_per')}
磁盘总容量:\t{disk.get('disk_total')}
已使用磁盘:\t{disk.get('disk_used')}
------------------------------
网卡发送流量:\t{network.get('network_sent')}
网卡接收流量:\t{network.get('network_resv')}
------------------------------
'''
flag = 0
services_msg = ''
for i in port.items():
if not i[1]:
flag = 1
services_msg += i[0] + '报错\n'
if flag:
info = services_msg + '\n' + info
elif int(cpu[:-4]) > 80:
info = 'CPU过载\n' + info
elif int(mem.get('mem_per')[:-4]) > 80:
info = '内存过载\n' + info
print(info)
if __name__ == '__main__':
main()