Trying to Use Arguments in Script
Posted: Fri Jan 31, 2014 12:28 pm
I’m still trying to get the hand of Nagios. Thankfully, this forum has been really helpful in research. Unfortunately my issue is probably so basic, that I cannot find many information on my particular issue.
When it comes, to arguments, I understand that they must be declared in define command, and can be defined in define service.
Like the following:
(Hopefully the –s applies to $ARG3$) My question is how would I implement those arguments into my script. Would I just use $ARG1$ in my script to get the defined values for I can use to compare in order to return a Status. I have successfully been able to get my script to work, hardcoding values, but I know things can be easier if I can understand how to implement the arguments into my script.
I provided a sample of my script, commenting where I hardcoded by values, to show what I am trying to do.
When it comes, to arguments, I understand that they must be declared in define command, and can be defined in define service.
Like the following:
Code: Select all
define command{
command_name WeatherGooseII.pl
command_line $USER1$/WeatherGooseII.pl –H $HOSTADDRESS$ c- $ARG1$ w- $ARG2$ s- $ARG3$
}
define service{
use generic-service
host_name localhost
description Check Calculus Temperature
check_command WeatherGooseII.pl!90!75!TempC
}
I provided a sample of my script, commenting where I hardcoded by values, to show what I am trying to do.
Code: Select all
#!/usr/bin/perl
use strict;
use warnings;
use Data::Dumper;
use XML::Simple;
sub read_xml {
my $file = shift;
my $xml = XML::Simple->new();
my $data = XMLin( $file );
return $data;
}
my $xml = read_xml( '/var/www/html/data.xml' );
my $fieldTempC = "TempC"; //hardcoded TempC
my $tempCValue = $xml->{'devices'}->{'device'}->{'field'}->{'TempC'}->{'value'}; //hardcoded TempC
if($tempCValue >= 90) //hardcoded critical value
{
print "CRITICAL: ", $fieldTempC, " is at or above 90";
print "Current Value: ", $tempCValue;
exit 2;
}
elsif($tempCValue >= 75) //hardcoded warning value
{
print "WARNING: ", $fieldTempC, " is nearing the max temperature of 75";
print "Current Value: ", $tempCValue;
}
exit 1;
}
elsif ($tempCValue < 75) //hardcoded TempC and warning value
{
print $fieldTempC, " is OK";
exit 0;
}
else
{
print "Unknown arguments\n";
print $tempCValue;
exit 3;
}