Virtualization
banshee/virtualization-collection/virtualization/hyperv/winrm/
host
The Hyper-V service, the virtual machines running and the memory of a Hyper-V host, by WinRM and PowerShell.
by banshee1 installation2 versions
// A Hyper-V host, by WinRM: the state of its Hyper-V service (vmms, Hyper-V Virtual Machine Management), the
// number of virtual machines it runs, and the memory left to the host.
// Runs one PowerShell script that only reads: Get-Service vmms, Win32_OperatingSystem (TotalVisibleMemorySize
// and FreePhysicalMemory, in KiB), and, when the service runs, the WMI class of Hyper-V Msvm_ComputerSystem
// (root\virtualization\v2), whose properties and codes Microsoft documents: a machine is told from the host by
// its Name, a GUID, and runs when its EnabledState is 2. With $ErrorActionPreference set to Stop, any failure,
// a missing right on Hyper-V among them, ends the script with no answer, rather than an answer of zero
// machines.
need('protocol:winrm');
output('service', 'text', array('label' => 'Hyper-V service'));
output('running', 'number', array('label' => 'Virtual machines running'));
output('memory-available', 'number', array('label' => 'Available memory', 'unit' => 'MiB'));
output('memory-used', 'number', array('label' => 'Used memory', 'unit' => '%'));
watch.expect('service', 'Hyper-V service not running', 'service', array('critical' => array('running')));
watch.above('memory', 'Memory saturated', 'memory-used', array('warning' => 90, 'critical' => 95, 'for' => '3 runs'));
watch.below('available', 'Little memory available', 'memory-available', array('critical' => 2048, 'enabled' => false));
$read = winrm.run("$ErrorActionPreference = 'Stop'; "
. "[Console]::OutputEncoding = New-Object System.Text.UTF8Encoding; "
. "$os = Get-CimInstance Win32_OperatingSystem; "
. "$status = @{service = [string](Get-Service vmms).Status; "
. "total = $os.TotalVisibleMemorySize; free = $os.FreePhysicalMemory}; "
. "if ($status.service -eq 'Running') { "
. "$status.running = @(Get-CimInstance -Namespace root\\virtualization\\v2 -ClassName Msvm_ComputerSystem"
. " | Where-Object { $_.Name -match '^[0-9A-Fa-f]{8}-' -and $_.EnabledState -eq 2 }).Count }; "
. "ConvertTo-Json -Compress -InputObject $status");
$answer = text.trim($read['output']);
if (list.count(text.match($answer, '^\{')) == 0) {
fail('misconfigured', 'PowerShell did not return the state of Hyper-V', $answer . $read['errors']);
}
$hyperv = json.read($answer);
if ($hyperv['total'] <= 0) {
fail('misconfigured', 'Win32_OperatingSystem gives no physical memory', $answer);
}
// The service state is "Running", "Stopped", "StartPending"...: returned in lower case. A service that does
// not run leaves the machines unread.
value('service', text.lower($hyperv['service']));
if (list.has($hyperv, 'running')) {
value('running', $hyperv['running']);
}
$total = $hyperv['total'] / 1024;
$available = $hyperv['free'] / 1024;
value('memory-available', number.round($available));
value('memory-used', number.round(100 * ($total - $available) / $total, 1));