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']
)
LOG_LEVEL = config.get('log_level', 'debug')
DATACENTER_LOCATION_DRIVER_FILE = None
DATACENTER_LOCATION = None
DATACENTER_LOCATION_REGEX = None

View file

@ -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)

View file

@ -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!')

View file

@ -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