Skip to content

Commit

Permalink
Update hbase template, make it more compatible
Browse files Browse the repository at this point in the history
  • Loading branch information
Staroon committed Jun 7, 2018
1 parent dc19e87 commit f41a5d5
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 38 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@

## 说明
- 目前支持监控HDFS、NameNode, YARN ResourceManager, Hive, Zookeeper, HBase Master.
- 在CentOS7,Zabbix3.2.6, Hadoop2.7.3, Hive1.2.1, Zookeeper3.4.10, HBase1.2.6下测试通过。
- 在CentOS7,Zabbix3.2.6, Hadoop2.7.3, Hive1.2.1, Zookeeper3.4.10, HBase1.2.6/0.98.6下测试通过。
- 部分监控项是参照:[zubayr/zabbix_hadoop_monitoring](https://github.com/zubayr/zabbix_hadoop_monitoring)[zabbix_mikoomi_templates](https://code.google.com/archive/p/mikoomi/)构建的,特别感谢这两个项目的开发者!!!
- 对于不同Haopp版本的集群有很大几率不适用,提供的脚本只是一个思路参考。
- 提供的脚本只是一个思路参考,理论上兼容Hadoop2.7.x所有版本
93 changes: 57 additions & 36 deletions hbase-master-template/zabbix-HMaster.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,16 @@
# Generate URL
# ---------------------------------
# This function converts the servername to URL which we need to query.
def get_url(server_name, listen_port):


def get_url(server_name, listen_port, topic_name):
"""
Generating URL to get the information
from namenode/namenode
:param server_name:
:param listen_port:
:param topic_name:
:return:
"""

Expand All @@ -27,7 +31,11 @@ def get_url(server_name, listen_port):
print("Pass valid Hostname")
exit()

URL = "http://"+server_name+":"+str(listen_port)+"/jmx?qry=Hadoop:*"
if not topic_name:
topic_name = "*"

URL = "http://"+server_name+":" + \
str(listen_port)+"/jmx?qry=Hadoop:"+topic_name
return URL


Expand All @@ -45,64 +53,79 @@ def load_url_as_dictionary(url):
return json.load(urllib.urlopen(url))


def json_processing(json_dict):
def json_processing(server_name, listen_port):

namenode_dict = {}

server_json = load_url_as_dictionary(
get_url(server_name, listen_port, "service=HBase,*,sub=Server"))
jvm_json = load_url_as_dictionary(
get_url(server_name, listen_port, "service=HBase,name=JvmMetrics"))
ipc_json = load_url_as_dictionary(
get_url(server_name, listen_port, "service=HBase,*,sub=IPC"))

# Server
isActiveMaster = str(json_dict['beans'][0]['tag.isActiveMaster'])
isActiveMaster = str(server_json['beans'][0]['tag.isActiveMaster'])
namenode_dict['isActive'] = isActiveMaster
startTimeStamp = float(str(json_dict['beans'][0]['masterStartTime'])[:10])
startTimeStamp = float(
str(server_json['beans'][0]['masterStartTime'])[:10])
startTimeArray = time.localtime(startTimeStamp)
namenode_dict['startTime'] = time.strftime('%Y-%m-%d %H:%M:%S', startTimeArray)
namenode_dict['startTime'] = time.strftime(
'%Y-%m-%d %H:%M:%S', startTimeArray)
if "true" == isActiveMaster:
activeTimeStamp = float(str(json_dict['beans'][0]['masterActiveTime'])[:10])
activeTimeStamp = float(
str(server_json['beans'][0]['masterActiveTime'])[:10])
activeTimeArray = time.localtime(activeTimeStamp)
namenode_dict['activeTime'] = time.strftime('%Y-%m-%d %H:%M:%S', activeTimeArray)
namenode_dict['regionServers'] = json_dict['beans'][0]['numRegionServers']
namenode_dict['deadRegionServers'] = json_dict['beans'][0]['numDeadRegionServers']
namenode_dict['activeTime'] = time.strftime(
'%Y-%m-%d %H:%M:%S', activeTimeArray)
namenode_dict['regionServers'] = server_json['beans'][0]['numRegionServers']
namenode_dict['deadRegionServers'] = server_json['beans'][0]['numDeadRegionServers']

# JvmMetrics
namenode_dict['non_heap_mem_used'] = json_dict['beans'][3]['MemNonHeapUsedM']
namenode_dict['committed_non_heap_mem'] = json_dict['beans'][3]['MemNonHeapCommittedM']
namenode_dict['heap_mem_used'] = json_dict['beans'][3]['MemHeapUsedM']
namenode_dict['committed_heap_mem'] = json_dict['beans'][3]['MemHeapCommittedM']
namenode_dict['max_heap_mem'] = json_dict['beans'][3]['MemHeapMaxM']
namenode_dict['gcCount'] = json_dict['beans'][3]['GcCount']
namenode_dict['gcTimeMillis'] = json_dict['beans'][3]['GcTimeMillis']
namenode_dict['threadsRunnable'] = json_dict['beans'][3]['ThreadsRunnable']
namenode_dict['threadsBlocked'] = json_dict['beans'][3]['ThreadsBlocked']
namenode_dict['threadsWaiting'] = json_dict['beans'][3]['ThreadsWaiting']
namenode_dict['non_heap_mem_used'] = jvm_json['beans'][0]['MemNonHeapUsedM']
namenode_dict['committed_non_heap_mem'] = jvm_json['beans'][0]['MemNonHeapCommittedM']
namenode_dict['heap_mem_used'] = jvm_json['beans'][0]['MemHeapUsedM']
namenode_dict['committed_heap_mem'] = jvm_json['beans'][0]['MemHeapCommittedM']
namenode_dict['max_heap_mem'] = jvm_json['beans'][0]['MemHeapMaxM']
namenode_dict['gcCount'] = jvm_json['beans'][0]['GcCount']
namenode_dict['gcTimeMillis'] = jvm_json['beans'][0]['GcTimeMillis']
namenode_dict['threadsRunnable'] = jvm_json['beans'][0]['ThreadsRunnable']
namenode_dict['threadsBlocked'] = jvm_json['beans'][0]['ThreadsBlocked']
namenode_dict['threadsWaiting'] = jvm_json['beans'][0]['ThreadsWaiting']

# IPC
namenode_dict['receivedBytes'] = json_dict['beans'][4]['receivedBytes']
namenode_dict['sentBytes'] = json_dict['beans'][4]['sentBytes']
namenode_dict['receivedBytes'] = ipc_json['beans'][0]['receivedBytes']
namenode_dict['sentBytes'] = ipc_json['beans'][0]['sentBytes']

if "false" == isActiveMaster:
# JvmMetrics
namenode_dict['non_heap_mem_used'] = json_dict['beans'][1]['MemNonHeapUsedM']
namenode_dict['committed_non_heap_mem'] = json_dict['beans'][1]['MemNonHeapCommittedM']
namenode_dict['heap_mem_used'] = json_dict['beans'][1]['MemHeapUsedM']
namenode_dict['committed_heap_mem'] = json_dict['beans'][1]['MemHeapCommittedM']
namenode_dict['max_heap_mem'] = json_dict['beans'][1]['MemHeapMaxM']
namenode_dict['gcCount'] = json_dict['beans'][1]['GcCount']
namenode_dict['gcTimeMillis'] = json_dict['beans'][1]['GcTimeMillis']
namenode_dict['threadsRunnable'] = json_dict['beans'][1]['ThreadsRunnable']
namenode_dict['threadsBlocked'] = json_dict['beans'][1]['ThreadsBlocked']
namenode_dict['threadsWaiting'] = json_dict['beans'][1]['ThreadsWaiting']
namenode_dict['non_heap_mem_used'] = jvm_json['beans'][0]['MemNonHeapUsedM']
namenode_dict['committed_non_heap_mem'] = jvm_json['beans'][0]['MemNonHeapCommittedM']
namenode_dict['heap_mem_used'] = jvm_json['beans'][0]['MemHeapUsedM']
namenode_dict['committed_heap_mem'] = jvm_json['beans'][0]['MemHeapCommittedM']
namenode_dict['max_heap_mem'] = jvm_json['beans'][0]['MemHeapMaxM']
namenode_dict['gcCount'] = jvm_json['beans'][0]['GcCount']
namenode_dict['gcTimeMillis'] = jvm_json['beans'][0]['GcTimeMillis']
namenode_dict['threadsRunnable'] = jvm_json['beans'][0]['ThreadsRunnable']
namenode_dict['threadsBlocked'] = jvm_json['beans'][0]['ThreadsBlocked']
namenode_dict['threadsWaiting'] = jvm_json['beans'][0]['ThreadsWaiting']

return namenode_dict


def write_data_to_file(json, file_path, name_in_zabbix):
txt_file = open(file_path, 'w+')
for keys in json:
txt_file.writelines(name_in_zabbix +' '+ str(keys) +' '+ str(json[keys]) + '\n')
txt_file.writelines(name_in_zabbix + ' ' +
str(keys) + ' ' + str(json[keys]) + '\n')


def usage():
print '''
Usage: $SCRIPT_NAME <HBaseMaster_host> <port> <file_path> <zabbix_server_name>
'''


if __name__ == '__main__':

if len(sys.argv) == 5:
Expand All @@ -112,9 +135,7 @@ def usage():
file_path = sys.argv[3]
name_in_zabbix = sys.argv[4]

url = get_url(hmaster_host, hmaster_listen_port)
json_as_dictionary = load_url_as_dictionary(url)
json_processed = json_processing(json_as_dictionary)
json_processed = json_processing(hmaster_host, hmaster_listen_port)
write_data_to_file(json_processed, file_path, name_in_zabbix)

else:
Expand Down

0 comments on commit f41a5d5

Please sign in to comment.