Checking Free Disk Space on NTFS Volume Mount Points in Nagios

As part of creating external scripts within Nagios I was required to create a script which would monitor NTFS volume mount points which is not available in the default monitoring wizard.

As the script was to be run against a number of different volume mount points, I did not want to create multiple scripts as well as multiple services within Nagios. Therefore, the script defined parameters for the NTFS volume mount points label name, which were called with the MountPoint argument.

Param ([string] $MountPoint)

I had a number of requirements when creating the script. Firstly, the Warning and Critical values as a percentage would be configured to be 80 and 90 respectively. These can be adjusted in the script as below.

$Warning = "80"
$Critical = "90"

Secondly, I had to return the Label, Capacity (GB), Free Space (GB) and Used Space (GB) of the NTFS volume mount point. The NTFS volume mount point was returned using the Get-WmiObject cmdlet and filtering by the Label name.

$Volume = Get-WmiObject Win32_Volume | Where-Object ($_.Label -eq $MountPoint)
$Label = $Volume.Name
$Capacity = $Volume.Capacity /1GB
$FreeSpace = $Volume.FreeSpace / 1GB 
$UsedSpace = ($Volume.Capacity - $Volume.FreeSpace) / 1GB

Finally, we then need to convert the Free Space (GB) and Used Space (GB) into a percentage for generating the status and return an exit code.

$PercentFree = [Math]::Round(($Volume.FreeSpace / $Volume.Capacity) * 100)
$PercentUsed = 100 - [Math]::Round(($Volume.FreeSpace / $Volume.Capacity) * 100)

Now we have the percentage of used space we  can generate the return code to pass to Nagios as a service state by using conditional logic within the script.

If ($PercentUsed -lt $Warning) {$returncode = 0}
ElseIf ($PercentUsed -gt $Warning -and $PercentUsed -lt $Critical) {$returncode = 1} 
ElseIf ($PercentUsed -gt $Critica) {$returncode = 2}

Now all that is left, is to exit the script and return the exit code to Nagios. However, before we do so I want to return Status Information as well to provide the Label, Capacity (GB), Used Space (%) and Free Space (%).

"Disk $Label -total: " + [System.Math]::Round($Capacity, 2) + "GB -used: " + [System.Math]::Round($UsedSpace, 2) + "GB ($PercentUsed%) -free " + [System.Math]::Round($FreeSpace, 2) + "GB ($PercentFree%)"
exit $returncode

Below, is an example of a formatted Status Information message generated:

Disk D:\Disk1\ -total: 200GB -used: 51.16GB (26%) -free 148.84GB (74%)

While the script was created to be executed as an external script within Nagios, this can be run standalone from Windows Powershell. If your are looking to add external scripts to Nagios such as this one see the below link for more information;

https://deangrant.wordpress.com/2013/09/12/creating-and-running-external-scripts-within-nagios-xi/

The full Windows Powershell script can be downloaded from the below link:

https://app.box.com/s/71ouenbu6vvcs6ewe3c7

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s