flink-session-minitor

# -*- 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)
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值