Need wrapper around mysql query

Hello Friends,

I am using check_mysql_query plugin in nagios to query database and get output. query is working fine but output which i am getting contain query. I want to remove query from output and give custom message which will be simple and understandable.

Can you help me here with any bash script or wrapper to use around to avoid query in output.
below is my query output.

QUERY OK: 'SELECT 0 as elapsed union SELECT avg(elapsed) as elapsed  FROM elapsed_total where datetime_a > adddate(now(),INTERVAL -1200 minute) order by 1 desc limit 1' returned 0.000000 | result=0.000000;1800.000000;3600.000000;

Hi,

Depending on the version of check_mysql_query you're running, you might not need a wrapper necessarily. If you check the man page, you'll see there are a few flags that might be helpful here, such as:

-m --message Message to output after result. Can take a printf string with a single substitution (defaults to 'query returned') 
-n --message-prepend Display message before rather than after result (prepend)
-s --short Shorten output, do not output message just result 

So you might very well be able to define your own custom output format or use the short output form to avoid having the query returned in the plugin output, if you're running a recent enough version of the plugin.

Hope this helps. If not, then there are other options, such (as you say) wrapping the whole thing in a Bash script, but with any luck just by using the appropriate flags you can avoid going down that road.

currently its running with
heck_mysql_query v2.1.1 (nagios-plugins 2.1.1). its does not have above option.

any idea how to use wrapper here.

Hi,

OK, you could generally wrap this in a script by doing something like this.

#!/bin/bash

check_mysql_query.pl <whatever your arguments are> >/dev/null 2>/dev/null
returned=$?

if [ "$returned" == "0" ]
then
     echo �OK�
     exit 0
elif [ "$returned" == "1" ]
then
     echo �WARNING�
     exit 1
elif [ "$returned" == "2" ]
then
     echo �CRITICAL�
     exit 2
else
     echo "UNKNOWN"
     exit 3
fi

You don�t strictly need to capture $? into $returned here, I just did it to make things a bit more readable for you.

So the idea is:

  1. Run your plugin
  2. Capture the return code
  3. Respond accordingly

Hope this helps.

Well, you do, if you want to compare it more than once, since $? is overwritten every time you execute a program or statement.

This is an ideal place to use a case statement though:

case "$?" in
0) # statement zero code
   # statement zero code
   ;;
1) # statement one code
   # statement one code
   ;;
2) # statement two code
   # statement two code
   ;;
*) # All other cases
   # All other cases
   ;;
esac
1 Like

Thanks for reply. its solved my purpose.