PageRenderTime 54ms CodeModel.GetById 26ms RepoModel.GetById 1ms app.codeStats 0ms

/includes/discovery/libvirt-vminfo.inc.php

https://bitbucket.org/MelFlynn/observium
PHP | 133 lines | 85 code | 22 blank | 26 comment | 29 complexity | cdff5fd3401d85f7a0f091b982b7c321 MD5 | raw file
Possible License(s): GPL-3.0, MIT
  1. <?php
  2. # FIXME should do the deletion etc in a common file perhaps? like for the sensors
  3. # Try to discover Libvirt Virtual Machines.
  4. if ($config['enable_libvirt'] == '1' && $device['os'] == "linux" )
  5. {
  6. $libvirt_vmlist = array();
  7. echo("Libvirt VM: ");
  8. $ssh_ok = 0;
  9. foreach ($config['libvirt_protocols'] as $method)
  10. {
  11. if (strstr($method,'qemu'))
  12. {
  13. $uri = $method.'://' . $device['hostname'] . '/system';
  14. }
  15. else
  16. {
  17. $uri = $method.'://' . $device['hostname'];
  18. }
  19. if (strstr($method,'ssh') && !$ssh_ok)
  20. {
  21. # Check if we are using SSH if we can log in without password - without blocking the discovery
  22. # 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 ;-)
  23. exec('ssh -o "StrictHostKeyChecking no" -o "PreferredAuthentications publickey" -o "IdentitiesOnly yes" ' . $device['hostname'] . ' echo -e', $out, $ret);
  24. if ($ret != 255) { $ssh_ok = 1; }
  25. }
  26. if ($ssh_ok || !strstr($method,'ssh'))
  27. {
  28. # Fetch virtual machine list
  29. unset($domlist);
  30. exec($config['virsh'] . ' -c '.$uri.' list',$domlist);
  31. foreach ($domlist as $dom)
  32. {
  33. list($dom_id,) = explode(' ',trim($dom),2);
  34. if (is_numeric($dom_id))
  35. {
  36. # Fetch the Virtual Machine information.
  37. unset($vm_info_array);
  38. exec($config['virsh'] . ' -c '.$uri.' dumpxml ' . $dom_id,$vm_info_array);
  39. # <domain type='kvm' id='3'>
  40. # <name>moo.example.com</name>
  41. # <uuid>48cf6378-6fd5-4610-0611-63dd4b31cfd6</uuid>
  42. # <memory>1048576</memory>
  43. # <currentMemory>1048576</currentMemory>
  44. # <vcpu>8</vcpu>
  45. # <os>
  46. # <type arch='x86_64' machine='pc-0.12'>hvm</type>
  47. # <boot dev='hd'/>
  48. # </os>
  49. # <features>
  50. # <acpi/>
  51. # (...)
  52. # Convert array to string
  53. unset($vm_info_xml);
  54. foreach ($vm_info_array as $line) { $vm_info_xml .= $line; }
  55. $xml = simplexml_load_string('<?xml version="1.0"?> ' . $vm_info_xml);
  56. if ($debug) { print_r($xml); }
  57. $vmwVmDisplayName = $xml->name;
  58. $vmwVmGuestOS = ''; # libvirt does not supply this
  59. $vmwVmMemSize = $xml->currentMemory / 1024;
  60. exec($config['virsh'] . ' -c '.$uri.' domstate ' . $dom_id,$vm_state);
  61. $vmwVmState = ucfirst($vm_state[0]);
  62. $vmwVmCpus = $xml->vcpu;
  63. # Check whether the Virtual Machine is already known for this host.
  64. $result = mysql_query("SELECT * FROM vminfo WHERE device_id = '" . $device["device_id"] . "' AND vmwVmVMID = '" . $dom_id . "' AND vm_type='libvirt'");
  65. if (mysql_num_rows($result) == 0)
  66. {
  67. mysql_query("INSERT INTO vminfo (device_id, vm_type, vmwVmVMID, vmwVmDisplayName, vmwVmGuestOS, vmwVmMemSize, vmwVmCpus, vmwVmState) VALUES (" . $device["device_id"] . ", 'libvirt',
  68. '" . $dom_id . "', '" . mres($vmwVmDisplayName) . "', '" . mres($vmwVmGuestOS) . "', '" . $vmwVmMemSize . "', '" . $vmwVmCpus . "', '" . mres($vmwVmState) . "')");
  69. echo("+");
  70. log_event("Virtual Machine added: $vmwVmDisplayName ($vmwVmMemSize MB)", $device, 'vm', mysql_insert_id());
  71. } else {
  72. $row = mysql_fetch_assoc($result);
  73. if ($row['vmwVmState'] != $vmwVmState
  74. || $row['vmwVmDisplayName'] != $vmwVmDisplayName
  75. || $row['vmwVmCpus'] != $vmwVmCpus
  76. || $row['vmwVmGuestOS'] != $vmwVmGuestOS
  77. || $row['vmwVmMemSize'] != $vmwVmMemSize)
  78. {
  79. mysql_query("UPDATE vminfo SET vmwVmState='" . mres($vmwVmState) . "', vmwVmGuestOS='" . mres($vmwVmGuestOS) . "', vmwVmDisplayName='". mres($vmwVmDisplayName) . "',
  80. vmwVmMemSize='" . mres($vmwVmMemSize) . "', vmwVmCpus='" . mres($vmwVmCpus) . "' WHERE device_id='" . $device["device_id"] . "' AND vm_type='libvirt' AND vmwVmVMID='" . $dom_id . "'");
  81. echo("U");
  82. # FIXME eventlog
  83. }
  84. else
  85. {
  86. echo(".");
  87. }
  88. }
  89. # Save the discovered Virtual Machine.
  90. $libvirt_vmlist[] = $dom_id;
  91. }
  92. }
  93. }
  94. # If we found VMs, don't cycle the other protocols anymore.
  95. if (count($libvirt_vmlist)) { break; }
  96. }
  97. # Get a list of all the known Virtual Machines for this host.
  98. $db_vm_list = mysql_query("SELECT id, vmwVmVMID, vmwVmDisplayName FROM vminfo WHERE device_id = '" . $device["device_id"] . "' AND vm_type='libvirt'");
  99. while ($db_vm = mysql_fetch_assoc($db_vm_list))
  100. {
  101. # Delete the Virtual Machines that are removed from the host.
  102. if (!in_array($db_vm["vmwVmVMID"], $libvirt_vmlist))
  103. {
  104. mysql_query("DELETE FROM vminfo WHERE id = '" . $db_vm["id"] . "'");
  105. echo("-");
  106. log_event("Virtual Machine removed: " . $db_vm['vmwVmDisplayName'], $device, 'vm', $db_vm['id']);
  107. }
  108. }
  109. echo("\n");
  110. }
  111. ?>