Ad Widget

Collapse

API HTTP Agent and LLD macros

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • cyberphantom
    Member
    • Dec 2021
    • 67

    #1

    API HTTP Agent and LLD macros

    Hello!
    I've configured a discovery rule that is an HTTP agent. This discovery is using a URL that returns a token value. What's happening is: I've created an prototype item on this discovery rule, it's a HTTP agent too, but this item will consum the discovery rule response (the token part) because this API requires a token change. So what I've done was:

    1. create a JavaScript preprocessing that transform this discovery rule http agent response in a array and it's working, my response is: [{"token":{"token":"MY-TOKEN","result":null,"resultCode":"0","requestId": "xxx"}]
    2. With this answer, I've created two LLD macros: {#TOKEN} with jsonpath: $.[0].token.token and {#REQUESTID} with jsonpath: $.[0].token.requestId, I've tested and it's right.

    Ok, for me, it seems that everything was right. After this, I've created my prototype item:

    1. put the key as: test.[#REQUESTID]
    2. it's an HTTP agent so I parse the URL, which is something like: http://hostname/aaaa/bbbb/cccc/dd/ff...token=MY-TOKEN, and in this token part, I did like this:
    Click image for larger version

Name:	Capturartoken.png
Views:	1400
Size:	12.6 KB
ID:	440144
    That's all. But it's not working... I don't know why, but I receive a return that my http agent item prototype is not receiving the correct token, it's invalid. It seems that it's not recognizing my token macro
    Help!

    Thanks a lot.
  • ISiroshtan
    Senior Member
    • Nov 2019
    • 324

    #2
    Hey mate. Me in your tread again.. and no, I'm not stalking you... probably

    Could you show how you set the LLD macro extraction in discovery?
    Also, right after the discovery run, did you check in item configuration of discovered item if it has token at all? If yes - was the token correct?

    Comment

    • cyberphantom
      Member
      • Dec 2021
      • 67

      #3
      Hello again!! hahah
      You mean this?

      I've checked my prototype item and I think it's not recognizing the macro... It's like this:
      Click image for larger version

Name:	capturarzabbixforum.png
Views:	1324
Size:	5.8 KB
ID:	440247
      and I receive the answer from API that the token is incorrect, so it's not catching the token at all... and I don't have idea why

      Comment

      • ISiroshtan
        Senior Member
        • Nov 2019
        • 324

        #4
        1-st - can you check that you copied the JSON correctly? One you shown missing one of the closing brackets
        [{"token":{"token":"MY-TOKEN","result":null,"resultCode":"0","requestId": "xxx"}}]
        2nd - Based on your JSON your LLD macro definition should be something like

        {#TOKEN} => $.[0].token.token

        3rd - After discovery was run, you should have actual item created on the host. If you open that item you should not see any LLD macros instead they all should be replaced by actual values. You can check if that happens.

        P.S. I've not used LLD in such scenario, so my biggest point of concern if it will actually properly update token when it changes. I'm really not sure if it will or if it will not. Guess you need to test and try.

        Comment

        • cyberphantom
          Member
          • Dec 2021
          • 67

          #5
          Sure!!
          1. This is my JSON after a JavaScript preprocessing, which is transforming my JSON in a array: [[{"token":{"token":"RANDOM-TOKEN-HERE","result":null,"resultCode":"0","requestId":" YYY"}}]]
          2. I'm using this jsonpath for LLD macro!!
          3. Discovery rule is not running, it says: "Cannot create item: item with the same key "myitemname.[{#REQUESTID}]" already exists." but there's no item on this host

          Comment

          • ISiroshtan
            Senior Member
            • Nov 2019
            • 324

            #6
            And what is JSONPath in discovery for {#REQUESTID}?

            Comment

            • cyberphantom
              Member
              • Dec 2021
              • 67

              #7
              $.[0].token.requestId

              Comment

              • ISiroshtan
                Senior Member
                • Nov 2019
                • 324

                #8
                Hm... all looks fine. But based on error it seems it does not expand macros properly. Which made me look in more details in your JSON again.
                Code:
                [[{"token":{"token":"RANDOM-TOKEN-HERE","result":null,"resultCode":"0","requestId":" YYY"}}]]
                it has 2 pairs [[ at start, meaning it's array -> array -> object. And in JSONPath we open only 1 array (hence [0]). In online JSONPath tester I used it give no damn about it(which makes me question if I should fine another one). But in Zabbix pre-processing JSONPath test it does make a difference. Could you try

                $.[0][0].token.requestId
                and
                $.[0][0].token.token

                respectively?

                Comment

                • cyberphantom
                  Member
                  • Dec 2021
                  • 67

                  #9
                  Tried both and still the same... with this, I receive the error: "no data matches the specified path", but with $.[0].token.requestId and $.[0].token.token I receive the correct answer, I've tested on zabbix.

                  Comment

                  • ISiroshtan
                    Senior Member
                    • Nov 2019
                    • 324

                    #10
                    Weeeeeird. Out of ideas as of right now.

                    Guess will have to bring back my Lab Zabbix 5 and test it out there. Maybe will manage to do it tomorrow.

                    Comment


                    • lptarik
                      lptarik commented
                      Editing a comment
                      Hello, Did you solve your issue?
                  • ISiroshtan
                    Senior Member
                    • Nov 2019
                    • 324

                    #11
                    Sorry guys, I'm from Ukraine and with current events Zabbix is really the least of my worries.

                    Comment


                    • lptarik
                      lptarik commented
                      Editing a comment
                      sorry man. I am from Turkey, my wife is from Ukraine , her mom stucked in Kiev. I feel you

                    • cyberphantom
                      cyberphantom commented
                      Editing a comment
                      oh... I really hope you get well and safe
                  Working...