References > Template Reference > Nagios Linux File & Directory Count Script

Nagios Linux File & Directory Count Script

This template uses one Nagios script monitor to provide file and directory count statistics using multiple value script outputs.

Works with the Orion Agent for Linux.

Prerequisites

SSH and Perl installed on the target server.

Credentials

Root credentials on the target server.

A maximum of 10 output pairs can be returned. The monitor in this template uses three.

Prerequisites for Orion agent for Linux

This template uses the Nagios Script monitor. You need to include credentials that can run scripts on the monitored Linux-based computer. Agentless monitoring does not require these credentials.

If you are using this component monitor with Nagios and polled by Orion agent for Linux, you need to enter a script as follows. The argument would be python $(SCRIPT) with a body of:

from random import randint
STAT_PAIRS_COUNT = 10
 
for stat_id in range(STAT_PAIRS_COUNT):
	print 'Statistic.random%d: %d\nMessage.random%d: %s' % (stat_id, randint(1,100), stat_id, 'random number: ' + str(randint(1,100)))

For details on the component monitor, see Nagios Script monitor.

Monitored Components

For details on monitors, see SAM Component Monitor Types.

File & Directory Count Monitor

This component monitor uses SSH to upload a script to a server, runs the script, then processes the script exit code and text output. Statistic: Defined by the script. This component monitor has the ability to return multiple results and has the ability to process Nagios resulting output.

If you are using this component monitor with Nagios and polled by Orion agent for Linux, you need to enter a script as follows. The argument would be python $(SCRIPT) with a body of:

from random import randint
STAT_PAIRS_COUNT = 10
 
for stat_id in range(STAT_PAIRS_COUNT):
	print 'Statistic.random%d: %d\nMessage.random%d: %s' % (stat_id, randint(1,100), stat_id, 'random number: ' + str(randint(1,100)))

Field Descriptions

Description

This field allows you to provide a description as to what this monitor will actually do for easy reference.

Component Type

This describes the type of monitor you are using.

Enable Component

This option determines whether or not the component is enabled. Disabling this component leaves it in the application as deactivated and does not influence application availability or status.

Credential for Monitoring

Select a credential that can access the Linux or Unix server over SSH and that has sufficient rights to run scripts. If the credential you need is not already present in the credentials list, use the Quick Credentials section to add a new credential.

Port Number

This field allows you to specify the port number used for the SSH connection. The default value is 22.

Script Working Directory

This field allows you to specify the working directory of the script process.

Check Type

This determines how the return code is evaluated.

Return Code

Service State

Host State

0

Ok

Up

1

Warning

Up or Down/Unreachable†

2

Critical

Down/Unreachable

3

Unknown

Down/Unreachable

† If the Use Aggressive Host Checking option is enabled, return codes of 1 will result in a host state of DOWN, otherwise return codes of 1 will result in a host state of UP.

Command Line

This field is in the script editing window and allows you to specify the script you want to run on the target node followed by the arguments. To enter your script, click the Edit button that reveals the script editing window. For more information, see http://nagios.sourceforge.net/docs/3_0/pluginapi.html.

In this template, the following code is used in the Command Line field:perl ${SCRIPT} /etc -sub

Body

This field is in the script editing window and allows you to enter your script via typing or pasting. In this template, the following code is used in the Script Body field to provide three script outputs:

Parameters:

  • directory - This is the path of the directory (e.g. /bin,/etc,/usr/local,/).
  • -sub - Include subdirectories. This parameter is optional.

This Perl script counts the number of files, directories, and symbolic links in a given directory.

#!/usr/bin/perlif ($#ARGV != 0 && $#ARGV != 1) 
	{ 
		print "Invalid arguments count. Usage: perl FileCount.pl directory\n"; 
		exit 3;
	}
$dir=$ARGV[0];
$sub="";
if ($#ARGV == 1) 
	{ 
		$sub=$ARGV[1];
	}
if ($sub ne "-sub") 
	{ 
		$fcount=`find $dir -maxdepth 1 -type f | wc -l`; 
		$dcount=`find $dir -maxdepth 1 -type d | wc -l`; 
		$lcount=`find $dir -maxdepth 1 -type l | wc -l`;
	}
else 
	{ 
		$fcount=`find $dir -depth -type f | wc -l`; 
		$dcount=`find $dir -depth -type d | wc -l`; 
		$lcount=`find $dir -depth -type l | wc -l`;
	}
$exit=`echo $?`;
				
#remove the last character (new line symbol)
chop($fcount);
chop($dcount);
chop($lcount);
				
#find list also parent directory, so we need to remove them
$dcount=$dcount-1;
print "Script OK - Directory: $dir FileCount: $fcount DirectoryCount: $dcount SymbolicLinkCount: $lcount | 'FileCount'=$fcount 'DirectoryCount'=$dcount 'SymbolicLinkCount'=$lcount\n";
exit 0;

Script Output #X

Each script output has the following fields:

  • Unique ID - This is the statistic identifier from the script output.
  • Display Name - This is the name displayed in the User Interface. After generation of the script output, the Unique ID and Display Name become the same. You can change the Display Name to something more user friendly if you so desire.
  • Convert Value - If this option is selected, you can convert your output with a variety of mathematical functions to suit your needs using either a selection from the drop down menu or selecting Custom Conversion. With the Custom Conversion option, you have the ability to enter your own formulas to manipulate the data output further.
  • Warning threshold - Select a threshold level in conjunction with the logical operators to alert you when the warning threshold has been breached.
  • Critical Threshold - Select a threshold level in conjunction with the logical operators to alert you when the critical threshold has been breached.

Status Roll-Up

This option allows you to choose how you would like the monitor to report based on the output provided by the script. The default selection is “Show worst status.”

User Notes

This field allows you to add notes for easy reference. You can access this field by using the variable, ${UserNotes}.