获取hadoop集群资源信息

根据hadoop提供的restful接口获取
http://hadoop.apache.org/docs/stable/  -->  YARN REST APIs  --> Resource Manager
1、获取每个node的信息

import urllib2
import json

def check_unhealth_nodes() :
		url = 'http://ip:port/ws/v1/cluster/nodes'
		req = urllib2.Request(url)
		res_data = urllib2.urlopen(req)
		nodes = json.loads(res_data.read())['nodes']['node']
		for node in nodes :
		    if node['state'] == u'UNHEALTHY' :
		        print("master ip:%s unhealthy nodes ip : %s" % (master_ip,node['nodeHostName']))
		        
if __name__ == "__main__":
    check_unhealth_nodes()

2、获取集群任务排队数与可用内存量

import urllib2
import json

url = 'http://ip:port/ws/v1/cluster/metrics'
req = urllib2.Request(url)
res_data = urllib2.urlopen(req)
cluster_info = json.loads(res_data.read())["clusterMetrics"]
availableMB = cluster_info["availableMB"]  #可用内存量,单位MB
appsPending = cluster_info["appsPending"]  #任务排队数量

 

你可能感兴趣的:(hadoop)