4.4
2021-09-08T07:13:05Z
Templates/Modules
s
Check and repair Zabbix API token
1) Create a user 'api' with password 'zabbix'
2) Configure global macros:
Let's say you access zabbix GUI via http://zabbix.contoso.com/index.php
This means we need to configure
{$Z_API_PHP} = http://zabbix.contoso.com/api_jsonrpc.php
{$Z_API_USER} = api
{$Z_API_PASSWORD} = zabbix
and empty:
{$Z_API_SESSIONID}
Templates/Modules
Zabbix API
Zabbix raw items
-
Validate session key raw
HTTP_AGENT
check
30s
0
0
TEXT
Zabbix raw items
DISCARD_UNCHANGED_HEARTBEAT
1h
25s
{$Z_API_PHP}
{
"jsonrpc": "2.0",
"method": "proxy.get",
"params": {
"output": ["name"]
},
"auth": "{$Z_API_SESSIONID}",
"id": 1
}
JSON
POST
-
Repair session key
DEPENDENT
repair
0
Zabbix API
API repair
JAVASCRIPT
if (value.match(/Session terminated/)) {
var req = new CurlHttpRequest();
// Zabbix API
var json_rpc='{$Z_API_PHP}'
// lib curl header
req.AddHeader('Content-Type: application/json');
// First request to get authorization token
var token = JSON.parse(req.Post(json_rpc,
'{"jsonrpc":"2.0","method":"user.login","params":{"user":"{$Z_API_USER}","password":"{$Z_API_PASSWORD}"},"id":1,"auth":null}'
));
// If authorization was unsuccessful
if ( token.error )
{
// Login name or password is incorrect
return 32500;
}
else {
// update the macro
// Get the global macro ID
// We cannot plot here a very native Zabbix macro because it will be automatically expanded
// Must use a workaround to distinguish a dollar sign from the actual macro name and merge together with '+'
var id = JSON.parse(req.Post(json_rpc,
'{"jsonrpc":"2.0","method":"usermacro.get","params":{"output":["globalmacroid"],"globalmacro":true,"filter":{"macro":"{$'+'Z_API_SESSIONID'+'}"}},"auth":"'+token.result+'","id":1}'
)).result[0].globalmacroid;
// This line contains a keywork '+value+' which will grab exactly the previous value outside this javascript snippet
var overwrite = JSON.parse(req.Post(json_rpc,
'{"jsonrpc":"2.0","method":"usermacro.updateglobal","params":{"globalmacroid":"'+id+'","value":"'+token.result+'"},"auth":"'+token.result+'","id":1}'
));
// Return the id (an iteger) of the macro which was updated
return overwrite.result.globalmacroids[0];
}
} else {
return 0;
}
DISCARD_UNCHANGED_HEARTBEAT
1h
check
{last()}=32500
Install Z_API_USER and Z_API_PASSWORD in global level
AVERAGE
Make sure macros:
Z_API_PHP = http://zabbix.contoso.com/api_jsonrpc.php
Z_API_USER = api
Z_API_PASSWORD = zabbix
Z_API_SESSIONID =
Open {$ZABBIX_URL}/adm.macros.php
YES
-
Status
DEPENDENT
status
0
Zabbix API
API status
REGEX
(404 Not Found|value is too long|Session terminated|result)
\1;result=0;value is too long=1;Session terminated=2;404 Not Found=404
REGEX
(404 Not Found|value is too long|Session terminated|result)(?=.*;\1=(\d+))
\2
DISCARD_UNCHANGED_HEARTBEAT
1h
check
{last()}=404
Invalid URL configureded for Zabbix API
DISASTER
Configure a correct Z_API_PHP at:
{$ZABBIX_URL}/adm.macros.php
If you access Zabbix GUI at:
http://zabbix.contoso.com/index.php
then install:
Z_API_PHP = http://zabbix.contoso.com/api_jsonrpc.php
YES
{last()}=2
Repairing Zabbix API session key in background
AVERAGE
Session key is not valid. HTTP works fine.
YES
{last()}=1
Z_API_SESSIONID value too long
AVERAGE
Must configure Z_API_SESSIONID at:
{$ZABBIX_URL}/adm.macros.php
YES
API repair
0
OK
32500
Login name or password is incorrect
API status
0
OK
1
sessionid too long
2
must relogin
404
page not found
32500
incorrect creds