25
Jan/10
0

Nagios Plugin: check_fsrm_quota.pl

You can find the current version of the Nagios check script for checking FSRM (File system ressource manager) quota on Microsoft Windows servers on this page. Furthermore you can find some descriptions and sample configurations.

Idea

It is possible to configure filesystem quotas on fileservers using Windows 2003 R2 or above on directory base. This is realized using the File Server Resource Manager (FSRM). For me it was important not only to have a limit and/or a notification mail from the FSRM but also a centralized graphing for trend analyzes and centralized visualisation in NagVis.

Until today I found no other way gathering those information than checking a Windows Server using NRPE. Since I didn’t want to modify anything on the Windows Fileservers I set up a dedicated Windows host for working as monitoring proxy between Nagios and the FSRM in the Windows world. So my check chain for this task looks as follows:

Nagios |--- Active Check ---> NSClient++ (NRPE enabled) |--- System call ---> check_fsrm_quota.pl |--- FSRM ---> Fileserver

Sample output
check_fsrm_quota sample output

Prerequisites

The check_fsrm_quota.pl is located on the “proxying” Windows host. The script is written in Perl so Active Perl needs to be installed and usable on that. The script only uses the Getopt::Long module so there are no special Perl module requirements.

It is needed to connect to the FSRM service on the Windows fileservers so the dirquota.exe and srm.dll are needed on the “proxying” Windows host.

check_fsrm_quota.pl

Copy the file to the Windows host to a directory of your choice. I copied it to C:\scripts.

# ##############################################################################
# check_fsrm_quota.pl - Nagios / NRPE check plugin (Windows)
#
# 2009-02-09 Lars Michelsen <lars@vertical-visions.de>
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,
#
# GNU General Public License: http://www.gnu.org/licenses/gpl-2.0.txt
#
# ##############################################################################
# SCRIPT:          check_fsrm_quota.pl
# VERSION:         1.1
# AUTHOR:          Lars Michelsen <lm@larsmichelsen.com>
# DECRIPTION:      Checks the FSRM service from local or remote for the quota of
#                  a defined directory. Thresholds for WARNINGs and CRITICALs
#                  can be configured in this script.
# HOMEPAGE:        <http://nagios.larsmichelsen.com/check_fsrm_quota/>
# BUGS:            <http://www.nagios-portal.org/>
# CHANGES:
# 2009-02-09 v1.0  - Initial code
# 2009-03-15 v1.0  - Added perfdata output
#                  - Some code cleanups
# ##############################################################################
 
use warnings;
use strict;
use Getopt::Long;
 
my $sRemoteHost;
my $sQuotaPath;
my $sWarn;
my $sCrit;
 
# Path to binaries
my $sDirquotaExe = 'dirquota.exe';
 
my $sOutput = '';
my $iState = 0;
my $sState = '';
my $sPerfdata = '';
my $iSummaryState = 0;
my $sSummaryState = '';
my @aStates = ('OK', 'WARNING', 'CRITICAL', 'UNKNOWN');
my %hStates = ( OK => 0, WARNING => 1, CRITICAL => 2, UNKNOWN => -1 );
 
###############################################################################
 
my ($oHelp,$oHostname,$oPath,$oWarn,$oCrit);
Getopt::Long::Configure('bundling');
GetOptions(
    'h|help' => \$oHelp,
    'H|host=s' => \$oHostname,
    'P|path=s' => \$oPath,
    'w|warn=s' => \$oWarn,
    'c|crit=s' => \$oCrit);
 
if($oHelp) {
print <<EOU;
  Usage: $0 -H <FQDN/IP: string> -P <Path: string> 
            -w <warning level: integer> -c <critical level: integer>
         $0 -h
 
 
    Options:
 
    -H --host STRING
        FQDN or IP-Address of the Windows Fileserver (W2k3 R2 or above)
    -P --path STRING
        Full system path the Quota has been configured for
    -w --warn INTEGER
        Warning treshold. Give the minimum memory in bytes, MB or GB.
 
        Examples:
        - Give 10GB to get a WARNING state when the free space is 10GB below
        the hard quota limit.
        - Give 1048576 to get a WARNING state when the free space is 1GB below
        the hard quota limit.
    -c --crit INTEGER
        Critical treshold. Give the minimum memory in bytes, MB or GB.
 
        Example:
        - Give 5GB to get a CRITICAL state when the free space is 5GB below
        the hard quota limit.
    -h --help
        Print this help text
EOU
	exit 0;
}
 
if(!$oHostname || $oHostname eq '') {
	print('ERROR: No hostname given');
	exit 0;
}
 
if(!$oPath || $oPath eq '') {
	print('ERROR: No path given');
	exit 0;
}
 
if(!$oWarn || $oWarn eq '') {
	print('ERROR: No warning treshold given');
	exit 0;
}
 
if(!$oCrit || $oCrit eq '') {
	print('ERROR: No critical treshold given');
	exit 0;
}
 
$sRemoteHost = $oHostname;
$sQuotaPath = $oPath;
$sWarn = $oWarn;
$sCrit = $oCrit;
 
###############################################################################
 
# Register dll
$sOutput = `regsvr32 /s c:\\windows\\system32\\srm.dll`;
 
# 1. Query quota list
$sOutput = `$sDirquotaExe Quota List /remote:$sRemoteHost /Path:$sQuotaPath`;
 
# Split on each line break
my @aOutput = split(/\n/, $sOutput);
 
# Loop each quota entry and fill hash
my @quotas = ();
my $i = -1;
foreach my $line (@aOutput) {
	if($line =~ /^([A-Za-z\s]+):\s+(.+)$/) {
		my $label = $1;
		my $value = $2;
 
		# Remove signs in brackets
		$value =~ s/\s+\(.+\)//g;
 
		if($label eq 'Quota Path') {
			push @quotas, {path => $value};
			$i++;
		} elsif($label eq 'Source Template') {
			$quotas[$i]->{'template'} = $value;
		} elsif($label eq 'Label') {
			$quotas[$i]->{'label'} = $value;
		} elsif($label eq 'Quota Status') {
			$quotas[$i]->{'status'} = $value;
		} elsif($label eq 'Limit') {
			$quotas[$i]->{'limit'} = $value;
		} elsif($label eq 'Used') {
			$quotas[$i]->{'used'} = $value;
		} elsif($label eq 'Available') {
			$quotas[$i]->{'available'} = $value;
		}
	}
}
 
# Now loop the quotas
foreach my $quota (@quotas) {
	my %quota = %{$quota};
 
	# Set initial state
	$iState = $hStates{'OK'};
	$sState = 'OK: Available space: '.$quota{'available'};
 
	my $val = str2bytes($quota{'available'});
 
	$sPerfdata = 'available='.$val.'b;'.str2bytes($sWarn).';'.str2bytes($sCrit);
 
	if($val < str2bytes($sWarn)) {
		$iState = $hStates{'WARNING'};
		$sState = 'WARNING: Free space is lower than '.$sWarn.' (Available: '.$quota{'available'}.')';
	}
 
	if($val < str2bytes($sCrit)) {
		$iState = $hStates{'CRITICAL'};
		$sState = 'CRITICAL: Free space is lower than '.$sCrit.' (Available: '.$quota{'available'}.')';
	}
}
 
###############################################################################
 
# Build summary output
print $sState.' | '.$sPerfdata;
exit($iState);
 
###############################################################################
 
sub str2bytes {
	my ($str) = @_;
 
	if($str =~ m/^([0-9\.\,]+)\s*([A-Z]+)/) {
		my $val = $1;
		my $uom = $2;
 
		# Change , to .
		$val =~ s/,/./g;
 
		if($uom eq 'GB') {
			$str = $val * 1024 * 1024 * 1024;
		} elsif($uom eq 'MB') {
			$str = $val * 1024 * 1024;
		}
	}
 
	return $str;
}

PNP-Template

I created no custom template yet. So I use the default PNP template for this script. If you created some please let me know.

Parameters

  Usage: C:\scripts\check_fsrm_quota.pl -H <FQDN/IP: string> -P <Path: string>
                      -w <warning level: integer> -c <critical level: integer>
         C:\scripts\check_fsrm_quota.pl -h
 
 
    Options:
 
    -H --host STRING
        FQDN or IP-Address of the Windows Fileserver (W2k3 R2 or above)
    -P --path STRING
        Full system path the Quota has been configured for
    -w --warn INTEGER
        Warning treshold. Give the minimum memory in bytes, MB or GB.
 
        Examples:
        - Give 10GB to get a WARNING state when the free space is 10GB below
        the hard quota limit.
        - Give 1048576 to get a WARNING state when the free space is 1GB below
        the hard quota limit.
    -c --crit INTEGER
        Critical treshold. Give the minimum memory in bytes, MB or GB.
 
        Example:
        - Give 5GB to get a CRITICAL state when the free space is 5GB below
        the hard quota limit.
    -h --help
        Print this help text

Sample configuration

Here are some sample command and service definition for Nagios configuration.

define command {
	command_name  check_proxy_fsrm_quota	
	command_line  $USER1$/check_nrpe -H <windows-gatway-host> -u -t 10 -c "check_fsrm_quota" -a "$HOSTADDRESS$" "$ARG1$" "$ARG2$" "$ARG3$"
}
 
define service {
  host_name             <hostname>
  service_description   fsrm-quota-test
  check_command         check_proxy_fsrm_quota!D:/path/to/directory!5GB!2GB
 
	use                   template-check-10-min
}

And here the command I used in the configuration file of NSClient++, nsc.ini:

[NRPE Handlers]
check_fsrm_quota=perl c:\scripts\check_fsrm_quota.pl -H $ARG1$ -P $ARG2$ -w $ARG3$ -c $ARG4$

Note: Please not that using additional command ARGs in NRPE like the above example uses may result in a security problem. Make sure to secure your installation to prevent code injection attacks.

To gather the available quotas on a remote system you can manually execute the following command on your Windows proxy host:

dirquota.exe Quota List /remote:<windows-fileserver>

Filed under: Nagios
Comments (0) Trackbacks (0)

No comments yet.

No trackbacks yet.