1、功能說明python
2、代碼詳情數據庫
1、經過阿里sdk獲取慢查詢列表,格式化。 2、企業微信報警接口 三、deamon
#!/usr/bin/python #-*- conding:utf-8 -*- from aliyunsdkcore.client import AcsClient from aliyunsdkrds.request.v20140815 import DescribeDBInstanceAttributeRequest from aliyunsdkrds.request.v20140815 import DescribeSlowLogRecordsRequest import requests import json import os import time import logging import subprocess #ali key client = AcsClient( "", "", "" ); log_file = "/tmp/rdsslowinfo.log" logging.basicConfig(filename=log_file,filemode='a',level=logging.DEBUG) def WARNING(*objs): print("[%s] : " % time.strftime("%y-%m-%d %H:%M:%S", time.localtime()), *objs, file=sys.stderr) class rdsInfo(object): ''' ali rds slow log get''' def __init__(self,dbId,startTime,endTime): self.dbId = dbId self.startTime = startTime self.endTime = endTime def slowlogGet(self): request = DescribeSlowLogRecordsRequest.DescribeSlowLogRecordsRequest() request.set_accept_format('json') request.set_DBInstanceId(self.dbId) request.set_StartTime(self.startTime) request.set_EndTime(self.endTime) response = client.do_action_with_exception(request) data = json.loads(response) return data def instanceGet(self): request = DescribeDBInstanceAttributeRequest.DescribeDBInstanceAttributeRequest() request.set_accept_format('json') request.set_DBInstanceId(self.dbId) response = client.do_action_with_exception(request) data = json.loads(response) #print (data['Items']) return data class WeChat: '''微信接口''' def __init__(self,user): self.CORPID = '' #企業ID, 登錄企業微信,在個人企業-->企業信息裏查看 self.CORPSECRET = '' #自建應用,每一個自建應用裏都有單獨的secret self.AGENTID = '' #應用代碼 self.TOUSER = user # 接收者用戶名, @all 全體成員 def _get_access_token(self): url = 'https://qyapi.weixin.qq.com/cgi-bin/gettoken' values = {'corpid': self.CORPID, 'corpsecret': self.CORPSECRET, } req = requests.post(url, params=values) data = json.loads(req.text) # print (data) return data["access_token"] def get_access_token(self): try: with open('access_token.conf', 'r') as f: t, access_token = f.read().split() except: with open('access_token.conf', 'w') as f: access_token = self._get_access_token() cur_time = time.time() f.write('\t'.join([str(cur_time), access_token])) return access_token else: cur_time = time.time() if 0 < cur_time - float(t) < 7200: #token的有效時間7200s return access_token else: with open('access_token.conf', 'w') as f: access_token = self._get_access_token() f.write('\t'.join([str(cur_time), access_token])) return access_token def send_data(self, msg): send_url = 'https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=' + self.get_access_token() send_values = { "touser": self.TOUSER, "msgtype": "text", "agentid": self.AGENTID, "text": { "content": msg }, "safe": "0" } send_msges=(bytes(json.dumps(send_values), 'utf-8')) respone = requests.post(send_url, send_msges) respone = respone.json()#當返回的數據是json串的時候直接用.json便可將respone轉換成字典 # print (respone["errmsg"]) return respone["errmsg"] def dataInit(): ''' get data info''' instanceInfo = { 'rm-阿里域名':'業務線', 'rm-':'', 'rm-':'', 'rm-':'', 'rm-':'', } alarmList = ['','','','','']#收報警人列表 for dbUrl in instanceInfo.keys(): nowTime = time.strftime("%Y-%m-%dT%H:%MZ") second = time.time() - 24*60*60 #查詢間隔 本身定義 tago = time.strftime('%Y-%m-%dT%H:%MZ', time.localtime(second)) rdsinfo = rdsInfo(dbUrl,tago,nowTime) slowlog_data = rdsinfo.slowlogGet() instance_data = rdsinfo.instanceGet() try: for recode in slowlog_data['Items']['SQLSlowRecord']: #print(recode) ParseRowCounts = recode['ParseRowCounts'] ReturnRowCounts = recode['ReturnRowCounts'] QueryTimes = recode['QueryTimes'] HostAddress = recode['HostAddress'] LockTimes = recode['LockTimes'] ExecutionStartTime = recode['ExecutionStartTime'] SQLText = recode['SQLText'] DBName = recode['DBName'] content = ''' 業務線 :{url} SQL來源:{HostAddress} 執行時間:{ExecutionStartTime} 數據庫名:{DBName} 執行時長:{QueryTimes} 鎖定時長:{LockTimes} 解析行數:{ParseRowCounts} 返回行數:{ReturnRowCounts} SQL詳情:{SQLText} ''' .format(url=instanceInfo[dbUrl],HostAddress=HostAddress,ExecutionStartTime\ =ExecutionStartTime,DBName=DBName,QueryTimes=QueryTimes,LockTimes=LockTimes,\ ParseRowCounts=ParseRowCounts,ReturnRowCounts=ReturnRowCounts,SQLText=SQLText) #print(content) logging.info(content) for alarm in alarmList: wx = WeChat(alarm) wx.send_data(msg=content) except Exception as e: WARNING('[%s]')%(e) class Daemon: ''' Daemon ''' def createDaemon(self): try: if os.fork() > 0: os._exit(0) # exit father… except OSError as error: print ('fork #1 failed: %d (%s)' % (error.errno, error.strerror)) os._exit(1) # it separates the son from the father os.chdir('/') os.setsid() os.umask(0) # create - fork 2 try: pid = os.fork() if pid > 0: print ('Daemon PID %d' % pid) logging.info('Daemon PID %d' % pid) os._exit(0) except OSError as error: print ('fork #2 failed: %d (%s)' % (error.errno, error.strerror)) os._exit(1) self.run() # function demo def run(self): while True: dataInit() time.sleep(24*60*60) #deamon運行間隔時間 #time.sleep(5) #except Exception,e: # traceback.print_exc() if __name__ == '__main__': daemon = Daemon() daemon.createDaemon()