Python采集Yarn Queue信息

16 篇文章 0 订阅
2 篇文章 0 订阅

      本文通过python代码调用Yarn restfull API ,将各队列的vcore和memory定时采集存储到TIDB,然后使用Apache Zeppelin JDBC读取数据,简单绘制vcore和memory使用率的趋势图。可以观察集群vcore和memory的分配情况,防止其中一项成为集群或者队列的性能瓶颈。

一、python代码:

 

import json
import urllib2
import datetime
# yarn rest api:
# http://hadoop.apache.org/docs/current/hadoop-yarn/hadoop-yarn-site/ResourceManagerRest.html#Cluster_Writeable_APIs

def getActiveRN(master1,master2):
    activemaster=""
    response=urllib2.urlopen("http://"+master1+"/ws/v1/cluster/info")
    jsonstring=response.read()
    print(jsonstring)
    j1=json.loads(jsonstring)
    print(master1 +" resourcemanager state is :"+j1['clusterInfo']['haState'])

    response=urllib2.urlopen("http://"+master2+"/ws/v1/cluster/info")
    jsonstring=response.read()
    print(jsonstring)
    j2=json.loads(jsonstring)
    print(master2 +" resourcemanager state is :"+j2['clusterInfo']['haState'])

    if j1['clusterInfo']['haState']=='ACTIVE':
        print("active master is "+master1)
        activemaster=master1
    elif j2['clusterInfo']['haState']=='ACTIVE':
        print("active master is "+master2)
        activemaster=master2
    else :
        raise Exception("on active resourcemanger in %s,%s "%(master1,master2))
    return activemaster


def getClusterScheduler(activeResourceManger):
    response=urllib2.urlopen("http://"+master1+"/ws/v1/cluster/scheduler")
    jsonstring=response.read()
    print(jsonstring)
    jsonarray=json.loads(jsonstring)
    print(jsonarray)
    return jsonarray


def getQueueInfo(queuename,ClusterScheduler):
    jsonarray=ClusterScheduler['scheduler']['schedulerInfo']['queues']['queue']
    print("**************** %s scheduler info :%s"%(queuename,jsonarray))

    print("*********************************************************")

    print("**************** %s scheduler1 info :%s"%(queuename,jsonarray[1]))
    for i in range(0,len(jsonarray)):
        if(jsonarray[i]['queueName']==queuename):
            print("find queuename:%s info %s"%(queuename,jsonarray[i]))
            return jsonarray[i]

def findSubQueueInfo(queuename,parenetClusterScheduler):
    print("*********************begin findSubQueueInfo:%s**********"%queuename)
    jsonarray=parenetClusterScheduler['queues']['queue']
    for i in range(0,len(jsonarray)):
        if(jsonarray[i]['queueName']==queuename):
          print("*********************finish findSubQueueInfo:%s**********" % queuename)
          return jsonarray[i]

def clusteMetrics(activeResourceManger):
    response = urllib2.urlopen("http://" + activeResourceManger + "/ws/v1/cluster/metrics")
    jsonstring = response.read()
    jsonarray = json.loads(jsonstring)
    return jsonarray


if __name__ == "__main__":
    master1="192.168.240.1:8088"
    master2="192.168.240.2:8088"
    parenetQueue=['sto','dm','bd','event']
    bdchildrenQueue = ['vip', 'tenhive', 'AthenaSysService', 'default']
    activemaster=getActiveRN(master1,master2)
    clustemetrics=clusteMetrics(activemaster)
    clustemetrics['clusterMetrics']['totalVirtualCores']

    allvcore = clustemetrics['clusterMetrics']['totalVirtualCores']
    allmemory = clustemetrics['clusterMetrics']['totalMB']

    clusterScheduler=getClusterScheduler(activemaster)
    bd=getQueueInfo('bd',clusterScheduler)
    #defaultQueueInfo=findSubQueueInfo('default', bd)
    #print(defaultQueueInfo['resourcesUsed']['vCores'])

    currentAllvcore=0
    currentAllmemory=0
    currentAllvcorePercentage=0.0
    currentAllmemoryPercentage=0.0
    fo = open("QueueInfo.txt", "a+")
    now_time='\''+datetime.datetime.strftime(datetime.datetime.now(),'%Y-%m-%d %H:%M:%S')+'\''

    for i in parenetQueue:
        queueInfo = getQueueInfo( i, clusterScheduler)
        currentAllvcore=currentAllvcore +queueInfo['resourcesUsed']['vCores']
        currentAllvcorePercentage=currentAllvcorePercentage+(queueInfo['resourcesUsed']['vCores']*1.0/allvcore)
        currentAllmemory=currentAllmemory +queueInfo['resourcesUsed']['memory']
        currentAllmemoryPercentage=currentAllmemoryPercentage +queueInfo['resourcesUsed']['memory']*1.0/allmemory
        queuename='\'root.'+i+'\''
        fo.write("insert into yarn_monitor.yarn_vcore_memory_monitor"
                 "(curr_time,queuename,currentAllvcore,currentAllmemory,"
                 "currentAllvcorePercentage,currentAllmemoryPercentage) "
                 "VALUES (%s,%s,%s,%s,%s,%s);\n" % (
            now_time,queuename,currentAllvcore, currentAllmemory, currentAllvcorePercentage, currentAllmemoryPercentage)
                 )



    for i in bdchildrenQueue :
        queueInfo = findSubQueueInfo(i, bd)
        currentAllvcore = currentAllvcore + queueInfo['resourcesUsed']['vCores']
        currentAllvcorePercentage = currentAllvcorePercentage + (queueInfo['resourcesUsed']['vCores'] * 1.0 / allvcore)
        currentAllmemory = currentAllmemory + queueInfo['resourcesUsed']['memory']
        currentAllmemoryPercentage = currentAllmemoryPercentage + queueInfo['resourcesUsed']['memory'] * 1.0 / allmemory
        queuename = '\'root.bd.' + i + '\''
        fo.write("insert into yarn_monitor.yarn_vcore_memory_monitor"
                 "(curr_time,queuename,currentAllvcore,currentAllmemory,"
                 "currentAllvcorePercentage,currentAllmemoryPercentage) "
                 "VALUES (%s,%s,%s,%s,%s,%s);\n" % (
                     now_time, queuename, currentAllvcore, currentAllmemory, currentAllvcorePercentage,
                     currentAllmemoryPercentage)
                 )


    print("currentAllvcore = %s"%currentAllvcore)
    print("currentAllallmemory = %s"%currentAllmemory)
    print("currentAllvcorePercentage = %s"%currentAllvcorePercentage)
    print("currentAllallmemoryPercentage = %s"%currentAllmemoryPercentage)


    fo.write("insert into yarn_monitor.yarn_vcore_memory_monitor"
             "(curr_time,queuename,currentAllvcore,currentAllmemory,"
             "currentAllvcorePercentage,currentAllmemoryPercentage) "
             "VALUES (%s,'root',%s,%s,%s,%s);\n"%(now_time,currentAllvcore,currentAllmemory,currentAllvcorePercentage,currentAllmemoryPercentage)
            )
    fo.close()

 

2、zeppelin TIDB jdbc  配置

创建TIDB 表并设置zeppelin jdbc 连接参数:

CREATE TABLE `yarn_vcore_memory_monitor` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `curr_time` datetime NOT NULL,
  `queuename` varchar(50) DEFAULT NULL,
  `currentAllvcore` int(11) DEFAULT NULL,
  `currentAllmemory` int(11) DEFAULT NULL,
  `currentAllvcorePercentage` double DEFAULT NULL,
  `currentAllmemoryPercentage` double DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin AUTO_INCREMENT=30001;

3、zeppelin查看数据

SELECT id, currentAllvcorePercentage, currentAllmemoryPercentage
FROM yarn_monitor.yarn_vcore_memory_monitor
WHERE curr_time < DATE_FORMAT(date_add(curdate(), INTERVAL 1 DAY), '%Y-%m-%d %H:%i:%S') #明天早上
	AND curr_time > DATE_FORMAT(curdate(), '%Y-%m-%d %H:%i:%S');#今天早上

     观察下面vcore和memory使用百分比趋势图,如果其中一项成为性能瓶颈,需要增加相应队列的资源。

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要连接 Spark YARN 集群,需要按照以下步骤进行操作: 1. 安装 Java 和 Spark 首先需要确保已经正确安装了 Java 和 Spark。安装步骤可以参考官方文档。 2. 配置 Hadoop 和 YARN 在连接 Spark YARN 集群之前,需要先配置 Hadoop 和 YARN。在配置之前,确保已经正确安装了 Hadoop 和 YARN。安装步骤可以参考官方文档。 3. 配置 Spark 在连接 Spark YARN 集群之前,需要先配置 Spark。在配置之前,确保已经正确安装了 Spark。安装步骤可以参考官方文档。 在 Spark 的配置文件中,需要设置以下参数: ``` spark.master yarn spark.submit.deployMode cluster ``` 其中,`spark.master` 参数指定 Spark 的运行模式为 YARN,`spark.submit.deployMode` 参数指定 Spark 应用程序的部署模式为集群模式。 4. 运行 Spark 应用程序 配置完成后,可以通过以下命令提交 Spark 应用程序: ``` $SPARK_HOME/bin/spark-submit \ --class <main-class> \ --master yarn \ --deploy-mode cluster \ <application-jar> \ [application-arguments] ``` 其中,`<main-class>` 参数指定主类名,`<application-jar>` 参数指定应用程序的 JAR 包路径,`[application-arguments]` 参数指定应用程序的参数。 5. 查看应用程序运行情况 可以通过以下命令查看应用程序的运行情况: ``` yarn application -list ``` 该命令会列出所有在 YARN 上运行的应用程序。可以通过应用程序的 ID 查看应用程序的详细信息: ``` yarn application -status <application-id> ``` 以上就是连接 Spark YARN 集群的步骤。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值