# moo.example.com
# 48cf6378-6fd5-4610-0611-63dd4b31cfd6
# 1048576
# 1048576
# 8
#
# hvm
#
#
#
#
# (...)
# Convert array to string
unset($vm_info_xml);
foreach ($vm_info_array as $line) { $vm_info_xml .= $line; }
$xml = simplexml_load_string(' ' . $vm_info_xml);
if ($debug) { print_r($xml); }
$vmwVmDisplayName = $xml->name;
$vmwVmGuestOS = ''; # libvirt does not supply this
$vmwVmMemSize = $xml->currentMemory / 1024;
exec($config['virsh'] . ' -c '.$uri.' domstate ' . $dom_id,$vm_state);
$vmwVmState = ucfirst($vm_state[0]);
$vmwVmCpus = $xml->vcpu;
# Check whether the Virtual Machine is already known for this host.
$result = mysql_query("SELECT * FROM vminfo WHERE device_id = '" . $device["device_id"] . "' AND vmwVmVMID = '" . $dom_id . "' AND vm_type='libvirt'");
if (mysql_num_rows($result) == 0)
{
mysql_query("INSERT INTO vminfo (device_id, vm_type, vmwVmVMID, vmwVmDisplayName, vmwVmGuestOS, vmwVmMemSize, vmwVmCpus, vmwVmState) VALUES (" . $device["device_id"] . ", 'libvirt',
'" . $dom_id . "', '" . mres($vmwVmDisplayName) . "', '" . mres($vmwVmGuestOS) . "', '" . $vmwVmMemSize . "', '" . $vmwVmCpus . "', '" . mres($vmwVmState) . "')");
echo("+");
log_event("Virtual Machine added: $vmwVmDisplayName ($vmwVmMemSize MB)", $device, 'vm', mysql_insert_id());
} else {
$row = mysql_fetch_assoc($result);
if ($row['vmwVmState'] != $vmwVmState
|| $row['vmwVmDisplayName'] != $vmwVmDisplayName
|| $row['vmwVmCpus'] != $vmwVmCpus
|| $row['vmwVmGuestOS'] != $vmwVmGuestOS
|| $row['vmwVmMemSize'] != $vmwVmMemSize)
{
mysql_query("UPDATE vminfo SET vmwVmState='" . mres($vmwVmState) . "', vmwVmGuestOS='" . mres($vmwVmGuestOS) . "', vmwVmDisplayName='". mres($vmwVmDisplayName) . "',
vmwVmMemSize='" . mres($vmwVmMemSize) . "', vmwVmCpus='" . mres($vmwVmCpus) . "' WHERE device_id='" . $device["device_id"] . "' AND vm_type='libvirt' AND vmwVmVMID='" . $dom_id . "'");
echo("U");
# FIXME eventlog
}
else
{
echo(".");
}
}
# Save the discovered Virtual Machine.
$libvirt_vmlist[] = $dom_id;
}
}
}
# If we found VMs, don't cycle the other protocols anymore.
if (count($libvirt_vmlist)) { break; }
}
# Get a list of all the known Virtual Machines for this host.
$db_vm_list = mysql_query("SELECT id, vmwVmVMID, vmwVmDisplayName FROM vminfo WHERE device_id = '" . $device["device_id"] . "' AND vm_type='libvirt'");
while ($db_vm = mysql_fetch_assoc($db_vm_list))
{
# Delete the Virtual Machines that are removed from the host.
if (!in_array($db_vm["vmwVmVMID"], $libvirt_vmlist))
{
mysql_query("DELETE FROM vminfo WHERE id = '" . $db_vm["id"] . "'");
echo("-");
log_event("Virtual Machine removed: " . $db_vm['vmwVmDisplayName'], $device, 'vm', $db_vm['id']);
}
}
echo("\n");
}
?>