在这里插入代码片
这里是第一个脚本,名称 send_vx.py
import json
import requests
import time
corpid = "ww6d8asdasdasdasdasdas这里是微信的corpid"
secret = "****** 这里是秘钥"
agentid = "1000002"
times = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
def GetTokenFromServer(Corpid,Secret):
access_token_url="https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid="+corpid+"&corpsecret="+secret
r = requests.get(url=access_token_url)
return r.json()['access_token']
def send_wechat(str):
access_token = GetTokenFromServer(corpid,secret)
Url = "https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=%s" % access_token
headers={
"Content-Type": "application/json"
}
data={
"touser": "用户a|用户b",
"agentid" : agentid,
"msgtype" : "text",
"text": {
"content": "updata finished ip =120.120.120.130 date = "+str
}
}
data_dict = json.dumps(data, ensure_ascii=False).encode('utf-8')
r = requests.post(url=Url, headers=headers, data=data_dict)
print(r.text)
def send_wechat_error(str):
access_token = GetTokenFromServer(corpid,secret)
Url = "https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=%s" % access_token
headers={
"Content-Type": "application/json"
}
data={
"touser": "用户a|用户b",
"agentid" : agentid,
"msgtype" : "text",
"text": {
"content": "***important *** "+str
}
}
data_dict = json.dumps(data, ensure_ascii=False).encode('utf-8')
r = requests.post(url=Url, headers=headers, data=data_dict)
print(r.text)
这里是第二个脚本 名称 watch_log.py
import os
import datetime
import pyinotify
import logging
import send_vx
import time
pos = 0
def printlog():
global pos
try:
fd = open(r'/backup/mysqlbkup.log')
if pos != 0:
fd.seek(pos,0)
while True:
line = fd.readline()
if line.strip():
print(line.strip())
times =' ok '+ time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())+' backup finished '
send_vx.send_wechat(times)
pos = pos + len(line)
if not line.strip():
break
fd.close()
except Exception,e:
print(str(e))
class MyEventHandler(pyinotify.ProcessEvent):
def process_IN_MODIFY(self, event):
try:
printlog()
except Exception,e:
print(str(e))
def main():
printlog()
wm = pyinotify.WatchManager()
wm.add_watch('/backup/mysqlbkup.log', pyinotify.ALL_EVENTS, rec=True)
eh = MyEventHandler()
notifier = pyinotify.Notifier(wm, eh)
notifier.loop()
if __name__ == '__main__':
main()
// 更新后的 wathc_log.py
import os
import datetime
import pyinotify
import logging
import send_vx
import time
from collections import deque
import os
pos = 0
fname = 'log'+str(time.strftime("%Y%m%d", time.localtime() ) )+ '.log'
print(fname)
times = time.strftime('%Y-%m-%d %H:%M:%S',time.localtime())
print(times)
def printlog():
global pos
try:
fd = open(r'/usr/logs_sys/'+fname)
output = deque(fd, 7)
list1 = list(output)
times_cpu = times + '服务器 1.2.16.11 mysql master cpu超出了40% 请注意观察 '
times_cpu1 = times + '服务器 1.2.16.11 mysql master 刚刚执行 cpu 检测,CPU结果正常'
times_mem = ',内存剩余(G)'
times_disk = '硬盘: '
cpu1state = 0
memstate = ''
diskstate = ''
tolstr = ''
b = 0
b2 = 0
c = 0
c2 = 0
for item in list1:
a = item.find('% ,CPU')
b = item.find('内存剩余: ')
b2 = item.rfind('G')
c = item.find('硬盘使用率: ')
c2 = item.find('硬盘 - 挂载点:')
if a >0:
cpu = int(item[12:a])
print(cpu)
if cpu >40:
cpu1state = 1
else:
cpu1state = 0
if b >0 and b2 >0 :
v = int ( b + 6 )
mem_state = times_mem + str(item[v:b2])
if c >=0 and c2 >=0 :
vd = int(c2)
if diskstate != '' :
diskstate = diskstate + times_disk + str(item[vd:])
else :
diskstate = times_disk + str(item[vd:])
print(mem_state)
print(diskstate)
if cpu1state == 1:
send_vx.send_wechat(times_cpu+ mem_state + diskstate)
if cpu1state == 0 :
send_vx.send_wechat(times_cpu1 +mem_state + diskstate)
fd.close()
except Exception as e:
print(str(e))
def main():
printlog()
if __name__ == '__main__':
main()