Ad Widget

Collapse

Global Macro update automation.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • cdslaughter
    Member
    • Jun 2018
    • 69

    #1

    Global Macro update automation.

    I have several item metrics I am collecting via Master/dependent items.
    The Master item is an HTTP check that requires authentication.
    My Current solution has me do a two step process where I request an auth token and then save it to a host macro (Note that this token is only good for a short time and thus must be re authenticated every so often). I then have to pull that host macro into another HTTP check that is used as a master item (It returns an XML).
    Now the interesting thing is that I would really like to store that auth key as a "Global Macro" because I have several checks (on various hosts) that can use that auth key to run various HTTP checks I need.
    Originally I thought I could just use a host > Discovery to pull the auth key and have the discovery just create the host macro for me but that is not the case.
    I was wondering if there might be a way to use the "populates host inventory" field to force it to populate a global or host acro instead?


    Any interesting ideas you have would be appreciated.
    Note that I am aware that I could potentially configure the host LLD Discovery to collect the auth token and create/update a discovery created item with the new token on a short timer, but the number of hosts I would have to do this on might create a problem.
  • Singularity
    Member
    • Aug 2020
    • 81

    #2
    There might be other options, With a quick thought
    1. Create a global macro
    2. Create a ( shell or python or perl or php script ... programming language that you are comfortable with ) cronjob that will get token and use zabbix api to modify the global macro.

    Comment

    • BlizzardTech-Adam
      Junior Member
      • Dec 2021
      • 6

      #3
      I am actually looking at the way to store a token generated by an HTTP Agent in a Macro and use it in the header of a HTTP call similar to the way you are doing. I cannot get it to work at all.

      Comment

      • ISiroshtan
        Senior Member
        • Nov 2019
        • 324

        #4
        Hey mate.

        So let me check: what is the version of Zabbix and how big is the token?

        Also, any specific errors? Or some points you got stuck at?

        Comment


        • BlizzardTech-Adam
          BlizzardTech-Adam commented
          Editing a comment
          Hey,
          Thank you for your reply it's much appreciated!

          My lab is version 4.4, the version I'm trying to build for is v4.

          I am struggling overall on how to get a token generated via a http agent in a macro to be used in other api calls in http agent header values.

          Two products I'm working with have different tokens


          1. Example - 13a0e66bea91f507680fb5d617daa6e5
          2. Example - Bearer token that is over 550 characters long.

          As the api's I work with token expires really quick. I need to generate a new token periodically and store it in the header value.

          I cannot find much online to assist.
      • BlizzardTech-Adam
        Junior Member
        • Dec 2021
        • 6

        #5
        ...........



        Last edited by BlizzardTech-Adam; 25-12-2021, 19:43.

        Comment

        • ISiroshtan
          Senior Member
          • Nov 2019
          • 324

          #6
          First of all - it will not work for system 2. Zabbix below 5.2 has limit on Macro size of 255 characters - documentation mentioning it (I think it's possible to find where it's limited and update, like DB + PHP code, I think I heard some one was doing it. But I can not help you with that) I ended up shifting my monitoring of system with too lengthy token towards "external script" type of monitoring.

          For system #1 you can read below.

          Now, despite me saying on Reddit comment I don't have code handy coz I could not apply the solution in my case, I did some digging in my files and found you a WIP version of Python3 code I was testing. You can get it here.

          Few points to note:
          1. I was writing it for Zabbix 4.4. It was working fine. Why I scrapped the idea - Macros limit was 255 chars and my token was 1024 or something. I don't think there should be major changes in API between 4.0 and 4.4, but I'm to lazy to go and read it right now. Sorry
          2. It was WIP to test the concept in my scenario(I can not even say I finished working on it). So I did not :
            • properly comment the code
            • add any error handling
            • move all value into arguments (so there are number of values hardcoded: like host name and macro name)
          3. I was writing it for a specific 3PP API in mind, so you might need to update the function that requests token (function name is APIOAuth)
          4. I was using requests module. You would need to install it as a dependency (after Python3 is installed run 'pip3 install requests')
          5. Despite people often advising to use Global level macro, I was actually aiming at host level macro, so scripts needs to have HostName of host we try to update and Macro name on that host (+ Macro should exist on Host)
          Lines with hardcoded values:
          88-91 - variable related to 3PP API
          93-95 - variable related to Zabbix Auth
          25 - contains Zabbix HostName of the host
          43 - name of the user macro

          Think that should mostly cover it. That is the best I could do to help with your question mate.

          Comment


          • BlizzardTech-Adam
            BlizzardTech-Adam commented
            Editing a comment
            Thank you!

            What about my first example?

            Could I store an item value in my macro if its below 200 characters.

            ..

            I did try external scripts and storing json in txt documents and then using vfs.content.files to pull this but zabbix has a limit of 64kb....


            There are too many limitations with zabbix ATM.
        • ISiroshtan
          Senior Member
          • Nov 2019
          • 324

          #7
          Well mate, 4.4 is not current version now. You can update to 5.2 or newer and have both systems covered by modification of script I shared.

          In regards to external scripts, I did the following:
          1. Create a script that can collect data from 3PP API and return all needed datapoints in single JSON - lets call it collector_script.
          2. Make said script read the token from specific file in system.
          3. Create a Zabbix item of "ExternalScript" type and make it execute collector_script
          4. Create a number of dependent items to extract needed data points from JSON returned by collector script
          5. Make new script that will do Auth in 3PP API and put token in file - auth_script
          6. Run auth_script by cronjob on specific intervals (token duration minus few min. In my case token lasts 20 min, I request a new token every 15 minutes)
          Not sure it's an optimal approach, but worked for the task I had at hand.

          Comment


          • BlizzardTech-Adam
            BlizzardTech-Adam commented
            Editing a comment
            I'll give this a try tonight. Again, thank you for your help, it is much appreciated
        Working...