2022-01-02 06:36:15 +01:00
#!/usr/bin/python3
2022-01-02 12:04:43 +01:00
"""
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 >
"""
2022-01-02 06:36:15 +01:00
import os
import xml . etree . ElementTree as xmlet
import pandas
import math
2022-01-02 12:04:43 +01:00
cluster = ' xxx '
tenant = ' xxx '
outfile = cluster + ' .csv '
2022-01-02 06:36:15 +01:00
columns = [ " name " , " status " , " role " , " cluster " , " tenant " , " platform " , " vcpus " , " memory " , " disk " , " comments " ]
rows = [ ]
2022-01-02 12:04:43 +01:00
domaindir = ' xmls/ ' + cluster + ' /domains '
diskdir = ' xmls/ ' + cluster + ' /disks '
2022-01-02 06:36:15 +01:00
2022-01-02 12:04:43 +01:00
status = ' active '
comment = ' Imported from libvirt. '
2022-01-02 06:36:15 +01:00
for domainxml in os . listdir ( domaindir ) :
domainparse = xmlet . parse ( domaindir + " / " + domainxml )
domainroot = domainparse . getroot ( )
name = domainroot . find ( " name " ) . text
vcpus = domainroot . find ( " vcpu " ) . text
memory = int ( domainroot . find ( " memory " ) . text )
memorysize = round ( memory * 0.001024 )
diskxml = diskdir + " / " + name + " .disk.export.xml "
2022-01-02 12:04:43 +01:00
if os . path . exists ( diskxml ) :
diskparse = xmlet . parse ( diskxml )
diskroot = diskparse . getroot ( )
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 = " "
2022-01-02 06:36:15 +01:00
while True :
2022-01-02 12:04:43 +01:00
role_choice = input ( " Assign role for " + name + " : \n 1) Internal Client \n 2) Internal Server \n 3) Public Client \n 4) Public Server \n 5) Customer \n 6) Router \n 7) Null \n > " )
if role_choice not in [ " 1 " , " 2 " , " 3 " , " 4 " , " 5 " , " 6 " , " 7 " ] :
print ( " Invalid choice. " )
2022-01-02 06:36:15 +01:00
if role_choice == " 1 " :
role = " Virtual Machine (Internal, Client) "
break
if role_choice == " 2 " :
role = " Virtual Machine (Internal, Server) "
break
if role_choice == " 3 " :
2022-01-02 12:04:43 +01:00
role = " Virtual Machine (Public, Client) "
break
if role_choice == " 4 " :
role = " Virtual Machine (Public, Server) "
break
if role_choice == " 5 " :
2022-01-02 06:36:15 +01:00
role = " Virtual Machine (Customer) "
break
2022-01-02 12:04:43 +01:00
if role_choice == " 6 " :
role = " Virtual Machine (Router) "
break
if role_choice == " 7 " :
role = " "
break
2022-01-02 06:36:15 +01:00
while True :
2022-01-02 12:04:43 +01:00
platform_choice = input ( " Assign platform for " + name + " : \n 1) openSUSE Leap x86_64 \n 2) OpenBSD x86_64 \n 3) FreeBSD x86_64 \n 4) OPNsense x86_64 \n 5) Arch Linux x86_64 \n 7) Null \n > " )
if platform_choice not in [ " 1 " , " 2 " , " 3 " , " 4 " , " 5 " , " 7 " ] :
print ( " Invalid choice. " )
2022-01-02 06:36:15 +01:00
if platform_choice == " 1 " :
2022-01-02 12:04:43 +01:00
platform = " openSUSE-Leap-x86_64 "
2022-01-02 06:36:15 +01:00
break
if platform_choice == " 2 " :
platform = " OpenBSD-x86_64 "
break
if platform_choice == " 3 " :
platform = " FreeBSD-x86_64 "
break
2022-01-02 12:04:43 +01:00
if platform_choice == " 4 " :
platform = " OPNsense-x86_64 "
break
if platform_choice == " 5 " :
platform = " ArchLinux-x86_64 "
break
if platform_choice == " 7 " :
platform = " "
break
2022-01-02 06:36:15 +01:00
rows . append (
{
" name " : name ,
" status " : status ,
" role " : role ,
" cluster " : cluster ,
" tenant " : tenant ,
" platform " : platform ,
" vcpus " : vcpus ,
" memory " : memorysize ,
" disk " : disksize ,
" comments " : comment
}
)
convert = pandas . DataFrame ( rows , columns = columns )
convert . to_csv ( outfile , index = False )