Local inventory #16
2 changed files with 21 additions and 33 deletions
|
@ -1,8 +1,8 @@
|
|||
import subprocess
|
||||
import re
|
||||
from shutil import which
|
||||
|
||||
from netbox_agent.config import netbox_instance as nb
|
||||
from netbox_agent.misc import is_tool
|
||||
from netbox_agent.raid.hp import HPRaid
|
||||
from netbox_agent.raid.dell import StorcliRaid
|
||||
import netbox_agent.dmidecode as dmidecode
|
||||
|
@ -25,39 +25,23 @@ for key, tag in INVENTORY_TAG.items():
|
|||
comments=tag['name'],
|
||||
)
|
||||
|
||||
|
||||
def is_tool(name):
|
||||
'''Check whether `name` is on PATH and marked as executable.'''
|
||||
return which(name) is not None
|
||||
|
||||
class DictDiffer(object):
|
||||
"""
|
||||
Calculate the difference between two dictionaries as:
|
||||
(1) items added
|
||||
(2) items removed
|
||||
(3) keys same in both but changed values
|
||||
(4) keys same in both and unchanged values
|
||||
"""
|
||||
|
||||
def __init__(self, current_dict, past_dict):
|
||||
self.current_dict, self.past_dict = current_dict, past_dict
|
||||
self.set_current, self.set_past = set(current_dict.keys()), set(past_dict.keys())
|
||||
self.intersect = self.set_current.intersection(self.set_past)
|
||||
|
||||
def added(self):
|
||||
return self.set_current - self.intersect
|
||||
def removed(self):
|
||||
return self.set_past - self.intersect
|
||||
def changed(self):
|
||||
return set(o for o in self.intersect if self.past_dict[o] != self.current_dict[o])
|
||||
def unchanged(self):
|
||||
return set(o for o in self.intersect if self.past_dict[o] == self.current_dict[o])
|
||||
|
||||
class Inventory():
|
||||
"""
|
||||
Better Inventory items coming, see:
|
||||
- https://github.com/netbox-community/netbox/issues/3087
|
||||
- https://github.com/netbox-community/netbox/issues/3333
|
||||
|
||||
This class implements for:
|
||||
* memory
|
||||
* cpu
|
||||
* raid cards
|
||||
* disks
|
||||
|
||||
methods that:
|
||||
* get local item
|
||||
* get netbox item
|
||||
* create netbox item
|
||||
* update netbox item
|
||||
"""
|
||||
|
||||
def __init__(self, server):
|
||||
|
@ -65,7 +49,6 @@ class Inventory():
|
|||
self.device_id = self.server.get_netbox_server().id
|
||||
self.raid = None
|
||||
self.disks = []
|
||||
self.memories = []
|
||||
|
||||
def get_cpus(self):
|
||||
model = None
|
||||
|
|
5
netbox_agent/misc.py
Normal file
5
netbox_agent/misc.py
Normal file
|
@ -0,0 +1,5 @@
|
|||
from shutil import which
|
||||
|
||||
def is_tool(name):
|
||||
'''Check whether `name` is on PATH and marked as executable.'''
|
||||
return which(name) is not None
|
Loading…
Add table
Reference in a new issue