zabbix API

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
#返回的数据
{
    "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
  1. 获取所有主机
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.通过API添加主机命令格式:
API添加主机为预先知道要添加的主机IP、预先安装并配置好zabbix agent、预先知道要关联的模板ID/组ID等信息,然后同API提交请求添加

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, #类型为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" #关联的模板的ID
             }
         ]
     },
     "auth": "977781251d1222ebead6f05da1a9ec4d", 
     "id": 1
 }' http://192.168.7.101/zabbix/api_jsonrpc.php | python -m json.tool   

7.通过API添加主机-不带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.通过API添加主机-带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.查看主机信息:

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.通过脚本调用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

你可能感兴趣的:(zabbix API)