python
1 week, 1 day ago
import re
from collections import defaultdict
def parse_hypervisor_details(text):
data = {}
current_section = None
for line in text.splitlines():
line = line.strip()
# Skip empty lines and separators
if not line or "=" in line:
continue
# Detect Sections
section_match = re.match(r'^(.*) Details', line)
if section_match:
current_section = section_match.group(1).replace(" ", "_").lower()
data[current_section] = {}
continue
# Match key-value pairs
if ":" in line:
key, value = map(str.strip, line.split(":", 1))
key = key.replace(" ", "_").lower()
data[current_section][key] = value
continue
# Process Hard Drives
if current_section == "hard_drives" and len(line.split()) == 3:
name, model, size = line.split(maxsplit=2)
if "hard_drives" not in data:
data["hard_drives"] = []
data["hard_drives"].append({"name": name, "model": model, "size": size})
continue
# Process LLDP Switch Information
if current_section == "lldp_switch_information" and "|" in line:
interface, switch_name, port = map(str.strip, line.split("|"))
if "lldp_switch_information" not in data:
data["lldp_switch_information"] = []
data["lldp_switch_information"].append({"interface": interface, "switch_name": switch_name, "port": port})
continue
return data
def parse_vm_details(text):
vms = []
vm_data = None
for line in text.splitlines():
line = line.strip()
# Match VM Name
vm_match = re.match(r'---- VM: (.*?) ----', line)
if vm_match:
if vm_data:
vms.append(vm_data) # Save previous VM data
vm_data = {"VM Name": vm_match.group(1), "Disks": []} # Start new VM entry
continue
if not vm_data:
continue # Skip lines until we find a VM entry
# Match CPU, RAM, OS, and IP Address
if line.startswith("CPU Assigned:"):
vm_data["CPU"] = int(line.split(":")[1].strip())
elif line.startswith("RAM Assigned:"):
vm_data["RAM"] = line.split(":")[1].strip()
elif line.startswith("OS Version:"):
vm_data["OS"] = line.split(":")[1].strip()
elif line.startswith("IP Address:"):
vm_data["IP Address"] = line.split(":")[1].strip()
elif re.match(r'^[a-z]+\s+/.*\.qcow2$', line):
disk_parts = line.split()
vm_data["Disks"].append({"Device": disk_parts[0], "Path": disk_parts[1]})
if vm_data:
vms.append(vm_data) # Add last VM entry
return vms
# Sample text input
text_output = """
==================================================================
KVM Hypervisor Details for Host: mtznjrsv208
==================================================================
IPv4 Address: 135.25.186.161
IPv6 Address: fe80::211:aff:fe6a:bf78/64
iDRAC/iLO IP Address: 135.25.186.158
Default Gateway: 135.25.186.1
DNS Server IP's: 135.25.120.104
==================================================================
System Hardware Details
==================================================================
Total CPU's: 48
Cores per Socket: 12
Total Sockets: 2
Threads per Core: 2
Total RAM: 251Gi
==================================================================
Hard Drives
==================================================================
Name Model Size
sda LOGICAL_VOLUME 111.8G disk
sdb LOGICAL_VOLUME 1.1T disk
sdc LOGICAL_VOLUME 1.1T disk
sdd LOGICAL_VOLUME 1.1T disk
sde LOGICAL_VOLUME 1.1T disk
==================================================================
Operating System
==================================================================
OS: Ubuntu 20.04.5 LTS
Kernel Version: 5.4.0-128-generic
==================================================================
Server Hardware and BIOS
==================================================================
Server Model: ProLiant DL380 Gen9
CPU Model: Intel(R) Xeon(R) CPU E5-2680 v3 @ 2.50GHz
BIOS Version: P89
BIOS Release Date: 10/25/2017
Server Serial Number: MXQ546057N
ATT Patch Level: Not Available
Running VMs: 7
==================================================================
LLDP Switch Information
==================================================================
Interface Switch Name Port
ens3f0 | nasa-arista-C | ifname Ethernet18
ens6f1 | nasa-qfx-B | local 677
ens4f0 | nasa-arista-D | ifname Ethernet18
==================================================================
Virtual Machine Details
==================================================================
---- VM: mtznjv1tunl01 ----
CPU Assigned: 4
RAM Assigned: 8GB
OS Version: Ubuntu 22.04.4 LTS
Disks Assigned:
------------------------------------------------------------
vda /kvmhome7/tunl/mtznjv1tunl01/mtznjv1tunl01.qcow2
IP Address: 135.25.186.88
---- VM: mtznjv1pinclts01 ----
CPU Assigned: 8
RAM Assigned: 14GB
OS Version: Ubuntu 22.04.4 LTS
Disks Assigned:
---------------------------------------------------------------------------
hda /kvmhome7/pinclts/mtznjv1pinclts01/mtznjv1pinclts01.qcow2
vda /kvmhome7/pinclts/mtznjv1pinclts01/mtznjv1pinclts01-disk1.qcow2
vdb /kvmhome7/pinclts/mtznjv1pinclts01/mtznjv1pinclts01-disk2.qcow2
IP Address: 135.25.186.89
---- VM: mtznjv1cnro11 ----
CPU Assigned: 16
RAM Assigned: 14GB
OS Version: Rocky Linux 8.10 (Green Obsidian)
Disks Assigned:
------------------------------------------------------------
hda /kvmhome7/cnro/mtznjv1cnro11/mtznjv1cnro11.qcow2
IP Address: 135.25.186.206
---- VM: mtznjv1cnro12 ----
CPU Assigned: 4
RAM Assigned: 14GB
OS Version: Rocky Linux 8.10 (Green Obsidian)
Disks Assigned:
------------------------------------------------------------
hda /kvmhome7/cnro/mtznjv1cnro12/mtznjv1cnro12.qcow2
IP Address: 135.25.186.226
---- VM: mtznjv1arub18 ----
CPU Assigned: 8
RAM Assigned: 8GB
OS Version: Ubuntu 18.04.6 LTS
Disks Assigned:
----------------------------------------------------------------
vda /kvmhome7/Arcadian/mtznjv1arub18/mtznjv1arub18.qcow2
IP Address: 135.25.186.29
---- VM: mtznjv1arrky8 ----
CPU Assigned: 8
RAM Assigned: 8GB
OS Version: Rocky Linux 8.9 (Green Obsidian)
Disks Assigned:
----------------------------------------------------------------
hda /kvmhome7/Arcadian/mtznjv1arrky8/mtznjv1arrky8.qcow2
"""
text2 = """
==================================================================
KVM Hypervisor Details for Host: mtznjrsv208
==================================================================
IPv4 Address: 135.25.186.161
IPv6 Address: fe80::211:aff:fe6a:bf78/64
iDRAC/iLO IP Address: 135.25.186.158
Default Gateway: 135.25.186.1
DNS Server IP's: 135.25.120.104
==================================================================
System Hardware Details
==================================================================
Total CPU's: 48
Cores per Socket: 12
Total Sockets: 2
Threads per Core: 2
Total RAM: 251Gi
==================================================================
Hard Drives
==================================================================
Name Model Size
sda LOGICAL_VOLUME 111.8G disk
sdb LOGICAL_VOLUME 1.1T disk
sdc LOGICAL_VOLUME 1.1T disk
sdd LOGICAL_VOLUME 1.1T disk
sde LOGICAL_VOLUME 1.1T disk
==================================================================
Operating System
==================================================================
OS: Ubuntu 20.04.5 LTS
Kernel Version: 5.4.0-128-generic
==================================================================
Server Hardware and BIOS
==================================================================
Server Model: ProLiant DL380 Gen9
CPU Model: Intel(R) Xeon(R) CPU E5-2680 v3 @ 2.50GHz
BIOS Version: P89
BIOS Release Date: 10/25/2017
Server Serial Number: MXQ546057N
ATT Patch Level: Not Available
Running VMs: 7
==================================================================
LLDP Switch Information
==================================================================
Interface Switch Name Port
ens3f0 | nasa-arista-C | ifname Ethernet18
ens6f1 | nasa-qfx-B | local 677
ens4f0 | nasa-arista-D | ifname Ethernet18
"""
# Parse and print the result
parsed_data = parse_vm_details(text_output)
print(parsed_data)
print(parse_hypervisor_details(text_output))
0 Comments
Please Login to Comment Here