用python脚本 监控内存使用 报警发送信息
# coding: utf-8 import json import os import logging import re import time import traceback import requests # import smtplib # from email.mime.text import MIMEText # from email.header import Header from urllib3.exceptions import InsecureRequestWarning requests.packages.urllib3.disable_warnings(InsecureRequestWarning) logger = logging.getLogger(__name__) # from_addr = '1111111@qq.com' # password = '111111' # 发信方的信息:发信邮箱,QQ 邮箱授权码 # # to_addr = '1111111@11111.com' # 接收邮件,可设置为你的QQ邮箱或者其他邮箱 # smtp_server = 'smtp.qq.com' # 发信服务器 # # # 三个参数:第一个为文本内容,第二个 plain 设置文本格式,第三个 utf-8 设置编码 # message = MIMEText('服务器Redis内存过高!!!', 'plain', 'utf-8') # message['From'] = Header("Dragon", 'utf-8') # 发送者 # message['To'] = Header("2哥", 'utf-8') # 接收者 # # subject = '服务器Redis内存过高!!!' # message['Subject'] = Header(subject, 'utf-8') def mem_ratio(): sram = 0 while True: # str1 = os.popen('ps aux','r').readlines() # strsum = os.popen('grep MemTotal /proc/meminfo','r').readlines() # total_mem = re.findall(r'\d+',strsum[0])[0] # for i in str1: # str2 = i.split() # new_rss = str2[5] # lis.append(new_rss) # for i in lis[1:-1]: # sram = sram + int(i) mem = os.popen('free','r').readlines() # 在终端运行free命令并得到结果 mem_details = re.findall(r'\d+', mem[1]) ratio = float(mem_details[-1])/float(mem_details[0]) if ratio < 0.2: sram += 1 print "内存剩余不足第%s次" % sram # 记录异常次数 mem = os.popen('ps aux --sort=-rss', 'r').readlines() # 按照使用内存降序排序 mem_top10 = map(lambda x: x.strip().split(re.findall(r'\s\d+:\d+\s',x,re.S)[-1]), mem[1:11]) which_mem = '# '.join(map(lambda x: 'Command:' + x[1].strip() + '\r\n'+ 'Using memory:' + str(float(re.findall(r'\d+(?:\.\d+)?',x[0])[4].strip())/float(1024)) + 'MB' +'\r\n\r\n', mem_top10)) # \d+(?:\.\d+)?:匹配字符串中的整数和小数 send_data = { "msgtype": "text", # 消息类型 "text": { "content": "!!!默默服务器 剩余内存%s,请及时处理!!!" % "%.2f%%" % (ratio * 100), # 文本内容,最长不超过2048个字节,必须是utf8编码 "mentioned_list": ["@all"] # userid的列表,提醒群中的指定成员(@某个成员),@all表示提醒所有人,如果开发者获取不到userid,可以使用mentioned_mobile_list } } send_data_1 = { "msgtype": "text", # 消息类型 "text": { "content": "进程占用前十为:\r\n%s" % which_mem, "mentioned_list": ["@all"] # userid的列表,提醒群中的指定成员(@某个成员),@all表示提醒所有人,如果开发者获取不到userid,可以使用mentioned_mobile_list } }
#企业微信 机器人 url = 'https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=1111111111212121212122121' headers = {"Content-Type": "text/plain"} headers_1 = {"Content-Type": "application/json"} try: requests.post(url=url, headers=headers_1, json=send_data_1, verify=False) requests.post(url=url,headers=headers,json=send_data,verify=False) except: print traceback.format_exc() # try: # # 开启发信服务,这里使用的是加密传输 # server = smtplib.SMTP_SSL(smtp_server) # server.connect(smtp_server, 465) # # 登录发信邮箱 # server.login(from_addr, password) # # 发送邮件 # server.sendmail(from_addr, to_addr, message.as_string()) # # 关闭服务器 # server.quit() # print "邮件发送成功" # except smtplib.SMTPException: # print "Error: 无法发送邮件" time.sleep(900) if __name__ == '__main__': mem_ratio()
https://www.cnblogs.com/sunjinchao/p/12703045.html