Nagios for Hyper-V: Alert on Low Cluster Shared Volume Space

This article’s script could be run on any Windows cluster that uses Cluster Shared Volumes. It is simple: it looks for the CSV that you specified, finds out its total space, finds out its free space, calculates the percentage, and returns an OK, warning, or critical status. In order to use this script, you must have a functioning Nagios environment and the NSClient++ software installed on each node in the cluster(s) to be monitored. You must also have placed the clusterbase.ps1 script as indicated in the cluster base script article. None of the Hyper-V scripts are required for this check script to operate.

If you’re new to Nagios you should definitely check out the article How To: Monitor Hyper-V with Nagios first, then move onto the cluster base script article linked about, and continue reading below.

Note: In the update to 1.2, the script has been altered to alert at the level of USED space as opposed to the level of FREE space. That brings its behavior more inline with similar checks by other authors. Upon updating to version 1.2, modify your checks accordingly. For example, you might be using “check-csvspace!CSV1!20!10” to warn at 20% remaining and report critical at 10% remaining; that same check will now be “check-csvspace!CSV1!80!90”.

NSClient++ Configuration

These changes are to be made to the NSClient++ files on all Hyper-V hosts to be monitored.


C:Program FilesNSClient++nsclient.ini

If the indicated INI section does not exist, create it. Otherwise, just add the second line to the existing section.

[/settings/external scripts/wrapped scripts]
check_csvspace=check_csvspace.ps1 $ARG1$ $ARG2$ $ARG3$

The NSClient++ service must be restarted after all changes to its ini file.


C:Program FilesNSClient++scriptscheck_csvspace.ps1

The required script clusterbase.ps1 must exist in the same folder.

<#
	check_csvspace.ps1
	Written by Eric Siron
	(c) Altaro Software 2017

	Version 1.2 November 19, 2017

	Intended for use with the NSClient++ module from http://nsclient.org
	Checks a Cluster Shared Volume's available space and returns the status to Nagios.
#>

param(
	[Parameter(Position = 1)][String]$CSVName,
	[Parameter(Position = 2)][UInt16]$WarningLevel,
	[Parameter(Position = 3)][UInt16]$CriticalLevel
)

if ([String]::IsNullOrEmpty($CSVName))
{
	Write-Host -Object 'No CSV was specified'
	Exit 3
}

$ClusterBase = Join-Path -Path $PSScriptRoot -ChildPath 'clusterbase.ps1'
. $ClusterBase

$CSVPartition = Get-ANClusterPartitionFromCSVName -CSVName $CSVName

if ($CSVPartition)
{
	if($WarningLevel -gt 100) { $WarningLevel = 100 }
	if($CriticalLevel -gt 100) { $CriticalLevel = 100 }
	$TotalSpace = $CSVPartition.TotalSize
	$FreeSpace = $CSVPartition.FreeSpace
	$FreePercent = $FreeSpace / $TotalSpace * 100
	$UsedSpace = $TotalSpace - $FreeSpace
	Write-Host -Object ('{0}MB free. {1}MB total. {2:N0}% free.|''CSV Used Space''={3}mb;{4};{5};;' -f $FreeSpace, $TotalSpace, $FreePercent, $UsedSpace, [uint32]($TotalSpace * $WarningLevel / 100), [uint32]($TotalSpace * $CriticalLevel / 100))
	if ($FreePercent -le (100 - $CriticalLevel))
	{
		exit 2
	}
	if ($FreePercent -le (100 - $WarningLevel))
	{
		exit 1
	}
}
else
{
	Write-Host -Object ('No CSV named {0} found.' -f $CSVName)
	exit 3
}
exit 0

Nagios Configuration

These changes are to be made on the Nagios host. I recommend using WinSCP as outlined in our main Nagios and Ubuntu Server articles.

/usr/local/nagios/etc/objects/commands.cfg

The Hyper-V Host Commands section should already exist if you followed our main Nagios article. Add this command there. If you are not working with a Hyper-V system, then you can create any section heading that makes sense to you, or just insert the command wherever you like.

define command{
	command_name	check-csvspace
	command_line	$USER1$/check_nrpe -H $HOSTADDRESS$ -t 30 -p 5666 -c check_csvspace -a $ARG1$ $ARG2$ $ARG3$ $ARG4$ $ARG5$ $ARG6$
}

/usr/local/nagios/etc/objects/hypervhost.cfg

This file and section were created in the Hyper-V base scripts article. As long as it appears somewhere in one of the activated .cfg files, it will work.

This is a sample! You must use your own cluster name object!

###############################################################################
###############################################################################
#
# CLUSTER SERVICE DEFINITIONS
#
###############################################################################
###############################################################################

# check CLHV1 for CSV free space
define service{
	use			generic-service
	host_name		clhv1
	service_description	CSV1 Free Space
	check_command		check-csvspace!CSV1!80!90

Nagios must be restarted after these files are modified.

sudo service nagios checkconfig
sudo service nagios restart

 

Share this post