Template di un plugin Nagios in Bash

From RVM Wiki
Revision as of 11:41, 8 October 2015 by Gabriele.vivinetto (talk | contribs) (Created page with "Questo template supporta getopt: <pre> #!/bin/bash # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public ...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Questo template supporta getopt:

#!/bin/bash

#   This program is free software; you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation; version 2 of the License only.
#
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#
#   You should have received a copy of the GNU General Public License
#   along with this program; if not, write to the Free Software
#   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

PROGNAME=`basename $0`
VERSION="Version 1.0"
AUTHOR="2011, Tyler Allen (http://www.the-tech-tutorial.com/)"

LV_W=100;
LV_C=100;


#Print Version
print_version() {
    echo "$VERSION $AUTHOR"
}

#Print Help
usage(){
        echo $PROGNAME $VERSION
        echo $AUTHOR
	echo 
	echo This is a Nagios plugin that will check the curremt memory usage of the system.
	echo 
	echo OPTIONS:
	echo  -h Shows this help
	echo  -v Shows the Version
	echo  -w sets the warning level
	echo  -c sets the critical level
}

#Parmature Getter
while getopts "hvw:c:" opt; do
  case $opt in
    h)
      usage
      exit
      ;;
    v)
      print_version
      exit
      ;;
    w)
      LV_W=$OPTARG
      ;;
    c)
      LV_C=$OPTARG 
      ;;

    \?)
      echo "Invalid option: -$OPTARG" >&2
      exit 1
      ;;
    :)
      echo "Option -$OPTARG requires an argument." >&2
      exit 1
      ;;
  esac
done

#Check the diffrence between the LV values
    if [ ${LV_W} -gt ${LV_C} ]
    then
        echo "Please adjust levels. The critical level must be higher than the warning level!"
	exit 666
    fi

#Values must be between 0 and 100
    if [ "$LV_W" -lt 0 -o "$LV_W" -gt 100 -o "$LV_C" -lt 0 -o "$LV_C" -gt 100 ]
    then
	echo "Warning and critical level values must be between 0 and 100."
    	exit 666
    fi

#Get the figures
MEM_TOTAL=`grep "^MemTotal" /proc/meminfo|awk '{print $2}'`
TMP_MEM_FREE=`grep "^MemFree" /proc/meminfo|awk '{print $2}'`
TMP_MEM_USED=`expr $MEM_TOTAL - $TMP_MEM_FREE`
BUFFERS=`grep "^Buffers" /proc/meminfo|awk '{print $2}'`
CACHED=`grep "^Cached" /proc/meminfo|awk '{print $2}'`

P_MEM_FREE=`echo "scale=2; $TMP_MEM_FREE / $MEM_TOTAL * 100" | bc -l | sed 's/.[0-9][0-9]//'`
P_MEM_USED=`echo "scale=0; 100 - $P_MEM_FREE" | bc -l`

if [ ! -z "$LV_W" -a ! -z "$LV_C" ]
    then
        if [ ${P_MEM_USED} -ge ${LV_W} -a ${P_MEM_USED} -lt ${LV_C} ]
        then
            echo "WARNING - Used: $P_MEM_USED%, Free: $P_MEM_FREE% | 'mem_used'=$P_MEM_USED;$LV_W;$LV_C 'mem_free'=$P_MEM_FREE"
            exit $ST_WR
        elif [ ${P_MEM_USED} -ge ${LV_C} ]
        then
            echo "CRITICAL - Used: $P_MEM_USED%, Free: $P_MEM_FREE% | 'mem_used'=$P_MEM_USED;$LV_W;$LV_C 'mem_free'=$P_MEM_FREE"
            exit $ST_CR
        else
            echo "OK - Used: $P_MEM_USED%, Free: $P_MEM_FREE% | 'mem_used'=$P_MEM_USED;$LV_W;$LV_C 'mem_free'=$P_MEM_FREE"
            exit $ST_OK
        fi
    else
            echo "OK - Used: $P_MEM_USED%, Free: $P_MEM_FREE% | 'mem_used'=$P_MEM_USED 'mem_free'=$P_MEM_FREE"
            exit $ST_OK
    fi