在这里插入代码片
这里是第一个脚本,名称 send_vx.py
# -*- coding: utf-8 -*-
# coding=utf-8
import json
import requests
import time
#企业唯一ID 王生私人微信,注意替换
corpid = "ww6d8asdasdasdasdasdas这里是微信的corpid"
#自定义应用密钥
secret = "****** 这里是秘钥"
#自定义应用编号
agentid = "1000002"
# 格式化成2016-03-20 11:45:39形式
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)
#print((r.json()['access_token']))
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", #消息接收人,多个用|分隔 如: xxx|xxx|xxx
#"toparty" : "1", #如需要发送消息到整个部门的人,则使用该参数, 数字代表该部门 ID
"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", #消息接收人,多个用|分隔 如: xxx|xxx|xxx
#"toparty" : "1", #如需要发送消息到整个部门的人,则使用该参数, 数字代表该部门 ID
"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)
#send_wechat()
这里是第二个脚本 名称 watch_log.py
# -*- coding: utf-8 -*-
#!/usr/bin/env python
import os
import datetime
import pyinotify
import logging
import send_vx
import time
pos = 0
#times = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
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():
#输出前面的log
printlog()
# watch manager
wm = pyinotify.WatchManager()
wm.add_watch('/backup/mysqlbkup.log', pyinotify.ALL_EVENTS, rec=True)
eh = MyEventHandler()
# notifier
notifier = pyinotify.Notifier(wm, eh)
notifier.loop()
if __name__ == '__main__':
main()
// 更新后的 wathc_log.py
# -*- coding: utf-8 -*-
#!/usr/bin/env python
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:
#print(item, end=' ')
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:
#al = os.system('killall -u git')
cpu1state = 1
else:
cpu1state = 0 #send_vx.send_wechat(times_cpu1)
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)
#diskstate = times_disk + str(item[vd:])
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()
服务器状态监听通知到个人企业微信的python 脚本
最新推荐文章于 2024-04-26 01:45:18 发布