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

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