Skip to content

Instantly share code, notes, and snippets.

@evandhoffman
Last active October 16, 2015 17:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save evandhoffman/b29245c1b38d3e19b531 to your computer and use it in GitHub Desktop.
Save evandhoffman/b29245c1b38d3e19b531 to your computer and use it in GitHub Desktop.
Python script that emits disks and mdadm raid devices in a form usable by Zabbix low-level discovery
#!/usr/bin/python
import json
import subprocess
blockdev_fullpath_macro = "{#BLOCKDEV_FULLPATH}"
blockdev_type_macro = "{#BLOCKDEV_TYPE}"
blockdev_shortname_macro = "{#BLOCKDEV_SHORTNAME}"
output = subprocess.Popen("lsblk -r -io KNAME,TYPE", stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True )
result = []
dedupe = {}
device_types = ['disk','raid0','raid5']
for line in output.stdout:
(dev_name, dev_type) = line.rstrip().split()
if any( dt in dev_type for dt in device_types):
dedupe[dev_name] = dev_type
for dev_name in dedupe:
dev_type = dedupe[dev_name]
dev_fullpath = '\/dev\/' + dev_name
row = { blockdev_shortname_macro: dev_name, blockdev_type_macro: dev_type , blockdev_fullpath_macro: dev_fullpath }
result.append(row)
data_arr = {"data": result }
print json.dumps(data_arr, indent=2)
@evandhoffman
Copy link
Author

See here for more information: https://www.zabbix.com/documentation/2.4/manual/discovery/low_level_discovery

# python blockdevices.py
{
  "data": [
    {
      "{#BLOCKDEV_FULLPATH}": "\\/dev\\/sdm",
      "{#BLOCKDEV_TYPE}": "disk",
      "{#BLOCKDEV_SHORTNAME}": "sdm"
    },
    {
      "{#BLOCKDEV_FULLPATH}": "\\/dev\\/md0",
      "{#BLOCKDEV_TYPE}": "raid0",
      "{#BLOCKDEV_SHORTNAME}": "md0"
    },
    {
      "{#BLOCKDEV_FULLPATH}": "\\/dev\\/sdo",
      "{#BLOCKDEV_TYPE}": "disk",
      "{#BLOCKDEV_SHORTNAME}": "sdo"
    }
  ]
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment