LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Running a Perl script on nagios (https://www.linuxquestions.org/questions/linux-newbie-8/running-a-perl-script-on-nagios-945230/)

waelkd 05-16-2012 02:12 AM

Running a Perl script on nagios
 
Good day everyone,

I installed nagios on centreon platform, now I wrote a perl script that fetches data from specif date from an sql table,to make it run when needed ,my question is , where should i write ,implement this file? Any ideas on this guys?

Thanks

MensaWater 05-16-2012 08:37 AM

It depends on what server has the SQL and whether your script pulls the data locally or remotely.

Nagios can run commands and use output (a single line of output and a return code) for monitoring. If your perl script can be run from the Nagios master to talk to the SQL on a remote host (or if the SQL is on the same host as the Nagios master) then you can put the perl script on the master (typical location for most scripts/plugins is /usr/local/nagios/libexec). You can configure the commands.cfg file to have the run of the perl script with variables to be passed in (if any) then add the entry to services.cfg to call the commands.cfg entry and pass in any variables.

If however the perl code is designed to run on a remote host which has the SQL and you want your Nagios master to interrogate you'd need an agent on that remote host. The most common one used for UNIX/Linux is NRPE. For Windows I'd recommend NSClientPlus (nsclient++). You can then use the check_nrpe plugin on the master to interrogate those hosts.
For NRPE on the remote UNIX/Linux you'd add a line calling the command into /usr/local/nagios/etc/nrpe.cfg then on the master you'd set it up to call check_nrpe in commands.cfg and set services.cfg.

The perl script itself as noted needs to output only one line (Nagios doesn't understand multiline output) along with a return code. The defined return codes are common among all Nagios scripts.

An example perl with Nagios return codes defined is below:

Code:

#!/usr/bin/perl
#
######################################################################################################
# 020604 jda
# This script checks memory usage HP9000 boxes via SNMP
#
######################################################################################################
use Getopt::Long;
GetOptions( "H=s"                => \$hostname);
$snmpget = "/usr/bin/snmpget";                                  # define path to command

$target_community = "-v 2c -t 1 -r 9 -c public";
$total_memory = ".1.3.6.1.4.1.11.2.3.1.1.8.0";
$used_memory = ".1.3.6.1.4.1.11.2.3.1.1.7.0";

# ----------------------------------------Variable initialization----------------------------------- #
# Define Variables: These are used to return exit values to NAGIOS
$UNKNOWN_STATE=3;
$CRITICAL_STATE=2;
$WARNING_STATE=1;
$OK_STATE=0;

# -------------------------------------------------------------------------------------------------- #
@totmem = split(/= /, qx/$snmpget $target_community $hostname $total_memory/);  # shell out and do it
@totmem = split(/: /, $totmem[1]);                                              # strip it down further
@usedmem = split(/: /, qx/$snmpget $target_community $hostname $used_memory/);  # shell out and do it
chomp($totmem[1]);
chomp($usedmem[1]);
$totmem = int($totmem[1] / 1024);
$usedmem = int($usedmem[1]/1024);
$total = ($totmem - $usedmem);
print $totmem . " Total Memory, " . $usedmem . " Used, " . $total . "MB Free\n";

# -------------------------------------------------------------------------------------------------- #
exit $OK_STATE;                                        # tell nagios its ok



All times are GMT -5. The time now is 01:04 AM.