Ad Widget

Collapse

JSON and LLD understanding

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Gutsycat
    Member
    • Nov 2017
    • 72

    #1

    JSON and LLD understanding

    Ok. I 'm very carefully read Creating custom LLD rules

    at https://www.zabbix.com/documentation...evel_discovery But didn't understand how it works.

    I have JSON answer from server like this:

    json1
    Code:
    {
       "data": {
          "carts": [
             {
                  "user_id": 1
                  "user_name": "John",
                  "total_price": 34
                  "in_cart" : [
                    {
                       "products": [{
                                   "product_id": 1,
                                   "price": 30,
                               },
                               {
                                   "product_id": 3,
                                   "price": 4
                               }
                       ]
                     }
                  ]
              },
             {
                  "user_id": 2
                  "user_name": "Kate",
                  "total_price": 5,
                  "in_cart" : [
                    {
                       "products": [
                           {
                               "product_id": 3,
                               "price": 5
                            }
                       ]
                    }
                  ]
              }
          ]
       }
    }
    If understand correctly I need to return with script JSON like:
    json2
    Code:
    {
        "data": [
              {
                  "{#USERID}": 1
                  "{#USERNAME}": "John",
                  "{#TOTALPRICE}": 34,
                  "{#PRODUCTID}": 1,
                  "{#PRICE}": 30,
              },
              {
                  "{#USERID}": 1
                  "{#USERNAME}": "John",
                  "{#TOTALPRICE}": 34,
                  "{#PRODUCTID}": 3,
                  "{#PRICE}": 4
              },
              {
                  "{#USERID}": 2
                  "{#USERNAME}": "Kate",
                  "{#TOTALPRICE}": 5,
                  "{#PRODUCTID}": 3,
                  "{#PRICE}": 5
              }
        ]
    }
    Ok.

    Now I need to create discovery rule. It's ok I understand.

    Now we received new json1 with data shuffled and some of them are updated:
    Code:
    {
       "data": {
          "carts": [
             {
                  "user_id": 2
                  "user_name": "Kate",
                  "total_price": 23,
                  "in_cart" : [
                     {
                       "products": [
                            {
                               "product_id": 1,
                               "price": 23
                            }
                       ]
                     }
                  ]
              },
              {
                  "user_id": 1
                  "user_name": "John",
                  "total_price":44
                  "in_cart" : [
                     {
                       "products": [
                               {
                                   "product_id": 3,
                                   "price": 44
                               }
                       ]
                     }
                  ]
              },
              {
                  "user_id": 3
                  "user_name": "Maria",
                  "total_price": 56
                  "in_cart" : [
                    {
                       "products": [
                               {
                                   "product_id": 4,
                                   "price": 56
                               }
                       ]
                     }
                  ]
              },
          ]
       }
    }
    How after that Zabbix understand where each parameter when json1 arrived?
    Last edited by Gutsycat; 07-08-2019, 14:04.
  • kloczek
    Senior Member
    • Jun 2006
    • 1771

    #2
    Defining LLD in "LLD Macros" you can add exact json path to be mapped to {#USERID} and other macros.
    In other words input json can be like in your json1 but you need to add definitions in "LLD Macros" tab.
    However that feature is available in zabbix +4.0.
    http://uk.linkedin.com/pub/tomasz-k%...zko/6/940/430/
    https://kloczek.wordpress.com/
    zapish - Zabbix API SHell binding https://github.com/kloczek/zapish
    My zabbix templates https://github.com/kloczek/zabbix-templates

    Comment

    • Gutsycat
      Member
      • Nov 2017
      • 72

      #3
      Originally posted by kloczek
      Defining LLD in "LLD Macros" you can add exact json path to be mapped to {#USERID} and other macros.
      In other words input json can be like in your json1 but you need to add definitions in "LLD Macros" tab.
      However that feature is available in zabbix +4.0.
      https://www.zabbix.com/documentation...ry_rule_filter
      Ok.

      I made Discovery preprocessing using $.data (now I'm on "carts" level)
      Next I have created LLD macros
      Code:
      {#USER_ID} $.user_id
      and it works fine, I can create items with name User_{#USER_ID}

      But next I have a problem again
      I need to generate (by lld) user items for each product like:
      1. User_1_product_1.price
      2. User_1_product_3.price
      So I need LLD macros like
      Code:
      {#PRODUCT_ID} $.in_cart.products
      but this LLD macros do not work because of array.

      In item name I write
      User_{#USER_ID}_product_{#PRODUCT_ID}_price
      What is the proper LLD macros for each product in discovery item?

      Last edited by Gutsycat; 26-08-2020, 10:25.

      Comment

      • isaqueprofeta
        Senior Member
        Zabbix Certified SpecialistZabbix Certified Professional
        • Aug 2020
        • 154

        #4
        Try looking at this example of mine and be careful to read about what different for versions=<4.0 and versions =>4.2: https://gist.github.com/isaqueprofet...eea23c07d656bf

        P.S. But if you getting a JSON from a URL, maybe LLD is not your best choice (You really need Item Prototypes or just the data?). Try looking for Dependant items with HTTP Agent + Preprocessing with JSONPath
        Last edited by isaqueprofeta; 26-08-2020, 19:04.

        Comment

        • Gutsycat
          Member
          • Nov 2017
          • 72

          #5
          Originally posted by isaqueprofeta
          Try looking for Dependant items with HTTP Agent + Preprocessing with JSONPath
          Yes I did that already using Zabbix API but I think Zabbix is strong enough to achieve this task with LLD.

          Thank you for code snippet but this is not what I'm trying to implement because kloczek said "In other words input json can be like in your json1 but you need to add definitions in "LLD Macros" tab."
          So I think it is possible to implement this task without additional scripts.

          Is this (https://support.zabbix.com/browse/ZBXNEXT-1527) feature request tied with my problem?

          Comment

          • isaqueprofeta
            Senior Member
            Zabbix Certified SpecialistZabbix Certified Professional
            • Aug 2020
            • 154

            #6
            LLD does not get item data, I think that you are misunderstanding LLD, you need to think LLD as a creator of prototypes, not a creator of "tables".

            Your example is a little off the edge for LLD or Zabbix but let's try and take an example using Item Prototype.
            • Item prototype = "Checkout for user {#USERNAME}"
              • Here you can use another call for your API to get the data from the "total_price" of one of {#USERNAME} checkouts, every new checkout is a new item value in history.
            That's why I asked you about the HTTP Agent + Preprocessing, with this you can even add some javascript for complex calculations.

            Comment

            • Gutsycat
              Member
              • Nov 2017
              • 72

              #7
              Yes there are some misunderstanding between us. I'm doing my best to be correct.

              So I have made some screenshots

              Click image for larger version  Name:	discovery_1.png Views:	10 Size:	10.1 KB ID:	407985Click image for larger version  Name:	discovery_2.png Views:	10 Size:	14.9 KB ID:	407986Click image for larger version  Name:	discovery_3.png Views:	10 Size:	11.0 KB ID:	407987Click image for larger version  Name:	discovery_4.png Views:	10 Size:	9.8 KB ID:	407988
              Last edited by Gutsycat; 28-08-2020, 12:25.

              Comment

              • isaqueprofeta
                Senior Member
                Zabbix Certified SpecialistZabbix Certified Professional
                • Aug 2020
                • 154

                #8
                Yes, you are really misunderstanding LLD and item prototypes, since you're using a nonexistent key for Zabbix Agent. There's no return_json[] key on Zabbix agent keys.

                Let me try to "Draw" a workflow of an LLD to you:
                1. LLD gets the index for Prototype items;
                2. Items are created (from Prototypes) and have a query to get their own data (the difference in the query is in the key with the LLD Macro, you want to make a new HTTP Agent query here filtering the data);
                3. Items get their own data from time to time.
                Yes I did that already using Zabbix API but I think Zabbix is strong enough to achieve this task with LLD.
                My earlier tip wasn't about Zabbix API... It's about Dependant items with HTTP Agent Item type (and I REALLY believe it's the right tool for you in that use case).

                Try a look at the docs:

                Comment

                • Gutsycat
                  Member
                  • Nov 2017
                  • 72

                  #9
                  Originally posted by isaqueprofeta
                  Yes, you are really misunderstanding LLD and item prototypes, since you're using a nonexistent key for Zabbix Agent. There's no return_json[] key on Zabbix agent keys
                  It's fine at this part. I have created UserParameter in zabbix agent. Called it "return_json". This user parameter is a python script which returns simple string using print
                  Code:
                  {"data":{"carts":[{"user_id":1,"user_name":"John","total_price":34,"in_cart":[{"products":[{"product_id":1,"price":30},{"product_id":3,"price":4}]}]},{"user_id":2,"user_name":"Kate","total_price":5,"in_cart":[{"products":[{"product_id":3,"price":5}]}]}]}}

                  Comment

                  • isaqueprofeta
                    Senior Member
                    Zabbix Certified SpecialistZabbix Certified Professional
                    • Aug 2020
                    • 154

                    #10
                    Ok, I'm out. That's some really complex use case that you have... (And I think that Zabbix is not your best solution, a framework like Laravel or a visualizer like Grafana seems a better option IMHO)

                    Comment

                    • verde
                      Member
                      • Jul 2021
                      • 39

                      #11
                      hi @Gutsycat,
                      did you figure this out? I'm in the same sort of requirement trying to get LLD macros values from a second-level JSON object. Did you find if that is possible?
                      Thanks

                      Comment

                      Working...