From b18a0c0897196926c54eedb84a587c3bdb1e7bf5 Mon Sep 17 00:00:00 2001 From: Twan Kamans Date: Fri, 16 Apr 2021 21:29:07 +0200 Subject: [PATCH] Added Tenant flag, fixed #1. Fixed duplicate 'Hostgroup not found' log message. --- README.md | 98 +++-- netbox_zabbix_sync | 892 +++++++++++++++++++++++---------------------- 2 files changed, 523 insertions(+), 467 deletions(-) diff --git a/README.md b/README.md index c4701c2..007e509 100644 --- a/README.md +++ b/README.md @@ -1,35 +1,63 @@ -A script to sync the Netbox inventory to Zabbix. - -Requires pyzabbix and pynetbox - -Use the following variables for the environment: - -* ZABBIX_HOST="https://zabbix.local" -* ZABBIX_USER="username" -* ZABBIX_PASS="Password" -* NETBOX_HOST="https://netbox.local" -* NETBOX_TOKEN="secrettoken" - -Logs are generated under sync.log, set the script for debugging / info options etc. - -Important: you need to set the hostgroup in Zabbix before a sync can occur. This is in the following format: - -{Site name}/{Manufacturer name}/{Device role name} - -Make sure that the Zabbix user has proper permissions to create hosts - -Use the following custom fields in Netbox to map the Zabbix URL: - -* Type: Integer -* Name: zabbix_hostid -* Required: False -* Default: null -* Object: dcim > device - -And this field for the Zabbix template - -* Type: Text -* Name: zabbix_template -* Required: False -* Default: null -* Object: dcim > device_type +A script to sync the Netbox device inventory to Zabbix. + +## Requires pyzabbix and pynetbox. + +### Script settings +#### Enviroment variables + +* ZABBIX_HOST="https://zabbix.local" +* ZABBIX_USER="username" +* ZABBIX_PASS="Password" +* NETBOX_HOST="https://netbox.local" +* NETBOX_TOKEN="secrettoken" + +#### Flags +| Flag | Option | Description | +| ------------ | ------------ | ------------ | +| -c | cluster | For clustered devices: only add the primary node of a cluster and use the cluster name as hostname. | +| -H | hostgroup | Create non-existing hostgroups in Zabbix. Usefull for a first run to add all required hostgroups. | +| -t | tenant | Add the tenant name to the hostgroup format (Tenant/Site/Manufacturer/Role) | +| -v | Verbose | Log with debugging on. | + + +#### Logging +Logs are generated under sync.log, set the script for debugging / info options etc. + +#### Hostgroups: manual mode + +In case of omitting the -H flag, manual hostgroup creation is required for devices in a new category. + +This is in the format: +{Site name}/{Manufacturer name}/{Device role name} +And with tenants (-t flag): +{Tenant name}/{Site name}/{Manufacturer name}/{Device role name} + +Make sure that the Zabbix user has proper permissions to create hosts. +The hostgroups are in a nested format. This means that proper permissions only need to be applied to the site name hostgroup and cascaded to any child hostgroups. + +### Netbox settings +#### Custom fields +Use the following custom fields in Netbox to map the Zabbix URL: +* Type: Integer +* Name: zabbix_hostid +* Required: False +* Default: null +* Object: dcim > device + +And this field for the Zabbix template + +* Type: Text +* Name: zabbix_template +* Required: False +* Default: null +* Object: dcim > device_type + +#### Permissions +Make sure that the user has proper permissions for device read and modify (modify to set the Zabbix HostID custom field) operations. + +#### Custom links +To make the user experience easier you could add a custom link that redirects users to the Zabbix latest data. + +* Name: zabbix_latestData +* Text: {% if obj.cf["zabbix_hostid"] %}Show host in Zabbix{% endif %} +* URL: {ZABBIX_URL} /zabbix.php?action=latest.view&filter_hostids[]={{ obj.cf["zabbix_hostid"] }}&filter_application=&filter_select=&filter_set=1 diff --git a/netbox_zabbix_sync b/netbox_zabbix_sync index 67e4674..9e7d46c 100755 --- a/netbox_zabbix_sync +++ b/netbox_zabbix_sync @@ -1,432 +1,460 @@ -#!/usr/bin/python3 -"""Netbox to Zabbix sync script.""" - -from os import environ -import logging -import argparse -from pynetbox import api -from pyzabbix import ZabbixAPI, ZabbixAPIException - -# Set logging -log_format = logging.Formatter('%(asctime)s - %(name)s - ' - '%(levelname)s - %(message)s') -lgout = logging.StreamHandler() -lgout.setFormatter(log_format) -lgout.setLevel(logging.DEBUG) - -lgfile = logging.FileHandler("sync.log") -lgfile.setFormatter(log_format) -lgfile.setLevel(logging.DEBUG) - -logger = logging.getLogger("Netbox-Zabbix-sync") -logger.addHandler(lgout) -logger.addHandler(lgfile) -logger.setLevel(logging.WARNING) - -template_cf = "zabbix_template" -device_cf = "zabbix_hostid" - - -def main(arguments): - """Module that runs the sync process.""" - # set environment variables - if(arguments.verbose): - logger.setLevel(logging.DEBUG) - env_vars = ["ZABBIX_HOST", "ZABBIX_USER", "ZABBIX_PASS", - "NETBOX_HOST", "NETBOX_TOKEN"] - for var in env_vars: - if var not in environ: - e = f"Environment variable {var} has not been defined." - logger.error(e) - raise EnvironmentVarError(e) - # Get all virtual environment variables - zabbix_host = environ.get("ZABBIX_HOST") - zabbix_user = environ.get("ZABBIX_USER") - zabbix_pass = environ.get("ZABBIX_PASS") - netbox_host = environ.get("NETBOX_HOST") - netbox_token = environ.get("NETBOX_TOKEN") - - # Set Zabbix API - try: - zabbix = ZabbixAPI(zabbix_host) - zabbix.login(zabbix_user, zabbix_pass) - except ZabbixAPIException as e: - e = f"Zabbix returned the following error: {str(e)}." - logger.error(e) - # Set Netbox API - netbox = api(netbox_host, netbox_token, threading=True) - # Get all Zabbix and Netbox data - netbox_devices = netbox.dcim.devices.all() - zabbix_groups = zabbix.hostgroup.get(output=['name']) - zabbix_templates = zabbix.template.get(output=['name']) - # Go through all Netbox devices - for nb_device in netbox_devices: - try: - device = NetworkDevice(nb_device, zabbix) - # Checks if device is part of cluster. - # Requires the cluster argument. - if(device.isCluster() and arguments.cluster): - # Check if device is master or slave - if(device.promoteMasterDevice()): - e = (f"Device {device.name} is " - f"part of cluster and primary.") - logger.info(e) - else: - # Device is secondary in cluster. - # Don't continue with this device. - e = (f"Device {device.name} is part of cluster " - f"but not primary. Skipping this host...") - logger.info(e) - continue - # Checks if device is in cleanup state - if(device.status != "Active"): - # Delete device from Zabbix - # and remove hostID from Netbox. - device.cleanup() - continue - if(device.zabbix_id): - # Device is already present in Zabbix - device.ConsistencyCheck(zabbix_groups, zabbix_templates) - else: - # Add hostgroup is flag is true - # and Hostgroup is not present in Zabbix - if(not device.getZabbixGroup(zabbix_groups) - and arguments.hostgroups): - # Create new hostgroup - device.createZabbixHostgroup() - zabbix_groups = zabbix.hostgroup.get(output=['name']) - # Add device to Zabbix - device.createInZabbix(zabbix_groups, zabbix_templates) - except SyncError: - # logger.error(e) - pass - - -class SyncError(Exception): - pass - - -class SyncExternalError(SyncError): - pass - - -class SyncInventoryError(SyncError): - pass - - -class SyncDuplicateError(SyncError): - pass - - -class EnvironmentVarError(SyncError): - pass - - -class NetworkDevice(): - """ - Represents Network device. - INPUT: (Netbox device class, ZabbixAPI class) - """ - def __init__(self, nb, zabbix): - self.nb = nb - self.id = nb.id - self.name = nb.name - self.status = nb.status.label - self.zabbix = zabbix - self.hg_format = (f"{self.nb.site.name}/" - f"{self.nb.device_type.manufacturer.name}/" - f"{self.nb.device_role.name}") - self._setBasics() - - def _setBasics(self): - """ - Sets basic information like IP address. - """ - # Return error if device does not have primary IP. - if(self.nb.primary_ip): - self.cidr = self.nb.primary_ip.address - self.ip = self.cidr.split("/")[0] - else: - e = f"Device {self.name}: no primary IP." - logger.warning(e) - raise SyncInventoryError(e) - - # Check if device_type has custom field - device_type_cf = self.nb.device_type.custom_fields - if(template_cf in device_type_cf): - self.template_name = device_type_cf[template_cf] - else: - e = (f"Custom field {template_cf} not " - f"found for {self.nb.device_type.name}.") - logger.warning(e) - raise SyncInventoryError(e) - - # Check if device has custom field - if(device_cf in self.nb.custom_fields): - self.zabbix_id = self.nb.custom_fields[device_cf] - else: - e = f"Custom field {template_cf} not found for {self.name}." - logger.warning(e) - raise SyncInventoryError(e) - - def isCluster(self): - """ - Checks if device is part of cluster. - """ - if(self.nb.virtual_chassis): - return True - else: - return False - - def getClusterMaster(self): - """ - Returns chassis master ID. - """ - if(not self.isCluster()): - e = (f"Unable to proces {self.name} for cluster calculation: " - f"not part of a cluster.") - logger.warning(e) - raise SyncInventoryError(e) - else: - return self.nb.virtual_chassis.master.id - - def promoteMasterDevice(self): - """ - If device is Primary in cluster, - promote device name to the cluster name. - Returns True if succesfull, returns False if device is secondary. - """ - masterid = self.getClusterMaster() - if(masterid == self.id): - logger.debug(f"Device {self.name} is primary cluster member. " - f"Modifying hostname from {self.name} to " + - f"{self.nb.virtual_chassis.name}.") - self.name = self.nb.virtual_chassis.name - return True - else: - logger.debug(f"Device {self.name} is non-primary cluster member.") - return False - - def getZabbixTemplate(self, templates): - """ - Returns Zabbix template ID - INPUT: list of templates - OUTPUT: True - """ - if(not self.template_name): - e = (f"Device template '{self.nb.device_type.display_name}' " - "has no template defined.") - logger.info(e) - raise SyncInventoryError() - for template in templates: - if(template['name'] == self.template_name): - self.template_id = template['templateid'] - e = (f"Found template ID {str(template['templateid'])} " - f"for host {self.name}.") - logger.debug(e) - return True - else: - e = (f"Unable to find template {self.template_name} " - f"for host {self.name} in Zabbix.") - logger.warning(e) - raise SyncInventoryError(e) - - def getZabbixGroup(self, groups): - """ - Returns Zabbix group ID - INPUT: list of hostgroups - OUTPUT: True / False - """ - # Go through all groups - for group in groups: - if(group['name'] == self.hg_format): - self.group_id = group['groupid'] - e = (f"Found group ID {str(group['groupid'])} " - f"for host {self.name}.") - logger.debug(e) - return True - else: - e = (f"Unable to find group '{self.hg_format}' " - f"for host {self.name} in Zabbix.") - logger.warning(e) - - def cleanup(self): - """ - Removes device from external resources. - Resets custom fields in Netbox. - """ - if(self.zabbix_id): - try: - self.zabbix.host.delete(self.zabbix_id) - self.nb.custom_fields[device_cf] = None - self.nb.save() - e = f"Deleted host {self.name} from Zabbix." - logger.info(e) - except ZabbixAPIException as e: - e = f"Zabbix returned the following error: {str(e)}." - logger.error(e) - raise SyncExternalError(e) - - def _zabbixHostnameExists(self): - """ - Checks if hostname exists in Zabbix. - """ - host = self.zabbix.host.get(filter={'name': self.name}, output=[]) - if(host): - return True - else: - return False - - def createInZabbix(self, groups, templates, - description="Host added by Netbox sync script."): - """ - Creates Zabbix host object with parameters from Netbox object. - """ - # Check if hostname is already present in Zabbix - if(not self._zabbixHostnameExists()): - # Get group and template ID's for host - if(not self.getZabbixGroup(groups)): - raise SyncInventoryError() - self.getZabbixTemplate(templates) - # Set interface, group and template configuration - interfaces = [{"type": 2, "main": 1, "useip": 1, - "ip": self.ip, "dns": "", "port": 161, - "details": {"version": 2, "bulk": 0, - "community": "{$SNMP_COMMUNITY}"}}] - groups = [{"groupid": self.group_id}] - templates = [{"templateid": self.template_id}] - # Add host to Zabbix - try: - host = self.zabbix.host.create(host=self.name, status=0, - interfaces=interfaces, - groups=groups, - templates=templates, - description=description) - self.zabbix_id = host["hostids"][0] - except ZabbixAPIException as e: - e = f"Couldn't add {self.name}, Zabbix returned {str(e)}." - logger.error(e) - raise SyncExternalError(e) - # Set Netbox custom field to hostID value. - self.nb.custom_fields[device_cf] = self.zabbix_id - self.nb.save() - logger.info(f"Created host {self.name} in Zabbix.") - else: - e = f"Unable to add {self.name} to Zabbix: host already present." - logger.warning(e) - - def createZabbixHostgroup(self): - """ - Creates Zabbix host group based on hostgroup format. - """ - try: - self.zabbix.hostgroup.create(name=self.hg_format) - e = f"Added hostgroup '{self.hg_format}'." - logger.info(e) - except ZabbixAPIException as e: - e = f"Couldn't add hostgroup, Zabbix returned {str(e)}." - logger.error(e) - raise SyncExternalError(e) - - def updateZabbixHost(self, **kwargs): - """ - Updates Zabbix host with given parameters. - INPUT: Key word arguments for Zabbix host object. - """ - try: - self.zabbix.host.update(hostid=self.zabbix_id, **kwargs) - except ZabbixAPIException as e: - e = f"Zabbix returned the following error: {str(e)}." - logger.error(e) - raise SyncExternalError(e) - logger.info(f"Updated host {self.name} with data {kwargs}.") - - def ConsistencyCheck(self, groups, templates): - """ - Checks if Zabbix object is still valid with Netbox parameters. - """ - self.getZabbixGroup(groups) - self.getZabbixTemplate(templates) - host = self.zabbix.host.get(filter={'hostid': self.zabbix_id}, - selectInterfaces=["interfaceid", "ip"], - selectGroups=["id"], - selectParentTemplates=["id"]) - if(len(host) > 1): - e = (f"Got {len(host)} results for Zabbix hosts " - f"with ID {self.zabbix_id} - hostname {self.name}.") - logger.error(e) - raise SyncInventoryError(e) - elif(len(host) == 0): - e = (f"No Zabbix host found for {self.name}. " - f"This is likely the result of a deleted Zabbix host " - f"without zeroing the ID field in Netbox.") - logger.error(e) - raise SyncInventoryError(e) - else: - host = host[0] - - if(host["host"] == self.name): - logger.debug(f"Device {self.name}: hostname in-sync.") - else: - logger.warning(f"Device {self.name}: hostname OUT of sync. " - f"Received value: {host['host']}") - self.updateZabbixHost(host=self.name) - - for template in host["parentTemplates"]: - if(template["templateid"] == self.template_id): - logger.debug(f"Device {self.name}: template in-sync.") - break - else: - logger.warning(f"Device {self.name}: template OUT of sync.") - self.updateZabbixHost(templates=self.template_id) - - for group in host["groups"]: - if(group["groupid"] == self.group_id): - logger.debug(f"Device {self.name}: hostgroup in-sync.") - break - else: - logger.warning(f"Device {self.name}: hostgroup OUT of sync.") - self.updateZabbixHost(groups={'groupid': self.group_id}) - - for interface in host["interfaces"]: - if(interface["ip"] == self.ip): - logger.debug(f"Device {self.name}: IP address in-sync.") - break - else: - if(len(host['interfaces']) == 1): - logger.warning(f"Device {self.name}: IP address OUT of sync.") - int_id = host["interfaces"][0]['interfaceid'] - try: - self.zabbix.hostinterface.update(interfaceid=int_id, - ip=self.ip) - e = f"Updated host {self.name} with IP {self.ip}." - logger.warning(e) - except ZabbixAPIException as e: - e = f"Zabbix returned the following error: {str(e)}." - logger.error(e) - raise SyncExternalError(e) - else: - e = (f"Device {self.name} has conflicting IP. Host has total " - f"of {len(host['interfaces'])} interfaces. Manual " - "interfention required.") - logger.error(e) - SyncInventoryError(e) - - -if(__name__ == "__main__"): - # Arguments parsing - parser = argparse.ArgumentParser( - description='A script to sync Zabbix with Netbox device data.' - ) - parser.add_argument("-v", "--verbose", help="Turn on debugging.", - action="store_true") - parser.add_argument("-c", "--cluster", action="store_true", - help=("Only add the primary node of a cluster " - "to Zabbix. Usefull when a shared virtual IP is " - "used for the control plane.")) - parser.add_argument("-H", "--hostgroups", - help="Create Zabbix hostgroups if not present", - action="store_true") - args = parser.parse_args() - - main(args) +#!/usr/bin/python3 +"""Netbox to Zabbix sync script.""" + +from os import environ +import logging +import argparse +from pynetbox import api +from pyzabbix import ZabbixAPI, ZabbixAPIException + +# Set logging +log_format = logging.Formatter('%(asctime)s - %(name)s - ' + '%(levelname)s - %(message)s') +lgout = logging.StreamHandler() +lgout.setFormatter(log_format) +lgout.setLevel(logging.DEBUG) + +lgfile = logging.FileHandler("sync.log") +lgfile.setFormatter(log_format) +lgfile.setLevel(logging.DEBUG) + +logger = logging.getLogger("Netbox-Zabbix-sync") +logger.addHandler(lgout) +logger.addHandler(lgfile) +logger.setLevel(logging.WARNING) + +template_cf = "zabbix_template" +device_cf = "zabbix_hostid" + + +def main(arguments): + """Module that runs the sync process.""" + # set environment variables + if(arguments.verbose): + logger.setLevel(logging.DEBUG) + env_vars = ["ZABBIX_HOST", "ZABBIX_USER", "ZABBIX_PASS", + "NETBOX_HOST", "NETBOX_TOKEN"] + for var in env_vars: + if var not in environ: + e = f"Environment variable {var} has not been defined." + logger.error(e) + raise EnvironmentVarError(e) + # Get all virtual environment variables + zabbix_host = environ.get("ZABBIX_HOST") + zabbix_user = environ.get("ZABBIX_USER") + zabbix_pass = environ.get("ZABBIX_PASS") + netbox_host = environ.get("NETBOX_HOST") + netbox_token = environ.get("NETBOX_TOKEN") + + # Set Zabbix API + try: + zabbix = ZabbixAPI(zabbix_host) + zabbix.login(zabbix_user, zabbix_pass) + except ZabbixAPIException as e: + e = f"Zabbix returned the following error: {str(e)}." + logger.error(e) + # Set Netbox API + netbox = api(netbox_host, netbox_token, threading=True) + # Get all Zabbix and Netbox data + netbox_devices = netbox.dcim.devices.all() + zabbix_groups = zabbix.hostgroup.get(output=['name']) + zabbix_templates = zabbix.template.get(output=['name']) + # Go through all Netbox devices + for nb_device in netbox_devices: + try: + device = NetworkDevice(nb_device, zabbix) + # Checks if device is part of cluster. + # Requires the cluster argument. + if(device.isCluster() and arguments.cluster): + # Check if device is master or slave + if(device.promoteMasterDevice()): + e = (f"Device {device.name} is " + f"part of cluster and primary.") + logger.info(e) + else: + # Device is secondary in cluster. + # Don't continue with this device. + e = (f"Device {device.name} is part of cluster " + f"but not primary. Skipping this host...") + logger.info(e) + continue + # With -t flag: add Tenant name to hostgroup name. + if(arguments.tenant): + if(device.tenant): + device.hg_format.insert(1, device.tenant.name) + device.setHostgroup() + logger.debug(f"Added Tenant {device.tenant.name} to " + f"hostgroup format of {device.name}.") + else: + logger.debug(f"{device.name} is not linked to a tenant. " + f"Using HG format '{device.hostgroup}'.") + # Checks if device is in cleanup state + if(device.status != "Active"): + if(device.zabbix_id): + # Delete device from Zabbix + # and remove hostID from Netbox. + device.cleanup() + logger.info(f"Cleaned up host {device.name}.") + + else: + # Device has been added to Netbox + # but is not in Activate state + logger.info(f"Skipping host {device.name} since its " + f"not in the active state.") + continue + if(device.zabbix_id): + # Device is already present in Zabbix + device.ConsistencyCheck(zabbix_groups, zabbix_templates) + else: + # Add hostgroup is flag is true + # and Hostgroup is not present in Zabbix + if(not device.hostgroup and arguments.hostgroups): + # Create new hostgroup + device.createZabbixHostgroup() + zabbix_groups = zabbix.hostgroup.get(output=['name']) + # Add device to Zabbix + device.createInZabbix(zabbix_groups, zabbix_templates) + except SyncError: + pass + + +class SyncError(Exception): + pass + + +class SyncExternalError(SyncError): + pass + + +class SyncInventoryError(SyncError): + pass + + +class SyncDuplicateError(SyncError): + pass + + +class EnvironmentVarError(SyncError): + pass + + +class NetworkDevice(): + """ + Represents Network device. + INPUT: (Netbox device class, ZabbixAPI class) + """ + def __init__(self, nb, zabbix): + self.nb = nb + self.id = nb.id + self.name = nb.name + self.status = nb.status.label + self.zabbix = zabbix + self.tenant = nb.tenant + self.hostgroup = None + self.hg_format = [self.nb.site.name, + self.nb.device_type.manufacturer.name, + self.nb.device_role.name] + self._setBasics() + self.setHostgroup() + + def _setBasics(self): + """ + Sets basic information like IP address. + """ + # Return error if device does not have primary IP. + if(self.nb.primary_ip): + self.cidr = self.nb.primary_ip.address + self.ip = self.cidr.split("/")[0] + else: + e = f"Device {self.name}: no primary IP." + logger.warning(e) + raise SyncInventoryError(e) + + # Check if device_type has custom field + device_type_cf = self.nb.device_type.custom_fields + if(template_cf in device_type_cf): + self.template_name = device_type_cf[template_cf] + else: + e = (f"Custom field {template_cf} not " + f"found for {self.nb.device_type.name}.") + logger.warning(e) + raise SyncInventoryError(e) + + # Check if device has custom field + if(device_cf in self.nb.custom_fields): + self.zabbix_id = self.nb.custom_fields[device_cf] + else: + e = f"Custom field {template_cf} not found for {self.name}." + logger.warning(e) + raise SyncInventoryError(e) + + def setHostgroup(self): + """Sets hostgroup to a string with hg_format parameters.""" + self.hostgroup = "/".join(self.hg_format) + + def isCluster(self): + """ + Checks if device is part of cluster. + """ + if(self.nb.virtual_chassis): + return True + else: + return False + + def getClusterMaster(self): + """ + Returns chassis master ID. + """ + if(not self.isCluster()): + e = (f"Unable to proces {self.name} for cluster calculation: " + f"not part of a cluster.") + logger.warning(e) + raise SyncInventoryError(e) + else: + return self.nb.virtual_chassis.master.id + + def promoteMasterDevice(self): + """ + If device is Primary in cluster, + promote device name to the cluster name. + Returns True if succesfull, returns False if device is secondary. + """ + masterid = self.getClusterMaster() + if(masterid == self.id): + logger.debug(f"Device {self.name} is primary cluster member. " + f"Modifying hostname from {self.name} to " + + f"{self.nb.virtual_chassis.name}.") + self.name = self.nb.virtual_chassis.name + return True + else: + logger.debug(f"Device {self.name} is non-primary cluster member.") + return False + + def getZabbixTemplate(self, templates): + """ + Returns Zabbix template ID + INPUT: list of templates + OUTPUT: True + """ + if(not self.template_name): + e = (f"Device template '{self.nb.device_type.display_name}' " + "has no Zabbix template defined.") + logger.info(e) + raise SyncInventoryError() + for template in templates: + if(template['name'] == self.template_name): + self.template_id = template['templateid'] + e = (f"Found template ID {str(template['templateid'])} " + f"for host {self.name}.") + logger.debug(e) + return True + else: + e = (f"Unable to find template {self.template_name} " + f"for host {self.name} in Zabbix.") + logger.warning(e) + raise SyncInventoryError(e) + + def getZabbixGroup(self, groups): + """ + Returns Zabbix group ID + INPUT: list of hostgroups + OUTPUT: True / False + """ + # Go through all groups + for group in groups: + if(group['name'] == self.hostgroup): + self.group_id = group['groupid'] + e = (f"Found group {group['name']} for host {self.name}.") + #e = (f"Found group ID {str(group['groupid'])} " + # f"for host {self.name}.") + logger.debug(e) + return True + else: + e = (f"Unable to find group '{self.hostgroup}' " + f"for host {self.name} in Zabbix.") + logger.warning(e) + raise SyncInventoryError(e) + + def cleanup(self): + """ + Removes device from external resources. + Resets custom fields in Netbox. + """ + if(self.zabbix_id): + try: + self.zabbix.host.delete(self.zabbix_id) + self.nb.custom_fields[device_cf] = None + self.nb.save() + e = f"Deleted host {self.name} from Zabbix." + logger.info(e) + except ZabbixAPIException as e: + e = f"Zabbix returned the following error: {str(e)}." + logger.error(e) + raise SyncExternalError(e) + + def _zabbixHostnameExists(self): + """ + Checks if hostname exists in Zabbix. + """ + host = self.zabbix.host.get(filter={'name': self.name}, output=[]) + if(host): + return True + else: + return False + + def createInZabbix(self, groups, templates, + description="Host added by Netbox sync script."): + """ + Creates Zabbix host object with parameters from Netbox object. + """ + # Check if hostname is already present in Zabbix + if(not self._zabbixHostnameExists()): + # Get group and template ID's for host + if(not self.getZabbixGroup(groups)): + raise SyncInventoryError() + self.getZabbixTemplate(templates) + # Set interface, group and template configuration + interfaces = [{"type": 2, "main": 1, "useip": 1, + "ip": self.ip, "dns": "", "port": 161, + "details": {"version": 2, "bulk": 0, + "community": "{$SNMP_COMMUNITY}"}}] + groups = [{"groupid": self.group_id}] + templates = [{"templateid": self.template_id}] + # Add host to Zabbix + try: + host = self.zabbix.host.create(host=self.name, status=0, + interfaces=interfaces, + groups=groups, + templates=templates, + description=description) + self.zabbix_id = host["hostids"][0] + except ZabbixAPIException as e: + e = f"Couldn't add {self.name}, Zabbix returned {str(e)}." + logger.error(e) + raise SyncExternalError(e) + # Set Netbox custom field to hostID value. + self.nb.custom_fields[device_cf] = self.zabbix_id + self.nb.save() + logger.info(f"Created host {self.name} in Zabbix.") + else: + e = f"Unable to add {self.name} to Zabbix: host already present." + logger.warning(e) + + def createZabbixHostgroup(self): + """ + Creates Zabbix host group based on hostgroup format. + """ + try: + self.zabbix.hostgroup.create(name=self.hostgroup) + e = f"Added hostgroup '{self.hostgroup}'." + logger.info(e) + except ZabbixAPIException as e: + e = f"Couldn't add hostgroup, Zabbix returned {str(e)}." + logger.error(e) + raise SyncExternalError(e) + + def updateZabbixHost(self, **kwargs): + """ + Updates Zabbix host with given parameters. + INPUT: Key word arguments for Zabbix host object. + """ + try: + self.zabbix.host.update(hostid=self.zabbix_id, **kwargs) + except ZabbixAPIException as e: + e = f"Zabbix returned the following error: {str(e)}." + logger.error(e) + raise SyncExternalError(e) + logger.info(f"Updated host {self.name} with data {kwargs}.") + + def ConsistencyCheck(self, groups, templates): + """ + Checks if Zabbix object is still valid with Netbox parameters. + """ + self.getZabbixGroup(groups) + self.getZabbixTemplate(templates) + host = self.zabbix.host.get(filter={'hostid': self.zabbix_id}, + selectInterfaces=["interfaceid", "ip"], + selectGroups=["id"], + selectParentTemplates=["id"]) + if(len(host) > 1): + e = (f"Got {len(host)} results for Zabbix hosts " + f"with ID {self.zabbix_id} - hostname {self.name}.") + logger.error(e) + raise SyncInventoryError(e) + elif(len(host) == 0): + e = (f"No Zabbix host found for {self.name}. " + f"This is likely the result of a deleted Zabbix host " + f"without zeroing the ID field in Netbox.") + logger.error(e) + raise SyncInventoryError(e) + else: + host = host[0] + + if(host["host"] == self.name): + logger.debug(f"Device {self.name}: hostname in-sync.") + else: + logger.warning(f"Device {self.name}: hostname OUT of sync. " + f"Received value: {host['host']}") + self.updateZabbixHost(host=self.name) + + for template in host["parentTemplates"]: + if(template["templateid"] == self.template_id): + logger.debug(f"Device {self.name}: template in-sync.") + break + else: + logger.warning(f"Device {self.name}: template OUT of sync.") + self.updateZabbixHost(templates=self.template_id) + + for group in host["groups"]: + if(group["groupid"] == self.group_id): + logger.debug(f"Device {self.name}: hostgroup in-sync.") + break + else: + logger.warning(f"Device {self.name}: hostgroup OUT of sync.") + self.updateZabbixHost(groups={'groupid': self.group_id}) + + for interface in host["interfaces"]: + if(interface["ip"] == self.ip): + logger.debug(f"Device {self.name}: IP address in-sync.") + break + else: + if(len(host['interfaces']) == 1): + logger.warning(f"Device {self.name}: IP address OUT of sync.") + int_id = host["interfaces"][0]['interfaceid'] + try: + self.zabbix.hostinterface.update(interfaceid=int_id, + ip=self.ip) + e = f"Updated host {self.name} with IP {self.ip}." + logger.warning(e) + except ZabbixAPIException as e: + e = f"Zabbix returned the following error: {str(e)}." + logger.error(e) + raise SyncExternalError(e) + else: + e = (f"Device {self.name} has conflicting IP. Host has total " + f"of {len(host['interfaces'])} interfaces. Manual " + "interfention required.") + logger.error(e) + SyncInventoryError(e) + + +if(__name__ == "__main__"): + # Arguments parsing + parser = argparse.ArgumentParser( + description='A script to sync Zabbix with Netbox device data.' + ) + parser.add_argument("-v", "--verbose", help="Turn on debugging.", + action="store_true") + parser.add_argument("-c", "--cluster", action="store_true", + help=("Only add the primary node of a cluster " + "to Zabbix. Usefull when a shared virtual IP is " + "used for the control plane.")) + parser.add_argument("-H", "--hostgroups", + help="Create Zabbix hostgroups if not present", + action="store_true") + parser.add_argument("-t", "--tenant", action="store_true", + help=("Add Tenant name to the Zabbix " + "hostgroup name scheme.")) + args = parser.parse_args() + + main(args)