zabbix api

 

zabbix官方文档:https://www.zabbix.com/documentation/4.0/zh/manual/api

概述

Zabbix API允许你以编程方式检索和修改Zabbix的配置,并提供对历史数据的访问。它广泛用于:

  • 创建新的应用程序以使用Zabbix;

  • 将Zabbix与第三方软件集成;

  • 自动执行常规任务 (创建一百台主机)

结构

Zabbix API由许多名义上分组的独立API方法组成。每个方法执行一个特定任务。例如,方法 host.create 隶属于 host 这个API分组 ,用于创建新主机。历史上,API分组有时被称为“类”。

大多数API至少包含四种方法: get, create, update 和 delete ,分别是检索,创建,更新和删除数据,但是某些API提供一套完全不同的一组方法。

执行请求

当完成了前端的安装配置后,你就可以使用远程HTTP请求来调用API。为此,需要向 api_jsonrpc.php 位于前端目录中的文件发送HTTP POST请求。例如,如果你的Zabbix前端安装在 http://company.com/zabbix, 那么用HTTP请求来调用 apiinfo.version 方法就如下面这样:

POST http://company.com/zabbix/api_jsonrpc.php HTTP/1.1
Content-Type: application/json-rpc
 
{
"jsonrpc":"2.0","method":"apiinfo.version","id":1,"auth":null,"params":{}}

请求方法如下:

验证

请求主体

{
    "jsonrpc": "2.0",
    "method": "user.login",
    "params": {
        "user": "Admin",
        "password": "zabbix"
    },
    "id": 1,
    "auth": null
}

拿到token

在zabbix-server上操作

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://10.0.0.71/zabbix/api_jsonrpc.php

#拿到的token
{"jsonrpc":"2.0","result":"37ebe2e28ecca09c855e832e1a226d99","id":1}
token=37ebe2e28ecca09c855e832e1a226d99

查询监控项

curl -X POST -H "Content-Type: application/json-rpc" -d {
    "jsonrpc": "2.0",
    "method": "item.get",
    "params": {
        "output": "extend",
        "hostids": "10304",
        "search": {
            "key_": "agent"
        },
        "sortfield": "name"
    },
    "auth": "37ebe2e28ecca09c855e832e1a226d99",
    "id": 1
}  http://10.0.0.71/zabbix/api_jsonrpc.php

注: 每一个监控项都有它的ID值

zabbix api

创建监控项

curl -X POST -H "Content-Type: application/json-rpc" -d {
    "jsonrpc": "2.0",
    "method": "item.create",
    "params": {
        "name": "oldboy",
        "key_": "test",
        "hostid": "10304",
        "type": 0,
        "value_type": 3,
        "interfaceid": "32",
        "delay": "30s"
    },
    "auth": "37ebe2e28ecca09c855e832e1a226d99",
    "id": 3
}  http://10.0.0.71/zabbix/api_jsonrpc.php

  注:监控项名称: "name": "oldboy", 监控项key值: "key_": "test", 监控主机ID: "hostid": "10304",

  注: 创建主机的 interfaceid 需要在查询监控项结果里去找

监控主机ID: 根据自己主机ID填写

zabbix api

创建结果

zabbix api

zabbix api

带应用集创建

注: "applications": ["1466"], 是应用集的ID,在应用集地址栏查找.

curl -X POST -H "Content-Type: application/json-rpc" -d {
    "jsonrpc": "2.0",
    "method": "item.create",
    "params": {
        "name": "wei",
        "key_": "wei123",
        "hostid": "10304",
        "type": 0,
        "applications": ["1466"],
        "value_type": 3,
        "interfaceid": "32",
        "delay": "30s"
    },
    "auth": "37ebe2e28ecca09c855e832e1a226d99",
    "id": 3
}  http://10.0.0.71/zabbix/api_jsonrpc.php

zabbix api

zabbix api

批量创建监控项

思路:

  1. 创建一个应用集,并 记录ID值 (这里id值是:1469)

  2. 创建脚本 (是一种状态)

  3. 执行脚本

添加是十一种状态监控项

$ vim piliang_create_item.sh
#!/bin/bash
cat piliang_create_item.sh 
for n  in ESTABLISHED SYN_SENT SYN_RECV FIN_WAIT1 FIN_WAIT2 TIME_WAIT CLOSE CLOSE_WAIT LAST_ACK LISTEN CLOSING
do
    curl -X POST -H "Content-Type: application/json-rpc" -d {
    "jsonrpc": "2.0",
    "method": "item.create",
    "params": {
        "name": "‘$n‘数量状态数量",
        "key_": "‘$n‘",
        "hostid": "10304",
        "type": 0,
            "applications": ["1469"],
        "value_type": 3,
        "interfaceid": "32",
        "delay": "30s"
    },
    "auth": "37ebe2e28ecca09c855e832e1a226d99",
    "id": 3
}  http://10.0.0.71/zabbix/api_jsonrpc.php
done

zabbix api

zabbix api

批量创建主机

注: 主机组ID "groupid": "15" 模板ID:"templateid": "10001" ID值到网页栏查看

  1. 先创建主机组群

  2. 执行脚本

#!/bin/bash
for i in 10.0.0.{1..100}
do
curl -X POST -H "Content-Type: application/json-rpc" -d {
    "jsonrpc": "2.0",
    "method": "host.create",
    "params": {
        "host": "‘$i‘",
        "interfaces": [
            {
                "type": 1,
                "main": 1,
                "useip": 1,
                "ip": "‘$i‘",
                "dns": "",
                "port": "10050"
            }
        ],
        "groups": [
            {
                "groupid": "21"
            }
        ],
        "templates": [
            {
                "templateid": "10001"
            }
        ]
    },
    "auth": "37ebe2e28ecca09c855e832e1a226d99",
    "id": 1
}  http://10.0.0.71/zabbix/api_jsonrpc.php
done

效果图

zabbix api

 

zabbix api

上一篇:重置Windows时被Bitlocker锁的解决方案


下一篇:Windows下配置Erlang环境