1

Check_MK provided a script to work with one of their built-in checks to return the health of Hyper-V VMs called hyperv_vms_guestinfos.psi. I can get the script to run with no issues at the shell. Check_mk runs the script (tested via telnet), but reports invalid information.

I've modified the script so that the first part of the VM output look no longer outputs the VM name, but now it only reports the last-recorded VM from the Get-VM command.

The script hasn't been updated since 2015, and I'm wondering if anyone else has gotten this to work without modifications, or if any modifications have needed to be made so that I can get reports for each hosted VM.

I've posted the code of the agent from my CMK installation (1.4.0p12) below. The lines I removed for testing are:

$OutputString = "<<<<" + $VM.name + ">>>>"    Script-Output -String 
$OutputString -File $OutputFile -FileOut $WriteFileOutput -Append $true

Original full script from CMK

#
# Hyper-V VM state
#
# Script must executed with local administrator credentials!
#
# This script gathers a few information about VM integration services,
# checkpoints and replication. All other information about the system
# health are gathered by the operating system agents on both, host and
# guest servers
#
# Version: 1.0
#
# Date: 2015-08-01
#
# Author: A. Exner, ACP

# Script parameters:

$OutputFile = "c:\scripts\VM-State.txt" # Path and filename for file output
$WriteFileOutput = $false


# DO NOT CHANGE ANYTHING BELOW THIS LINE!
#-------------------------------------------------------------------------------

function Script-Output
{
    param([Parameter(Mandatory = $true)][string]$String,[Parameter(Mandatory = $true)][string]$File,[Parameter(Mandatory = $false)][bool]$FileOut=$false,[Parameter(Mandatory = $false)][bool]$Append=$true)

    Write-Host $String

    If($FileOut)
    {
        If($Append)
        {
            Out-File -FilePath $File -Encoding unicode -Append -InputObject $OutputString
        }
        Else
        {
            Out-File -FilePath $OutputFile -Encoding unicode -Force -InputObject $OutputString
        }
    }
}

# Open / overwrite file output

If($WriteFileOutput)
{
    $OutputString = Get-Date -Format yyyy-MM-dd_hh-mm-ss
    Script-Output -String $OutputString -File $OutputFile -FileOut $WriteFileOutput -Append $false
}

# Get VM's from host and collect informations

$VMList = Get-VM
$now = Get-Date

Foreach ($VM in $VMList)
{
    $OutputString = "<<<<" + $VM.name + ">>>>"
    Script-Output -String $OutputString -File $OutputFile -FileOut $WriteFileOutput -Append $true
    $OutputString = "<<<hyperv_vmstatus>>>"
    Script-Output -String $OutputString -File $OutputFile -FileOut $WriteFileOutput -Append $true

    # Integration Services

    $VMI = Get-VMIntegrationService -VMName $VM.name
    $VMIStat = $VMI | where {$_.OperationalStatus -match "ProtocolMismatch"}

    If($VMIStat.Count -gt 0)
    {
        $OutputString = "Integration_Services Protocol_Mismatch"
        Script-Output -String $OutputString -File $OutputFile -FileOut $WriteFileOutput -Append $true
    }
    Else
    {
        $OutputString = "Integration_Services Ok"
        Script-Output -String $OutputString -File $OutputFile -FileOut $WriteFileOutput -Append $true
    }


    #Replica

    $OutputString = "Replica_Health None"
    Script-Output -String $OutputString -File $OutputFile -FileOut $WriteFileOutput -Append $true

    #Checkpoints

    $VMCP = Get-VMSnapshot -VMName $VM.name

    $OutputString = "<<<hyperv_checkpoints>>>"
    Script-Output -String $OutputString -File $OutputFile -FileOut $WriteFileOutput -Append $true

    If ($VMCP)
    {
        Foreach($CP in $VMCP)
        {
            $OutputString = [string]$CP.Id + " " + [string][System.Math]::Round((($now - $CP.CreationTime).TotalSeconds), 0)
            Script-Output -String $OutputString -File $OutputFile -FileOut $WriteFileOutput -Append $true
        }
    }
    Else
    {
        $OutputString = "No_Checkpoints"
        Script-Output -String $OutputString -File $OutputFile -FileOut $WriteFileOutput -Append $true
    }
}

$OutputString = "<<<<>>>>"
Script-Output -String $OutputString -File $OutputFile -FileOut $WriteFileOutput -Append $true
Larry G. Wapnitsky
  • 1,216
  • 2
  • 16
  • 35

0 Answers0