-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathauto_import_devices_v4.py
604 lines (527 loc) · 24.4 KB
/
auto_import_devices_v4.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
import time
import os
import pandas as pd
import pynetbox
import requests
import re
import urllib3
import warnings
from urllib3.exceptions import InsecureRequestWarning
from openpyxl import load_workbook
from datetime import datetime
import random
WIDTH=19
U_HEIGHT=42
STATUS = 'active'
TAG_NAME_AUTO_IMPORT = ["AutoImportExcel", "Tag1"]
# FILE_PATH = '/opt/netbox/netbox/plugin/netbox-import-tool/Auto_Import_Device/Version_4/Rack_M1-10.xlsx'
FILE_PATH = '/opt/netbox/netbox/plugin/netbox-import-tool/Auto_Import_Device/Version_4/test_date_time.xlsx'
# FILE_PATH = '/opt/netbox/netbox/plugin/netbox-import-tool/Auto_Import_Device/Version_4/DC3_check_23122024_test5.xlsx'
NetBox_URL = 'http://172.16.66.177:8000'
NetBox_Token = '633a7508b878bcbf33091699289a8a3026a3fbf6'
SITE_NAME = 'VNPT NTL' # Site của netbox
SHEET_NAME = 'Input' # Tên của sheet muốn import
# Khai báo biến global
TAG_ID_AUTO_IMPORT = []
DEVICE_HEIGHTS = []
LIST_ADD_DEVICE_ROLE_ERROR = []
LIST_ADD_MANUFACTURES_ERROR = []
LIST_ADD_DEVICE_TYPE_ERROR = []
LIST_ADD_DEVICE_ERROR = []
# Class lưu thông tin độ cao của device type
class DeviceHight:
def __init__(self, name, height):
self.name = name
self.height = height
def __repr__(self):
return f"DeviceHight(name='{self.name}', height={self.height})"
def file_check(input_file):
if os.path.exists(input_file):
# read file excel
workbook = load_workbook(input_file)
global sheet
sheet = workbook.active
global df
columns = [cell.value for cell in sheet[1]]
# new code
df = pd.read_excel(input_file, sheet_name=SHEET_NAME)
else:
print(f"File '{input_file}' doesn't exist!")
exit()
def netbox_connection_check(netboxurl, netboxtoken):
try:
warnings.simplefilter("ignore", InsecureRequestWarning)
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
response = requests.get(
netboxurl,
headers={"Authorization": f"Token {netboxtoken}"},
timeout=20,
verify=False
)
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
if response.status_code == 200:
global nb
nb = pynetbox.api(netboxurl, token=netboxtoken)
nb.http_session.verify = False
print("Connection Check complete!")
else:
print(f"Connection Error: {response.status_code} - {response.reason}")
return None
except requests.exceptions.SSLError as e:
print(f"SSL Error: Can't verify SSL certificate. More: {e}")
except requests.exceptions.ConnectionError as e:
print(f"Connection Error: Unable to reach NetBox. More: {e}")
except requests.exceptions.Timeout as e:
print(f"Timeout Error: NetBox did not respond in time. More: {e}")
except requests.exceptions.RequestException as e:
print(f"Error: An unknown error occurred. More: {e}")
return None
def site_check(site_name):
site_record = nb.dcim.sites.get(name=site_name)
if site_record:
print("Site check complete!")
else:
print(f"Site '{site_name}' does not exist in NetBox!")
print("Do you want to auto add new Site?")
choice = input("yes/no: ").strip().lower()
if choice == 'yes':
new_site = nb.dcim.sites.create(
name=site_name,
slug=site_name.lower().replace(" ", "-"),
tags=TAG_ID_AUTO_IMPORT,
status=STATUS,
description='Create by Auto_Import_Tool',
)
print(f"Successfully created Site: {site_name}")
else:
print("Please check the Site again before using this Tool!")
# Hàm check xem có tag AutoImportExcel chưa
def tag_check():
# get tag
for tag in TAG_NAME_AUTO_IMPORT:
print(tag)
# check exist tag
tag_exist = nb.extras.tags.filter(name=tag)
# add tag id to array id
if not tag_exist:
try:
new_tag = nb.extras.tags.create(
name=tag,
slug=tag,
color="9e9e9e",
description= f'Create tag {tag}',
)
print(f"Create success tag {tag}")
# get ID tag
tag_id = get_tag_id(tag)
TAG_ID_AUTO_IMPORT.append(tag_id)
except:
print(f"Error while create tag {tag}")
else:
# get ID tag
tag_id = get_tag_id(tag)
TAG_ID_AUTO_IMPORT.append(tag_id)
def get_tag_id(tag_name):
tag_auto_import_excel = nb.extras.tags.filter(name=tag_name)
first_tag = next(tag_auto_import_excel, None)
if first_tag:
return first_tag.id
def device_role_check():
device_role_names = df["Role"].dropna().tolist() # chuyển pandas thành list
device_role_names = [item.strip() for item in device_role_names] # Xoá space cho các item
device_role_names = list(set(device_role_names)) # lọc trùng
all_roles_exist = True # tất cả đã tồn tại chưa
missing_roles = [] # danh sách các roles chưa có
# tìm kiếm các role bị thiếu
for device_roles in device_role_names:
dvr = nb.dcim.device_roles.filter(name=device_roles)
if not dvr:
print(f"Device Role: {device_roles} does not exist in NetBox, please check again!")
all_roles_exist = False
missing_roles.append(device_roles)
# kiểm tra tất cả role đã tồn tại hay chưa
if all_roles_exist:
print("Device Role check complete: All roles exist in NetBox!")
else:
print("Do you want to auto add Role?")
choice = input("yes/no: ").strip().lower()
if choice == 'yes':
for role in missing_roles:
try:
new_role = nb.dcim.device_roles.create(
name=role,
slug=role.lower().replace(" ", "-"),
tags=TAG_ID_AUTO_IMPORT,
color="9e9e9e",
description='Create device role by Auto_Import_Tool',
)
print(f"Successfully created Device Role: {new_role['name']}")
except Exception as e:
LIST_ADD_DEVICE_ROLE_ERROR.append(role)
print(f"Failed to create Device Role {role}: {e}")
else:
print("No roles were added. Please update NetBox manually if needed.")
exit()
def rack_check():
rack_names = df['Rack'].drop_duplicates().dropna().tolist()
rack_names = [item.strip() for item in rack_names] # Xoá space cho các item
rack_names = list(set(rack_names))# lọc trùng
missing_racks = []
for rack_name in rack_names:
record = nb.dcim.racks.get(name=rack_name)
if record:
continue
else:
print(f"Rack '{rack_name}' does not exist in NetBox!")
missing_racks.append(rack_name)
if missing_racks:
print("Do you want to auto add new Rack?")
choice = input("yes/no: ").strip().lower()
if choice == 'yes':
for rack in missing_racks:
try:
site = nb.dcim.sites.get(name=SITE_NAME)
if not site:
print(f"Site '{SITE_NAME}' does not exist in NetBox. Please create it first.")
break
new_rack = nb.dcim.racks.create(
site=site.id,
name=rack,
status='active',
tags=TAG_ID_AUTO_IMPORT,
width=WIDTH,
u_height=U_HEIGHT,
description='Create by Auto_Import_Tool',
)
print(f"Successfully created Rack: {new_rack['name']}")
except Exception as e:
print(f"Failed to create Rack {rack}: {e}")
else:
print("No racks were added. Please update NetBox manually if needed.")
exit()
else:
print("Rack Check complete: All racks exist in NetBox!")
def manufacturer_check():
manufacturer_names = df["Manufacturer"].dropna().tolist()
manufacturer_names = [item.strip() for item in manufacturer_names] # Xoá space cho các item
manufacturer_names = list(set(manufacturer_names))# lọc trùng
all_manufacturers_exist = True
missing_manufacturers = []
for manufacturer in manufacturer_names:
dvr = nb.dcim.manufacturers.filter(name=manufacturer)
if not dvr:
print(f"Manufacturer: {manufacturer} does not exist in NetBox, please check again!")
all_manufacturers_exist = False
missing_manufacturers.append(manufacturer)
if all_manufacturers_exist:
print("Device manufacturer check complete: All manufacturers exist in NetBox!")
else:
print("Do you want to auto add manufacturer?")
choice = input("yes/no: ").strip().lower()
if choice == 'yes':
for manufacturer in missing_manufacturers:
try:
new_manufacturer = nb.dcim.manufacturers.create(
name=manufacturer,
slug=manufacturer.lower().replace(" ", "-"),
tags=TAG_ID_AUTO_IMPORT,
description='Create manufacture by Auto_Import_Tool',
)
print(f"Successfully created Manufacture: {new_manufacturer['name']}")
except Exception as e:
LIST_ADD_MANUFACTURES_ERROR.append(manufacturer)
print(f"Failed to create Manufacture {manufacturer}: {e}")
else:
print("No manufacturers were added. Please update NetBox manually if needed.")
exit()
def custom_feild_check():
custom_feild_names = ["device_owner", "contract_number" ,"year_of_investment"]
all_custom_feild_exist = True
missing_custom_feild = []
for custom_feild in custom_feild_names:
dvr = nb.extras.custom_fields.filter(name=custom_feild)
if not dvr:
print(f"custom_feild: {custom_feild} does not exist in NetBox, please check again!")
all_custom_feild_exist = False
missing_custom_feild.append(custom_feild)
if all_custom_feild_exist:
print("Device custom_feild check complete: All custom_feild exist in NetBox!")
else:
print("Do you want to auto add custom_feild?")
choice = input("yes/no: ").strip().lower()
if choice == 'yes':
for custom_feild in missing_custom_feild:
try:
type_custom_feild = "text"
new_custom_feild = nb.extras.custom_fields.create({
"name" : custom_feild,
"type" : type_custom_feild,
"object_types" : ["dcim.device"],
"search_weight" : 1000,
"weight" : 100,
"tags": TAG_ID_AUTO_IMPORT,
"filter_logic" : "loose",
"ui_visible" : "always",
"ui_editable" : "yes",
"description" : 'Create by Auto_Import_Tool',
})
print(f"Successfully created custom feild : {new_custom_feild['name']}")
except Exception as e:
print(f"Failed to create custom feild {custom_feild}: {e}")
else:
print("No custom_feild were added. Please update NetBox manually if needed.")
exit()
#Hàm tạo ra mảng chứa trường name và u_height của device_type
def device_type_height():
# Tìm u_height của device type
print("Merged cell ranges in column H spanning rows:")
for merged_range in sheet.merged_cells.ranges:
# Lấy giá trị ở cột H
if merged_range.min_col == 8 and merged_range.max_col != merged_range.max_row:
# lấy value của ô
top_left_cell = sheet.cell(merged_range.min_row, merged_range.min_col)
device_type_name = top_left_cell.value
# Tính các row đã merg của ô
rows_spanned = merged_range.max_row - merged_range.min_row + 1
exists = any(device.name == device_type_name for device in DEVICE_HEIGHTS)
# kiểm tra giá trị đã có trong list chưa
if not exists:
new_device_height = DeviceHight(device_type_name,rows_spanned)
DEVICE_HEIGHTS.append(new_device_height)
# Hàm auto add device type vào netbox
def device_types_check():
device_types_in_file = df['Type'].dropna().tolist()
device_types_in_file = [item.strip() for item in device_types_in_file] # Xoá space cho các item
device_types_in_file = list(set(device_types_in_file)) # lọc trùng
device_type_not_in_netbox = []
# Tìm phần tử chưa có trong netbox
for device_type in device_types_in_file:
search_result = nb.dcim.device_types.filter(model=device_type)
if not search_result:
device_type_not_in_netbox.append(device_type)
if device_type_not_in_netbox:
print("Device Types not in NetBox:")
print(device_type_not_in_netbox)
print("\nDo you want to add Device Type automatically?")
choice = input("Enter your choice? (yes/no): ")
if choice == 'no':
print("\nPlease Add Device Types manually!")
exit()
elif choice == "yes":
print("You chose to Add Device Types Automatically with sample information")
print("Trying to Add Automatically...")
for device_type in device_type_not_in_netbox:
try:
# Tìm manufature của device type trong danh sách
all_manufacturer_device_type = df[["Manufacturer","Type"]].dropna()
# manufacturer_device_type = all_manufacturer_device_type.query(f"Type == '{device_type}'")
manufacturer_device_type = all_manufacturer_device_type[all_manufacturer_device_type['Type'].str.contains(device_type, case=False)]
manufacturer_name = manufacturer_device_type.iloc[0]['Manufacturer']
manufacturer_name = manufacturer_name.strip()
manufacturer = nb.dcim.manufacturers.get(name = manufacturer_name)
# Kiểm tra xem có tìm thấy manuyfacturers không
if not manufacturer:
print(f"Error while adding {device_type}: Not found manufactures")
continue
# Lấy chiều cao trong list đã tạo
device_height = 1 # mặc định height bằng 1
#Tìm kiếm trong danh sách thiết bị có height > 1
for device in DEVICE_HEIGHTS:
if device.name == device_type:
device_height = device.height
break
# create device type
device_type_slug = re.sub(r'[^a-z0-9-]', '-', device_type.lower()).strip('-')
new_device_type = nb.dcim.device_types.create({
'model': device_type,
'slug': device_type_slug,
'manufacturer': manufacturer.id,
"tags": TAG_ID_AUTO_IMPORT,
'u_height': device_height,
'is_full_depth': 'yes',
})
print(f"Automatically added success: {new_device_type}")
except Exception as e:
print(f"Error while adding {device_type}: {e}")
LIST_ADD_DEVICE_TYPE_ERROR.append(device_type)
else:
print("Device Types check complete!")
def get_device_types_ids(device_types_names):
try:
device_types_names = device_types_names.strip()
device_types = nb.dcim.device_types.filter(name=device_types_names)
if device_types:
for device_type in device_types:
if device_type.model == device_types_names:
return device_type.id
else:
print(f"Device type '{device_types_names}' not found in NetBox.")
return None
except Exception as e:
print(f"Error fetching device type '{device_types_names}': {e}")
return None
def get_device_roles_ids(device_role_name):
try:
device_role_name = device_role_name.strip()
device_roles = nb.dcim.device_roles.filter(name=device_role_name)
if device_roles:
for device_role in device_roles:
if device_role.name == device_role_name:
return device_role.id
else:
print(f"Device Role '{device_role_name}' not found in NetBox.")
return None
except Exception as e:
print(f"Error fetching device role'{device_role_name}': {e}")
return None
def get_site_id(site_name):
try:
site = nb.dcim.sites.get(name=site_name)
return site.id
except Exception as e:
print(f"Error fetching site '{site_name}': {e}")
return None
def get_rack_id(rack_name):
try:
rack_name = rack_name.strip()
rack = nb.dcim.racks.get(name=rack_name)
return rack.id
except Exception as e:
print(f"Error fetching rack '{rack_name}': {e}")
return None
# Hàm auto add device vào netbox
def import_device_to_NetBox():
# list device need add
device_names = df[["Rack", "U", "Manufacturer", "Name", "Role", "Owner Device", "Contract number","Type", "Serial Number","Year of Investment", "Comments"]]
device_names= device_names[device_names['Name'].notna()] # lọc tất cả hàng có trường Name là nan
number_of_device_in_file = 0 # Số lượng device đã được add
number_of_device_has_been_added = 0 # Tổng số lượn device trong danh sách
# check device have serial number null
device_null_serial_numbers = device_names[device_names['Serial Number'].isnull()]
if len(device_null_serial_numbers) > 0:
print(f"List device have serial_number null:\n {device_null_serial_numbers}")
print("Do you want add serial_number null?")
choice = input("Enter your choice? (yes/no): ")
if choice == "yes":
print("\nPlease Add Device Types manually!")
exit()
for index, row in device_names.iterrows():
number_of_device_in_file+=1
try:
# Thêm mới device đến netbox
rack_id = get_rack_id(row['Rack'])
site_id = get_site_id(site_name=SITE_NAME)
device_types_id = get_device_types_ids(row['Type'])
device_roles_id = get_device_roles_ids(row['Role'])
# Xử lý trường position
device_position = row['U']
#Tìm kiếm trong danh sách thiết bị có height > 1
for device in DEVICE_HEIGHTS:
if device.name == row['Type']:
device_position = device_position - device.height + 1
break
#Kiểm tra nan cho các param
device_description = ""
device_owner = ""
contract_number = ""
device_year_of_investment = ""
device_serial_number = ""
if not pd.isna(row['Comments']):
device_description = row['Comments']
if not pd.isna(row['Owner Device']):
device_owner = row['Owner Device']
if not pd.isna(row['Contract number']):
contract_number = row['Contract number']
if not pd.isna(row['Year of Investment']):
device_year_of_investment = row['Year of Investment']
if type(device_year_of_investment) is datetime:
device_year_of_investment = device_year_of_investment.strftime("%d/%m/%Y")
elif type(device_year_of_investment) is int:
device_year_of_investment = str(device_year_of_investment)
# Nếu như không phải null thì sẽ lấy giá trị đó (Mặc định là random)
if not pd.isna(row['Serial Number']):
device_serial_number = row['Serial Number']
else:
random_number = random.randint(100000, 999999)
device_serial_number = random_number
# kiểm tra xem device name đã được add trên netbox chưa
device_name = row['Name']
device_name = device_name.strip()
exist_device = nb.dcim.devices.get(name=device_name)
if exist_device:
serial_number_random = random.randint(100000, 999999)
device_name = device_name + f"-{serial_number_random}"
new_device = nb.dcim.devices.create(
{
"name": device_name,
"device_type": device_types_id,
"role": device_roles_id,
"site": site_id,
"serial": device_serial_number,
"rack": rack_id,
"face": "front",
"position": device_position,
"tags":TAG_ID_AUTO_IMPORT,
"status": STATUS,
"description": device_description,
"custom_fields": {
"device_owner": device_owner,
"contract_number": contract_number,
"year_of_investment": device_year_of_investment,
}
}
)
number_of_device_has_been_added+=1
print(f"Successfully created device: {device_name}")
except pynetbox.RequestError as e:
# Handle errors
LIST_ADD_DEVICE_ERROR.append(device_name)
if "non_field_errors" in e.error:
print(f"Vị trí U-{device_position} ở tủ Rack {row['Rack']} đã có thiết bị")
elif "position" in e.error:
print(f"Khoảng trống của vị trí U-{device_position} ở tủ Rack {row['Rack']} không đủ cho thiết bị")
else:
print(f"Error creating device '{device_name}': {e}")
except Exception as e:
LIST_ADD_DEVICE_ERROR.append(device_name)
print(f"Error creating device '{device_name}': {e}")
if number_of_device_has_been_added > 0:
print(f"{number_of_device_has_been_added}/{number_of_device_in_file} device has been added to NetBox!" )
def main():
try:
start_time = time.time() # Thời gian bắt đầu
print("Step 1: Checking input file...")
file_check(FILE_PATH)
print("Step 2: Checking NetBox connection...")
netbox_connection_check(NetBox_URL, NetBox_Token)
print(f"Step 3: Check tag exist")
tag_check()
print("Step 4: check site name")
site_check(site_name=SITE_NAME)
print("Step 5: check rack")
rack_check()
print("Step 6: check device role")
device_role_check()
print("Step 7: check manufacturer")
manufacturer_check()
print("Step 8: check custom feild")
custom_feild_check()
print("Step 9: Check height device type")
device_type_height()
print("Step 10: check device type")
device_types_check()
print("Step 11: Importing Devices into NetBox...")
import_device_to_NetBox()
print(f"List Manufacture error while create new record:\n {LIST_ADD_MANUFACTURES_ERROR}")
print(f"List Device Type error while create new record:\n {LIST_ADD_DEVICE_TYPE_ERROR}")
print(f"List Device Role error while create new record:\n {LIST_ADD_DEVICE_ROLE_ERROR}")
print(f"List Device error while create new record:\n {LIST_ADD_DEVICE_ERROR}")
end_time = time.time() # Thời gian kết thúc
duration = end_time - start_time # Thời gian xử lý
duration_total = round(duration / 60, 2)
print(f"Time import file: {duration_total} m")
print("Process completed successfully!")
except Exception as e:
print(f"Error during execution: {e}")
if __name__ == "__main__":
main()