Init libvirt-xml2netbox-csv.py v2

Signed-off-by: Georg <georg@lysergic.dev>
This commit is contained in:
Georg Pfuetzenreuter 2022-01-02 12:04:43 +01:00
parent 935b8c642f
commit ff1ea4dad3

View File

@ -1,21 +1,27 @@
#!/usr/bin/python3 #!/usr/bin/python3
"""
Takes a directory with libvirt XML domain dumps and creates a NetBox importable CSV. Since the role and platform fields cannot be fed from the XML files, the user is given the option to pick those for each VM.
Created and Last modified: 02/01/2022 by Georg Pfuetzenreuter <georg@lysergic.dev>
"""
import os import os
import xml.etree.ElementTree as xmlet import xml.etree.ElementTree as xmlet
import pandas import pandas
import math import math
outfile = 'xxx.csv' cluster = 'xxx'
tenant = 'xxx'
outfile = cluster + '.csv'
columns = [ "name", "status", "role", "cluster", "tenant", "platform", "vcpus", "memory", "disk", "comments" ] columns = [ "name", "status", "role", "cluster", "tenant", "platform", "vcpus", "memory", "disk", "comments" ]
rows = [] rows = []
domaindir = 'xmls/domains' domaindir = 'xmls/' + cluster + '/domains'
diskdir = 'xmls/disks' diskdir = 'xmls/' + cluster + '/disks'
status = 'Active' status = 'active'
cluster = 'xxx' comment = 'Imported from libvirt.'
tenant = 'xxx'
comment = 'Imported from libvirt. Manual verification pending.'
for domainxml in os.listdir(domaindir): for domainxml in os.listdir(domaindir):
domainparse = xmlet.parse(domaindir + "/" + domainxml) domainparse = xmlet.parse(domaindir + "/" + domainxml)
@ -26,13 +32,19 @@ for domainxml in os.listdir(domaindir):
memory = int(domainroot.find("memory").text) memory = int(domainroot.find("memory").text)
memorysize = round(memory*0.001024) memorysize = round(memory*0.001024)
diskxml = diskdir + "/" + name + ".disk.export.xml" diskxml = diskdir + "/" + name + ".disk.export.xml"
diskparse = xmlet.parse(diskxml) if os.path.exists(diskxml):
diskroot = diskparse.getroot() diskparse = xmlet.parse(diskxml)
diskcapacity = int(diskroot.find("capacity").text) diskroot = diskparse.getroot()
disksize = round(diskcapacity / (math.pow(1024, (int(math.floor(math.log(diskcapacity, 1024))))))) diskcapacity = int(diskroot.find("capacity").text)
disksize = round(diskcapacity / (math.pow(1024, (int(math.floor(math.log(diskcapacity, 1024)))))))
if not os.path.exists(diskxml):
print("No disk XML for " + name + ", assuming there is no VHD.")
disksize = ""
while True: while True:
role_choice = input ("Assign a role for " + name + ":\n1) Internal Client\n2) Internal Server\n3) ???\n4) ???\n> ") role_choice = input ("Assign role for " + name + ":\n1) Internal Client\n2) Internal Server\n3) Public Client\n4) Public Server\n5) Customer\n6) Router\n7) Null\n> ")
if role_choice not in ["1", "2", "3", "4", "5", "6", "7"]:
print("Invalid choice.")
if role_choice == "1": if role_choice == "1":
role = "Virtual Machine (Internal, Client)" role = "Virtual Machine (Internal, Client)"
break break
@ -40,15 +52,27 @@ for domainxml in os.listdir(domaindir):
role = "Virtual Machine (Internal, Server)" role = "Virtual Machine (Internal, Server)"
break break
if role_choice == "3": if role_choice == "3":
role = "Virtual Machine (Public, Client)"
break
if role_choice == "4":
role = "Virtual Machine (Public, Server)"
break
if role_choice == "5":
role = "Virtual Machine (Customer)" role = "Virtual Machine (Customer)"
break break
if role_choice not in ["1", "2", "3"]: if role_choice == "6":
print("Invalid choice.") role = "Virtual Machine (Router)"
break
if role_choice == "7":
role = ""
break
while True: while True:
platform_choice = input ("Assign platform for " + name + ":\n 1) openSUSE-x86_64\n2)OpenBSD-x86_64\n3)FreeBSD-x86_64\n> ") platform_choice = input ("Assign platform for " + name + ":\n1) openSUSE Leap x86_64\n2) OpenBSD x86_64\n3) FreeBSD x86_64\n4) OPNsense x86_64\n5) Arch Linux x86_64\n7) Null\n> ")
if platform_choice not in ["1", "2", "3", "4", "5", "7"]:
print("Invalid choice.")
if platform_choice == "1": if platform_choice == "1":
platform = "openSUSE-x86_64" platform = "openSUSE-Leap-x86_64"
break break
if platform_choice == "2": if platform_choice == "2":
platform = "OpenBSD-x86_64" platform = "OpenBSD-x86_64"
@ -56,9 +80,16 @@ for domainxml in os.listdir(domaindir):
if platform_choice == "3": if platform_choice == "3":
platform = "FreeBSD-x86_64" platform = "FreeBSD-x86_64"
break break
if platform_choice not in ["1", "2", "3"]: if platform_choice == "4":
print("Invalid choice.") platform = "OPNsense-x86_64"
break
if platform_choice == "5":
platform = "ArchLinux-x86_64"
break
if platform_choice == "7":
platform = ""
break
rows.append( rows.append(
{ {
"name": name, "name": name,
@ -76,4 +107,3 @@ for domainxml in os.listdir(domaindir):
convert = pandas.DataFrame(rows, columns=columns) convert = pandas.DataFrame(rows, columns=columns)
convert.to_csv(outfile, index=False) convert.to_csv(outfile, index=False)