-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlnms_device_inventory.py
83 lines (61 loc) · 2.18 KB
/
lnms_device_inventory.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#!/usr/bin/python3
# This Script Gather infromation need for device list
import logging
from logging.handlers import RotatingFileHandler
from Libs.LibreNMSAPIClient.LibreNMSAPIClient import LibreNMSAPIClient
import os
lnms = LibreNMSAPIClient()
# mmaking file location
from logging.handlers import RotatingFileHandler
log_dir = os.environ['log_dir']
if not os.path.exists(log_dir):
os.makedirs(log_dir)
log_file = log_dir + "librenms_device_inventory.log"
logging.basicConfig (
level = logging.INFO,
format = "%(asctime)s:%(levelname)s - %(message)s",
datefmt = "%Y-%m-%d %H:%M:%S",
handlers = [
RotatingFileHandler(
log_file,
maxBytes = 5*1024*1024,
backupCount = 10
)
]
)
# file location
output_dir = os.environ['info_dir']
if not os.path.exists(output_dir):
os.makedirs(output_dir)
def make_device_list(name):
f=open(output_dir + name,"w")
try:
f.write("NAME, IP, HARDWARE, VERSION, SERIAL"+"\n")
for device in lnms.list_devices():
device['short_name'] = device['sysName'].split(".")[0]
logging.info(f"device added to list: {device['short_name']}")
if device['serial'] == None:
logging.warning(f"Unable to get serialnumber from {device['short_name']}")
f.write(f"{device['short_name']}, {device['hostname']}, {device['hardware']}, {device['version']}, {device['serial']}"+"\n")
f.close()
except:
print("fail")
devices_w_serial = []
_devices_wo_serial = []
try:
for device in lnms.list_devices():
device['short_name'] = device['sysName'].split(".")[0]
if device['serial'] != None:
devices_w_serial.append(device)
else:
_devices_wo_serial.append(device)
except:
print("fail")
print("\n"+"Devices with a serial number:")
for device in devices_w_serial:
print(f" {device['short_name']} S:{device['serial']}")
# print("\n"+"Devices without a serial number:")
# for device in devices_w_serial:
# print(f" {device['short_name']} ({device['hostname']})")
make_device_list("device-list.csv")
print("\n"+f"Done, detailed list can be found under: ./{output_dir}"+"\n")