PoshCode Archive  Artifact Content

Artifact 6de55ac65254e57c0e2e2904579369b8899fd07884cc932b990071b97d09ed0b:

  • File check-nsca.ps1 — part of check-in [4480353dad] at 2018-06-10 12:59:26 on branch trunk — Sends with nsca (Nagios Client) all Status Informations over VMs (user: Patrick size: 1448)

# encoding: ascii
# api: powershell
# title: check-nsca.ps1
# description: Sends with nsca (Nagios Client) all Status Informations over VMs
# version: 0.1
# author: Patrick
# license: CC0
# x-poshcode-id: 1630
# x-archived: 2016-05-26T20:42:03
# x-published: 2010-02-08T03:59:00
#
#
#region vars
$statvalues=("mem.usage.average", "cpu.usage.average")
$nsca_stat = ""
[int]$warnlevel = 85
[int]$criticallevel = 90
$status = ""
$nagsrv = "nagios-srv.local"
#endregion

$vms = Get-VM | Where-Object { $_.PowerState -eq "PoweredOn" } | sort-object

foreach ($vm in $vms) {
	$statvalues | foreach {
		[int]$statavg = ($vm | Get-Stat -Stat $_ -Start ((get-date).AddMinutes(-5)) -MaxSamples 500 | Measure-Object -Property Value -Average).Average
		$vmdns = ($vm | Get-VMGuest).Hostname
		switch ($_) {
			"mem.usage.average" { $nsca_stat = "mem_vm"; $desc = "Memory Usage" }
			"cpu.usage.average" { $nsca_stat = "cpu_vm"; $desc = "CPU Usage" }
		}
		if ($statavg -gt $criticallevel) {
			$status = "2"
			$desc = "CRITICAL: " + $desc
		} elseif ($statavg -gt $warnlevel) {
			$status = "1"
			$desc = "WARNING: " + $desc
		} elseif ($statavg -lt $warnlevel) {
			$status = "0"
		}
		$nsca = "${vmdns};${nsca_stat};${status};${desc} ${statavg}% | ${nsca_stat}=${statavg};$warnlevel;$criticallevel;0;100"
		Write-Host $nsca
		if ($vmdns) { echo $nsca | ./send_nsca.exe -H $nagsrv -c send_nsca.cfg -d ";" }
	}
}