Zabbix的API的使用

  上一篇:Zabbix低级主动发现之MySQL多实例

  

  登录请求(返回一个token,在后面的api中需要用到)

curl -s -X POST -H 'Content-Type:application/json' -d'
{
    "jsonrpc": "2.0",
    "method": "user.login",
    "params": {
        "user": "Admin",
        "password": "zabbix"
        
    },
    "id": 1
}' http://192.168.80.130/zabbix/api_jsonrpc.php | python -m json.tool


{
    "id": 1,
    "jsonrpc": "2.0",
    "result": "940da948f7aabb512d71c13ced76699a"
}

  

  host get

curl -s -X POST -H 'Content-Type:application/json' -d'
{
    "jsonrpc": "2.0",
    "method": "host.get",
    "params": {
        "output":["host"]
    },
    
    "auth": "940da948f7aabb512d71c13ced76699a",
    "id": 1
}' http://192.168.80.130/zabbix/api_jsonrpc.php | python -m json.tool


{
    "id": 1,
    "jsonrpc": "2.0",
    "result": [
        {
            "host": "Zabbix server",
            "hostid": "10084"
        },
        {
            "host": "linux-node2.example.com",
            "hostid": "10117"
        }
    ]
}

  获取到了host和hostid  怎么查看

  添加主机的api

  修改配置

  修改了ip groupid 和templateid(删除了官方的资产信息)

curl -s -X POST -H 'Content-Type:application/json' -d'
{
    "jsonrpc": "2.0",
    "method": "host.create",
    "params": {
        "host": "Linux server",
        "interfaces": [
            {
                "type": 1,
                "main": 1,
                "useip": 1,
                "ip": "192.168.80.160",
                "dns": "",
                "port": "10050"
            }
        ],
        "groups": [
            {
                "groupid": "2"
            }
        ],
        "templates": [
            {
                "templateid": "10001"
            }
        ]
    },
    "auth": "940da948f7aabb512d71c13ced76699a",
    "id": 1
}' http://192.168.80.130/zabbix/api_jsonrpc.php | python -m json.tool

 

  添加成功

  

  创建主机api的应用脚本批量添加主机(list.txt写好需要添加主机的ip列表)

  zabbix_host_creates.sh

#!/bin/bash
#login
tok=`curl -s -X POST -H 'Content-Type:application/json' -d'
{
    "jsonrpc": "2.0",
    "method": "user.login",
    "params": {
        "user": "Admin",
        "password": "zabbix"
        
    },
    "id": 1
}' http://192.168.80.130/zabbix/api_jsonrpc.php | python -m json.tool`
jsson= echo "$tok" | grep result | awk -F '"' '{print $4}'
#create hosts
for ip in `cat list.txt` 
do
curl -s -X POST -H 'Content-Type:application/json' -d'
{
    "jsonrpc": "2.0",
    "method": "host.create",
    "params": {
        "host": '\"$ip\"',
        "interfaces": [
            {
                "type": 1,
                "main": 1,
                "useip": 1,
                "ip": '\"$ip\"',
                "dns": "",
                "port": "10050"
            }
        ],
        "groups": [
            {
                "groupid": "11"
            }
        ],
        "templates": [
            {
                "templateid": "10001"
            }
        ]
    },
    "auth": '\"$jsson\"',
    "id": 1
}' http://192.168.80.130/zabbix/api_jsonrpc.php | python -m json.tool
done

  运行报错了

 

  下一篇:Zabbix分布式监控

  

  

posted @ 2018-03-25 08:52  minseo  阅读(296)  评论(0编辑  收藏  举报