jq is a great tool for converting command output to JSON. USing it with Zabbix you can create some templates for monitoring linux services. You have to be careful with:
- escaping characters
- jq 1.5 and jq 1.6
This jq filter will cleanup the output from systemctl to JSON of all enabled services using jq 1.5. Test on https://jqplay.org/.
split("\n") | map_values(select(.) | split(" +";"") | {"unit": .[0], "description": (.[4:] | join(" "))})
This is cleaner, but will only work with jq 1.6
split("\n") | map_values(select(.) | capture("(?<unit>\S+)\s+(?<load>\S+)\s+(?<active>\S+)\s+(?<sub>\S+)\s+(?<desc>.+)"))
To use this in in Zabbix, you must escape all ” with \”, any double \\ need to be escaped once with \\\ and any $ used in other commands must also be escaped \$.
The final Zabbix Agent key would become the following for jq 1.5
system.run["systemctl list-units --type service --no-legend --plain | jq --slurp --raw-input 'split(\"\n\") | map_values(select(.) | split(\" +\";\"\") | {\"unit\": .[0], \"description\": (.[4:] | join(\" \"))})'"]
This cannot be tested with zabbix_get as the square brackets cannot be parsed.