熬死你的 发表于 2018-11-8 07:26:12

nagios监控nginx status-linuxer

  
#!/bin/bash
  
PROGNAME=`basename $0`
  
VERSION="Version 1.0"
  
AUTHOR="2010.11.18-www.nginxs.com"
  

  
ST_OK=0
  
ST_WR=1
  
ST_CR=2
  
ST_UK=3
  

  
print_version() {
  
      echo "$VERSION $AUTHOR"
  
}
  

  
print_help() {
  
      print_version $PROGNAME$VERSION
  
      echo "$PROGNAME is a Nagios plugin to monitor nginx status"
  
      echo "Use of wget nginxstatus page"
  
      echo "When using optional warning/critical thresholds all values except"
  
      echo "Usage parameters:"
  
      echo ""
  
      echo "$PROGNAME [-u|--url] [-p|--path] [-w/--warning] [-c/--critical]"
  
      echo ""
  
      echo "Options:"
  
                echo "--url|-u)"
  
                echo "       Sets nginx status url"
  
                echo ""
  
                echo "--path|-p)"
  
                echo "       Sets nginx status url path"
  
                echo ""
  
                echo "--warning|-w)"
  
                echo "       Sets a warning level for nginx Active connections. Default is: off"
  
                echo ""
  
                echo "--critical|-c)"
  
                echo "       Sets a critical level for nginx Active connections. Default is: off"
  
      echo ""
  
      echo "Example:"
  
      echo "http://www.nginxs.com/status"
  
      echo "./check_nginx.sh -u www.nginxs.com -p /status -w 10000 -c 15000"
  
      exit $ST_UK
  
}
  

  
while test -n "$1";do
  
   case "$1" in
  
      --help|-h)
  
                print_help
  
                exit $ST_UK
  
                ;;
  
      --url|-u)
  
                url=$2
  
                shift
  
                ;;
  
      --path|-p)
  
                path=$2
  
                shift
  
                ;;
  
      --warning|-w)
  
                warn=$2
  
                shift
  
                ;;
  
      --critical|-c)
  
                crit=$2
  
                shift
  
                ;;
  
      *)
  
                echo "Unknown argument: $1"
  
                print_help
  
                exit $ST_UK
  
                ;;
  
    esac
  
    shift
  
done
  

  
if [ -z $url ];then
  
      echo "Must Sets --url|-u) Parameters"
  
      exit $ST_UK
  
elif [ -z $path ];then
  
      echo "Must sets --path|-p) Parameters"
  
      echo "Please look help"
  
      echo "$PROGNAME --help"
  
      exit $ST_UK
  
fi
  

  
if [ -n "$warn" -a -n "$crit" ];then
  
      if [ $warn -ge $crit ];then
  
                echo "Please adjust your warning/critical thresholds. The warning must be lower than the critical level!"
  
                exit $ST_UK
  
      fi
  
fi
  

  
do_status() {
  
wget -qNO /tmp/nginx.html ${url}${path}
  
ActiveConn=`awk -F: {'print $2'} /tmp/nginx.html |head -1`
  
serveraccepts=`awk {'print $1'} /tmp/nginx.html |tail -n 2|head -1`
  
handled=`awk {'print $2'} /tmp/nginx.html |tail -n 2|head -1`
  
requests=`awk {'print $3'} /tmp/nginx.html |tail -n 2|head -1`
  
reading=`tail -n 1 /tmp/nginx.html|awk {'print $2'}`
  
writing=`tail -n 1 /tmp/nginx.html|awk {'print $4'}`
  
waiting=`tail -n 1 /tmp/nginx.html|awk {'print $6'}`
  
}
  
do_output() {
  
      output="ActiveConn:${ActiveConn},serveraccepts:${serveraccepts},handled:${handled},requests:${requests},reading:${reading},writing:${writing},waiting:${waiting}"
  
}
  
do_perfdata() {
  
      perfdata="'ActiveConn'=${ActiveConn},'serveraccepts'=${serveraccepts},'handled'=${handled},'requests'=${requests},'reading'=${reading},'writing'=${writing},'waiting'=${waiting}"
  
}
  

  
do_status
  
do_output
  
do_perfdata
  

  
if [ -n "warn" -a -n "$crit" ];then
  
      if [ $ActiveConn -ge $warn -a $ActiveConn -lt $crit ];then
  
                echo"WARNING - $output |$perfdata"
  
                exit $ST_WR
  
      elif [ $ActiveConn -ge $crit ];then
  
                echo"CRITICAL - $output|$perfdata"
  
                exit $ST_CR
  
      else
  
                echo"OK - $output|$perfdata"
  
                exit $ST_OK
  
      fi
  
else
  
      echo "OK - $output|$perfdata"
  
      exit $ST_OK
  
fi


页: [1]
查看完整版本: nagios监控nginx status-linuxer