Ad Widget
Collapse
Monitoring with authentication bearer in header
Collapse
X
-
-
-
Not sure if this will help anybody, but I am currently getting my token via Powershell then using Powershell to update my Template Macro with obtained token.
You'll need to create a Zabbix API key for authentication with this.
Your API endpoint may vary, so verify $contentType, etc.
You'll need to replace some variables in the script, so read through it first.
Code:<# Get token from URL/token. ($token) Post value to macro {$ZBX.TOKEN} in Template #> $username="ZabbixUsername" $password="ZabbixPassword" $zabapiurl="ZabbixURL/api_jsonrpc.php" # Get token $body = @{grant_type='password' username=$username password=$password} $contentType = 'application/x-www-form-urlencoded' $tokenResponse = Invoke-WebRequest -Method POST -Uri "YourAPIEndpoint" -body $body -ContentType $contentType $tokenResponseJSON = ConvertFrom-Json $tokenResponse.Content # Extract the token from the response $tokenBearer = $tokenResponseJSON.token_type $token = $tokenResponseJSON.access_token # Update Zabbix Macro via API $params = @{ body = @{ "jsonrpc"= "2.0" "method"= "usermacro.update" "params"= @{ "hostmacroid"= 666 "value"= $token } "id"= 1 "auth"= "Zabbix API Key goes here" } | ConvertTo-Json uri = $zabapiurl headers = @{"Content-Type" = "application/json"} method = "Post" } $result = Invoke-WebRequest @paramsComment
-
Hey, everyone. Here is a template example. Maybe will need some adjusts (preprocessing) but the idea is there.Attached FilesComment
-
You can use Script item which appeared in Zabbix 5.2 . An example JavaScript code that uses HttpRequest with access token that is first fetched could be found in one of the official templates:
Comment
-
Thanks! I am not a web\dev guy so most of that Javascript is beyond my understanding as far as converting it to something I can use with our own sites. I do see and understand the overall flow of the script item, and how the template then filters out the returned JSON data after the main item gathers data from the API endpoints. Guess it's time to learn Javascript
-
Thanks! I'll try this method when upgrade to the next LTS version, I'm still at 5.0.
-
Comment