亞寵展、全球?qū)櫸锂a(chǎn)業(yè)風(fēng)向標(biāo)——亞洲寵物展覽會(huì)深度解析
1800
2022-05-30
zabbix api使用
1.zabbix api介紹
我們可以調(diào)用api實(shí)現(xiàn)二次開(kāi)發(fā),或者執(zhí)行一些批量的操作,例如批量創(chuàng)建主機(jī)、批量創(chuàng)建監(jiān)控項(xiàng)
Zabbix API允許你以編程方式檢索和修改Zabbix的配置,并提供對(duì)歷史數(shù)據(jù)的訪問(wèn)。它廣泛用于:
創(chuàng)建新的應(yīng)用程序以使用Zabbix;
將Zabbix與第三方軟件集成;
自動(dòng)執(zhí)行常規(guī)任務(wù)。
具體看官方文檔介紹
2.zabbix使用api登陸令牌認(rèn)證
登陸zabbix令牌 [root@192_168_81_160 ~]# curl -X POST -H 'Content-Type: application/json' -d ' { "jsonrpc": "2.0", "method": "user.login", "params": { "user": "Admin", "password": "zabbix" }, "id": 1, "auth": null }' http://192.168.81.250/zabbix/api_jsonrpc.php 執(zhí)行出現(xiàn)這個(gè)表示成功 {"jsonrpc":"2.0","result":"a0ee3094c8577a376faec27d0a548d70","id":1} 我們將令牌值做成變量,這樣就能避免每次都要輸入了 [root@192_168_81_160 ~]# token=a0ee3094c8577a376faec27d0a548d70
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
3.獲取主機(jī)群組id以及模板id
主機(jī)群組id和模板id都存放于數(shù)據(jù)庫(kù)中,我們只需要知道在那張表即可
3.1.獲取主機(jī)群組id
主機(jī)群組id位于hstgrp表,可以看到我們的主機(jī)群組centos7的id為15
3.2.獲取監(jiān)控模板id
監(jiān)控模板id位于hosts_templates,只能說(shuō)可以在這里取到,我們只需要將一臺(tái)主機(jī)添加一個(gè)模板,然后去hosts表找到我們添加的主機(jī),然后在復(fù)制hostid去hosts_templates這個(gè)表即可拿到我們添加的模板id
我們可以寫(xiě)個(gè)SQL去查詢(xún)
SELECT h.hostid, t.templateid FROM `hosts` h LEFT JOIN hosts_templates t ON h.hostid = t.hostid WHERE h.HOST = '192.168.81.170'
1
2
3
4
5
6
7
8
4.API簡(jiǎn)單使用
api用到的json都可以從官網(wǎng)拿到
https://www.zabbix.com/documentation/4.0/zh/manual/api/reference
1
4.1.創(chuàng)建一臺(tái)主機(jī)
需要實(shí)現(xiàn)安裝好agent,并配置好啟動(dòng)
執(zhí)行api
創(chuàng)建主機(jī) curl -X POST -H 'Content-Type: application/json' -d ' { "jsonrpc": "2.0", "method": "host.create", "params": { "host": "192.168.81.160", "interfaces": [ { "type": 1, "main": 1, "useip": 1, "ip": "192.168.81.160", "dns": "", "port": "10050" } ], "groups": [ { "groupid": "15" } ], "templates": [ { "templateid": "10271" } ] }, "auth": "'$token'", "id": 1 }' http://192.168.81.250/zabbix/api_jsonrpc.php 執(zhí)行完出現(xiàn)這個(gè)表示成功 {"jsonrpc":"2.0","result":{"hostids":["10279"]},"id":1}
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
字段解釋?zhuān)?/p>
解釋?zhuān)?/p>
“host”: “192.168.81.160”, #主機(jī)名稱(chēng)
“interfaces”: [
{undefined
“type”: 1, #使用agent客戶(hù)端
“main”: 1, #默認(rèn)
“useip”: 1, #ip地址
“ip”: “192.168.81.160”, #agent的地址
“dns”: “”,
“port”: “10050” #agent端口
}
],
“groups”: [
{undefined
“groupid”: “15” #主機(jī)群組的id
}
],
“templates”: [
{
“templateid”: “10271” #模板id
}
]
添加成功,且監(jiān)控成功
4.2.刪除多臺(tái)主機(jī)
我們將192.168.81.180/190這兩臺(tái)機(jī)器給刪掉
獲取主機(jī)hostid 在zabbix庫(kù)中執(zhí)行 SELECT h.hostid, t.templateid FROM `hosts` h LEFT JOIN hosts_templates t ON h.hostid = t.hostid WHERE h.HOST = '192.168.81.180' or h.HOST = '192.168.81.190'
1
2
3
4
5
6
7
8
9
10
刪除主機(jī) [root@192_168_81_160 ~]# curl -X POST -H 'Content-Type: application/json' -d ' { "jsonrpc": "2.0", "method": "host.delete", "params": [ "10281", "10280" ], "auth": "'$token'", "id": 1 }' http://192.168.81.250/zabbix/api_jsonrpc.php 輸出這個(gè)表示成功 {"jsonrpc":"2.0","result":{"hostids":["10281","10280"]},"id":1}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
刪除成功
4.3.創(chuàng)建用戶(hù)
添加用戶(hù) [root@192_168_81_160 ~]# curl -X POST -H 'Content-Type: application/json' -d ' { "jsonrpc": "2.0", "method": "user.create", "params": { "alias": "jiangxl", "passwd": "123", "usrgrps": [ { "usrgrpid": "7" } ], "user_medias": [ { "mediatypeid": "1", "sendto": [ "jiangxl@qq.com" ], "active": 0, "severity": 63, "period": "1-7,00:00-24:00" } ] }, "auth": "'$token'", "id": 1 }' http://192.168.81.250/zabbix/api_jsonrpc.php
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
必要字段說(shuō)明
“alias”: “jiangxl”, 名稱(chēng)
“passwd”: “123”, 密碼
“usrgrpid”: “7” 用戶(hù)組id
active": 0 是否有效
API Zabbix
版權(quán)聲明:本文內(nèi)容由網(wǎng)絡(luò)用戶(hù)投稿,版權(quán)歸原作者所有,本站不擁有其著作權(quán),亦不承擔(dān)相應(yīng)法律責(zé)任。如果您發(fā)現(xiàn)本站中有涉嫌抄襲或描述失實(shí)的內(nèi)容,請(qǐng)聯(lián)系我們jiasou666@gmail.com 處理,核實(shí)后本網(wǎng)站將在24小時(shí)內(nèi)刪除侵權(quán)內(nèi)容。