Page 1 of 4
Checking APC UPS battery runtime
Posted: Tue Dec 15, 2015 6:16 pm
by rdobbsmacu
I have created a script to monitor the my battery runtime on my APC USPs that converts the number the SNMP command returns into minutes. Here is the script I created.
#!/bin/bash
host=$1
runtime=$(/usr/local/nagios/libexec/check_snmp -H $host -C macu-pub -o .1.3.6.1.4.1.318.1.1.1.2.2.3.0|awk '{print $5}'| grep -o '[0-9]*')
min=$(expr $runtime / 6000)
a=27000
b=12000
##### IF else then begining #####
##### OK State #####
if (( "$runtime" > "$a" ));
then
echo "Current Battery Runtime $(($min)) Minutes""|calles=$runtime;27000;12000"
stateid=0
######Warning State ######
elif (( "$runtime" < "$a" ));
then
echo "Current_Battery_Runtime $(($min)) Minutes""|calles=$runtime;27000;12000"
stateid=1
######Critical State #####
elif (( "$runtime" < "$b$ ));
then
echo "Current_Battery_Runtime $(($min)) Minutes""|calles=$runtime;27000;12000"
stateid=2
##### End of If Else Then ######
fi
exit $stateid
This is what i'm getting from the command line of the nagios server.
expr: syntax error
./check_apc_runtime: line 27: unexpected EOF while looking for matching `"'
./check_apc_runtime: line 37: syntax error: unexpected end of file
Any thoughts on why i'm getting these syntax errors?
Thank you.
Re: Checking APC UPS battery runtime
Posted: Tue Dec 15, 2015 7:27 pm
by Box293
The main cause of this error was because on line 25, $b didn't have a closing double quote "
I use a text editor called Geany, and once I pasted the code into Geany and saved it as a .sh file, Geany added colour to the text which highlighted the problem pretty quickly.
After fixing that, I changed < > to -lt and -gt and replace round brackets with square brackets. It should work now:
Try this:
Code: Select all
#!/bin/bash
host=$1
runtime=$(/usr/local/nagios/libexec/check_snmp -H $host -C macu-pub -o .1.3.6.1.4.1.318.1.1.1.2.2.3.0|awk '{print $5}'| grep -o '[0-9]*')
min=$(expr $runtime / 6000)
a=27000
b=12000
##### IF else then begining #####
##### OK State #####
if [[ "$runtime" -gt "$a" ]]
then
echo "Current Battery Runtime $(($min)) Minutes""|calles=$runtime;27000;12000"
stateid=0
######Warning State ######
elif [[ "$runtime" -lt "$a" ]]
then
echo "Current_Battery_Runtime $(($min)) Minutes""|calles=$runtime;27000;12000"
stateid=1
######Critical State #####
elif [[ "$runtime" -lt "$b$" ]]
then
echo "Current_Battery_Runtime $(($min)) Minutes""|calles=$runtime;27000;12000"
stateid=2
##### End of If Else Then ######
fi
exit $stateid
Current Battery Runtime 47 Minutes|calles=282000;27000;12000
Re: Checking APC UPS battery runtime
Posted: Wed Dec 16, 2015 1:13 pm
by rdobbsmacu
That fix that problem but now i'm getting
expr: syntax error
Current_Battery_Runtime 0 Minutes|calles=;27000;12000
any thoughts on why or how to resolve this?
Re: Checking APC UPS battery runtime
Posted: Wed Dec 16, 2015 1:33 pm
by rkennedy
What is the command you are running that produces a syntax error?
Aditonally, what is the result if you run the check_snmp command?
Code: Select all
/usr/local/nagios/libexec/check_snmp -H $host -C macu-pub -o .1.3.6.1.4.1.318.1.1.1.2.2.3.0
Re: Checking APC UPS battery runtime
Posted: Wed Dec 16, 2015 1:39 pm
by rdobbsmacu
this is the output of that command
SNMP OK - Timeticks: (570000) 1:35:00.00 |
that is why I have this |awk '{print $5}'| grep -o '[0-9]*' at the end of the runitme=$ so runtime will only be a number so I can the math to convert it into minutes plus also have a number to make the state change in nagios.
Re: Checking APC UPS battery runtime
Posted: Wed Dec 16, 2015 1:50 pm
by rkennedy
What is the command you are running that produces a syntax error?
Can you provide what you're inputting to run against the bash script?
Re: Checking APC UPS battery runtime
Posted: Wed Dec 16, 2015 2:07 pm
by rdobbsmacu
the command i'm running is ./check_apc_runtime -H ipaddress
I can have also do
host=ipaddress
and run the command as so ./check_apc_runtime
and I get the same syntax error.
If i comment out the min= line and change the all the echo line from min to runtime I don't get the error message but i still get Current_Battery_Runtime 0 Minutes. I have also noticed that it looks like it is shipping the first if statement. the reason I that is i'm getting 570000 from the chcek_snmp command call and that number is greater than 27000. So I should be seeing a stateid of 0 but I'm getting a 1.
I can run echo $runtime right after the script completes and i get back 570000
Re: Checking APC UPS battery runtime
Posted: Wed Dec 16, 2015 2:14 pm
by rkennedy
rdobbsmacu wrote:the command i'm running is ./check_apc_runtime -H ipaddress
The script that you wrote is setting the $host variable from $1 actually. You'll need to run the script as ./check_apc_runtime ipaddress
See below for reference -
Code: Select all
#!/bin/bash
host=$1
runtime=$(/usr/local/nagios/libexec/check_snmp -H $host -C macu-pub -o .1.3.6.1.4.1.318.1.1.1.2.2.3.0|awk '{print $5}'| grep -o '[0-9]*')
Re: Checking APC UPS battery runtime
Posted: Wed Dec 16, 2015 4:47 pm
by rdobbsmacu
ok running it that why i'm getting back this
95
Current Battery Runtime 0 Minutes|calles=570000;27000;12000
and in nagios web interface i'm seeing this
apc_runtime.JPG
Re: Checking APC UPS battery runtime
Posted: Wed Dec 16, 2015 6:41 pm
by Box293
Looks like I had too many quotes in some sections.
Try this:
Code: Select all
#!/bin/bash
host=$1
runtime=$(/usr/local/nagios/libexec/check_snmp -H $host -C macu-pub -o .1.3.6.1.4.1.318.1.1.1.2.2.3.0|awk '{print $5}'| grep -o '[0-9]*')
min=$(expr $runtime / 6000)
a=27000
b=12000
##### IF else then begining #####
##### OK State #####
if [[ "$runtime" -gt "$a" ]]
then
echo "Current Battery Runtime $(($min)) Minutes|calles=$runtime;27000;12000"
stateid=0
######Warning State ######
elif [[ "$runtime" -lt "$a" ]]
then
echo "Current_Battery_Runtime $(($min)) Minutes|calles=$runtime;27000;12000"
stateid=1
######Critical State #####
elif [[ "$runtime" -lt "$b$" ]]
then
echo "Current_Battery_Runtime $(($min)) Minutes|calles=$runtime;27000;12000"
stateid=2
##### End of If Else Then ######
fi
exit $stateid