simpler logging, log_level from config file

This commit is contained in:
Solvik Blum 2019-08-07 10:32:23 +02:00
parent cc08300fee
commit e8a9d318cc
No known key found for this signature in database
GPG key ID: CC12B3DC262B6C47
4 changed files with 34 additions and 44 deletions

View file

@ -10,6 +10,8 @@ netbox_instance = pynetbox.api(
token=config['netbox']['token'] token=config['netbox']['token']
) )
LOG_LEVEL = config.get('log_level', 'debug')
DATACENTER_LOCATION_DRIVER_FILE = None DATACENTER_LOCATION_DRIVER_FILE = None
DATACENTER_LOCATION = None DATACENTER_LOCATION = None
DATACENTER_LOCATION_REGEX = None DATACENTER_LOCATION_REGEX = None

View file

@ -1,23 +1,11 @@
import logging 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.setLevel(logging.DEBUG)
else:
logger_formatter = logging.Formatter('%(name)s - %(levelname)s - %(message)s') logger.setLevel(logging.INFO)
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()

View file

@ -1,4 +1,5 @@
from itertools import chain from itertools import chain
import logging
import os import os
import re import re
@ -7,7 +8,6 @@ import netifaces
from netbox_agent.config import netbox_instance as nb from netbox_agent.config import netbox_instance as nb
from netbox_agent.ethtool import Ethtool from netbox_agent.ethtool import Ethtool
from netbox_agent.logging import logger
IFACE_TYPE_100ME_FIXED = 800 IFACE_TYPE_100ME_FIXED = 800
IFACE_TYPE_1GE_FIXED = 1000 IFACE_TYPE_1GE_FIXED = 1000
@ -82,7 +82,7 @@ class Network():
def create_netbox_nic(self, device, nic): def create_netbox_nic(self, device, nic):
# TODO: add Optic Vendor, PN and Serial # TODO: add Optic Vendor, PN and Serial
type = self.get_netbox_type_for_nic(nic) 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)) name=nic['name'], mac=nic['mac'], device=device.name))
return nb.dcim.interfaces.create( return nb.dcim.interfaces.create(
device=device.id, device=device.id,
@ -92,7 +92,7 @@ class Network():
) )
def create_netbox_network_cards(self): def create_netbox_network_cards(self):
logger.info('Creating NIC..') logging.debug('Creating NIC...')
device = self.server.get_netbox_server() device = self.server.get_netbox_server()
for nic in self.nics: for nic in self.nics:
interface = nb.dcim.interfaces.get( interface = nb.dcim.interfaces.get(
@ -110,22 +110,22 @@ class Network():
address=ip, address=ip,
) )
if netbox_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)) ip=ip, interface=new_interface))
netbox_ip.interface = new_interface netbox_ip.interface = new_interface
netbox_ip.save() netbox_ip.save()
else: else:
logger.info('Create new IP {ip} on {interface}'.format( logging.info('Create new IP {ip} on {interface}'.format(
ip=ip, interface=new_interface)) ip=ip, interface=new_interface))
netbox_ip = nb.ipam.ip_addresses.create( netbox_ip = nb.ipam.ip_addresses.create(
address=ip, address=ip,
interface=new_interface.id, interface=new_interface.id,
status=1, status=1,
) )
logger.info('Finished creating NIC!') logging.debug('Finished creating NIC!')
def update_netbox_network_cards(self): def update_netbox_network_cards(self):
logger.debug('Updating NIC..') logging.debug('Updating NIC...')
device = self.server.get_netbox_server() device = self.server.get_netbox_server()
# delete IP on netbox that are not known on this server # delete IP on netbox that are not known on this server
@ -137,7 +137,7 @@ class Network():
])) ]))
for netbox_ip in netbox_ips: for netbox_ip in netbox_ips:
if netbox_ip.address not in all_local_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)) ip=netbox_ip.address, interface=netbox_ip.interface))
netbox_ip.interface = None netbox_ip.interface = None
netbox_ip.save() netbox_ip.save()
@ -151,7 +151,7 @@ class Network():
nic_update = False nic_update = False
if nic['name'] != interface.name: if nic['name'] != interface.name:
nic_update = True 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=interface, name=nic['name']))
interface.name = nic['name'] interface.name = nic['name']
@ -168,12 +168,12 @@ class Network():
interface=interface.id, interface=interface.id,
status=1, status=1,
) )
logger.info('Created new IP {ip} on {interface}'.format( logging.info('Created new IP {ip} on {interface}'.format(
ip=ip, interface=interface)) ip=ip, interface=interface))
else: else:
if netbox_ip.interface.id != interface.id: if netbox_ip.interface.id != interface.id:
logger.info( logging.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})' '(id: {old_id}), new interface is {new_interface} (id: {new_id})'
.format( .format(
old_interface=netbox_ip.interface, new_interface=interface, old_interface=netbox_ip.interface, new_interface=interface,
@ -183,4 +183,4 @@ class Network():
netbox_ip.save() netbox_ip.save()
if nic_update: if nic_update:
interface.save() interface.save()
logger.debug('Finished updating NIC!') logging.debug('Finished updating NIC!')

View file

@ -1,10 +1,10 @@
import logging
from pprint import pprint from pprint import pprint
import socket import socket
from netbox_agent.config import netbox_instance as nb from netbox_agent.config import netbox_instance as nb
import netbox_agent.dmidecode as dmidecode import netbox_agent.dmidecode as dmidecode
from netbox_agent.location import Datacenter, Rack from netbox_agent.location import Datacenter, Rack
from netbox_agent.logging import logger
from netbox_agent.network import Network from netbox_agent.network import Network
@ -82,13 +82,13 @@ class ServerBase():
) )
if not device_type: if not device_type:
error_msg = 'Chassis "{}" doesn\'t exist'.format(self.get_chassis()) error_msg = 'Chassis "{}" doesn\'t exist'.format(self.get_chassis())
logger.error(error_msg) logging.error(error_msg)
raise Exception(error_msg) raise Exception(error_msg)
device_role = nb.dcim.device_roles.get( device_role = nb.dcim.device_roles.get(
name='Server Chassis', name='Server Chassis',
) )
serial = self.get_chassis_service_tag() serial = self.get_chassis_service_tag()
logger.info('Creating chassis blade (serial: {serial})'.format( logging.info('Creating chassis blade (serial: {serial})'.format(
serial=serial)) serial=serial))
new_chassis = nb.dcim.devices.create( new_chassis = nb.dcim.devices.create(
name=''.format(), name=''.format(),
@ -108,7 +108,7 @@ class ServerBase():
) )
serial = self.get_service_tag() serial = self.get_service_tag()
hostname = self.get_hostname() hostname = self.get_hostname()
logger.info( logging.info(
'Creating blade (serial: {serial}) {hostname} on chassis {chassis_serial}'.format( 'Creating blade (serial: {serial}) {hostname} on chassis {chassis_serial}'.format(
serial=serial, hostname=hostname, chassis_serial=chassis.serial serial=serial, hostname=hostname, chassis_serial=chassis.serial
)) ))
@ -133,7 +133,7 @@ class ServerBase():
raise Exception('Chassis "{}" doesn\'t exist'.format(self.get_chassis())) raise Exception('Chassis "{}" doesn\'t exist'.format(self.get_chassis()))
serial = self.get_service_tag() serial = self.get_service_tag()
hostname = self.get_hostname() hostname = self.get_hostname()
logger.info('Creating server (serial: {serial}) {hostname}'.format( logging.info('Creating server (serial: {serial}) {hostname}'.format(
serial=serial, hostname=hostname)) serial=serial, hostname=hostname))
new_server = nb.dcim.devices.create( new_server = nb.dcim.devices.create(
name=hostname, name=hostname,
@ -148,7 +148,7 @@ class ServerBase():
return nb.dcim.devices.get(serial=self.get_service_tag()) return nb.dcim.devices.get(serial=self.get_service_tag())
def netbox_create(self): def netbox_create(self):
logger.debug('Creating Server..') logging.debug('Creating Server..')
datacenter = self.get_netbox_datacenter() datacenter = self.get_netbox_datacenter()
if self.is_blade(): if self.is_blade():
# let's find the blade # let's find the blade
@ -174,7 +174,7 @@ class ServerBase():
name='Blade {}'.format(slot), name='Blade {}'.format(slot),
) )
if len(device_bays) > 0: 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)) serial=serial, slot=slot))
device_bay = device_bays[0] device_bay = device_bays[0]
device_bay.installed_device = blade device_bay.installed_device = blade
@ -185,7 +185,7 @@ class ServerBase():
self._netbox_create_server() self._netbox_create_server()
self.network.create_netbox_network_cards() self.network.create_netbox_network_cards()
logger.debug('Server created!') logging.debug('Server created!')
def netbox_update(self): def netbox_update(self):
""" """
@ -197,7 +197,7 @@ class ServerBase():
* hostname update * hostname update
* new network infos * new network infos
""" """
logger.debug('Updating Server...') logging.debug('Updating Server...')
server = nb.dcim.devices.get(serial=self.get_service_tag()) server = nb.dcim.devices.get(serial=self.get_service_tag())
if not server: if not server:
raise Exception("The server (Serial: {}) isn't yet registered in Netbox, register" 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) chassis = self._netbox_create_blade_chassis(datacenter)
if move_device_bay or device_bay.name != 'Blade {}'.format(self.get_blade_slot()): 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)) serial=server.serial))
device_bay.installed_device = None device_bay.installed_device = None
device_bay.save() device_bay.save()
@ -237,7 +237,7 @@ class ServerBase():
name='Blade {}'.format(slot), name='Blade {}'.format(slot),
) )
if len(device_bays) > 0: if len(device_bays) > 0:
logger.info( logging.info(
'Setting device ({serial}) new slot on Blade {slot} ' 'Setting device ({serial}) new slot on Blade {slot} '
'(Chassis {chassis_serial})..'.format( '(Chassis {chassis_serial})..'.format(
serial=server.serial, slot=slot, chassis_serial=chassis.serial serial=server.serial, slot=slot, chassis_serial=chassis.serial
@ -255,7 +255,7 @@ class ServerBase():
self.network.update_netbox_network_cards() self.network.update_netbox_network_cards()
if update: if update:
server.save() server.save()
logger.debug('Finished updating Server!') logging.debug('Finished updating Server!')
def print_debug(self): def print_debug(self):
# FIXME: do something more generic by looping on every get_* methods # FIXME: do something more generic by looping on every get_* methods