Documentation forServer & Application Monitor
Monitoring your applications and environment is a key capability of Hybrid Cloud Observability and is also available in a standalone module, Server & Application Monitor (SAM). Hybrid Cloud Observability and SAM are built on the self-hosted SolarWinds Platform.

Nagios Linux File & Directory Count Script

This SAM application monitor template uses a Nagios Script Monitor to provide file and directory count statistics using multiple value script outputs.

Works with the SolarWinds Platform agents for Linux and AIX.

Prerequisites

SSH and Perl installed on the target server.

Credentials

Root credentials on the target server.

Prerequisites for SolarWinds Platform 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.

To use this component monitor with Nagios, as polled by the SolarWinds Platform agent for Linux, enter a script as follows. For the script argument, use python $(SCRIPT).

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)))

Component monitors

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.

To use this component monitor with Nagios, as polled by the SolarWinds Platform agent for Linux, enter a script as follows. For the script argument, use python $(SCRIPT).

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)))

A maximum of 10 output pairs can be returned. The monitor in this template uses 3. If you exceed the maximum allowed, remove the excess output pairs or they will be ignored. For details, see Write scripts for SAM script monitors.

Field descriptions

Description

Provide a description as to what this monitor will actually do for easy reference.

Component Type

The type of monitor you are using.

Enable Component

Determines if 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.

Click a credential in the list, or use the <Inherit credential from node> option. If the credential you need is not in the credentials list, add it in the Credentials Library that you can access by clicking Settings > All Settings > SAM Settingsin the SolarWinds Platform Web Console.

Port Number

Specify the port number used for the SSH connection. The default value is 22.

Script Working Directory

Specify the working directory of the script process.

Check Type

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

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

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 can 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

Choose how you want the monitor to report based on the output provided by the script. The default selection is “Show worst status.”

User Notes

Add notes for easy reference. You can access this field by using the variable, ${UserNotes}.