From cc08300fee519782a312833c0ec093c8b0e2884a Mon Sep 17 00:00:00 2001 From: Solvik Blum Date: Tue, 6 Aug 2019 17:59:46 +0200 Subject: [PATCH 1/8] add some logging --- netbox_agent/logging.py | 23 ++++++++++++++++++ netbox_agent/network.py | 29 +++++++++++++++++++++-- netbox_agent/server.py | 52 +++++++++++++++++++++++++++++++++-------- 3 files changed, 92 insertions(+), 12 deletions(-) create mode 100644 netbox_agent/logging.py diff --git a/netbox_agent/logging.py b/netbox_agent/logging.py new file mode 100644 index 0000000..95b31ef --- /dev/null +++ b/netbox_agent/logging.py @@ -0,0 +1,23 @@ +import logging + + +def init_log(): + logger = logging.getLogger('netbox_agent') + logger.setLevel(logging.DEBUG) + + logger_formatter = logging.Formatter('%(name)s - %(levelname)s - %(message)s') + + fh = logging.FileHandler('netbox.log') + fh.setLevel(logging.DEBUG) + fh.setFormatter(logger_formatter) + + ch = logging.StreamHandler() + ch.setLevel(logging.DEBUG) + ch.setFormatter(logger_formatter) + + logger.addHandler(fh) + logger.addHandler(ch) + return logger + + +logger = init_log() diff --git a/netbox_agent/network.py b/netbox_agent/network.py index a2ac399..0adca94 100644 --- a/netbox_agent/network.py +++ b/netbox_agent/network.py @@ -7,6 +7,7 @@ import netifaces from netbox_agent.config import netbox_instance as nb from netbox_agent.ethtool import Ethtool +from netbox_agent.logging import logger IFACE_TYPE_100ME_FIXED = 800 IFACE_TYPE_1GE_FIXED = 1000 @@ -80,14 +81,18 @@ class Network(): def create_netbox_nic(self, device, nic): # TODO: add Optic Vendor, PN and Serial + type = self.get_netbox_type_for_nic(nic) + logger.info('Creating NIC {name} ({mac}) on {device}'.format( + name=nic['name'], mac=nic['mac'], device=device.name)) return nb.dcim.interfaces.create( device=device.id, name=nic['name'], mac_address=nic['mac'], - type=self.get_netbox_type_for_nic(nic), + type=type, ) def create_netbox_network_cards(self): + logger.info('Creating NIC..') device = self.server.get_netbox_server() for nic in self.nics: interface = nb.dcim.interfaces.get( @@ -105,16 +110,22 @@ class Network(): address=ip, ) if netbox_ip: + logger.info('Assigning existing IP {ip} to {interface}'.format( + ip=ip, interface=new_interface)) netbox_ip.interface = new_interface netbox_ip.save() else: + logger.info('Create new IP {ip} on {interface}'.format( + ip=ip, interface=new_interface)) netbox_ip = nb.ipam.ip_addresses.create( address=ip, interface=new_interface.id, status=1, ) + logger.info('Finished creating NIC!') def update_netbox_network_cards(self): + logger.debug('Updating NIC..') device = self.server.get_netbox_server() # delete IP on netbox that are not known on this server @@ -126,6 +137,8 @@ class Network(): ])) for netbox_ip in netbox_ips: if netbox_ip.address not in all_local_ips: + logger.info('Unassigning IP {ip} from {interface}'.format( + ip=netbox_ip.address, interface=netbox_ip.interface)) netbox_ip.interface = None netbox_ip.save() @@ -138,6 +151,8 @@ class Network(): nic_update = False if nic['name'] != interface.name: nic_update = True + logger.info('Updating interface {interface} name to: {name}'.format( + interface=interface, name=nic['name'])) interface.name = nic['name'] if nic['ip']: @@ -153,9 +168,19 @@ class Network(): interface=interface.id, status=1, ) + logger.info('Created new IP {ip} on {interface}'.format( + ip=ip, interface=interface)) else: - if netbox_ip.interface != interface: + if netbox_ip.interface.id != interface.id: + logger.info( + 'Dected interface change: old interface is {old_interface} ' + '(id: {old_id}), new interface is {new_interface} (id: {new_id})' + .format( + old_interface=netbox_ip.interface, new_interface=interface, + old_id=netbox_ip.id, new_id=interface.id + )) netbox_ip.interface = interface netbox_ip.save() if nic_update: interface.save() + logger.debug('Finished updating NIC!') diff --git a/netbox_agent/server.py b/netbox_agent/server.py index b445664..895f09f 100644 --- a/netbox_agent/server.py +++ b/netbox_agent/server.py @@ -2,9 +2,9 @@ from pprint import pprint import socket from netbox_agent.config import netbox_instance as nb - import netbox_agent.dmidecode as dmidecode from netbox_agent.location import Datacenter, Rack +from netbox_agent.logging import logger from netbox_agent.network import Network @@ -81,14 +81,19 @@ class ServerBase(): model=self.get_chassis(), ) if not device_type: - raise Exception('Chassis "{}" doesn\'t exist'.format(self.get_chassis())) + error_msg = 'Chassis "{}" doesn\'t exist'.format(self.get_chassis()) + logger.error(error_msg) + raise Exception(error_msg) device_role = nb.dcim.device_roles.get( name='Server Chassis', ) + serial = self.get_chassis_service_tag() + logger.info('Creating chassis blade (serial: {serial})'.format( + serial=serial)) new_chassis = nb.dcim.devices.create( name=''.format(), device_type=device_type.id, - serial=self.get_chassis_service_tag(), + serial=serial, device_role=device_role.id, site=datacenter.id if datacenter else None, ) @@ -101,9 +106,15 @@ class ServerBase(): device_type = nb.dcim.device_types.get( model=self.get_product_name(), ) + serial = self.get_service_tag() + hostname = self.get_hostname() + logger.info( + 'Creating blade (serial: {serial}) {hostname} on chassis {chassis_serial}'.format( + serial=serial, hostname=hostname, chassis_serial=chassis.serial + )) new_blade = nb.dcim.devices.create( - name=self.get_hostname(), - serial=self.get_service_tag(), + name=hostname, + serial=serial, device_role=device_role.id, device_type=device_type.id, parent_device=chassis.id, @@ -120,9 +131,13 @@ class ServerBase(): ) if not device_type: raise Exception('Chassis "{}" doesn\'t exist'.format(self.get_chassis())) + serial = self.get_service_tag() + hostname = self.get_hostname() + logger.info('Creating server (serial: {serial}) {hostname}'.format( + serial=serial, hostname=hostname)) new_server = nb.dcim.devices.create( - name=self.get_hostname(), - serial=self.get_service_tag(), + name=hostname, + serial=serial, device_role=device_role.id, device_type=device_type.id, site=datacenter.id if datacenter else None, @@ -133,10 +148,12 @@ class ServerBase(): return nb.dcim.devices.get(serial=self.get_service_tag()) def netbox_create(self): + logger.debug('Creating Server..') datacenter = self.get_netbox_datacenter() if self.is_blade(): # let's find the blade - blade = nb.dcim.devices.get(serial=self.get_service_tag()) + serial = self.get_service_tag() + blade = nb.dcim.devices.get(serial=serial) chassis = nb.dcim.devices.get(serial=self.get_chassis_service_tag()) # if it doesn't exist, create it if not blade: @@ -151,11 +168,14 @@ class ServerBase(): blade = self._netbox_create_blade(chassis, datacenter) # Find the slot and update it with our blade + slot = self.get_blade_slot() device_bays = nb.dcim.device_bays.filter( device_id=chassis.id, - name='Blade {}'.format(self.get_blade_slot()), + name='Blade {}'.format(slot), ) if len(device_bays) > 0: + logger.info('Updating blade ({serial}) slot on: Blade {slot}'.format( + serial=serial, slot=slot)) device_bay = device_bays[0] device_bay.installed_device = blade device_bay.save() @@ -165,6 +185,7 @@ class ServerBase(): self._netbox_create_server() self.network.create_netbox_network_cards() + logger.debug('Server created!') def netbox_update(self): """ @@ -176,6 +197,7 @@ class ServerBase(): * hostname update * new network infos """ + logger.debug('Updating Server...') server = nb.dcim.devices.get(serial=self.get_service_tag()) if not server: raise Exception("The server (Serial: {}) isn't yet registered in Netbox, register" @@ -203,14 +225,23 @@ class ServerBase(): chassis = self._netbox_create_blade_chassis(datacenter) if move_device_bay or device_bay.name != 'Blade {}'.format(self.get_blade_slot()): + logger.info('Device ({serial}) seems to have moved, reseting old slot..'.format( + serial=server.serial)) device_bay.installed_device = None device_bay.save() + + slot = self.get_blade_slot() # Find the slot and update it with our blade device_bays = nb.dcim.device_bays.filter( device_id=chassis.id, - name='Blade {}'.format(self.get_blade_slot()), + name='Blade {}'.format(slot), ) if len(device_bays) > 0: + logger.info( + 'Setting device ({serial}) new slot on Blade {slot} ' + '(Chassis {chassis_serial})..'.format( + serial=server.serial, slot=slot, chassis_serial=chassis.serial + )) device_bay = device_bays[0] device_bay.installed_device = server device_bay.save() @@ -224,6 +255,7 @@ class ServerBase(): self.network.update_netbox_network_cards() if update: server.save() + logger.debug('Finished updating Server!') def print_debug(self): # FIXME: do something more generic by looping on every get_* methods From cc45d9ded91137d9419e6cc6b2e6dcb504cccfa6 Mon Sep 17 00:00:00 2001 From: Solvik Date: Wed, 7 Aug 2019 10:12:52 +0200 Subject: [PATCH 2/8] Update netbox_agent/network.py Co-Authored-By: Guillaume Gelin --- netbox_agent/network.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/netbox_agent/network.py b/netbox_agent/network.py index 0adca94..f11e06e 100644 --- a/netbox_agent/network.py +++ b/netbox_agent/network.py @@ -173,7 +173,7 @@ class Network(): else: if netbox_ip.interface.id != interface.id: logger.info( - 'Dected interface change: old interface is {old_interface} ' + 'Detected interface change: old interface is {old_interface} ' '(id: {old_id}), new interface is {new_interface} (id: {new_id})' .format( old_interface=netbox_ip.interface, new_interface=interface, From e8a9d318ccf792d40266c87f4a9453b91f0cdfc1 Mon Sep 17 00:00:00 2001 From: Solvik Blum Date: Wed, 7 Aug 2019 10:32:23 +0200 Subject: [PATCH 3/8] simpler logging, log_level from config file --- netbox_agent/config.py | 2 ++ netbox_agent/logging.py | 26 +++++++------------------- netbox_agent/network.py | 26 +++++++++++++------------- netbox_agent/server.py | 24 ++++++++++++------------ 4 files changed, 34 insertions(+), 44 deletions(-) diff --git a/netbox_agent/config.py b/netbox_agent/config.py index e2b4230..6d24c48 100644 --- a/netbox_agent/config.py +++ b/netbox_agent/config.py @@ -10,6 +10,8 @@ netbox_instance = pynetbox.api( token=config['netbox']['token'] ) +LOG_LEVEL = config.get('log_level', 'debug') + DATACENTER_LOCATION_DRIVER_FILE = None DATACENTER_LOCATION = None DATACENTER_LOCATION_REGEX = None diff --git a/netbox_agent/logging.py b/netbox_agent/logging.py index 95b31ef..4a21dc3 100644 --- a/netbox_agent/logging.py +++ b/netbox_agent/logging.py @@ -1,23 +1,11 @@ import logging +from netbox_agent.config import LOG_LEVEL -def init_log(): - logger = logging.getLogger('netbox_agent') + +logger = logging.getLogger() + +if LOG_LEVEL == 'debug': logger.setLevel(logging.DEBUG) - - logger_formatter = logging.Formatter('%(name)s - %(levelname)s - %(message)s') - - fh = logging.FileHandler('netbox.log') - fh.setLevel(logging.DEBUG) - fh.setFormatter(logger_formatter) - - ch = logging.StreamHandler() - ch.setLevel(logging.DEBUG) - ch.setFormatter(logger_formatter) - - logger.addHandler(fh) - logger.addHandler(ch) - return logger - - -logger = init_log() +else: + logger.setLevel(logging.INFO) diff --git a/netbox_agent/network.py b/netbox_agent/network.py index 0adca94..a3b0490 100644 --- a/netbox_agent/network.py +++ b/netbox_agent/network.py @@ -1,4 +1,5 @@ from itertools import chain +import logging import os import re @@ -7,7 +8,6 @@ import netifaces from netbox_agent.config import netbox_instance as nb from netbox_agent.ethtool import Ethtool -from netbox_agent.logging import logger IFACE_TYPE_100ME_FIXED = 800 IFACE_TYPE_1GE_FIXED = 1000 @@ -82,7 +82,7 @@ class Network(): def create_netbox_nic(self, device, nic): # TODO: add Optic Vendor, PN and Serial type = self.get_netbox_type_for_nic(nic) - logger.info('Creating NIC {name} ({mac}) on {device}'.format( + logging.info('Creating NIC {name} ({mac}) on {device}'.format( name=nic['name'], mac=nic['mac'], device=device.name)) return nb.dcim.interfaces.create( device=device.id, @@ -92,7 +92,7 @@ class Network(): ) def create_netbox_network_cards(self): - logger.info('Creating NIC..') + logging.debug('Creating NIC...') device = self.server.get_netbox_server() for nic in self.nics: interface = nb.dcim.interfaces.get( @@ -110,22 +110,22 @@ class Network(): address=ip, ) if netbox_ip: - logger.info('Assigning existing IP {ip} to {interface}'.format( + logging.info('Assigning existing IP {ip} to {interface}'.format( ip=ip, interface=new_interface)) netbox_ip.interface = new_interface netbox_ip.save() else: - logger.info('Create new IP {ip} on {interface}'.format( + logging.info('Create new IP {ip} on {interface}'.format( ip=ip, interface=new_interface)) netbox_ip = nb.ipam.ip_addresses.create( address=ip, interface=new_interface.id, status=1, ) - logger.info('Finished creating NIC!') + logging.debug('Finished creating NIC!') def update_netbox_network_cards(self): - logger.debug('Updating NIC..') + logging.debug('Updating NIC...') device = self.server.get_netbox_server() # delete IP on netbox that are not known on this server @@ -137,7 +137,7 @@ class Network(): ])) for netbox_ip in netbox_ips: if netbox_ip.address not in all_local_ips: - logger.info('Unassigning IP {ip} from {interface}'.format( + logging.info('Unassigning IP {ip} from {interface}'.format( ip=netbox_ip.address, interface=netbox_ip.interface)) netbox_ip.interface = None netbox_ip.save() @@ -151,7 +151,7 @@ class Network(): nic_update = False if nic['name'] != interface.name: nic_update = True - logger.info('Updating interface {interface} name to: {name}'.format( + logging.info('Updating interface {interface} name to: {name}'.format( interface=interface, name=nic['name'])) interface.name = nic['name'] @@ -168,12 +168,12 @@ class Network(): interface=interface.id, status=1, ) - logger.info('Created new IP {ip} on {interface}'.format( + logging.info('Created new IP {ip} on {interface}'.format( ip=ip, interface=interface)) else: if netbox_ip.interface.id != interface.id: - logger.info( - 'Dected interface change: old interface is {old_interface} ' + logging.info( + 'Detected interface change: old interface is {old_interface} ' '(id: {old_id}), new interface is {new_interface} (id: {new_id})' .format( old_interface=netbox_ip.interface, new_interface=interface, @@ -183,4 +183,4 @@ class Network(): netbox_ip.save() if nic_update: interface.save() - logger.debug('Finished updating NIC!') + logging.debug('Finished updating NIC!') diff --git a/netbox_agent/server.py b/netbox_agent/server.py index 895f09f..6bdfd71 100644 --- a/netbox_agent/server.py +++ b/netbox_agent/server.py @@ -1,10 +1,10 @@ +import logging from pprint import pprint import socket from netbox_agent.config import netbox_instance as nb import netbox_agent.dmidecode as dmidecode from netbox_agent.location import Datacenter, Rack -from netbox_agent.logging import logger from netbox_agent.network import Network @@ -82,13 +82,13 @@ class ServerBase(): ) if not device_type: error_msg = 'Chassis "{}" doesn\'t exist'.format(self.get_chassis()) - logger.error(error_msg) + logging.error(error_msg) raise Exception(error_msg) device_role = nb.dcim.device_roles.get( name='Server Chassis', ) serial = self.get_chassis_service_tag() - logger.info('Creating chassis blade (serial: {serial})'.format( + logging.info('Creating chassis blade (serial: {serial})'.format( serial=serial)) new_chassis = nb.dcim.devices.create( name=''.format(), @@ -108,7 +108,7 @@ class ServerBase(): ) serial = self.get_service_tag() hostname = self.get_hostname() - logger.info( + logging.info( 'Creating blade (serial: {serial}) {hostname} on chassis {chassis_serial}'.format( serial=serial, hostname=hostname, chassis_serial=chassis.serial )) @@ -133,7 +133,7 @@ class ServerBase(): raise Exception('Chassis "{}" doesn\'t exist'.format(self.get_chassis())) serial = self.get_service_tag() hostname = self.get_hostname() - logger.info('Creating server (serial: {serial}) {hostname}'.format( + logging.info('Creating server (serial: {serial}) {hostname}'.format( serial=serial, hostname=hostname)) new_server = nb.dcim.devices.create( name=hostname, @@ -148,7 +148,7 @@ class ServerBase(): return nb.dcim.devices.get(serial=self.get_service_tag()) def netbox_create(self): - logger.debug('Creating Server..') + logging.debug('Creating Server..') datacenter = self.get_netbox_datacenter() if self.is_blade(): # let's find the blade @@ -174,7 +174,7 @@ class ServerBase(): name='Blade {}'.format(slot), ) if len(device_bays) > 0: - logger.info('Updating blade ({serial}) slot on: Blade {slot}'.format( + logging.info('Updating blade ({serial}) slot on: Blade {slot}'.format( serial=serial, slot=slot)) device_bay = device_bays[0] device_bay.installed_device = blade @@ -185,7 +185,7 @@ class ServerBase(): self._netbox_create_server() self.network.create_netbox_network_cards() - logger.debug('Server created!') + logging.debug('Server created!') def netbox_update(self): """ @@ -197,7 +197,7 @@ class ServerBase(): * hostname update * new network infos """ - logger.debug('Updating Server...') + logging.debug('Updating Server...') server = nb.dcim.devices.get(serial=self.get_service_tag()) if not server: raise Exception("The server (Serial: {}) isn't yet registered in Netbox, register" @@ -225,7 +225,7 @@ class ServerBase(): chassis = self._netbox_create_blade_chassis(datacenter) if move_device_bay or device_bay.name != 'Blade {}'.format(self.get_blade_slot()): - logger.info('Device ({serial}) seems to have moved, reseting old slot..'.format( + logging.info('Device ({serial}) seems to have moved, reseting old slot..'.format( serial=server.serial)) device_bay.installed_device = None device_bay.save() @@ -237,7 +237,7 @@ class ServerBase(): name='Blade {}'.format(slot), ) if len(device_bays) > 0: - logger.info( + logging.info( 'Setting device ({serial}) new slot on Blade {slot} ' '(Chassis {chassis_serial})..'.format( serial=server.serial, slot=slot, chassis_serial=chassis.serial @@ -255,7 +255,7 @@ class ServerBase(): self.network.update_netbox_network_cards() if update: server.save() - logger.debug('Finished updating Server!') + logging.debug('Finished updating Server!') def print_debug(self): # FIXME: do something more generic by looping on every get_* methods From f991ac85f7b4984fac77d4b2c16d34ec43770bd1 Mon Sep 17 00:00:00 2001 From: Solvik Blum Date: Wed, 7 Aug 2019 15:12:38 +0200 Subject: [PATCH 4/8] call logging.py for setup --- netbox_agent/cli.py | 1 + 1 file changed, 1 insertion(+) diff --git a/netbox_agent/cli.py b/netbox_agent/cli.py index 6b29d80..4f27bcb 100644 --- a/netbox_agent/cli.py +++ b/netbox_agent/cli.py @@ -2,6 +2,7 @@ import argparse from netbox_agent.dell.dell import DellHost import netbox_agent.dmidecode as dmidecode +from netbox_agent.logging import logging from netbox_agent.hp.hp import HPHost MANUFACTURERS = { From 49731644fdcdb0da4ca880390361db9441cff7b0 Mon Sep 17 00:00:00 2001 From: Solvik Blum Date: Wed, 7 Aug 2019 15:36:03 +0200 Subject: [PATCH 5/8] fix bug while testing debug --- netbox_agent/ethtool.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/netbox_agent/ethtool.py b/netbox_agent/ethtool.py index 91522c9..312df95 100644 --- a/netbox_agent/ethtool.py +++ b/netbox_agent/ethtool.py @@ -61,7 +61,7 @@ class Ethtool(): if status != 0: return {} r = re.search(r'Identifier.*\((\w+)\)', output) - if r and r.groups() > 0: + if r and len(r.groups()) > 0: return {'form_factor': r.groups()[0]} def parse(self): From d7af66b325139a01a9352bb9c4e3a352ee2048a5 Mon Sep 17 00:00:00 2001 From: Solvik Blum Date: Wed, 7 Aug 2019 15:36:24 +0200 Subject: [PATCH 6/8] fix bug while testing debug with an interface not existing while updating --- netbox_agent/network.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/netbox_agent/network.py b/netbox_agent/network.py index a3b0490..07ec6c0 100644 --- a/netbox_agent/network.py +++ b/netbox_agent/network.py @@ -147,6 +147,11 @@ class Network(): interface = nb.dcim.interfaces.get( mac_address=nic['mac'], ) + if not interface: + logging.info('Interface {} not found, creating..'.format( + mac_address=nic['mac']) + ) + interface = self.create_netbox_nic(device, nic) nic_update = False if nic['name'] != interface.name: From b60860e0f5315a033d97b6163a5cd2d0058b92e3 Mon Sep 17 00:00:00 2001 From: Solvik Blum Date: Wed, 7 Aug 2019 15:36:47 +0200 Subject: [PATCH 7/8] fix bug while testing server creation --- netbox_agent/server.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/netbox_agent/server.py b/netbox_agent/server.py index 6bdfd71..636fdde 100644 --- a/netbox_agent/server.py +++ b/netbox_agent/server.py @@ -182,7 +182,7 @@ class ServerBase(): else: server = nb.dcim.devices.get(serial=self.get_service_tag()) if not server: - self._netbox_create_server() + self._netbox_create_server(datacenter) self.network.create_netbox_network_cards() logging.debug('Server created!') From 14e061159dc47a0bddcbf579e079b4098bb93a99 Mon Sep 17 00:00:00 2001 From: Solvik Blum Date: Wed, 7 Aug 2019 15:37:14 +0200 Subject: [PATCH 8/8] ignore unused statement --- netbox_agent/cli.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/netbox_agent/cli.py b/netbox_agent/cli.py index 4f27bcb..d7ee2bb 100644 --- a/netbox_agent/cli.py +++ b/netbox_agent/cli.py @@ -2,8 +2,8 @@ import argparse from netbox_agent.dell.dell import DellHost import netbox_agent.dmidecode as dmidecode -from netbox_agent.logging import logging from netbox_agent.hp.hp import HPHost +from netbox_agent.logging import logging # NOQA MANUFACTURERS = { 'Dell Inc.': DellHost,