We have various scripts on a server that process files that are uploaded to a specific directory so we needed a way to monitor these directories and verify that all files are being processed. Once they are processed on our server they are moved to different directory so the file count should never get over a certain amount of files. The below script assumes that you have NRPE installed on the server so you can reference the bash script locally. Follow the directions below with some monitor modifications and you can monitor directories for the number of files with Nagios very easily.
Nagios NRPE Plugin To Monitor The Number Of Files In A Directory:
- Create Bash Script: First you need to create the below bash script on the server where you want to monitor the number of files in a specific directory. Please note that you will need to modify the directory you are checking along with the number of files in each IF statement to warn once the number of files crosses your alert threshold.
Bash Script To Generate Nagios Warning Based On Number Of Files In A Directory:
- #!/bin/bash
- #
- # Simple Bash Script To Check Directory File Count
- # Nagios Plugin For NRPE
- # Created for www.question-defense.org originally
- # Described More >> http://link.as/age
- #OK_STATE=0
- WARNING_STATE=1
- CRITICAL_STATE=2
- file_count=0file_count=`ls /home/some/project/files/ | wc -l`if [ $file_count -gt 15 ]
- then
- echo "WARNING:" $file_count "files"
- exit $WARNING_STATE
- fiif [ $file_count -gt 25 ]
- then
- echo "CRITICAL:" $file_count "files"
- exit $CRITICAL_STATE
- fi
- echo "OK:" $file_count "files"
- exit $OK_STATE
As you can see in the script a WARNING state will be returned if there are more than 15 files in the directory and a CRITICAL state will be returned if there are more than 25 files in the directory. If the file count is below 15 in the directory then an OK state is returned.
- Modify NRPE Configuration:Now a line needs to be added to the nrpe.cfg file on the server where you will be monitoring the file count in a specific directory. The line tells NRPE that when a specific variable is called to execute the bash script created in step one. Make sure that the line points to the proper directory and that NRPE has the permissions necessary to execute the script.
NRPE Configuration Addition: nrpe.cfg
- command[check_file_count]=/usr/local/nagios/libexec/check_file_count
- Add Nagios Service:You should already have a NRPE check_command configured in Nagios so you will only need to add a Nagios service and apply the service to the proper hosts. Below is an example showing how the service is applied to a host and references the check_nrpe command.
Example Nagios Service Applied To A Host:
- define service {
- service_description check_file_count
- use generic-service
- host_name some-server
- check_command check_nrpe!check_file_count
- contact_groups contact-users
- }
- Confirm & Verify Nagios Alerts:Now test to make sure things are working properly either by adding a large enough amount of files to the directory or modify the bash script temporarily to warn on a low amount of files. Instead of waiting for the Nagios cycle to process the requests you can test from the CLI using the check_nrpe command with the below syntax.
Test Check File Count Bash Script Using NRPE From CLI:
- [root@ali plugins]# ./check_nrpe -H dev.example.com -c check_file_count
That is all that needs to be done to monitor the number of files in any specific directory. You can obviously make small modifications to the script to modify the numbers of files in many sub directories or multiple directories so feel free to do so. If you have any feedback please provide in the below comments.
I am trying to monitoring Windows Server 2008 Directory from Nagios but getting following error: UNKNOWN: No handler for that command
any idea ?
Hello Abdul Waheed,
Not sure but since this is out of the scope of this article you may try asking the question on QD Engage where you can ask questions. It sounds as if it is simply a Nagios configuration error. If you find a solution without posting on Engage we would love to hear what it is.
Thanks.
alex
Have you installed NSCLIENT64++?..
is there any plugin which we can use easily to check windows file age and counts.. I found lot more stuff but none of them is easy to configure.. as easy as check_file_count..
any suggestion would be greatly appreciated.
Hello Ganesh,
I do not know of a specific plugin that does this though writing plugins for Nagios is fairly easy. My suggestion would be to expand something like check_file_count to accomodate your needs.
Thanks.
alex
Hi Alex,
I’ve found one good plugin check_file.pl @ itefix.no/i2/check_winfile it works fine with Fedora 15 but gives NULL with Fedora 16
Hello Ganesh,
I would be that the plugin just needs to be tweaked for Fedora 16. Unfortunately I don’t use Fedora or that plugin so its tough to say without spending time looking into it.
Let us know how you resolve it.
Thanks.
alex
Hi,
This perl script is another option. It outputs number of files as performance data as well.
#!/usr/bin/perl
#
# check_dir plugin for nagios
# Checks the number of certain file types in the specified directory.
#
# usage:
# check_dir -d [ -t ] -w -c
#
use warnings;
use strict;
use Time::Local;
use lib “/usr/lib/nagios/plugins” ;
use utils qw($TIMEOUT %ERRORS &print_revision &support);
use vars qw($PROGNAME);
my ($verbose,%prognum,$host,$prognum,$state);
my ($array_ref,$test,$element);
my ($opt_c,$opt_d,$opt_h,$opt_t,$opt_w);
my (@FILES,$no,$msg,$perfdata);
$state = ‘UNKNOWN’;
$PROGNAME = “check_dir”;
sub print_help ();
sub print_usage ();
sub in ($$);
$ENV{‘BASH_ENV’}=”;
$ENV{‘ENV’}=”;
$ENV{‘PATH’}=”;
$ENV{‘LC_ALL’}=’C’;
use Getopt::Long;
Getopt::Long::Configure(‘bundling’);
GetOptions(
“h” => \$opt_h, “help” => \$opt_h,
“d=s” => \$opt_d, “dir=s” => \$opt_d,
“t=s” => \$opt_t, “type=s” => \$opt_t,
“w=i” => \$opt_w, “warning=i” => \$opt_w, # warning if above this number
“c=i” => \$opt_c, “critical=i” => \$opt_c, # critical if above this number
“v” => \$verbose, “verbose” => \$verbose
);
# -h means display verbose help screen
if ($opt_h) { print_help(); exit $ERRORS{‘OK’}; }
## Supply warning and Critical levels
unless ( defined $opt_w && defined $opt_c ) {
print_usage();
exit $ERRORS{‘UNKNOWN’};
}
if ( $opt_w >= $opt_c) {
print “Warning (-w) must be lower than Critical (-c)!\n”;
exit $ERRORS{‘UNKNOWN’};
}
# -d tells us which directory to check
$opt_d = shift unless ($opt_d);
unless ($opt_d) { print_usage(); exit -1; }
if ( ( -e “$opt_d”) && ( -d “$opt_d” ) ) {
$prognum = $1;
} else {
print “ERROR: $!\n”;
exit $ERRORS{‘UNKNOWN’};
}
get_directory();
if($perfdata) {
print (“$msg|$perfdata\n”);
}else{
print (“$msg\n”);
}
exit $ERRORS{$state};
######## Subroutines ==========================
sub get_directory {
opendir(my $dh, “$opt_d”) or die “Error opening $opt_d: $!\n”;
if ( $opt_t ) {
$opt_t =~ tr/[*();\\\/]//d; # Remove unwanted characters
print “Files matching pattern: ‘$opt_t’\n” if $verbose;
@FILES = grep { /$opt_t/ && -f “$opt_d/$_” } readdir($dh); # Specific file type
$no = @FILES;
}else{
@FILES = grep {! /^\./ && -f “$opt_d/$_” } readdir($dh); # Regular files only
$no = @FILES;
}
if ($verbose) {
foreach (@FILES) {
print “$_\n”;
}
}
report();
close $dh;
}
sub report() {
if ($no >= $opt_c) {
$state = ‘CRITICAL’;
$msg = “$state: $no files found in directory ($opt_d)”;
$perfdata = “Files=$no;$opt_w;$opt_c;0”;
}elsif ($no >= $opt_w) {
$state = ‘WARNING’;
$msg = “$state: $no files found in directory ($opt_d)”;
$perfdata = “Files=$no;$opt_w;$opt_c;0”;
}else{
$state = ‘OK’;
$msg = “$state: $no files found in directory ($opt_d)”;
$perfdata = “Files=$no;$opt_w;$opt_c;0”;
}
}
sub print_usage () {
print “Usage: $PROGNAME -d directory -w warning -c critical [-t ] [-v verbose]\n”;
}
sub print_help () {
print_usage();
print “\n”;
print ” Checks the number of files in directory\n”;
print “-h (–help)\n”;
print “-v (–verbose) = debugging output\n”;
print “”;
print “\n\n”;
support();
}
sub in ($$) {
$array_ref = shift;
$test = shift;
while ( $element = shift @{$array_ref} ) {
if ($test eq $element) {
return 1;
}
}
return 0;
}