API介面管理zabbix
阿新 • • 發佈:2019-01-01
1.檢視zabbix監控系統的api介面:
使用user.login
登入並獲得身份驗證令牌
[[email protected] ~]# vim zabbix-api.sh curl -s -X POST -H 'Content-Type:application/json' -d ' { "jsonrpc": "2.0", "method": "user.login", "params": { "user": "Admin", "password": "redhat" }, "id": 1, "auth": null }' http://172.25.12.10/zabbix/api_jsonrpc.php | python -m json.tool
[[email protected] ~]# ./zabbix-api.sh
{
"id": 1,
"jsonrpc": "2.0",
"result": "ff50cc18e45ab6da19b8868ec780b932" #autu
}
使用host.get得到主機資訊
[[email protected] ~]# vim zabbix-api.sh curl -s -X POST -H 'Content-Type:application/json' -d ' { "jsonrpc": "2.0", "method": "host.get", "params": { "output": [ "hostid", "host" ], "selectInterfaces":[ "interfaceid", "ip" ] }, "auth": "ff50cc18e45ab6da19b8868ec780b932", #這裡填寫前面得到的autu "id": 2 }' http://172.25.12.10/zabbix/api_jsonrpc.php | python -m json.tool
[[email protected] ~]# ./zabbix-api.sh { "id": 2, "jsonrpc": "2.0", "result": [ { "host": "Zabbix server", #zabbix server的資訊 "hostid": "10084", "interfaces": [ { "interfaceid": "1", "ip": "127.0.0.1" } ] }, { "host": "server1", #被監控主機資訊 "hostid": "10256", "interfaces": [ { "interfaceid": "4", "ip": "172.25.12.1" } ] } ] }
使用host.delete刪除zabbix監控中的主機
[[email protected] ~]# vim zabbix-api.sh
curl -s -X POST -H 'Content-Type:application/json' -d '
{
"jsonrpc": "2.0",
"method": "host.delete",
"params": [
"10256" #hosttid
],
"auth": "ff50cc18e45ab6da19b8868ec780b932",
"id": 2
}' http://172.25.12.10/zabbix/api_jsonrpc.php | python -m json.tool
[[email protected] ~]# ./zabbix-api.sh
{
"id": 2,
"jsonrpc": "2.0",
"result": {
"hostids": [
"10256"
]
}
}
使用host.create
建立被監控主機
curl -s -X POST -H 'Content-Type:application/json' -d '
{
"jsonrpc": "2.0",
"method": "host.create",
"params": {
"host": "server1",
"interfaces": [
{
"type": 1,
"main": 1,
"useip": 1,
"ip": "172.25.12.1",
"dns": "",
"port": "10050"
}
],
"groups": [
{
"groupid": "2"
}
],
"templates": [
{
"templateid": "10001"
}
]
},
"id": 2,
"auth": "ff50cc18e45ab6da19b8868ec780b932"
}' http://172.25.12.10/zabbix/api_jsonrpc.php | python -m json.tool
[[email protected] ~]# ./zabbix-api.sh
{
"id": 2,
"jsonrpc": "2.0",
"result": {
"hostids": [
"10258"
]
}
}