iT邦幫忙

2018 iT 邦幫忙鐵人賽
DAY 15
1

增加自動化應用或做二次開發,利用Zabbix API方便界接,這章簡單介紹一下

第一步需先用帳密取得token
以下兩種方式
1.Python
參考:http://blog.51cto.com/yangrong/1559123

>>> import json,urllib2
>>> url = "http://172.16.8.176/zabbix/api_jsonrpc.php"
>>> token = json.dumps({"jsonrpc": "2.0","method": "user.login","params": {"user": "abc","password": "abc"},"id": 0})
>>> request = urllib2.Request(url,token)
>>> request.headers.update({'Content-Type':'application/json'})
>>> result = urllib2.urlopen(request)
>>> response = json.loads(result.read())
>>> response['result']
u'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'

2.API

curl -i -X POST -H 'Content-Type:application/json' -d'{"jsonrpc": "2.0","method":"user.login","params":{"user":"abc","password":"abc"},"auth": null,"id":0}' http://172.16.8.176/zabbix/api_jsonrpc.php


上面結果90f2befc33b9f2b4771e76ea7b8dcccd是我們的key(一段時間會失效)

有token後就可以對zabbix做操作

host.get去取host的hostid,以下取得Zabbix server是10084

>>> data = json.dumps({"jsonrpc":"2.0","method":"host.get","params":{"output":["hostid","name"],},"auth":"90f2befc33b9f2b4771e76ea7b8dcccd", "id":0})
>>> request = urllib2.Request(url,data)
>>> request.headers.update({'Content-Type':'application/json'})
>>> result = urllib2.urlopen(request)
>>> response = json.loads(result.read())
>>> response['result'][0]
{u'hostid': u'10084', u'name': u'Zabbix server'}

用hostid取得itemid再去查找history
https://www.zabbix.com/documentation/3.4/zh/manual/api/reference/history/get

#略
{u'itemid': u'25482', u'key_': u'system.cpu.load[all,avg1]'}
#略
{u'jsonrpc': u'2.0', u'result': [{u'itemid': u'25482', u'ns': u'636540803', u'value': u'5.5900', u'clock': u'1514522742'}

目前有py-zabbix這個可以用來接zabbix-sender
https://github.com/adubkov/py-zabbix


上一篇
4-2.應用:透過Jenkins執行F5下線 (下)
下一篇
1-12.監控工具之一:Zabbix's database
系列文
成長型維運平台 - 單人新手到多人團隊都適用的工具27
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言