API的使用
1、獲取token:
需要指定用的賬戶名 密碼和id
[root@docker-node4 ~]# curl -s -X POST -H 'Content-Type:application/json' -d '
{
"jsonrpc": "2.0",
"method": "user.login",
"params": {
"user": "zabbixadmin",
"password": "123456"
},
"id": 1
}' http://192.168.7.101/zabbix/api_jsonrpc.php | python -m json.tool
#返回的數(shù)據(jù)
{
"id": 1,
"jsonrpc": "2.0",
"result": "977781251d1222ebead6f05da1a9ec4d"
}
curl -s -X POST -H 'Content-Type:application/json' -d '
{
"jsonrpc": "2.0",
"method": "host.get",
"params": {
"filter": {
"host": [
"192.168.7.104"
]
}
},
"auth": "178bb976840186b6412461ca30d249d5",
"id": 1
}' http://192.168.7.101/zabbix/api_jsonrpc.php | python -m json.tool
- 獲取所有主機(jī)
curl -s -X POST -H 'Content-Type:application/json' -d '
{
"jsonrpc": "2.0",
"method": "host.get",
"params": {
"output": ["host"]
},
"auth": "178bb976840186b6412461ca30d249d5",
"id": 1
}' http://192.168.7.101/zabbix/api_jsonrpc.php | python -m json.tool
3.獲取所有用戶:
[root@docker-node4 ~]# curl -s -X POST -H 'Content-Type:application/json' -d '
{
"jsonrpc": "2.0",
"method": "user.get",
"params": {
"output": "extend"
},
"auth": "178bb976840186b6412461ca30d249d5",
"id": 1
}' http://192.168.7.101/zabbix/api_jsonrpc.php | python -m json.tool
4.獲取模板信息:
curl -s -X POST -H 'Content-Type:application/json' -d '
{
"jsonrpc": "2.0",
"method": "template.get",
"params": {
"output": "extend",
"filter": {
"host": [
"NGINX_Check_Statuc"
]
}
},
"auth": "178bb976840186b6412461ca30d249d5",
"id": 1
}' http://192.168.7.101/zabbix/api_jsonrpc.php | python -m json.tool
5.基于腳本獲取token
[root@docker-node4 ~]# cat token.py
#!/usr/bin/env python
# -*- coding:utf-8 -*-
import requests
import json
url = 'http://192.168.7.101/zabbix/api_jsonrpc.php'
post_data = {
"jsonrpc": "2.0",
"method": "user.login",
"params": {
"user": "zabbixadmin",
"password": "123456"
},
"id": 1
}
post_header = {'Content-Type': 'application/json'}
ret = requests.post(url, data=json.dumps(post_data), headers=post_header)
zabbix_ret = json.loads(ret.text)
if not zabbix_ret.has_key('result'):
print 'login error'
else:
print zabbix_ret.get('result')
6.通過(guò)API添加主機(jī)命令格式:
API添加主機(jī)為預(yù)先知道要添加的主機(jī)IP蒿囤、預(yù)先安裝并配置好zabbix agent艘绍、預(yù)先知道要關(guān)聯(lián)的模板ID/組ID等信息固蚤,然后同API提交請(qǐng)求添加
curl -s -X POST -H 'Content-Type:application/json' -d '
{
"jsonrpc": "2.0",
"method": "host.create", #定義方法巷嚣,N多鐘
"params": {
"host": "API Add Host Test", #自定義添加后的agent的名稱
"interfaces": [
{
"type": 1, #類(lèi)型為1表示agent,2是SNMP午阵,3是IMPI,4是JMX
"main": 1, #more接口
"useip": 1, #0是使用DNS县袱,1是使用IP地址
"ip": "192.168.0.24", #添加的zabbix agent的IP地址
"dns": "",
"port": "10050" #agent端口
}
],
"groups": [
{
"groupid": "2" #添加到的組的ID
}
],
"templates": [
{
"templateid": "10001" #關(guān)聯(lián)的模板的ID
}
]
},
"auth": "977781251d1222ebead6f05da1a9ec4d",
"id": 1
}' http://192.168.7.101/zabbix/api_jsonrpc.php | python -m json.tool
7.通過(guò)API添加主機(jī)-不帶proxy模式:
IP="
192.168.7.103
192.168.7.104
192.168.7.105
"
curl -s -X POST -H 'Content-Type:application/json' -d '
{
"jsonrpc": "2.0",
"method": "host.create",
"params": {
"host": "API Add Host Test 192.168.7.103",
"interfaces": [
{
"type": 1,
"main": 1,
"useip": 1,
"ip": "192.168.7.103",
"dns": "",
"port": "10050"
}
],
"groups": [
{
"groupid": "15"
}
],
"templates": [
{
"templateid": "10268"
}
]
},
"auth": "bbfb5beefc3a5846d2714298ec552575",
"id": 1
}' http://192.168.7.101/zabbix/api_jsonrpc.php | python -m json.tool
8.通過(guò)API添加主機(jī)-帶proxy模式:
[root@docker-node4 ~]# curl -s -X POST -H 'Content-Type:application/json' -d '
{
"jsonrpc": "2.0",
"method": "host.create",
"params": {
"host": "API Add Host Test-192.168.7.107",
"proxy_hostid": "10273",
"interfaces": [
{
"type": 1,
"main": 1,
"useip": 1,
"ip": "192.168.7.107",
"dns": "",
"port": "10050"
}
],
"groups": [
{
"groupid": "2"
}
],
"templates": [
{
"templateid": "10001"
}
]
},
"auth": "977781251d1222ebead6f05da1a9ec4d",
"id": 1
}' http://192.168.7.101/zabbix/api_jsonrpc.php | python -m json.tool
9.查看主機(jī)信息:
curl -s -X POST -H 'Content-Type:application/json' -d '
{
"jsonrpc": "2.0",
"method": "host.get",
"params": {
"filter": {
"host": [
"192.168.7.104"
]
}
},
"auth": "977781251d1222ebead6f05da1a9ec4d",
"id": 1
}' http://192.168.7.101/zabbix/api_jsonrpc.php | python -m json.tool
10.通過(guò)腳本調(diào)用API快速添加:
[root@docker-node4 ~]# cat zabbix_add_node.sh
#!/bin/bash
IP="
192.168.7.107
192.168.7.108
"
for node_ip in ${IP};do
curl -s -X POST -H 'Content-Type:application/json' -d '
{
"jsonrpc": "2.0",
"method": "host.create",
"params": {
"host": "'${node_ip}'",
"name": "linux36-nginx-web_'${node_ip}'",
"proxy_hostid": "10273",
"interfaces": [
{
"type": 1,
"main": 1,
"useip": 1,
"ip": "'${node_ip}'",
"dns": "",
"port": "10050"
}
],
"groups": [
{
"groupid": "15"
}
],
"templates": [
{
"templateid": "10268"
}
]
},
"auth": "bbfb5beefc3a5846d2714298ec552575",
"id": 1
}' http://192.168.7.101/zabbix/api_jsonrpc.php | python -m json.tool
done