add some logging

This commit is contained in:
Solvik Blum 2019-08-06 17:59:46 +02:00
parent 99c205ca65
commit cc08300fee
No known key found for this signature in database
GPG key ID: CC12B3DC262B6C47
3 changed files with 92 additions and 12 deletions

23
netbox_agent/logging.py Normal file
View file

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

View file

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

View file

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