python 监控 linux 系统状态并发送邮件

监控linux的 磁盘,cpu个数,内存使用,负载,并发送邮件。

  1 [root@js-93 monitor_linux]# cat check_linux_status.py 
  2 #!/usr/bin/env python3
  3 # -*- coding:utf-8 -*-
  4 import os,sys
  5 import smtplib
  6 import email.mime.multipart
  7 import email.mime.text
  8 import psutil
  9 import time,datetime
 10 import subprocess
 11 import re
 12 
 13 
 14 start_system_time = datetime.datetime.fromtimestamp(psutil.boot_time())
 15 now_time = datetime.datetime.now()
 16 start_system_time_value = time.mktime(start_system_time.timetuple())
 17 now_time_value = time.mktime(now_time.timetuple())
 18 sys_start_days = round((now_time_value-start_system_time_value)/(3600*24))
 19 #print("系统已经启动了 %s 天!" % sys_start_days)
 20 
 21 
 22 def get_ip_info():
 23     cmd_get_ip = "ifconfig |sed -n '/inet addr/p'|sed 's#^.*addr:##g'|sed 's#  Bcast.*$##g'|grep -v '127.*'"
 24     res=subprocess.Popen(cmd_get_ip,shell=True,stderr=subprocess.PIPE,stdin=subprocess.PIPE,stdout=subprocess.PIPE)
 25     ip = res.stdout.read()
 26     return ip.decode('utf-8')
 27 
 28 def send_email(message):
 29     msg = email.mime.multipart.MIMEMultipart()
 30     msg['from'] = '发件箱账号'
 31     msg['to'] = '收件箱账号'
 32     msg['subject'] = "%s 服务器监控报警" % get_ip_info()
 33     content = '''
 34  IP   :   %s
 35 当前系统启动天数:%s days
 36 报警信息:
 37 %s
 38 
 39 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 40 系统当前状况:
 41 %s
 42 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 43 ''' % (get_ip_info(),sys_start_days,message,linux_all_check_info())
 44     txt = email.mime.text.MIMEText(content)
 45     msg.attach(txt)
 46 
 47     smtp = smtplib.SMTP()
 48     smtp.connect('smtp.exmail.qq.com', '25')
 49     smtp.login( '发送邮件的账号','发件箱密码')
 50     smtp.sendmail( 'support@deepbrain.ai',['ryan***','***@qq.com'], str(msg))
 51     smtp.quit()
 52 
 53 def mem_check():
 54     mem_dict = {}
 55     #memory 使用check
 56     mem = psutil.virtual_memory()
 57     total_mem = mem.total/1024/1024/1024
 58     used_mem = mem.used/1024/1024/1024
 59     free_mem = mem.free/1024/1024/1024
 60     real_used_mem = total_mem - free_mem
 61     mem_used_precent = "%s" % round((real_used_mem / total_mem * 100))
 62     mem_dict['mem_used_precent'] = mem_used_precent
 63     # swap 分区check
 64     swap_info = psutil.swap_memory()  #swap 
 65     swap_total = round(int(swap_info[0])/1024/1024/1024,2)
 66     swap_free = round(int(swap_info[2])/1024/1024/1024 ,2)
 67     swap_used_precent = "%s" % round(((swap_total  - swap_free)/swap_total ))
 68     mem_dict['swap_used_precent'] = swap_used_precent
 69     return mem_dict
 70 
 71 def cpu_check():
 72     cpu_info_dic = {}
 73     cpu_number = psutil.cpu_count()
 74     physics_cpu_number = psutil.cpu_count(logical=False)
 75     cpu_info_dic['logic_cpu'] = cpu_number
 76     cpu_info_dic['physics_cpu'] = physics_cpu_number
 77     return cpu_info_dic
 78 
 79 def check_disk():
 80     cmd_get_disk_use = '/bin/df'
 81     res = subprocess.Popen(cmd_get_disk_use,shell=True,stderr=subprocess.PIPE,stdin=subprocess.PIPE,stdout=subprocess.PIPE)
 82     fp = res.stdout.read()
 83     with open('disk_info.log','w') as obj01:
 84         obj01.write(fp.decode('utf-8'))
 85     re_obj = re.compile(r'^/dev/.+\s+(?P<used>\d+)%\s+(?P<mount>.+)')
 86     disk_use = {}
 87     with open('disk_info.log','r') as obj02:
 88         for line in obj02:
 89             match = re_obj.search(line)
 90             if match is not None:
 91                 disk_use[match.groupdict()['mount']] = match.groupdict()['used']
 92     return  disk_use
 93 
 94 def load_stat_check():
 95     loadavg = {} 
 96     f = open("/proc/loadavg") 
 97     con = f.read().split() 
 98     f.close() 
 99     loadavg['lavg_1']=con[0] 
100     loadavg['lavg_5']=con[1] 
101     loadavg['lavg_15']=con[2] 
102     loadavg['nr']=con[3] 
103     loadavg['last_pid']=con[4] 
104     return loadavg 
105 
106 
107 def linux_all_check_info():
108     info_list = []
109     #内存信息
110     dic = mem_check()
111     mem_pcrent = dic.get('mem_used_precent')
112     swap_pcrent = dic.get('swap_used_precent')
113     into_list_mem = "当前使用内存:%s%%" % mem_pcrent
114     into_list_swap = "当前使用swap:%s%%" % swap_pcrent
115     info_list.append(into_list_mem) 
116     info_list.append(into_list_swap) 
117     #cpu信息
118     cpu_info = cpu_check()
119     logic_cpu_num = cpu_info.get('logic_cpu')
120     physics_cpu_num = cpu_info.get('physics_cpu')
121     into_list_cpu_logic = "逻辑CPU个数:%s " % logic_cpu_num
122     into_list_cpu_physics = "物理CPU个数:%s" % physics_cpu_num
123     info_list.append(into_list_cpu_logic)
124     info_list.append(into_list_cpu_physics)
125     #磁盘信息
126     disk_info = check_disk()
127     for key,val in disk_info.items():
128         check_info = '磁盘" %s " 使用超过百分之80 , 当前使用了百分之 %s .' % (key,val)
129         info_list.append(check_info)
130     #负载统计
131     load_1 = load_stat_check()['lavg_1']   
132     load_5 = load_stat_check()['lavg_5']   
133     load_15 = load_stat_check()['lavg_15'] 
134     load_info = "当前负载为:%s,前5分钟负载为:%s ,前15分钟负载为: %s " % (load_1,load_5,load_15)
135     info_list.append(load_info)
136 
137     info_last = "\n".join(info_list)   
138     #print(info_last)
139     return info_last
140 
141 def main():
142     message = []
143     #内存检查
144     dic = mem_check()
145     mem_precent = dic.get('mem_used_precent')
146     swap_precent = dic.get('swap_used_precent')
147     if int(mem_precent) > 10  :
148         info = u'内存使用已超过百分之85,当前已使用了百分之 %s ' % mem_precent
149         message.append(info)
150     if int(swap_precent) > 70:
151         info02 = u'swap使用已超过百分之70,当前已使用了百分之 %s ' % swap_precent
152         message.append(info02)
153     #磁盘检查
154     disk_info = check_disk()
155     for key,val in  disk_info.items():
156         if  int(val) > 80:
157             check_info = '磁盘" %s " 使用超过百分之80 , 当前使用了百分之 %s .' % (key,val)
158             message.append(check_info)
159             
160    #负载检查
161     load_1 = load_stat_check()['lavg_1']   
162     load_5 = load_stat_check()['lavg_5']   
163     load_15 = load_stat_check()['lavg_15']   
164     if float(load_1) > 1.00:
165        load_info = "当前负载为:%s,前5分钟负载为:%s ,前15分钟负载为: %s " % (load_1,load_5,load_15)
166        message.append(load_info)
167 
168  
169     info_last = "\n".join(message) 
170     if len(message) > 0:
171         send_email(info_last)
172         #print(info_last)
173 
174 
175 if __name__ == '__main__':
176     main()

 

posted @ 2017-05-23 17:32  Nice_keep-going  阅读(267)  评论(0编辑  收藏  举报