美文网首页
脚本集锦-python监控yarn中的任务

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

作者: CarsonCao | 来源:发表于2019-07-15 16:50 被阅读0次
    # -*- 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中的任务

          本文链接:https://www.haomeiwen.com/subject/iiznkctx.html