108 lines
2.5 KiB
Python
108 lines
2.5 KiB
Python
import json
|
|
import copy
|
|
import openpyxl
|
|
|
|
|
|
def get_colors():
|
|
colors = ["#E74C3C", "#9B59B6", "#2980B9", "#27AE60", "#F1C40F", "#D35400", "#95A5A6", "#2E4053"]
|
|
return colors
|
|
|
|
def load_json_file(file_path):
|
|
try:
|
|
with open(file_path, 'r') as file:
|
|
data = json.load(file)
|
|
return data
|
|
except FileNotFoundError:
|
|
print(f"Error: File '{file_path}' not found.")
|
|
except json.JSONDecodeError:
|
|
print(f"Error: Unable to decode JSON in '{file_path}'. Check if the file is a valid JSON.")
|
|
except Exception as e:
|
|
print(f"An unexpected error occurred: {e}")
|
|
|
|
def get_tag(tag_name, monitor_id):
|
|
|
|
colors = get_colors()
|
|
|
|
if not hasattr(get_tag, "tags"):
|
|
get_tag.tags = {}
|
|
|
|
if not hasattr(get_tag, "tag_id"):
|
|
get_tag.tag_id = 0
|
|
|
|
if not hasattr(get_tag, "id"):
|
|
get_tag.id = 0
|
|
|
|
tag = copy.deepcopy(get_tag.tags.get(tag_name, None))
|
|
|
|
# Access and modify the static variable
|
|
if not tag:
|
|
get_tag.tag_id += 1
|
|
tag = load_json_file('tag_object.json')
|
|
tag["name"] = tag_name
|
|
tag["tag_id"] = get_tag.tag_id
|
|
tag["color"] = colors[get_tag.tag_id % len(colors)]
|
|
get_tag.tags[tag_name] = tag
|
|
|
|
tag["monitor_id"] = monitor_id
|
|
get_tag.id += 1
|
|
tag["id"] = get_tag.id
|
|
|
|
return tag
|
|
|
|
def get_monitor(name, ip_address, tag_list):
|
|
|
|
if not hasattr(get_monitor, "id"):
|
|
get_monitor.id = 0
|
|
|
|
get_monitor.id += 1
|
|
|
|
monitor = load_json_file('monitor_object.json')
|
|
|
|
monitor["id"] = get_monitor.id
|
|
monitor["name"] = name
|
|
monitor["pathName"] = name
|
|
monitor["hostname"] = ip_address
|
|
|
|
tags = monitor.get('tags', [])
|
|
|
|
for tag in tag_list:
|
|
tag_obj = get_tag(tag, get_monitor.id)
|
|
tags.append(tag_obj)
|
|
|
|
return monitor
|
|
|
|
def read_excel_file(file_path):
|
|
|
|
df = openpyxl.load_workbook(file_path)
|
|
df = df.active
|
|
|
|
return df
|
|
|
|
|
|
|
|
|
|
|
|
# Load the JSON file
|
|
outer_structure = load_json_file('outer_structure.json')
|
|
|
|
monitorList = outer_structure.get('monitorList', [])
|
|
|
|
|
|
data = read_excel_file("Z:\ips.xlsx")
|
|
|
|
for row_num in range(1, data.max_row + 1):
|
|
service_name = data.cell(row=row_num, column=2).value
|
|
ip_address = data.cell(row=row_num, column=1).value
|
|
tag = data.cell(row=row_num, column=4).value
|
|
|
|
if service_name is not None:
|
|
monitorList.append(get_monitor(service_name, ip_address, [tag]))
|
|
|
|
|
|
|
|
# Save the configuration json
|
|
file_path = "monitor_configuration.json"
|
|
|
|
with open(file_path, 'w') as json_file:
|
|
json.dump(outer_structure, json_file, indent=2)
|