Search code examples
jsonparsingjsonpathzabbix

Zabbix JSONpath LLD


I am trying to make discoverig instances from AWS Auto Scaling Group. I have json like this:

{
  "AutoScalingGroups": [
    {
      "AutoScalingGroupName": "xxx",
      "AutoScalingGroupARN": "arn:aws:autoscaling:eu-central-1:xxx",
      "LaunchTemplate": {
        "LaunchTemplateId": "lt-xxx",
        "LaunchTemplateName": "xxx",
        "Version": "$Latest"
      },
      "MinSize": 2,
      "MaxSize": 10,
      "DesiredCapacity": 2,
      "DefaultCooldown": 300,
      "AvailabilityZones": [
        "eu-central-1a",
        "eu-central-1c",
        "eu-central-1b"
      ],
      "LoadBalancerNames": [],
      "TargetGroupARNs": [],
      "HealthCheckType": "EC2",
      "HealthCheckGracePeriod": 0,
      "Instances": [
        {
          "InstanceId": "i-xxx111",
          "InstanceType": "c5.2xlarge",
          "AvailabilityZone": "eu-central-1b",
          "LifecycleState": "InService",
          "HealthStatus": "Healthy",
          "LaunchTemplate": {
            "LaunchTemplateId": "lt-xxx",
            "LaunchTemplateName": "xxx",
            "Version": "11"
          },
          "ProtectedFromScaleIn": false
        },
        {
          "InstanceId": "i-xxx222",
          "InstanceType": "c5.2xlarge",
          "AvailabilityZone": "eu-central-1a",
          "LifecycleState": "InService",
          "HealthStatus": "Healthy",
          "LaunchTemplate": {
            "LaunchTemplateId": "lt-xxx",
            "LaunchTemplateName": "xxx",
            "Version": "11"
          },
          "ProtectedFromScaleIn": false
        }
      ]
    }
  ]
}

And I want to create discovered items with name of instance id and values - healthstatus.

I created discovery rule with master item as previous json. And i got instance ids with item prototype that have next preprocessing JSONPath - $.AutoScalingGroups.[*].Instances.[*].InstanceId

This item has value next format ["i-xxx111","i-xxx222"], but i dont understand how to create next item prototype with names as each instance-id from this list.

So, can someone help me?

I am using zabbix version 6.2.4


Solution

  • You need to turn that object in the Zabbix LLD format. Using JSONpath won't help us in this case. You can use a JavaScript preprocessing in the Zabbix Item.

    res = []
    obj = JSON.parse(value)
    for (asg in obj.AutoScalingGroups) {
        for (i in obj.AutoScalingGroups[asg].Instances) {
            res.push({"id": obj.AutoScalingGroups[asg].Instances[i].InstanceId})
      }
    }
    return JSON.stringify(res)
    

    result: [{"id":"i-xxx111"},{"id":"i-xxx222"}]

    Don't forget to turn $.id in {#ID} in Zabbix LLD macro.

    (Would be easier if we could use for (.. of ..) in ducktape...)

    Here's a fiddle: https://jsfiddle.net/egch0vap/1/

    See: https://www.zabbix.com/documentation/current/en/manual/config/items/preprocessing/javascript

    test in zabbix