Ad Widget

Collapse

Nagios check wrapper script for zabbix

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • globifrosch
    Member
    • Sep 2005
    • 74

    #1

    Nagios check wrapper script for zabbix

    i wanted to use the check_mysql_health script in zabbix. wrote a little wrapper script which returns the data in zabbix usable format. it may work with other nagios checks too.

    - Thomas

    Code:
    #!/bin/sh
    # 
    # zabbix-nagios-wrapper <plugin> <args> (<state>|<value> <valuename>)
    #
    # Examples:
    # zabbix-nagios-wrapper check_mysql_health "--mode slave-lag" value slave_lag
    # <numeric return>
    #
    # zabbix-nagios-wrapper check_mysql_health "--mode slave-lag" state
    # <0=OK,1=WARNING,2=CRITICAL>
    #
    # Howto configure ZABBIX Agentd:
    # zabbix_agentd.conf: Add
    # UserParameter=zabbix-nagios-wrapper[*],<Path-to>/zabbix-nagios-wrapper "$1" "$2" "$3" "$4"
    # 1 = pluginname
    # 2 = args
    # 3 = state or value
    # 4 = name of value (only if 3 = value)
    #
    # Edit /etc/zabbix/nagios-wrapper.env to point env PLUGINDIR to the nagios plugins directory
    # Add any other env vars needed by checks to nagios-wrapper.env
    #
    # Limitation:
    # Don't know any way how to pass a , (comma) in the args field in zabbix webgui
    #
    # (c) Thomas Mueller <[email protected]>
    # License: GPL v3 (http://www.gnu.org/licenses/gpl-3.0.txt)
    
    test -f /etc/zabbix/nagios-wrapper.env && . /etc/zabbix/nagios-wrapper.env
    
    PLUGIN=$1
    ARGS=$2
    STATEORVALUE=$3
    VALUENAME=$4
    
    PLUGINDIR=${PLUGINDIR:=/usr/libexec}
    
    if [ -f "$PLUGINDIR/$PLUGIN" ]
    then
    	if [ "$STATEORVALUE" = "state" ]
    	then
    		state=$($PLUGINDIR/$PLUGIN $ARGS | cut -d" " -f1)
    		case "$state" in
    			OK) echo 0;;
    			WARNING) echo 1;;
    			CRITICAL) echo 2;;
    			*) echo "ERROR: not found OK, WARNING or CRITICAL";;
    		esac
    	elif [ "$STATEORVALUE" = "value" ]
    	then
    		values=$($PLUGINDIR/$PLUGIN $ARGS | cut -d\| -f2)
    
    		for i in $values
    		do 
    			if echo "$i" | grep $VALUENAME >/dev/null
    			then
    				value=$(echo $i | cut -f2 -d= | cut -f1 -d\; | egrep -o '[[:digit:]\.]+')
    				echo $value;
    				exit
    			fi
    		done
    	fi
    else
    	echo "ERROR: $PLUGINDIR/$PLUGIN not found" && exit 1
    fi
  • t2y
    Member
    • Feb 2009
    • 60

    #2
    Hi globifrosch,

    It's absorbing script!
    I have tried invoking that wrapper script.
    Here is my result, and I have a question.

    Code:
    # /usr/libexec/check_mysql_health --username root --password xxxxxxxx --database zabbix --mode uptime
    OK - database is up since 36 minutes | uptime=2208s
    # ./zabbix-nagios-wrapper.sh check_mysql_health "--username root --password xxxxxxxx --database zabbix --mode uptime" value uptime
    2209
    It works for me.

    Code:
    # /usr/libexec/check_mysql_health --username root --password xxxxxxxx --database zabbix --mode tablecache-hitrate
    CRITICAL - table cache hitrate 40.76% | tablecache_hitrate=40.76%;99:;95: tablecache_fillrate=100.00%
    # echo $?
    2
    It's displayed error message. I don't know the specification of the check_mysql_health script.
    In this case,

    Code:
    # ./zabbix-nagios-wrapper.sh check_mysql_health "--username root --password xxxxxxxx --database zabbix --mode tablecache-hitrate" value tablecache-hitrate
    # echo $?
    0
    that wrapper script displays nothing and the status code is zero. I think it's better to output error message which check_mysql_health displays. Or, it should change the status code.
    Last edited by t2y; 09-04-2009, 03:28.

    Comment

    • globifrosch
      Member
      • Sep 2005
      • 74

      #3
      thanks, i only tested OK state so far...

      here a new version of the script with better error handling.

      Code:
      #!/bin/sh
      # 
      # zabbix-nagios-wrapper <plugin> <args> (state|<value> <valuename>)
      #
      # Examples:
      # zabbix-nagios-wrapper check_mysql_health "--mode slave-lag" value slave_lag
      # <numeric return>
      #
      # zabbix-nagios-wrapper check_mysql_health "--mode slave-lag" state
      # <0=OK,1=WARNING,2=CRITICAL>
      #
      # Howto configure ZABBIX Agentd:
      # zabbix_agentd.conf: Add
      # UserParameter=zabbix-nagios-wrapper[*],<Path-to>/zabbix-nagios-wrapper "$1" "$2" "$3" "$4"
      # 1 = pluginname
      # 2 = args
      # 3 = state or value
      # 4 = name of value (only if 3 = value)
      #
      # Edit /etc/zabbix/nagios-wrapper.env to point env PLUGINDIR to the nagios plugins directory
      # Add any other env vars needed by checks to nagios-wrapper.env
      #
      # Limitation:
      # Don't know any way how to pass a , (comma) in the args field in zabbix webgui
      #
      # (c) Thomas Mueller <[email protected]>
      # License: GPL v3 (http://www.gnu.org/licenses/gpl-3.0.txt)
      
      test -f /etc/zabbix/nagios-wrapper.env && . /etc/zabbix/nagios-wrapper.env
      
      PLUGIN=$1
      ARGS=$2
      STATEORVALUE=$3
      VALUENAME=$4
      
      
      PLUGINDIR=${PLUGINDIR:=/usr/libexec}
      
      if [ -x "$PLUGINDIR/$PLUGIN" ]
      then
      	if [ "$STATEORVALUE" = "state" ]
      	then
      		"$PLUGINDIR/$PLUGIN" $(echo $ARGS) >/dev/null
      		ret=$?
                      if [ $ret -gt 2 ]
      		then
      			echo "ERROR: check exited with $ret (params: $PLUGIN , $ARGS , $STATEORVALUE , $VALUENAME)"
      			exit $ret
      		fi
      		echo $ret
      	elif [ "$STATEORVALUE" = "value" ]
      	then
      		if [ -z "$VALUENAME" ]
      		then
      			echo "ERROR: valuename is missing (params: $PLUGIN , $ARGS , $STATEORVALUE , $VALUENAME)"
      			exit 3
      		fi
      
      		values=$($PLUGINDIR/$PLUGIN $ARGS)
      		ret=$?
      
      		if [ $ret -gt 2 ]
                      then
                              echo "ERROR: check exited with $ret (params: $PLUGIN , $ARGS , $STATEORVALUE , $VALUENAME)"
                              exit $ret
                      fi
      
      		values=$(echo $values | cut -d\| -f2)
      
      		for i in $values
      		do 
      			if echo "$i" | grep "$VALUENAME=" >/dev/null
      			then
      				value=$(echo $i | cut -f2 -d= | cut -f1 -d\; | egrep -o '[[:digit:]\.]+')
      				echo $value;
      				exit
      			fi
      		done
      		echo ERROR: \"$VALUENAME\" not found
      		exit 3
      	fi
      else
      	echo "ERROR: $PLUGINDIR/$PLUGIN not found or not executable" && exit 3
      fi
      Last edited by globifrosch; 09-04-2009, 11:21.

      Comment

      • t2y
        Member
        • Feb 2009
        • 60

        #4
        Thank you for quick response. It's kindness for users!
        Code:
        # ./zabbix-nagios-wrapper.sh check_mysql_health "--username root --password xxxxxxxx --database zabbix --mode tablecache-hitrate" value tablecache-hitrate
        ERROR: "tablecache-hitrate" not found
        # echo $?
        3
        Last edited by t2y; 09-04-2009, 21:34.

        Comment

        • randybb
          Junior Member
          • Oct 2009
          • 1

          #5
          , -&gt; .

          # Limitation:
          # Don't know any way how to pass a , (comma) in the args field in zabbix webgui


          you can replace comma with dot.

          values=$($PLUGINDIR/$PLUGIN $ARGS | sed "s/\([0-9]\),\([0-9]\)/\1.\2/g")

          Comment

          Working...