# -*- coding: UTF-8 -*-
import sys
reload(sys)
sys.setdefaultencoding('utf-8')
import httplib
import urllib
import json
import time
import os
import commands
import requests
import numpy as np
import schedule
wx_url = 'https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=a4dc5249-3d5e-4286-935a-9122d3b503f5' #机器人的key
def readAppIdFromLocal(fileDir):
try:
content_list = []
with open(fileDir) as Flie:
all_lines = Flie.readlines()
content_text = ''.join(all_lines)
content_list.append(content_text)
return content_list
except Exception as e:
print e
finally:
Flie.close()
return ""
def func():
re_msg = commands.getoutput("yarn application -list | grep -w RUNNING | grep -w flink| awk '{print $2}'")
re_msg1= str(re_msg)
arrs = set(re_msg1.split("\n"))
a=""
for b in arrs:
if len(b)>50:
a=b
arrs.remove(a)
print (str(arrs))
apps = readAppIdFromLocal("/data/flink-monitor/sessions.txt")
apps = set(str(apps).replace("'","").replace("[","").replace("]","").split("\\r\\n"))
print (str(apps))
exce = apps.symmetric_difference(arrs)
if len(exce) != 0:
header = {
"Content-Type": "application/json;charset=UTF-8"
}
message_body = {
"msgtype": "markdown",
"markdown": {
"content": str(exce)+"状态异常,请检查"
},
"at": {
"atMobiles": [],
"isAtAll": False
}
}
send_data = json.dumps(message_body)
ChatBot = requests.post(url=wx_url, data=send_data, headers=header)
opener = ChatBot.json()
if opener["errmsg"] == "ok":
print(u"%s 通知消息发送成功!" % opener)
else:
print(u"通知消息发送失败,原因:{}".format(opener))
else:
header = {
"Content-Type": "application/json;charset=UTF-8"
}
message_body = {
"msgtype": "markdown",
"markdown": {
"content": str(exce)+"所有session状态正常!"
},
"at": {
"atMobiles": [],
"isAtAll": False
}
}
send_data = json.dumps(message_body)
ChatBot = requests.post(url=wx_url, data=send_data, headers=header)
opener = ChatBot.json()
if opener["errmsg"] == "ok":
print(u"%s 通知消息发送成功!" % opener)
else:
print(u"通知消息发送失败,原因:{}".format(opener))
if __name__ == "__main__" :
#创建定时
schedule.every().hour.do(func) #每小时运行一次
#开循环
while True:
schedule.run_pending()
time.sleep(120)
flink-session-minitor
最新推荐文章于 2024-10-07 12:04:27 发布