Checking APC UPS battery runtime

This support forum board is for support questions relating to Nagios XI, our flagship commercial network monitoring solution.
rdobbsmacu
Posts: 36
Joined: Tue Apr 28, 2015 3:11 pm

Checking APC UPS battery runtime

Post 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.
User avatar
Box293
Too Basu
Posts: 5126
Joined: Sun Feb 07, 2010 10:55 pm
Location: Deniliquin, Australia
Contact:

Re: Checking APC UPS battery runtime

Post 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
As of May 25th, 2018, all communications with Nagios Enterprises and its employees are covered under our new Privacy Policy.
rdobbsmacu
Posts: 36
Joined: Tue Apr 28, 2015 3:11 pm

Re: Checking APC UPS battery runtime

Post 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?
rkennedy
Posts: 6579
Joined: Mon Oct 05, 2015 11:45 am

Re: Checking APC UPS battery runtime

Post 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
Former Nagios Employee
rdobbsmacu
Posts: 36
Joined: Tue Apr 28, 2015 3:11 pm

Re: Checking APC UPS battery runtime

Post 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.
rkennedy
Posts: 6579
Joined: Mon Oct 05, 2015 11:45 am

Re: Checking APC UPS battery runtime

Post 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?
Former Nagios Employee
rdobbsmacu
Posts: 36
Joined: Tue Apr 28, 2015 3:11 pm

Re: Checking APC UPS battery runtime

Post 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
rkennedy
Posts: 6579
Joined: Mon Oct 05, 2015 11:45 am

Re: Checking APC UPS battery runtime

Post 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]*')
Former Nagios Employee
rdobbsmacu
Posts: 36
Joined: Tue Apr 28, 2015 3:11 pm

Re: Checking APC UPS battery runtime

Post 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
You do not have the required permissions to view the files attached to this post.
User avatar
Box293
Too Basu
Posts: 5126
Joined: Sun Feb 07, 2010 10:55 pm
Location: Deniliquin, Australia
Contact:

Re: Checking APC UPS battery runtime

Post 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
As of May 25th, 2018, all communications with Nagios Enterprises and its employees are covered under our new Privacy Policy.
Locked