Friday, 21 September 2018

Zabbix API create item using CURL

Zabbix allow to create and configure items using API. It faster than clicking using GUI.

On this example I use CURL to call api.
First you need to generate token to identifying you credentials.



Check API version

curl  -H "Content-Type: application/json-rpc" -X POST  http://zabbix/zabbix/api_jsonrpc.php -d '{"jsonrpc":"2.0","method":"apiinfo.version","id":1,"auth":null,"params":{}}'


Generate API token


curl -i -X POST -H 'Content-type:application/json' -d '{"jsonrpc":"2.0","method":"user.login","params":{ "user":"adm***","password":"****"},"auth":null,"id":0}' http://zabbix/zabbix/api_jsonrpc.php


Check host ID not using API


Login to zabbix and show configuration of you host, ID of this host will be in URL address

It is a simplest way to find your host ID.


Prepare new item using existing item 


Creating item require id of host and id of interface which is used to check item.
When you know the host ID you can create simple item from interface and next create item based on existing item.

To get configuration of item you need to specify item.key

Simple json file to call api:

{
    "jsonrpc": "2.0",
    "method": "item.get",
    "params": {
        "output": "extend",
        "hostids": "10336",
        "search": {
            "key_": "system.uptime"
        },
        "sortfield": "name"
    },
    "auth": "193ab2b48e6c428477869f6dcfa4dd26",
    "id": 1
}

10336 - is your hostid
system.uptime - is you item.key which you want to show
auth - your token genarated in step generate api token.

Save you file as js.json and call

curl -i -X POST -H 'Content-type:application/json' -d @js.json http://zabbix/zabbix/api_jsonrpc.php



Your output should look like this

{
  "jsonrpc": "2.0",
  "result": [
    {
      "itemid": "54816",
      "type": "0",
      "snmp_community": "",
      "snmp_oid": "",
      "hostid": "10336",
      "name": "System uptime",
      "key_": "system.uptime",
      "delay": "1m",
      "history": "1w",
      "trends": "365d",
      "status": "0",
      "value_type": "3",
      "trapper_hosts": "",
      "units": "uptime",
      "snmpv3_securityname": "",
      "snmpv3_securitylevel": "0",
      "snmpv3_authpassphrase": "",
      "snmpv3_privpassphrase": "",
      "formula": "",
      "error": "",
      "lastlogsize": "0",
      "logtimefmt": "",
      "templateid": "23150",
      "valuemapid": "0",
      "params": "",
      "ipmi_sensor": "",
      "authtype": "0",
      "username": "",
      "password": "",
      "publickey": "",
      "privatekey": "",
      "mtime": "0",
      "flags": "0",
      "interfaceid": "219",
      "port": "",
      "description": "",
      "inventory_link": "0",
      "lifetime": "30d",
      "snmpv3_authprotocol": "0",
      "snmpv3_privprotocol": "0",
      "state": "0",
      "snmpv3_contextname": "",
      "evaltype": "0",
      "jmx_endpoint": "",
      "master_itemid": "0",
      "lastclock": "1537556736",
      "lastns": "842421756",
      "lastvalue": "13830471",
      "prevvalue": "13830411"
    }
  ],
  "id": 1
}

From this values you can check important keys:
itemid
interfaceid
and many others

Now you can customize and create your new item.


Create new item (with using SNMP)


Create json file  item.json:
 
{
"jsonrpc": "2.0",
"method": "item.create",
"params": {
"name": "Active_Connection",
"key_": "Active_Connection",
"hostid": "10336",
"delay": "60s",
"history": "90d",
"type": 1,
"trends": "365d",
"status": "0",
"value_type": "3",
"snmp_oid": "1.3.6.1.4.1.140.625.190.1.25.16.18.144.146.180.197.194.18.77.60.51.78.5.246.62.52.18",
"snmp_community": "public",
"interfaceid": "219",
"port": "1161"
},
"auth": "e8a754b6fb4c55932bc6205dac3af55a",
"id": 0
}

And run


curl -i -X POST -H 'Content-type:application/json' -d @item.json http://zabbix/zabbix/api_jsonrpc.php


No comments:

Post a Comment