最近在搞kafka的监控,主要是通过jmx端口来获取item和对应的值。
由于监控项比较多,直接通过api来添加item就方便很多。。
简单写了一个脚本,调用api来批量添加item,写得比较简单(哈哈,异常都懒得去处理了。),有兴趣的同学可以扩展下。
首先手动获取template的hostid和item的app id。
select hostid,name from hosts where name like '%spark%'; select * from applications order by applicationid desc limit 5;
使用下面脚本创建对应的item:
import os import subprocess import urllib2 import sys import json def requestJason(url,values): data = json.dumps(values) req = urllib2.Request(url, data, {'Content-Type': 'application/json-rpc'}) response = urllib2.urlopen(req, data) data_get = response.read() output = json.loads(data_get) print output try: message = output['result'] except: message = output['error']['data'] raise Exception(message) print json.dumps(message) return output def authenticate(url, username, password): values = {'jsonrpc': '2.0', 'method': 'user.login', 'params': { 'user': username, 'password': password }, 'id': '0' } idvalue = requestJason(url,values) return idvalue['result'] def createitem(itemname,itemkey): url = 'http://xxx/api_jsonrpc.php' username = 'xxx' password = 'xxx' auth = authenticate(url, username, password) values = ( {'jsonrpc': '2.0', "method": "item.create", "params": { "name":itemname, "key_":itemkey, "hostid": xxxx, "applications": xxxx, "type":"7", "delay":"60", "value_type":"0", }, "auth": auth, "id": '3' }) print values output = requestJason(url,values) print output['result'] if __name__ == '__main__': f=open('/tmp/kafka.log','r') for i in f.readlines(): cmd = """ java -jar /apps/sh/zabbix_scripts/java/cmdline-jmxclient-0.10.3.jar - 127.0.0.1:9999 '%s' |grep -B 100 Operations|egrep -v 'Attributes|Operations'|awk '{print $1}'|sed 's/://g' """ % (i.strip()) itemname = i.strip().split('=')[1].replace(',type','').replace('"','') p = subprocess.Popen(cmd, stdin = subprocess.PIPE,stdout = subprocess.PIPE, stderr = subprocess.PIPE, shell = True) itemname2 = i.strip().replace('"','') for x in p.stdout.readlines(): if 'RateUnit' in x or 'EventType' in x: pass else: #print "9093_" + itemname + "_" + x.strip() + " kafka[9999,"+ itemname2 + "," + x.strip() + ']' itemnamex = "9093_" + itemname + "_" + x.strip() itemkeyx = "kafka[9999,"+ itemname2 + "," + x.strip() + ']' print itemnamex,itemkeyx createitem(itemnamex,itemkeyx) try: createitem(itemnamex,itemkeyx) except Exception,e: print str(e) f.close()