plugins/Invoke-IcingaCheckHyperVVMHealth.psm1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
<#
.SYNOPSIS
    Determines the current state of the Hyper-V virtual machine.
.DESCRIPTION
    Invoke-IcingaCheckHyperVVMHealth determines the current state of all available Hyper-V virtual machines by default.
    It also offers you the possibility to ex/include virtual machines with wildcard search i.e. if you enter the
    following ``-IncludeVms *test*`` as parameter, only ``VMs`` will be added to the check, which the name of the
    VMs contains this wildcard name.
.ROLE
    ### WMI Permissions

    * Root\Virtualization\v2
    * Root\Cimv2

    ### Performance Counter

    * Processor(*)\% processor time

    ### Required User Groups

    * Performance Monitor Users
    * Hyper-V Administrator
.PARAMETER IncludeVms
    Include only virtual machines with a specific name. Supports wildcard usage (*)
.PARAMETER ExcludeVms
    Exclude virtual machines with a specific name. Supports wildcard usage (*)
.PARAMETER ActiveVms
    Include only virtual machines that are currently running
.PARAMETER WarningActiveVms
    Allows to monitor on how many active VM's are currently present and throws a
    warning in case it is within the threshold
.PARAMETER CriticalActiveVms
    Allows to monitor on how many active VM's are currently present and throws a
    warning in case it is within the threshold
.PARAMETER SkipVMHeartbeat
    Skips the current virtual machine heartbeat status check.
.PARAMETER VmEnabledState
    Critical threshold for the Hyper-V VM current status
.PARAMETER NegateVMState
    Negates the VmEnabledState of this plugin and will then report all Vms
    CRITICAL, in case they are not matching the VmEnabledState.
.PARAMETER NoPerfData
    Disables the performance data output of this plugin.
.PARAMETER Verbosity
    Changes the behavior of the plugin output which check states are printed:
    0 (default): Only service checks/packages with state not OK will be printed
    1: Only services with not OK will be printed including OK checks of affected check packages including Package config
    2: Everything will be printed regardless of the check state
    3: Identical to Verbose 2, but prints in addition the check package configuration e.g (All must be [OK])
.EXAMPLE
    PS> icinga { Invoke-IcingaCheckHyperVVMHealth -ActiveVms -IncludeVms '*sales*' -Verbosity 2 }
    [OK] Check package "Virtual Computers" (Match All)
    \_ [OK] Check package "vm-01" (Match All)
        \_ [OK] vm-01 HealthState: OK
        \_ [OK] vm-01 Heartbeat: OK
        \_ [OK] vm-01 State: Enabled
    \_ [OK] Check package "vm-02" (Match All)
        \_ [OK] vm-02 HealthState: OK
        \_ [OK] vm-02 Heartbeat: OK
        \_ [OK] vm-02 State: Enabled
    \_ [OK] Check package "vm-03" (Match All)
        \_ [OK] vm-03 HealthState: OK
        \_ [OK] vm-03 Heartbeat: OK
        \_ [OK] vm-03 State: Enabled
    \_ [OK] Check package "vm-04" (Match All)
        \_ [OK] vm-04 HealthState: OK
        \_ [OK] vm-04 Heartbeat: OK
        \_ [OK] vm-04 State: Enabled
    \_ [OK] Check package "vm-05" (Match All)
        \_ [OK] vm-05 HealthState: OK
        \_ [OK] vm-05 Heartbeat: OK
        \_ [OK] vm-05 State: Enabled
    \_ [OK] Check package "vm-06" (Match All)
        \_ [OK] vm-06 HealthState: OK
        \_ [OK] vm-06 Heartbeat: OK
        \_ [OK] vm-06 State: Enabled
    | 'vm01_healthstate'=5;; 'vm01_state'=2;; 'vm01_heartbeat'=2;; 'vm02_heartbeat'=2;; 'vm02_state'=2;; 'vm02_healthstate'=5;; 'vm03_heartbeat'=2;;
    'vm03_healthstate'=5;; 'vm03_state'=2;; 'vm04_heartbeat'=2;; 'vm04_state'=2;; 'vm04_healthstate'=5;; 'vm05_state'=2;; 'vm05_healthstate'=5;; 'vm05_heart
    beat'=2;; 'vm06_healthstate'=5;; 'vm06_state'=2;; 'vm06_heartbeat'=2;;
    0
.LINK
    https://github.com/Icinga/icinga-powershell-hyperv
#>


function Invoke-IcingaCheckHyperVVMHealth()
{
    param (
        [array]$IncludeVms        = @(),
        [array]$ExcludeVms        = @(),
        [switch]$ActiveVms        = $FALSE,
        $WarningActiveVms         = $null,
        $CriticalActiveVms        = $null,
        [switch]$NoPerfData       = $FALSE,
        [switch]$SkipVMHeartbeat  = $FALSE,
        [ValidateSet('Unknown', 'Other', 'Enabled', 'Disabled', 'Shutting Down', 'Not Applicable', 'Enabled but Offline', 'In Test', 'Deferred', 'Quiesce', 'Starting')]
        $VmEnabledState           = $null,
        [switch]$NegateVMState    = $FALSE,
        [ValidateSet(0, 1, 2, 3)]
        $Verbosity                = 0
    );

    # Create a main CheckPackage, under which all following CheckPackages are added
    $CheckPackage       = New-IcingaCheckPackage -Name 'Virtual Computers' -OperatorAnd -Verbose $Verbosity -IgnoreEmptyPackage -AddSummaryHeader;
    $HiddenCheckPackage = New-IcingaCheckPackage -Name 'Hidden PerfData' -Hidden;
    # We get all information about the virtual machine from our provider
    $VirtualComputers   = Get-IcingaVirtualComputerInfo -IncludeVms $IncludeVms -ExcludeVms $ExcludeVms -ActiveVms:$ActiveVms;

    $ActiveVMCountCheck = New-IcingaCheck `
        -Name 'Active VMs' `
        -Value $VirtualComputers.Summary.RunningVms `
        -NoPerfData;

    $CheckPackage.AddCheck($ActiveVMCountCheck.WarnOutOfRange($WarningActiveVms).CritOutOfRange($CriticalActiveVms));

    foreach ($vm in $VirtualComputers.VMs.Keys) {
        $virtualComputer = $VirtualComputers.VMs[$vm];
        $VMCheckPackage  = New-IcingaCheckPackage -Name $vm -OperatorAnd -Verbose $Verbosity;

        # Heartbeat check is always OK if the VM is not started
        if ($virtualComputer.EnabledState -eq $HypervProviderEnums.VMEnabledStateName.Enabled) {
            $HeartBeatCheck = New-IcingaCheck `
                -Name ([string]::Format('{0} Heartbeat', $vm)) `
                -Value $virtualComputer.Heartbeat `
                -Translation $HypervProviderEnums.VMHeartbeat;

            if ($SkipVMHeartbeat -eq $FALSE) {
                $HeartBeatCheck.WarnIfMatch(
                    $HypervProviderEnums.VMHeartbeatName.'No Contact'
                ).WarnIfMatch(
                    $HypervProviderEnums.VMHeartbeatName.'Lost Communication'
                ).CritIfMatch(
                    $HypervProviderEnums.VMHeartbeatName.Error
                ) | Out-Null;
            }

            $VMCheckPackage.AddCheck($HeartbeatCheck);
        }

        $EnabledStateCheck = New-IcingaCheck `
            -Name ([string]::Format('{0} State', $vm)) `
            -Value $virtualComputer.EnabledState `
            -Translation $HypervProviderEnums.VMEnabledState;

        if ($NegateVMState) {
            $EnabledStateCheck.CritIfNotMatch($HypervProviderEnums.VMEnabledStateName[[string]$VmEnabledState]) | Out-Null;
        } else {
            $EnabledStateCheck.CritIfMatch($HypervProviderEnums.VMEnabledStateName[[string]$VmEnabledState]) | Out-Null;
        }

        $VMCheckPackage.AddCheck($EnabledStateCheck);

        $VMCheckPackage.AddCheck(
            (
                New-IcingaCheck `
                    -Name ([string]::Format('{0} HealthState', $vm)) `
                    -Value $virtualComputer.HealthState `
                    -Translation $HypervProviderEnums.VMHealthState
            ).WarnIfNotMatch(
                $HypervProviderEnums.VMHealthStateName.OK
            )
        );

        $CheckPackage.AddCheck($VMCheckPackage);
    }

    # we create here a hidden checkpackage
    if ($VirtualComputers.Summary.TotalVms -ne 0) {
        foreach ($item in $VirtualComputers.Summary.Keys) {
            if ($item -eq 'AccessDeniedVms') {
                continue;
            }

            $HiddenCheckPackage.AddCheck(
                (
                    New-IcingaCheck -Name $item -Value ($VirtualComputers.Summary[$item])
                )
            );
        }
    }

    if ($HiddenCheckPackage.HasChecks()) {
        $CheckPackage.AddCheck($HiddenCheckPackage);
    }

    return (New-IcingaCheckResult -Check $CheckPackage -NoPerfData $NoPerfData -Compile);
}