emr_queue_alarm.py 1.99 KB
# -*- coding: utf8 -*-
import requests
from requests.auth import HTTPBasicAuth

DING =  "https://oapi.dingtalk.com/robot/send?access_token=31cf1a526c98862b7945e561c5a0d857a069350dbee0e807074b83b39fa1e1af"

emr_schedulers = {'ops': 'http://10.66.80.5:5004/ws/v1/cluster/scheduler',
'rec':'http://10.67.7.2:5004/ws/v1/cluster/scheduler',
'real':'http://10.67.90.15:5004/ws/v1/cluster/scheduler',
'ware':'http://10.66.80.5:5004/ws/v1/cluster/scheduler'}

emr_auth = {'ops': HTTPBasicAuth('root','Yoho@9646'),
'rec': HTTPBasicAuth('root','yohoISD@emr12354'),
'real': HTTPBasicAuth('root','yohoISD@emr1235'),
'ware': HTTPBasicAuth('root','yohoISD@emr12354')}

print('Start Hello World function')
def main_handler(event, context):
    check_interval()
    return "hello from scf"  #return

def parse_queue_info(emr,resp):
    queues = {}
    for x in resp['queues']['queue']:
        q_name = emr+'.'+x['queueName']
        queues[q_name] = float(x['usedCapacity'])
        if x.has_key('queues'):
           childQueues = parse_queue_info(q_name,x)
           for k,v in childQueues.iteritems():
               queues[k] = v
    return queues

def check_interval():
    alarm_queue={}
    for emr, emr_scheduler in emr_schedulers.iteritems():
        resp = requests.get(emr_scheduler,auth=emr_auth[emr])
        if resp.status_code == 200:
            queues = parse_queue_info(emr,resp.json()['scheduler']['schedulerInfo'])
            for k,v in queues.iteritems():
                if v > float(95):
                   alarm_queue[k] = str(v)+'%'
    print alarm_queue
    if 0 < len(alarm_queue):
        sent_alarm(alarm_queue)

def sent_alarm(queues_info):
    ding_req = {}
    ding_req["msgtype"]="markdown"
    ding_req["markdown"] = {}
    ding_req["markdown"]['title']="emr_resoucemanager"
    ding_req["markdown"]["text"]= "### EMR_ResourceManager Queue Alarm.  Info: **%s** ." %(str(queues_info))
    r = requests.post(url = DING, json = ding_req) 
    print("Send DingDing message result:%s, request: %s "  %(r, ding_req))