Get VM info in malpd from vm-info script via SSH

This commit is contained in:
Josh Holtrop 2026-04-17 22:35:35 -04:00
parent ea932dd641
commit ad23419b99

View File

@ -145,42 +145,9 @@ def read_vm_dominfo(name)
info
end
def read_vm_disks(name)
out = `virsh --connect qemu:///system domblklist #{Shellwords.escape(name)} --details 2>/dev/null`
targets = []
out.each_line do |line|
parts = line.strip.split(/\s+/)
next if parts.size < 3
next if parts[0] == "Type" || parts[0].start_with?("-")
next unless parts[1] == "disk"
targets << parts[2]
end
total_cap = 0
total_alloc = 0
targets.each do |tgt|
blk = `virsh --connect qemu:///system domblkinfo #{Shellwords.escape(name)} #{Shellwords.escape(tgt)} --bytes 2>/dev/null`
blk.each_line do |l|
k, v = l.split(":", 2).map { |s| s&.strip }
next unless k && v
total_cap += v.to_i if k == "Capacity"
total_alloc += v.to_i if k == "Allocation"
end
end
{ "disk_total" => total_cap, "disk_used" => total_alloc }
end
def read_vm_os(name)
xml = `virsh --connect qemu:///system dumpxml #{Shellwords.escape(name)} 2>/dev/null`
return nil if xml.empty?
if xml =~ %r{<libosinfo:os\s+id="([^"]+)"}
path = $1.sub(%r{^https?://[^/]+/}, "")
parts = path.split("/").reject(&:empty?)
return parts.map { |p| p.sub(/^\w/, &:upcase) }.join(" ") unless parts.empty?
end
if xml =~ %r{<title>([^<]+)</title>}
return $1.strip
end
nil
def read_vm_info(name)
ssh_out = `ssh -Ti /root/.ssh/malp-vm-key malp@#{name}`
JSON.parse(ssh_out)
end
def read_virtual_machines
@ -190,20 +157,12 @@ def read_virtual_machines
return [] if names.empty?
names.map do |name|
dom = read_vm_dominfo(name)
disk = read_vm_disks(name)
dom_info = read_vm_dominfo(name)
vm_info = read_vm_info(name)
{
"type" => "vm",
"name" => name,
"state" => dom["state"],
"vcpus" => dom["vcpus"],
"memory" => dom["memory"],
"used_memory" => dom["used_memory"],
"autostart" => dom["autostart"],
"os" => read_vm_os(name),
"disk_total" => disk["disk_total"],
"disk_used" => disk["disk_used"]
}
}.merge(dom_info).merge(vm_info)
end
end