In this lab we will use a bash environment and utilities ‘curl’ and ‘jq’ to perform Zabbix API calls, do some scripting.
‘curl’ is a tool to exchange JSON messages over HTTP/HTTPS.
‘jq’ utility helps to locate and extract specific elements in output.
To follow the lab we need to install ‘jq’:
# On CentOS7/RHEL7: yum install epel-release && yum install jq # On CentOS8/RHEL8: dnf install jq # On Ubuntu/Debian: apt install jq # On any 64-bit Linux platform: curl -skL "https://github.com/stedolan/jq/releases/download/jq1.5/jq-linux64" -o /usr/bin/jq && chmod +x /usr/bin/jq
Obtaining an authorization token
In order to operate with API calls we need to:
- Define an API endpoint. this is an URL, a PHP file which is designed to accept requests
- Obtain an authorization token
If you tend to execute API calls from frontend server then most likelly.
url=http://127.0.0.1/api_jsonrpc.php # or: url=http://127.0.0.1/zabbix/api_jsonrpc.php
It’s required to set the URL variable to jump to the next step. Test if you have it configured:
echo $url
Any API call needs to be used via authorization token. To put one token in variable use the command:
auth=$(curl -s -X POST -H 'Content-Type: application/json-rpc' \ -d ' {"jsonrpc":"2.0","method":"user.login","params": {"user":"api","password":"zabbix"}, "id":1,"auth":null} ' $url | \ jq -r .result )
Note
Notice there is user ‘api’ with password ‘zabbix’. This is a dedicated user for API calls.
Check if you have a session key. It should be 32 character HEX string:
echo $auth
Though process
1) visit the documentation page and pick an API flavor for example alert.get:
{ "jsonrpc": "2.0", "method": "alert.get", "params": { "output": "extend", "actionids": "3" }, "auth": "038e1d7b1735c6a5436ee9eae095879e", "id": 1 }
2) Let’s use our favorite text editor and build in Find&Replace functionality to escape all double quotes:
{ \"jsonrpc\": \"2.0\", \"method\": \"alert.get\", \"params\": { \"output\": \"extend\", \"actionids\": \"3\" }, \"auth\": \"038e1d7b1735c6a5436ee9eae095879e\", \"id\": 1 }
NOTE
Don’t ever think to do this process manually by hand!
3) Replace session key 038e1d7b1735c6a5436ee9eae095879e with our variable $auth
{ \"jsonrpc\": \"2.0\", \"method\": \"alert.get\", \"params\": { \"output\": \"extend\", \"actionids\": \"3\" }, \"auth\": \"$auth\", \"id\": 1 }
4) Now let’s encapsulate the API command with curl:
curl -s -X POST \ -H 'Content-Type: application/json-rpc' \ -d " \ { \"jsonrpc\": \"2.0\", \"method\": \"alert.get\", \"params\": { \"output\": \"extend\", \"actionids\": \"3\" }, \"auth\": \"$auth\", \"id\": 1 } " $url
By executing the previous command, it should already print a JSON content in response.
To make the output more beautiful we can pipe it to jq .:
curl -s -X POST \ -H 'Content-Type: application/json-rpc' \ -d " \ { \"jsonrpc\": \"2.0\", \"method\": \"alert.get\", \"params\": { \"output\": \"extend\", \"actionids\": \"3\" }, \"auth\": \"$auth\", \"id\": 1 } " $url | jq .
Wrap everything together in one file
This is ready to use the snippet:
#!/bin/bash # 1. set connection details url=http://127.0.0.1/api_jsonrpc.php user=api password=zabbix # 2. get authorization token auth=$(curl -s -X POST \ -H 'Content-Type: application/json-rpc' \ -d " \ { \"jsonrpc\": \"2.0\", \"method\": \"user.login\", \"params\": { \"user\": \"$user\", \"password\": \"$password\" }, \"id\": 1, \"auth\": null } " $url | \ jq -r '.result' ) # 3. show triggers in problem state curl -s -X POST \ -H 'Content-Type: application/json-rpc' \ -d " \ { \"jsonrpc\": \"2.0\", \"method\": \"trigger.get\", \"params\": { \"output\": \"extend\", \"selectHosts\": \"extend\", \"filter\": { \"value\": 1 }, \"sortfield\": \"priority\", \"sortorder\": \"DESC\" }, \"auth\": \"$auth\", \"id\": 1 } " $url | \ jq -r '.result' # 4. logout user curl -s -X POST \ -H 'Content-Type: application/json-rpc' \ -d " \ { \"jsonrpc\": \"2.0\", \"method\": \"user.logout\", \"params\": [], \"id\": 1, \"auth\": \"$auth\" } " $url
Conveniences
We can use https://jsonpathfinder.com/ to identify what should be the path to extract an element.
For example, to list all Zabbix proxies we will use and API call:
curl -s -X POST \ -H 'Content-Type: application/json-rpc' \ -d " \ { \"jsonrpc\": \"2.0\", \"method\": \"proxy.get\", \"params\": { \"output\": [\"host\"] }, \"auth\": \"$auth\", \"id\": 1 } " $url
It may print content like:
{"jsonrpc":"2.0","result":[{"host":"broceni","proxyid":"10387"},{"host":"mysql8mon","proxyid":"12066"},{"host":"riga","proxyid":"12585"}],"id":1}
Inside JSONPathFinder by using a mouse click at the right panel, we can locate a sample element what we need to extract:
It suggests a path ‘x.result[1].host’. This means to extract all elements we can remove the number and use ‘.result[].host’ like this:
curl -s -X POST \ -H 'Content-Type: application/json-rpc' \ -d " \ { \"jsonrpc\": \"2.0\", \"method\": \"proxy.get\", \"params\": { \"output\": [\"host\"] }, \"auth\": \"$auth\", \"id\": 1 } " $url | jq -r '.result[].host'
Now it prints only the proxy titles:
broceni mysql8mon riga
That is it for today. Bye.
Hi there! Good guide! But i have a problem to add trigger to ready template. What can you recommend? Trigger never added
# 2. get authorization token
auth=$(curl -s -X POST
-H ‘Content-Type: application/json-rpc’
-d ”
{
“jsonrpc”: “2.0”,
“method”: “user.login”,
“params”: {
“user”: “$user”,
“password”: “$password”
},
“id”: 1,
“auth”: null
}
” $url |
jq -r ‘.result’
)
# 3. create trigger for template
curl -s -X POST
-H ‘Content-Type: application/json-rpc’
-d ”
{
“jsonrpc”: “2.0”,
“method”: “trigger.create”,
“params”: {
“templateid”: “10351”,
“description”: “{HOST.NAME} Power monitoring on port 22 is DOWN”,
“expression”: “{D-Link DES-1210-28ME:dlink.des1210.ifOperStatus[22].last()}=2”,
“priority”: “average”,
“status”: “disabled”
},
“auth”: “$auth”,
“id”: 1
}
” $url |
jq -r ‘.result’
# 4. logout user
curl -s -X POST
-H ‘Content-Type: application/json-rpc’
-d ”
{
“jsonrpc”: “2.0”,
“method”: “user.logout”,
“params”: [],
“id”: 1,
“auth”: “$auth”
}
” $url
If possible, please update the name of the user parameter to username, thanks for the guide
No need to escape double quote:
curl -s -X POST \
-H ‘Content-Type: application/json-rpc’ \
-d ‘{
“jsonrpc”: “2.0”,
“method”: “alert.get”,
“params”: {
“output”: “extend”,
“actionids”: “3”
},
“auth”: “$token”,
“id”: 1
}’ https://my.zabbix.com/api_jsonrpc.php | jq .