📜 ⬆️ ⬇️

How to make friends Zabbix and Observium

Not so long ago, I came across an interesting Observium monitoring system in which I was interested in storing configs of network glands and several other chips that are described in the article .

I have already used Zabbix and I don’t even want to completely switch to the new monitoring, as well as adding new hosts to two at once is also lazy, so I got the idea to take the hosts from Zabix with a script via the API and add them to the Observium.

For some reason, you cannot ideologically add hosts to an IP address in Observium, so before adding a host that for some reason does not resolve the DNS, you must add it to the / etc / hosts file. My Cisco devices are divided into groups (routers, access points, switches), and the names begin with the abbreviation of the city, for example, NSK Cisco 2811 - 10.20.23.1

We will add hosts to Observium in the form of city-type-XXXX.domain.name. Also, the script will take the snmp community from the macro at the Zabbix host.
')
First, put pyZabbix:

pip install pyzabbix 

Well, then, in fact, the script itself:

 #!/usr/bin/python import os from pyzabbix import ZabbixAPI zapi = ZabbixAPI(server="http://servername/zabbix") zapi.login("login", "password") file_hosts = open('/etc/hosts', 'w') file_hosts.truncate() file_hosts.write ("##### Auto generated by script /opt/observium/scripts/get_zabbix_hosts.py" + '\n') file_script_add_device = open('/opt/observium/scripts/_my_script_add_device.sh', 'w') file_script_add_device.truncate() file_script_add_device.write ("#!/bin/bash" + '\n\n') file_script_add_device.write ("##### Auto generated by script /opt/observium/scripts/_my_script_import_from_zbx.py" + '\n') file_hosts.write ('\n' + "##### Cisco Routers" + '\n') file_script_add_device.write ('\n' + "##### Cisco Routers" + '\n') routers = zapi.host.get(output = 'extend', groupids = '11', ) if (routers != 0) and (len(routers) != 0): for h in routers: if h['status'] == '0': hid = h['hostid'] ipadr = zapi.hostinterface.get(hostids = hid ,filter={"main":["1"]})[0]['ip'] ipadr_spliter = ipadr.split(".") city = h['name'].split(" ")[0].lower() dns_name = (city + "-gw-" + ipadr_spliter[0] + "-" + ipadr_spliter[1] + "-" + ipadr_spliter[2] + "-" + ipadr_spliter[3] + ".domain.name") snmp_community = zapi.usermacro.get(output = 'extend', hostids = hid, filter={"macro":["{$SNMP_COMMUNITY}"]})[0]['value'] line_hosts = (ipadr + " " + dns_name) file_hosts.write (line_hosts + '\n') line_add_device = ("/opt/observium/add_device.php " + dns_name + " " + snmp_community + " v2c") file_script_add_device.write (line_add_device + '\n') file_hosts.write ('\n' + "##### Cisco Access Points" + '\n') file_script_add_device.write ('\n' + "##### Cisco Access Points" + '\n') access_points = zapi.host.get(output = 'extend', groupids = '20', ) if (access_points != 0) and (len(access_points) != 0): for h in access_points: if h['status'] == '0': hid = h['hostid'] ipadr = zapi.hostinterface.get(hostids = hid ,filter={"main":["1"]})[0]['ip'] ipadr_spliter = ipadr.split(".") city = h['name'].split(" ")[0].lower() dns_name = (city + "-ap-" + ipadr_spliter[0] + "-" + ipadr_spliter[1] + "-" + ipadr_spliter[2] + "-" + ipadr_spliter[3] + ".domain.name") snmp_community = zapi.usermacro.get(output = 'extend', hostids = hid, filter={"macro":["{$SNMP_COMMUNITY}"]})[0]['value'] line_hosts = (ipadr + " " + dns_name) file_hosts.write (line_hosts + '\n') line_add_device = ("/opt/observium/add_device.php " + dns_name + " " + snmp_community + " v2c") file_script_add_device.write (line_add_device + '\n') file_hosts.write ('\n' + "##### Cisco Switches" + '\n') file_script_add_device.write ('\n' + "##### Cisco Switches" + '\n') switches = zapi.host.get(output = 'extend', groupids = '19', ) if (switches != 0) and (len(switches) != 0): for h in switches: if h['status'] == '0': hid = h['hostid'] ipadr = zapi.hostinterface.get(hostids = hid ,filter={"main":["1"]})[0]['ip'] ipadr_spliter = ipadr.split(".") city = h['name'].split(" ")[0].lower() dns_name = (city + "-sw-" + ipadr_spliter[0] + "-" + ipadr_spliter[1] + "-" + ipadr_spliter[2] + "-" + ipadr_spliter[3] + ".domain.name") snmp_community = zapi.usermacro.get(output = 'extend', hostids = hid, filter={"macro":["{$SNMP_COMMUNITY}"]})[0]['value'] line_hosts = (ipadr + " " + dns_name) file_hosts.write (line_hosts + '\n') line_add_device = ("/opt/observium/add_device.php " + dns_name + " " + snmp_community + " v2c") file_script_add_device.write (line_add_device + '\n') file_hosts.close() file_script_add_device.close() os.system("chmod 750 /opt/observium/scripts/_my_script_add_device.sh") os.system("/opt/observium/scripts/_my_script_add_device.sh | grep 'Could not reach ' > /var/log/import_hosts_from_zabbix.log") 

This script takes the hosts in turn from different groups of devices and places them in the hosts file, also creates a script for adding hosts to the Observium and runs for execution.

Source: https://habr.com/ru/post/309532/


All Articles