# ------------GENERAL INFO---------------------------------------------------------------------------- # date: 23-12-2011 # creator: Ferdi Ogier (NL) # script: Powershell (for Nagios) # version - v1.0 # purpose: Checks the runtimestatus of HA Agents within vSphere Clusters. # # Script calls the vSphere PowerCLI Snapin, thus this script can be run directly in plain Powershell # ----------------------------------------------------------------------------------------------------- # ------------PREREQUISITES---------------------------------------------------------------------------- # Windows machine to run the script # Powershell installed # VMWare vSphere PowerCLI 4 or later installed # Account with access to Virtual Center # ----------------------------------------------------------------------------------------------------- # ------------BACKGROUND INFO HA AGENT ON VSPHERE------------------------------------------------------ # The only status that is good is "Running" and results in an exitcode 0 (OK) # Every other status is wrong, thus results in an exitcode 2 (Critical) # A HA Agent can have the following status: # uninitialized, initialized, error, agentShutdown, nodeFailed, running # ----------------------------------------------------------------------------------------------------- # ------------VARIABLES USED IN SCRIPT----------------------------------------------------------------- # $VCServer = Virtual Center Server # $HAreport = location txt-file which will hold de status of every host. # $HAreporttemp = temp txt-file which is used to conduct formatchanges to the original report. Will be deleted automatically. # $pass = Holds the password which the script gets from securePassword.txt (see below) # $credential = Creats an PScredentials object with the username/password for authentication Virtual Center # ----------------------------------------------------------------------------------------------------- # ------------HOW TO CREATE THE SECURE PASSWORD------------------------------------------------------------------- # Use script below to create the securePassword.txt. # This script must be run under the same account which will be used to make the connection to Virtual Center # That user is the only user who can decrypt the securePassword.txt # # <> # $secure = ConvertTo-SecureString $password -force -asPlainText # $bytes = ConvertFrom-SecureString $secure # $bytes | Out-File securePassword.txt # # $secure = Read-Host "Enter Password" -asSecureString # $bytes = ConvertFrom-SecureString $secure # $bytes | Out-File securePassword.txt # <> # ----------------------------------------------------------------------------------------------------- # Load VMWare PowerCLI Add-PSSnapin VMware.VimAutomation.Core $VCServer = "virtualcenterservername" $HAreport = "c:\tmp\HAReport.txt" $HAreporttemp = "c:\tmp\HAReport_tmp.txt" $pass = cat "securePassword.txt" | ConvertTo-SecureString $credential = new-object -typename System.Management.Automation.PSCredential -argumentlist "domain\account",$pass # Create connection with Virtual Center Connect-VIServer -Server $VCServer -Credential $credential # Command to get the status of all HA Agents within ALL clusters in Virtual Center. Output goes to a txt-file Get-View -ViewType "ClusterComputeResource" -Property "Name" | Foreach-Object {$_.RetrieveDasAdvancedRuntimeInfo().DasHostInfo.HostDasState} | Format-Table -AutoSize "RunTimeState" | Out-File $HAreport # Removes the first 3 header lines of the report (txtfile). Only the statuslines stay. gc $HAreport | select -Skip 3 | sc $HAreporttemp move $HAreporttemp $HAreport -Force # If any status mentioned in the line below is present in the report (txtfile), output is CRITICAL (exitcode 2) # If variable $status is empty, then all HA Agent are "running" and is outputs with OK (exitcode 0) $status = (Select-String $HAreport -pattern "uninitialized","error","agentShutdown","nodeFailed") if (!$status) { write-host "vSphere HA - OK" exit 0 } else { write-host "vSphere HA - Critical" $host.SetShouldExit(2) }