Loading...
 

Zabbix API

Introduction

Getting Started

This document assumes Zabbix v3.2

Key APIs

authentication

  • To get a token which enables further API requests*

https://www.zabbix.com/documentation/3.2/manual/api#authentication

configuration

  • used to export and import templates, host, etc*

https://www.zabbix.com/documentation/3.2/manual/api/reference/configuration

Examples

Get authentication token

$ export ZBX_PASS=some_password
$ export ZBX_USER=some_user
$ export ZABBIX_API_DEV=zabbix-web-dev.some.domain.com
$ export ZABBIX_API_PROD=zabbix-web-prod.some.domain.com
$ export TOKEN=$(curl -s -X POST -H 'Content-Type: application/json-rpc' -d "{    \"jsonrpc\": \"2.0\",    \"method\": \"user.login\",    \"params\": {        \"user\": \"$ZBX_USER\",        \"password\": \"$ZBX_PASS\"    },    \"id\": 1,    \"auth\": null }" http://$ZABBIX_API_DEV/api_jsonrpc.php| jq -r .result)

Exporting templates (from DEV)

TEMPLATES=$(curl -s -X POST -H 'Content-Type: application/json-rpc' -d "{\"params\": {\"output\": \"templateid\"}, \"jsonrpc\": \"2.0\", \"method\": \"template.get\", \"auth\": \"$TOKEN\", \"id\": 0}" http://$ZABBIX_API_DEV/api_jsonrpc.php | jq -c '[.result[]|.templateid]');  curl -s -X POST -H 'Content-Type: application/json-rpc' -d "{\"jsonrpc\": \"2.0\", \"method\": \"configuration.export\", \"params\": {\"options\": {\"templates\": $TEMPLATES}, \"format\": \"json\"}, \"auth\": \"$TOKEN\", \"id\": 1}"  http://$ZABBIX_API_DEV/api_jsonrpc.php | jq .result > templates.json

Exporting Host Groups (from DEV)

HOSTGROUPS=$(curl -s -X POST -H 'Content-Type: application/json-rpc' -d "{\"params\": {\"output\": \"groupid\"}, \"jsonrpc\": \"2.0\", \"method\": \"hostgroup.get\", \"auth\": \"$TOKEN\", \"id\": 0}" http://$ZABBIX_API_DEV/api_jsonrpc.php | jq -c '[.result[]|.groupid]');  curl -s -X POST -H 'Content-Type: application/json-rpc' -d "{\"jsonrpc\": \"2.0\", \"method\": \"configuration.export\", \"params\": {\"options\": {\"groups\": $HOSTGROUPS}, \"format\": \"json\"}, \"auth\": \"$TOKEN\", \"id\": 1}"  http://$ZABBIX_API_DEV/api_jsonrpc.php | jq .result > hostgroups.json

Exporting Hosts (from DEV)

HOSTS=$(curl -s -X POST -H 'Content-Type: application/json-rpc' -d "{\"params\": {\"output\": \"hostid\"}, \"jsonrpc\": \"2.0\", \"method\": \"host.get\", \"auth\": \"$TOKEN\", \"id\": 0}" http://$ZABBIX_API_DEV/api_jsonrpc.php | jq -c '[.result[]|.hostid]');  curl -s -X POST -H 'Content-Type: application/json-rpc' -d "{\"jsonrpc\": \"2.0\", \"method\": \"configuration.export\", \"params\": {\"options\": {\"hosts\": $HOSTS}, \"format\": \"json\"}, \"auth\": \"$TOKEN\", \"id\": 1}"  http://$ZABBIX_API_DEV/api_jsonrpc.php | jq .result > hosts.json

Saving media type config

WARNING. THIS WILL OUTPUT PASSWORDS, SO TAKE CARE TO MASK THEM AFTERWARDS

curl -s -X POST -H 'Content-Type: application/json-rpc' -d "{\"jsonrpc\": \"2.0\", \"method\": \"mediatype.get\", \"params\": {\"output\": \"extend\"}, \"auth\": \"$TOKEN\", \"id\": 1}"  http://$ZABBIX_API_DEV/api_jsonrpc.php > mediatypes.json

Saving auto registration actions

eventsource “2” is auto registration, “0” is triggers

curl -s -X POST -H 'Content-Type: application/json-rpc' -d "{\"jsonrpc\": \"2.0\", \"method\": \"action.get\", \"params\": {\"output\": \"extend\", \"selectOperations\": \"extend\", \"selectRecoveryOperations\": \"extend\", \"selectFilter\": \"extend\", \"filter\": {\"eventsource\": 2}}, \"auth\": \"$TOKEN\", \"id\": 1}"  http://$ZABBIX_API_DEV/api_jsonrpc.php > reg_actions.json

Importing host groups (into PROD)

$ curl -s -X POST -H 'Content-Type: application/json-rpc' -d "{\"jsonrpc\": \"2.0\", \"method\": \"configuration.import\", \"params\": {\"source\": $(cat host_groups.json), \"format\": \"json\", \"rules\": {\"groups\": {\"createMissing\": true}}}, \"auth\": \"$TOKEN\", \"id\": 1}"  http://$ZABBIX_API_PROD/api_jsonrpc.php
{"jsonrpc":"2.0","result":true,"id":1}