Ad Widget

Collapse

Monitoring with authentication bearer in header

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Grumpyblade1994
    Junior Member
    • Feb 2022
    • 2

    #16
    Originally posted by fsnsantana
    I did it using a LLD, where discovery item is a HTTP agent requesting the access_token and preprocessing to output something as LLD macro {#TOKEN} with the interval you need before token expires. Then all item prototypes HTTP agent use that macro at Header "Authentication: Bearer {#TOKEN}".

    If still useful to someone, I can post a template as example.
    Can you show me how you did that?
    I have a similar problem in my environment where i have to deploy this to multiple Hosts.

    Comment

    • Pedro Ourique
      Junior Member
      • Oct 2021
      • 12

      #17
      Hi!

      Could you post your Template please?

      Comment

      • Zablab
        Junior Member
        • Jan 2022
        • 28

        #18
        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 @params

        Comment

        • fsnsantana
          Junior Member
          • May 2021
          • 2

          #19
          Hey, everyone. Here is a template example. Maybe will need some adjusts (preprocessing) but the idea is there.
          Attached Files

          Comment


          • fsnsantana
            fsnsantana commented
            Editing a comment
            P.S.: Please use your credential by using secret text macros.
        • dimir
          Zabbix developer
          • Apr 2011
          • 1080

          #20
          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


          • Zablab
            Zablab commented
            Editing a 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

          • fsnsantana
            fsnsantana commented
            Editing a comment
            Thanks! I'll try this method when upgrade to the next LTS version, I'm still at 5.0.
        Working...