add power supply support
This commit is contained in:
parent
d39b692985
commit
175073cf31
5 changed files with 94 additions and 2 deletions
|
|
@ -24,7 +24,7 @@ def run(config):
|
||||||
if config.register:
|
if config.register:
|
||||||
server.netbox_create(config)
|
server.netbox_create(config)
|
||||||
if config.update_all or config.update_network or config.update_location or \
|
if config.update_all or config.update_network or config.update_location or \
|
||||||
config.update_inventory:
|
config.update_inventory or config.update_psu:
|
||||||
server.netbox_update(config)
|
server.netbox_update(config)
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -22,6 +22,7 @@ def get_config():
|
||||||
p.add_argument('--update-network', action='store_true', help='Update network')
|
p.add_argument('--update-network', action='store_true', help='Update network')
|
||||||
p.add_argument('--update-inventory', action='store_true', help='Update inventory')
|
p.add_argument('--update-inventory', action='store_true', help='Update inventory')
|
||||||
p.add_argument('--update-location', action='store_true', help='Update location')
|
p.add_argument('--update-location', action='store_true', help='Update location')
|
||||||
|
p.add_argument('--update-psu', action='store_true', help='Update PSU')
|
||||||
|
|
||||||
p.add_argument('--log_level', default='debug')
|
p.add_argument('--log_level', default='debug')
|
||||||
p.add_argument('--netbox.url', help='Netbox URL')
|
p.add_argument('--netbox.url', help='Netbox URL')
|
||||||
|
|
|
||||||
|
|
@ -503,7 +503,6 @@ class Network():
|
||||||
|
|
||||||
def update_netbox_network_cards(self):
|
def update_netbox_network_cards(self):
|
||||||
if config.update_all is None or config.update_network is None:
|
if config.update_all is None or config.update_network is None:
|
||||||
print(config)
|
|
||||||
return None
|
return None
|
||||||
logging.debug('Updating NIC...')
|
logging.debug('Updating NIC...')
|
||||||
|
|
||||||
|
|
|
||||||
83
netbox_agent/power.py
Normal file
83
netbox_agent/power.py
Normal file
|
|
@ -0,0 +1,83 @@
|
||||||
|
import logging
|
||||||
|
|
||||||
|
from netbox_agent.config import netbox_instance as nb, config
|
||||||
|
|
||||||
|
PSU_DMI_TYPE = 39
|
||||||
|
|
||||||
|
|
||||||
|
class PowerSupply():
|
||||||
|
def __init__(self, server=None):
|
||||||
|
self.server = server
|
||||||
|
netbox_server = self.server.get_netbox_server()
|
||||||
|
if self.server.is_blade():
|
||||||
|
self.device_id = netbox_server.parent_device.id if netbox_server else None
|
||||||
|
else:
|
||||||
|
self.device_id = netbox_server.id if netbox_server else None
|
||||||
|
|
||||||
|
def get_power_supply(self):
|
||||||
|
power_supply = []
|
||||||
|
for psu in self.server.dmi.get_by_type(PSU_DMI_TYPE):
|
||||||
|
if 'Present' not in psu['Status']:
|
||||||
|
continue
|
||||||
|
|
||||||
|
max_power = psu.get('Max Power Capacity').split()[0]
|
||||||
|
desc = '{} - {}'.format(
|
||||||
|
psu.get('Manufacturer', 'No Manufacturer').strip(),
|
||||||
|
psu.get('Name', 'No name').strip(),
|
||||||
|
)
|
||||||
|
power_supply.append({
|
||||||
|
'name': psu.get('Serial Number', 'No S/N').strip(),
|
||||||
|
'description': desc,
|
||||||
|
'allocated_draw': None,
|
||||||
|
'maximum_draw': int(max_power),
|
||||||
|
'device': self.device_id,
|
||||||
|
})
|
||||||
|
return power_supply
|
||||||
|
|
||||||
|
def get_netbox_power_supply(self):
|
||||||
|
return nb.dcim.power_ports.filter(
|
||||||
|
device_id=self.device_id
|
||||||
|
)
|
||||||
|
|
||||||
|
def create_or_update_power_supply(self):
|
||||||
|
nb_psus = self.get_netbox_power_supply()
|
||||||
|
psus = self.get_power_supply()
|
||||||
|
|
||||||
|
# Delete unknown PSU
|
||||||
|
delete = False
|
||||||
|
for nb_psu in nb_psus:
|
||||||
|
if nb_psu.name not in [x['name'] for x in psus]:
|
||||||
|
logging.info('Deleting unknown locally PSU {name}'.format(
|
||||||
|
name=nb_psu.name
|
||||||
|
))
|
||||||
|
nb_psu.delete()
|
||||||
|
delete = True
|
||||||
|
|
||||||
|
if delete:
|
||||||
|
nb_psus = self.get_netbox_power_supply()
|
||||||
|
|
||||||
|
# sync existing Netbox PSU with local infos
|
||||||
|
for nb_psu in nb_psus:
|
||||||
|
local_psu = next(
|
||||||
|
item for item in psus if item['name'] == nb_psu.name
|
||||||
|
)
|
||||||
|
update = False
|
||||||
|
if nb_psu.description != local_psu['description']:
|
||||||
|
update = True
|
||||||
|
nb_psu.description = local_psu['description']
|
||||||
|
if nb_psu.maximum_draw != local_psu['maximum_draw']:
|
||||||
|
update = True
|
||||||
|
nb_psu.maximum_draw = local_psu['maximum_draw']
|
||||||
|
if update:
|
||||||
|
nb_psu.save()
|
||||||
|
|
||||||
|
for psu in psus:
|
||||||
|
if psu['name'] not in [x.name for x in nb_psus]:
|
||||||
|
logging.info('Creating PSU {name} ({description}), {maximum_draw}W'.format(
|
||||||
|
**psu
|
||||||
|
))
|
||||||
|
nb_psu = nb.dcim.power_ports.create(
|
||||||
|
**psu
|
||||||
|
)
|
||||||
|
|
||||||
|
return True
|
||||||
|
|
@ -7,6 +7,7 @@ import netbox_agent.dmidecode as dmidecode
|
||||||
from netbox_agent.location import Datacenter, Rack
|
from netbox_agent.location import Datacenter, Rack
|
||||||
from netbox_agent.inventory import Inventory
|
from netbox_agent.inventory import Inventory
|
||||||
from netbox_agent.network import Network
|
from netbox_agent.network import Network
|
||||||
|
from netbox_agent.power import PowerSupply
|
||||||
|
|
||||||
|
|
||||||
class ServerBase():
|
class ServerBase():
|
||||||
|
|
@ -232,6 +233,10 @@ class ServerBase():
|
||||||
|
|
||||||
self.network = Network(server=self)
|
self.network = Network(server=self)
|
||||||
self.network.create_netbox_network_cards()
|
self.network.create_netbox_network_cards()
|
||||||
|
|
||||||
|
self.power = PowerSupply(server=self)
|
||||||
|
self.power.create_or_update_power_supply()
|
||||||
|
|
||||||
if config.inventory:
|
if config.inventory:
|
||||||
self.inventory = Inventory(server=self)
|
self.inventory = Inventory(server=self)
|
||||||
self.inventory.create()
|
self.inventory.create()
|
||||||
|
|
@ -313,6 +318,10 @@ class ServerBase():
|
||||||
if config.update_all or config.update_inventory:
|
if config.update_all or config.update_inventory:
|
||||||
self.inventory = Inventory(server=self)
|
self.inventory = Inventory(server=self)
|
||||||
self.inventory.update()
|
self.inventory.update()
|
||||||
|
# update psu
|
||||||
|
if config.update_all or config.update_psu:
|
||||||
|
self.power = PowerSupply(server=self)
|
||||||
|
self.power.create_or_update_power_supply()
|
||||||
if update:
|
if update:
|
||||||
server.save()
|
server.save()
|
||||||
logging.debug('Finished updating Server!')
|
logging.debug('Finished updating Server!')
|
||||||
|
|
|
||||||
Loading…
Add table
editor.link_modal.header
Reference in a new issue