Skip to content

Instantly share code, notes, and snippets.

@bakaut
Last active January 24, 2019 08:44
Show Gist options
  • Select an option

  • Save bakaut/8a05d3479e4e08a976f974a4255c9d38 to your computer and use it in GitHub Desktop.

Select an option

Save bakaut/8a05d3479e4e08a976f974a4255c9d38 to your computer and use it in GitHub Desktop.
Zabbix linux lld discovery example. Parse from json url
Zabbix lld discovery example. Data source url, that return json formated data.
We have a url than provide some data, we want monitor. We want do less, get more. Thus we diside use zabbix lld discovery script and write on python.
Functions:
1. Get from url json, that we have to monitor.
2. Parse it and make output json in zabbix json format.
3. According to script arguments, we can get:
-All data from json.
-All items, that have string type.
-All items, that have integer type.
Using:
1. Create script, put it in /etc/zabbix/ directory and make it executable.
2. Create 3 custom parameters in /etc/zabbix-client/conf.d/userparameter_json.conf
3. Import template zabbix_json_discovery.xml to zabbix server.
4. Link template to server, on that we confugure custom zabbix parameters.
{
"data":[
{"{#ROOTITEM}":"redis"}, {"{#PARENTITEMNAME}":"connected_clients"}, {"{#PARENTITEMVALUE}":"176"},
{"{#ROOTITEM}":"redis"}, {"{#PARENTITEMNAME}":"redis_version"}, {"{#PARENTITEMVALUE}":"3.2.10"},
{"{#ROOTITEM}":"redis"}, {"{#PARENTITEMNAME}":"uptime_in_days"}, {"{#PARENTITEMVALUE}":"0"},
{"{#ROOTITEM}":"redis"}, {"{#PARENTITEMNAME}":"used_memory_human"}, {"{#PARENTITEMVALUE}":"232.28M"},
{"{#ROOTITEM}":"redis"}, {"{#PARENTITEMNAME}":"used_memory_peak_human"}, {"{#PARENTITEMVALUE}":"266.57M"},
{"{#ROOTITEM}":"redis"}, {"{#PARENTITEMNAME}":"used_memory_peak_human"}, {"{#PARENTITEMVALUE}":"266.57M"},
{"{#ROOTITEM}":"sidekiq"}, {"{#PARENTITEMNAME}":"busy"}, {"{#PARENTITEMVALUE}":11},
{"{#ROOTITEM}":"sidekiq"}, {"{#PARENTITEMNAME}":"dead"}, {"{#PARENTITEMVALUE}":4224},
{"{#ROOTITEM}":"sidekiq"}, {"{#PARENTITEMNAME}":"default_latency"}, {"{#PARENTITEMVALUE}":0},
{"{#ROOTITEM}":"sidekiq"}, {"{#PARENTITEMNAME}":"enqueued"}, {"{#PARENTITEMVALUE}":0},
{"{#ROOTITEM}":"sidekiq"}, {"{#PARENTITEMNAME}":"failed"}, {"{#PARENTITEMVALUE}":8443968},
{"{#ROOTITEM}":"sidekiq"}, {"{#PARENTITEMNAME}":"processed"}, {"{#PARENTITEMVALUE}":1617036633},
{"{#ROOTITEM}":"sidekiq"}, {"{#PARENTITEMNAME}":"processes"}, {"{#PARENTITEMVALUE}":11},
{"{#ROOTITEM}":"sidekiq"}, {"{#PARENTITEMNAME}":"retries"}, {"{#PARENTITEMVALUE}":17},
{"{#ROOTITEM}":"sidekiq"}, {"{#PARENTITEMNAME}":"scheduled"}, {"{#PARENTITEMVALUE}":26},
{"{#ROOTITEM}":"sidekiq"}, {"{#PARENTITEMNAME}":"scheduled"}, {"{#PARENTITEMVALUE}":26}
]
}
#!/usr/bin/python
import json
import sys
import requests
from requests.auth import HTTPBasicAuth
res = []
res_txt = {}
res_int = {}
with requests.get('http://url/stats', auth=('user', 'pass')) as url:
data = url.json()
for root in data:
for parent in data[root]:
res.append({"{#ROOTPARENT}": root+parent, {"{#ROOTITEM}": root, "{#PARENTITEMNAME}": parent, "{#PARENTITEMVALUE}": data[root][parent]})
if isinstance(data[root][parent], int):
res_int.update({parent: data[root][parent]})
else:
res_txt.update({parent: data[root][parent]})
resData = {"data": res}
if len(sys.argv) == 1:
print json.dumps(resData)
else:
if sys.argv[2] == 'txt':
print res_txt[sys.argv[1]]
else:
print res_int[sys.argv[1]]
#!/usr/bin/python
import sys
import json
import requests
from requests.auth import HTTPBasicAuth
with requests.get('http://url/stats', auth=('user', 'pass')) as url:
data = url.json()
res = []
for root in data:
for parent in data[root]:
if isinstance(data[root][parent], int):
res.append({"{#DATATYPE}": "INT","{#ROOTITEM}": root, "{#PARENTITEM}": parent, "{#PARENTVALUE}": data[root][parent]})
else:
res.append({"{#DATATYPE}": "TEXT","{#ROOTITEM}": root, "{#PARENTITEM}": parent, "{#PARENTVALUE}": data[root][parent]})
resData = {"data": res}
if len(sys.argv) == 1:
print json.dumps(resData)
else:
print data[sys.argv[1]][sys.argv[2]]
[{
"data": [{
"#parentitemvalue": "232.28M",
"#parentitemname": "used_memory_human",
"#rootitem": "redis"
}, {
"#parentitemvalue": "266.57M",
"#parentitemname": "used_memory_peak_human",
"#rootitem": "redis"
}, {
"#parentitemvalue": "3.2.10",
"#parentitemname": "redis_version",
"#rootitem": "redis"
}, {
"#parentitemvalue": "0",
"#parentitemname": "uptime_in_days",
"#rootitem": "redis"
}, {
"#parentitemvalue": "176",
"#parentitemname": "connected_clients",
"#rootitem": "redis"
}, {
"#parentitemvalue": 26,
"#parentitemname": "scheduled",
"#rootitem": "sidekiq"
}, {
"#parentitemvalue": 17,
"#parentitemname": "retries",
"#rootitem": "sidekiq"
}, {
"#parentitemvalue": 11,
"#parentitemname": "processes",
"#rootitem": "sidekiq"
}, {
"#parentitemvalue": 11,
"#parentitemname": "busy",
"#rootitem": "sidekiq"
}, {
"#parentitemvalue": 0,
"#parentitemname": "enqueued",
"#rootitem": "sidekiq"
}, {
"#parentitemvalue": 8443968,
"#parentitemname": "failed",
"#rootitem": "sidekiq"
}, {
"#parentitemvalue": 1617036633,
"#parentitemname": "processed",
"#rootitem": "sidekiq"
}, {
"#parentitemvalue": 0,
"#parentitemname": "default_latency",
"#rootitem": "sidekiq"
}, {
"#parentitemvalue": 4224,
"#parentitemname": "dead",
"#rootitem": "sidekiq"
}]
}]
{
"sidekiq": {
"processed": 1617036633,
"failed": 8443968,
"busy": 11,
"processes": 11,
"enqueued": 0,
"scheduled": 26,
"retries": 17,
"dead": 4224,
"default_latency": 0
},
"redis": {
"redis_version": "3.2.10",
"uptime_in_days": "0",
"connected_clients": "176",
"used_memory_human": "232.28M",
"used_memory_peak_human": "266.57M"
}
}
UserParameter=json.data.keys, /usr/lib/zabbix/externalscripts/json-keys.py
UserParameter=json.data.new[*], /usr/lib/zabbix/externalscripts/json-keys.py $1 $2
# -*- mode: ruby -*-
# vi: set ft=ruby :
# All Vagrant configuration is done below. The "2" in Vagrant.configure
# configures the configuration version (we support older styles for
# backwards compatibility). Please don't change it unless you know what
# you're doing.
Vagrant.configure("2") do |config|
# The most common configuration options are documented and commented below.
# For a complete reference, please see the online documentation at
# https://docs.vagrantup.com.
# Every Vagrant development environment requires a box. You can search for
# boxes at https://atlas.hashicorp.com/search.
config.vm.box = "debian/jessie64"
# Disable automatic box update checking. If you disable this, then
# boxes will only be checked for updates when the user runs
# `vagrant box outdated`. This is not recommended.
# config.vm.box_check_update = false
# Create a forwarded port mapping which allows access to a specific port
# within the machine from a port on the host machine. In the example below,
# accessing "localhost:8080" will access port 80 on the guest machine.
# config.vm.network "forwarded_port", guest: 80, host: 8080
# Create a private network, which allows host-only access to the machine
# using a specific IP.
# config.vm.network "private_network", ip: "192.168.33.10"
# Create a public network, which generally matched to bridged network.
# Bridged networks make the machine appear as another physical device on
# your network.
config.vm.network "public_network"
# Share an additional folder to the guest VM. The first argument is
# the path on the host to the actual folder. The second argument is
# the path on the guest to mount the folder. And the optional third
# argument is a set of non-required options.
config.vm.synced_folder "data", "/vagrant_data", type: "virtualbox"
# Provider-specific configuration so you can fine-tune various
# backing providers for Vagrant. These expose provider-specific options.
# Example for VirtualBox:
#
# config.vm.provider "virtualbox" do |vb|
# # Display the VirtualBox GUI when booting the machine
# vb.gui = true
#
# # Customize the amount of memory on the VM:
# vb.memory = "1024"
# end
#
# View the documentation for the provider you are using for more
# information on available options.
# Define a Vagrant Push strategy for pushing to Atlas. Other push strategies
# such as FTP and Heroku are also available. See the documentation at
# https://docs.vagrantup.com/v2/push/atlas.html for more information.
# config.push.define "atlas" do |push|
# push.app = "YOUR_ATLAS_USERNAME/YOUR_APPLICATION_NAME"
# end
# Enable provisioning with a shell script. Additional provisioners such as
# Puppet, Chef, Ansible, Salt, and Docker are also available. Please see the
# documentation for more information about their specific syntax and use.
config.vm.provision "shell", inline: <<-SHELL
apt-get update
apt-get install curl wget rsync -y
curl -O http://vestacp.com/pub/vst-install.sh
bash vst-install.sh --nginx yes --apache yes --phpfpm no --named yes --remi yes --vsftpd no --proftpd yes --iptables yes --fail2ban yes --quota yes --exim yes --dovecot no --spamassassin no --clamav no --mysql yes --postgresql yes
SHELL
end
<?xml version="1.0" encoding="UTF-8"?>
<zabbix_export>
<version>3.4</version>
<date>2017-09-11T07:59:35Z</date>
<groups>
<group>
<name>Templates</name>
</group>
</groups>
<templates>
<template>
<template>Template json discovery</template>
<name>Template json discovery</name>
<description/>
<groups>
<group>
<name>Templates</name>
</group>
</groups>
<applications>
<application>
<name>json-example</name>
</application>
</applications>
<items/>
<discovery_rules>
<discovery_rule>
<name>json.data.parce discovery</name>
<type>0</type>
<snmp_community/>
<snmp_oid/>
<key>json.data.keys</key>
<delay>30s</delay>
<status>0</status>
<allowed_hosts/>
<snmpv3_contextname/>
<snmpv3_securityname/>
<snmpv3_securitylevel>0</snmpv3_securitylevel>
<snmpv3_authprotocol>0</snmpv3_authprotocol>
<snmpv3_authpassphrase/>
<snmpv3_privprotocol>0</snmpv3_privprotocol>
<snmpv3_privpassphrase/>
<params/>
<ipmi_sensor/>
<authtype>0</authtype>
<username/>
<password/>
<publickey/>
<privatekey/>
<port/>
<filter>
<evaltype>0</evaltype>
<formula/>
<conditions/>
</filter>
<lifetime>30d</lifetime>
<description/>
<item_prototypes>
<item_prototype>
<name>json data values(int) {#ROOTITEM}-{#PARENTITEM}</name>
<type>0</type>
<snmp_community/>
<snmp_oid/>
<key>json.data.new[ {#PARENTITEM} int]</key>
<delay>30s</delay>
<history>90d</history>
<trends>365d</trends>
<status>0</status>
<value_type>0</value_type>
<allowed_hosts/>
<units/>
<snmpv3_contextname/>
<snmpv3_securityname/>
<snmpv3_securitylevel>0</snmpv3_securitylevel>
<snmpv3_authprotocol>0</snmpv3_authprotocol>
<snmpv3_authpassphrase/>
<snmpv3_privprotocol>0</snmpv3_privprotocol>
<snmpv3_privpassphrase/>
<params/>
<ipmi_sensor/>
<authtype>0</authtype>
<username/>
<password/>
<publickey/>
<privatekey/>
<port/>
<description/>
<inventory_link>0</inventory_link>
<applications>
<application>
<name>json-example</name>
</application>
</applications>
<valuemap/>
<logtimefmt/>
<preprocessing/>
<jmx_endpoint/>
<application_prototypes/>
<master_item_prototype/>
</item_prototype>
<item_prototype>
<name>json data values(txt) {#ROOTITEM}-{#PARENTITEM}</name>
<type>0</type>
<snmp_community/>
<snmp_oid/>
<key>json.data.new[ {#PARENTITEM} txt]</key>
<delay>30s</delay>
<history>90d</history>
<trends>0</trends>
<status>0</status>
<value_type>4</value_type>
<allowed_hosts/>
<units/>
<snmpv3_contextname/>
<snmpv3_securityname/>
<snmpv3_securitylevel>0</snmpv3_securitylevel>
<snmpv3_authprotocol>0</snmpv3_authprotocol>
<snmpv3_authpassphrase/>
<snmpv3_privprotocol>0</snmpv3_privprotocol>
<snmpv3_privpassphrase/>
<params/>
<ipmi_sensor/>
<authtype>0</authtype>
<username/>
<password/>
<publickey/>
<privatekey/>
<port/>
<description/>
<inventory_link>0</inventory_link>
<applications>
<application>
<name>json-example</name>
</application>
</applications>
<valuemap/>
<logtimefmt/>
<preprocessing/>
<jmx_endpoint/>
<application_prototypes/>
<master_item_prototype/>
</item_prototype>
</item_prototypes>
<trigger_prototypes/>
<graph_prototypes/>
<host_prototypes/>
<jmx_endpoint/>
</discovery_rule>
</discovery_rules>
<httptests/>
<macros/>
<templates/>
<screens/>
</template>
</templates>
</zabbix_export>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment