initial commit; version 22.5.12042
This commit is contained in:
114
includes/discovery/virtual-machines/libvirt.inc.php
Normal file
114
includes/discovery/virtual-machines/libvirt.inc.php
Normal file
@ -0,0 +1,114 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Observium
|
||||
*
|
||||
* This file is part of Observium.
|
||||
*
|
||||
* @package observium
|
||||
* @subpackage discovery
|
||||
* @copyright (C) 2006-2013 Adam Armstrong, (C) 2013-2019 Observium Limited
|
||||
*
|
||||
*/
|
||||
|
||||
// Try to discover Libvirt Virtual Machines.
|
||||
|
||||
if ($config['enable_libvirt'] == '1' && $device['os'] == "linux" ) // FIXME. need os definition 'libvirt' = 1
|
||||
{
|
||||
$libvirt_vmlist = array();
|
||||
|
||||
echo("Libvirt VM: ");
|
||||
|
||||
$ssh_ok = 0;
|
||||
|
||||
foreach ($config['libvirt_protocols'] as $method)
|
||||
{
|
||||
$uri = $method.'://' . $device['hostname'];
|
||||
|
||||
if (strstr($method, 'ssh') && !$ssh_ok)
|
||||
{
|
||||
// Append ssh port to uri
|
||||
$uri .= ':'.$device['ssh_port'];
|
||||
}
|
||||
|
||||
if (strstr($method,'qemu'))
|
||||
{
|
||||
$uri .= '/system';
|
||||
}
|
||||
|
||||
if (isset($config['libvirt_socket']) && $config['libvirt_socket'] != "")
|
||||
{
|
||||
// Allow setting of socket, used to force use of read only socket
|
||||
// $config['libvirt_socket'] = "/var/run/libvirt/libvirt-sock-ro";
|
||||
|
||||
$uri = $uri."?socket=".$config['libvirt_socket'];
|
||||
}
|
||||
|
||||
if (strstr($method,'ssh') && !$ssh_ok)
|
||||
{
|
||||
// Check if we are using SSH if we can log in without password - without blocking the discovery
|
||||
// Also automatically add the host key so discovery doesn't block on the yes/no question, and run echo so we don't get stuck in a remote shell ;-)
|
||||
external_exec('ssh -p '.$device['ssh_port'].' -o "StrictHostKeyChecking no" -o "PreferredAuthentications publickey" -o "IdentitiesOnly yes" ' . $device['hostname'] . ' echo -e', $out, $ret);
|
||||
if ($ret != 255) { $ssh_ok = 1; }
|
||||
}
|
||||
|
||||
if ($ssh_ok || !strstr($method,'ssh'))
|
||||
{
|
||||
// Fetch virtual machine list
|
||||
unset($domlist);
|
||||
$domlist = explode("\n", external_exec($config['virsh'] . ' -c '.$uri.' list --all'));
|
||||
|
||||
foreach ($domlist as $dom)
|
||||
{
|
||||
$dom_split = str_word_count($dom, 1, '1234567890-.');
|
||||
|
||||
$dom_id = $dom_split[0];
|
||||
$vm_DisplayName = $dom_split[1];
|
||||
$vm_State = implode(' ',array_slice($dom_split,2)); // Convert split words back into one
|
||||
|
||||
if (is_numeric($dom_id) || $dom_id == '-') // - when domain is not running
|
||||
{
|
||||
// Fetch the Virtual Machine information.
|
||||
unset($vm_info_array);
|
||||
$vm_info_xml = external_exec($config['virsh'] . ' -c '.$uri.' dumpxml ' . $vm_DisplayName);
|
||||
|
||||
// <domain type='kvm' id='3'>
|
||||
// <name>moo.example.com</name>
|
||||
// <uuid>48cf6378-6fd5-4610-0611-63dd4b31cfd6</uuid>
|
||||
// <memory>1048576</memory>
|
||||
// <currentMemory>1048576</currentMemory>
|
||||
// <vcpu>8</vcpu>
|
||||
// <os>
|
||||
// <type arch='x86_64' machine='pc-0.12'>hvm</type>
|
||||
// <boot dev='hd'/>
|
||||
// </os>
|
||||
// <features>
|
||||
// <acpi/>
|
||||
// (...)
|
||||
|
||||
// Parse XML, add xml header in front as this is required by the parser but not supplied by libvirt
|
||||
$xml = simplexml_load_string('<?xml version="1.0"?> ' . $vm_info_xml);
|
||||
if (OBS_DEBUG && $xml) { print_vars($xml); }
|
||||
|
||||
// Call VM discovery
|
||||
discover_virtual_machine($valid, $device, array('id' => $xml->uuid, 'name' => $vm_DisplayName, 'cpucount' => $xml->vcpu,
|
||||
'memory' => $xml->currentMemory * 1024, 'status' => $vm_State, 'type' => 'libvirt', 'source' => 'libvirt', 'protocol' => $method));
|
||||
|
||||
// Save the discovered Virtual Machine.
|
||||
$libvirt_vmlist[] = $vm_DisplayName;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// If we found VMs, don't cycle the other protocols anymore.
|
||||
if (count($libvirt_vmlist)) { break; }
|
||||
}
|
||||
|
||||
unset($libvirt_vmlist);
|
||||
|
||||
// Clean up removed VMs (our type - libvirt - only, so we don't clean up other modules' VMs)
|
||||
check_valid_virtual_machines($device, $valid, 'libvirt');
|
||||
echo(PHP_EOL);
|
||||
}
|
||||
|
||||
// EOF
|
@ -0,0 +1,49 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Observium
|
||||
*
|
||||
* This file is part of Observium.
|
||||
*
|
||||
* @package observium
|
||||
* @subpackage discovery
|
||||
* @copyright (C) 2006-2013 Adam Armstrong, (C) 2013-2019 Observium Limited
|
||||
*
|
||||
*/
|
||||
|
||||
// Try to discover any Virtual Machines.
|
||||
|
||||
// Variable to hold the discovered Virtual Machines.
|
||||
$vmw_vmlist = array();
|
||||
|
||||
/*
|
||||
* Fetch the Virtual Machine information.
|
||||
*
|
||||
* VMWARE-VMINFO-MIB::vmwVmDisplayName.224 = STRING: My First VM
|
||||
* VMWARE-VMINFO-MIB::vmwVmDisplayName.416 = STRING: My Second VM
|
||||
* VMWARE-VMINFO-MIB::vmwVmGuestOS.224 = STRING: windows7Server64Guest
|
||||
* VMWARE-VMINFO-MIB::vmwVmGuestOS.416 = STRING: winLonghornGuest
|
||||
* VMWARE-VMINFO-MIB::vmwVmMemSize.224 = INTEGER: 8192 megabytes
|
||||
* VMWARE-VMINFO-MIB::vmwVmMemSize.416 = INTEGER: 8192 megabytes
|
||||
* VMWARE-VMINFO-MIB::vmwVmState.224 = STRING: poweredOn
|
||||
* VMWARE-VMINFO-MIB::vmwVmState.416 = STRING: poweredOn
|
||||
* VMWARE-VMINFO-MIB::vmwVmVMID.224 = INTEGER: 224
|
||||
* VMWARE-VMINFO-MIB::vmwVmVMID.416 = INTEGER: 416
|
||||
* VMWARE-VMINFO-MIB::vmwVmCpus.224 = INTEGER: 2
|
||||
* VMWARE-VMINFO-MIB::vmwVmCpus.416 = INTEGER: 2
|
||||
*/
|
||||
|
||||
$oids = snmpwalk_cache_oid($device, 'vmwVmTable', array(), 'VMWARE-VMINFO-MIB');
|
||||
|
||||
foreach ($oids as $index => $entry)
|
||||
{
|
||||
// Call VM discovery
|
||||
discover_virtual_machine($valid, $device, array('id' => $entry['vmwVmUUID'], 'name' => $entry['vmwVmDisplayName'], 'cpucount' => $entry['vmwVmCpus'],
|
||||
'memory' => $entry['vmwVmMemSize'] * 1024 * 1024, 'status' => $entry['vmwVmState'], 'os' => $entry['vmwVmGuestOS'],'type' => 'vmware', 'source' => 'vmware-snmp'));
|
||||
}
|
||||
|
||||
// Clean up removed VMs (our type - vmware-snmp - only, so we don't clean up other modules' VMs)
|
||||
check_valid_virtual_machines($device, $valid, 'vmware-snmp');
|
||||
echo(PHP_EOL);
|
||||
|
||||
// EOF
|
Reference in New Issue
Block a user