脚本集锦-python监控yarn中的任务

# -*- coding: UTF-8 -*-

import httplib
import urllib
import json
import time
import os

yarnWebServer = ["hadoop-05.bj", "hadoop-04.bj"]
port = '22301'

expectedState = 'RUNNING'
notExpectedState = ''
expectedStatus = 'UNDEFINED'
expectedPkn = 'com.clc.streaming.dataAnalysis.ExpBasicInfoMinute'

def readAppIdFromLocal(fileDir):
    try:
        file = open(fileDir, 'r')
        line = file.readline()
        if line != "" and len(line) > 0 and line.find("=") != -1:
            return line.split("=")[1].strip()
    except Exception as e:
        print e
    finally:
        file.close()
    return ""

def getApplicationInfo(appId,yarnServer = 0):
    headers = {"accept": "application/json"}
    url = "/ws/v1/cluster/apps/%s" % appId
    try:
        conn = httplib.HTTPConnection(yarnWebServer[yarnServer], port)
        conn.request("GET", url, None, headers)
        response = conn.getresponse()
        data = response.read()
        result = data
        if data.find("This is standby RM") != -1:
            result = getApplicationInfo(appId, 1)
    except Exception as e:
        print e
    finally:
        conn.close()
    return result

if __name__ == "__main__":
    appid = readAppIdFromLocal("appId")
    if appid != "" and appid.find("application") == 0:
        data = getApplicationInfo(appid)

    obj = json.loads(data)
    state = obj['app']['state']
    finalStatus = obj['app']['finalStatus']
    pkgName = obj['app']['name']

    print "\nCurrent time: ", time.asctime( time.localtime(time.time()) )
    print "===Current application info:==="
    if state == expectedState and finalStatus == expectedStatus and pkgName == expectedPkn:
        print "\tstate:\t\t%s,\n\tfinalStatus:\t%s,\n\tappName:\t%s" % (state, finalStatus, pkgName)
    elif state != expectedState and pkgName == expectedPkn:
        print "\tstate:\t\t%s,\n\tfinalStatus:\t%s,\n\tappName:\t%s" % (state, finalStatus, pkgName)
        os.system("sh ~/mqsas/caolianchao/scripts/run.sh")


你可能感兴趣的:(脚本集锦-python监控yarn中的任务)