In my previous Zabbix blog post, I wanted to monitor JVM garbage collection metrics. As there are 17 items to create, I looked for a way to automatically create them. I found it ๐Ÿ™‚ . In this blog, I will also demonstrate how to create:

  • A template
  • A discovery rule
  • An item prototype

Note that configuration created in previous blog was removed before creating the template.

Template

Create a template is easy. From left menu, select Data collection, click Templates and Create template:

Master Item

Creating the master item, which will be used for dependent items, is not different from previous blog:

  • Item definition:

Preprocessing rules:

Final view once master item has been created:

Discovery Rule

In template, select Discovery rule tab and click Create discover rule. Unfortunately, discover rule does not accept a static list of value, so I had to use a trick:

  • Create a JSON file on the target host
  • Use vfs.file.contents to get file content

jstat columns are not changing so often, so it is fine like that.

Let’s create the JSON file on the server with following content:

[
  { "{#JSTATCOUNTER}": "S0C" },
  { "{#JSTATCOUNTER}": "S1C"},
  { "{#JSTATCOUNTER}": "S0U"},
  { "{#JSTATCOUNTER}": "S1U"},
  { "{#JSTATCOUNTER}": "EC"},
  { "{#JSTATCOUNTER}": "EU"},
  { "{#JSTATCOUNTER}": "OC"},
  { "{#JSTATCOUNTER}": "OU"},
  { "{#JSTATCOUNTER}": "MC"},
  { "{#JSTATCOUNTER}": "MC"},
  { "{#JSTATCOUNTER}": "CCSC"},
  { "{#JSTATCOUNTER}": "CCSU"},
  { "{#JSTATCOUNTER}": "YGC"},
  { "{#JSTATCOUNTER}": "YGCT"},
  { "{#JSTATCOUNTER}": "FGC"},
  { "{#JSTATCOUNTER}": "FGCT"},
  { "{#JSTATCOUNTER}": "GCT"}
]

It is also possible to create a script, in the language of your choice, that will return the same content based on the columns from jstat output.

{#JSTATCOUNTER} is a custom macro. which I will be able to use in item prototype.

Now, discover rule look like that:

Item Prototype

Finally coming to the interesting (ie. automated) part of this blog which will save me some time.

Change Type to Dependent item (same as we did in previous blog).

To do that, in the Discovery rules list, click Item prototypes. and click Create item prototype.

{#JSTATCOUNTER} will be usable in any field of the item. In my case, I am using it in:

  • Item name
  • Item Key
  • Preprocessing rule

Remember that this prototype is a dependent item thus we have to change the type and select master item.

Final item looks like this:

And the associated Preprocessing tab:

It is similar definition as previous blog with addition of custom macro.

Verification

Next, we have to apply the template to the host. On the existing WildFly host, in templates field look for jstat and then Update button:

Now, if we click on hostname and Items, I can filter on “jstat” and find all my automatically created items:

Orange means that these items were automatically discovered and created.

Is this Really Better?

You might wonder if all this was really make anything easier for me. It is, in deed, as I can export the template (in YAML, JSON or XML) and import it in any Zabbix server setup:

{
    "zabbix_export": {
        "version": "6.4",
        "template_groups": [
            {
                "uuid": "a571c0d144a111b2aa9fcd6",
                "name": "Templates/Applications"
            }
        ],
        "templates": [
            {
                "uuid": "42c55f6c4ecd48dabdca9f2c6c5e2dca",
                "template": "jstat",
                "name": "jstat",
                "description": "Java Virtual Machine Garbage Collection monitoring template",
                "groups": [
                    {
                        "name": "Templates/Applications"
                    }
                ],
                "items": [
                    {
                        "uuid": "5a6dce56aeb4111394bba08b9efc80a4",
                        "name": "JVM GC stats",
                        "key": "system.run[sudo /app/Java/jdk/bin/jstat -gc `pgrep java`|tr -s ' '|cut -c2-]",
                        "trends": "0",
                        "value_type": "TEXT",
                        "preprocessing": [
                            {
                                "type": "TRIM",
                                "parameters": [
                                    " "
                                ]
                            },
                            {
                                "type": "CSV_TO_JSON",
                                "parameters": [
                                    " ",
                                    "",
                                    "1"
                                ]
                            }
                        ]
                    }
                ],
                "discovery_rules": [
                    {
                        "uuid": "7b8a286247d741115e11362b65ec99c",
                        "name": "Discover jstat counters",
                        "key": "vfs.file.contents[\"/tmp/jstat_metrics.json\"]",
                        "item_prototypes": [
                            {
                                "uuid": "c9270a10d6ec4997ae11194695ae53",
                                "name": "Java jstat {#JSTATCOUNTER}",
                                "type": "DEPENDENT",
                                "key": "jstat.[\"{#JSTATCOUNTER}\"]",
                                "delay": "0",
                                "value_type": "FLOAT",
                                "preprocessing": [
                                    {
                                        "type": "JSONPATH",
                                        "parameters": [
                                            "$[0].{#JSTATCOUNTER}"
                                        ]
                                    }
                                ],
                                "master_item": {
                                    "key": "system.run[sudo /app/Java/jdk/bin/jstat -gc `pgrep java`|tr -s ' '|cut -c2-]"
                                }
                            }
                        ]
                    }
                ]
            }
        ]
    }
}