最近在搞kafka的监控,主要是通过jmx端口来获取item和对应的值。
由于监控项比较多,直接通过api来添加item就方便很多。。
简单写了一个脚本,调用api来批量添加item,写得比较简单(哈哈,异常都懒得去处理了。),有兴趣的同学可以扩展下。
首先手动获取template的hostid和item的app id。
1
2
|
select
hostid,
name
from
hosts
where
name
like
'%spark%'
;
select
*
from
applications
order
by
applicationid
desc
limit 5;
|
使用下面脚本创建对应的item:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
|
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()
|
本文转自菜菜光 51CTO博客,原文链接:http://blog.51cto.com/caiguangguang/1427498,如需转载请自行联系原作者