Nagios 监控内存脚本

 #!/bin/bash

STATE_OK=0
STATE_WARING=1
STAT_CRITICAL=2
STAT_UNKNOWN=3
USAGE_Method="$(basename $0) [-w|--warning] <Free Percent> [-c|--critical] <Free Percent>"
if [ $# -lt 4 ]
then
 echo
 echo "Usage:$USAGE_Method"
 exit 0
fi
if [ $# -gt 0 -a $# -eq 4 ]
then
 if [ "$1" == "-w" -a "$3" == "-c" ]
 then
  WARING_VALUE=$2
  CRITICAL_VALUE=$4
 else
  echo "Usage:$USAGE_Method"
 fi
 
fi

if [ $WARING_VALUE -le $CRITICAL_VALUE ]
then
 echo
 echo "Usage:$USAGE_Method"
 echo
 exit 0
fi

FREE_MEM=$(free -m |grep -| awk -F ' ' '{print $4}')
TOTAL_MEM=$(free -m | grep Mem | awk -F ' ' '{print $2}')
PERCENT=$(bc <<< "scale=2;$FREE_MEM/$TOTAL_MEM" | sed 's/^.//g')

if [ $PERCENT -le $CRITICAL_VALUE ]
then
 echo "THE MEM is Free $PERCENT%"
 exit 2
fi

if [ $PERCENT -le $WARING_VALUE ]
then
 echo "THE MEM is Free $PERCENT%"
 exit 1
fi


if [ $PERCENT -gt $CRITICAL_VALUE ]
then
        echo "THE MEM is Free $PERCENT%"
        exit 0
fi

你可能感兴趣的:(内存,监控)