8870188 发表于 2019-1-16 14:18:51

Nagios监控内存脚本

1. 脚本如下 check_mem.sh  # Script to check real memory usage
  # L.Gill 02/05/06 - V.1.0
  # ------------------------------------------
  # ########Script Modifications##########
  # ------------------------------------------
  # Who         When         What
  # ---    ----      ----
  # LGill         17/05/06"$percent" lt 1% fix - sed edits dc result beggining with "."
  #
  #
  #!/bin/bash
  USAGE="`basename $0` [-w|--warning] [-c|--critical]"
  THRESHOLD_USAGE="WARNING threshold must be greater than CRITICAL: `basename $0` $*"
  calc=/tmp/memcalc
  percent_free=/tmp/mempercent
  critical=""
  warning=""
  STATE_OK=0
  STATE_WARNING=1
  STATE_CRITICAL=2
  STATE_UNKNOWN=3
  # print usage
  if [[ $# -lt 4 ]]
  then
  echo ""
  echo "Wrong Syntax: `basename $0` $*"
  echo ""
  echo "Usage: $USAGE"
  echo ""
  exit 0
  fi
  # read input
  while [[ $# -gt 0 ]]
  do
  case "$1" in
  -w|--warning)
  shift
  warning=$1
  ;;
  -c|--critical)
  shift
  critical=$1
  ;;
  esac
  shift
  done
  # verify input
  if [[ $warning -eq $critical || $warning -lt $critical ]]
  then
  echo ""
  echo "$THRESHOLD_USAGE"
  echo ""
  echo "Usage: $USAGE"
  echo ""
  exit 0
  fi
  # Total memory available
  total=`free -m | head -2 |tail -1 |gawk '{print $2}'`
  # Total memory used
  used=`free -m | head -2 |tail -1 |gawk '{print $3}'`
  # Calc total minus used
  free=`free -m | head -2 |tail -1 |gawk '{print $2-$3}'`
  # normal values
  #echo "$total"MB total
  #echo "$used"MB used
  #echo "$free"MB free
  # make it into % percent free = ((free mem / total mem) * 100)
  echo "5" > $calc # decimal accuracy
  echo "k" >> $calc # commit
  echo "100" >> $calc # multiply
  echo "$free" >> $calc # division integer
  echo "$total" >> $calc # division integer
  echo "/" >> $calc # division sign
  echo "*" >> $calc # multiplication sign
  echo "p" >> $calc # print
  percent=`/usr/bin/dc $calc|/bin/sed 's/^\./0./'|/usr/bin/tr "." " "|/usr/bin/gawk {'print $1'}`
  #percent1=`/usr/bin/dc $calc`
  #echo "$percent1"
  if [[ "$percent" -le$critical ]]
  then
  echo "CRITICAL - $free MB ($percent%) Free Memory"
  exit 2
  fi
  if [[ "$percent" -le$warning ]]
  then
  echo "WARNING - $free MB ($percent%) Free Memory"
  exit 1
  fi
  if [[ "$percent" -gt$warning ]]
  then
  echo "OK - $free MB ($percent%) Free Memory"
  exit 0
  fi
  2. modify /usr/local/nagios/etc/command.cfg
  # 'check_mem' command definition
  define command{
  command_name    check_mem
  command_line    $USER1$/check_mem.sh -H $HOSTADDRESS$ -w $ARG1$ -c $ARG2$ -p $ARG1$
  }
  3. define service{
  use                           generic-service         ; Name of service template to use
  host_name                     192.168.31.130
  service_description             check_mem
  is_volatile                     0
  check_period                  24x7
  max_check_attempts            4
  normal_check_interval         5
  retry_check_interval            1
  contact_groups                  nagios
  notification_options            w,u,c,r
  notification_interval         960
  notification_period             24x7
  check_command                   check_mem!20!5
  }
  4.以上可以监控本地的内存。
  5.这是网上找的内存监控脚本,从脚本看,作者并不了解linux内存的使用原理,LINUX平的有效内存不是看第二行,而是看第三行,需要对脚本修改
  另外怎样查看其他机器的内存使用情况 ?

页: [1]
查看完整版本: Nagios监控内存脚本