Multithreaded multiple ping targets from a remote server through nrpe / nsclient.
For example, if you have a remote site with one server, you can then use it to ping different targets (local switch, google…).
Return number of online/offline targets.
the multithreaded allow a quick execution even with high number of targets, even if they are offline.
typical execution time: 1.75 seconds per target, in average
Provide performance data to get the ratio offline/online.
Can specify number of target that must be offline before triggering warning and critical state.
Tested setup
Linux:
- Centos 6.4 x64
- Nagios 3.4.4
- check_nrpe 2.13
- Centreon 2.4.2
Windows:
- Windows Server 2008 R2 / 2012
- nsclient++ 0.4.1 x64 et x86
- Servers Core & GUI
Script arguments
- targets (separated by commas, MANDATORY)
- maxWarn (Warning if offline number above) (1 par défaut)
- maxError (Critical if offline number above) (5 par défaut)
maxWarn et maxCrit must be integer
Sample usages
Directly in PowerShell:
PS C:Program FilesNSClient++scripts>. .lotp_check_multiping.ps1 www.google.com,www.lotp.fr 1 2 OK:2 online - 0 offline - |online=2;1;2; offline=0;1;2; PS C:Program FilesNSClient++scripts>
Through NRPE:
[root~]# /usr/lib64/nagios/plugins/check_nrpe -H myMonitoredServer -n -c check_multiping -a www.google.com,www.lotp.fr 1 2 OK:2 online - 0 offline - |'online'=2;1;2 'offline'=0;1;2 [root~]#
Install:
On Windows:
- Enable powershell script execution without signed : Set-ExecutionPolicy RemoteSigned
- copy script in folder C:Program FilesNSClient++scripts
- Add to nsclient.ini:
- [/settings/external scripts/wrapped scripts]
- check_multiping=lotp_check_multiping.ps1 -targets $ARG1$ -maxWarn $ARG2$ -maxError $ARG3$
Setup:
On Centreon, by adding this command:
$USER1$/check_nrpe -H $HOSTADDRESS$ -n -t 60 -c check_multiping -a $ARG1$ $ARG2$ $ARG3$
Download
(remove .txt at the end)
Source code :
# ====================================================================
# Ping a list of targets through NRPE
# Author: Mathieu Chateau - LOTP
# mail: mathieu.chateau@lotp.fr
# version 0.1
# ====================================================================
#
# Require Set-ExecutionPolicy RemoteSigned.. or sign this script with your PKI
#
# ============================================================
#
# Do not change anything behind that line!
#
param
(
[string]$targets,
[int]$maxWarn = 1,
[int]$maxError = 5
)
$output=""
$exitcode=2
$countOK=0
$countKO=0
$targetsArray=@()
$targetsArray=$targets -split(' ')
Remove-Job -Name * -Confirm:$false -Force
foreach($t in $targetsArray)
{
Start-Job -Name $t -ArgumentList $t -ScriptBlock {param($t);if(Test-Connection -ComputerName $t -Count 2 -Quiet -ErrorAction SilentlyContinue){return $true}else{return $false}} |Out-Null
}
while(Get-Job -State Running)
{
Start-Sleep -Milliseconds 500
}
foreach ($job in Get-Job)
{
$temp=Receive-Job -Name $job.Name
if($temp)
{
$countOK++
}
else
{
$countKO++
$output+=$job.Name+" - "
}
}
if ($countKO -gt $maxError)
{
$state="CRITICAL"
$exitcode=2
}
elseif ($countKO -gt $maxWarn)
{
$state="WARNING"
$exitcode=1
}
else
{
$state="OK"
$exitcode=0
}
$output=$state+":"+$countOK+" online"+" - "+$countKO+" offline - "+$output
$output+='|'
$output+="online="+$countOK+";"+$maxWarn+";"+$maxError+";"+" "
$output+="offline="+$countKO+";"+$maxWarn+";"+$maxError+";"
Write-Host $output
exit $exitcode