1.  服务器写如下脚本:放到/usr/local/nagios/libexec下:

 

#!/bin/bash
#
PROGNAME=`basename $0`
VERSION="Version 1.0,"
AUTHOR="2012, u (http://www.nianw.com/) yangpenghao666@gmail.com"

#exit status
ST_OK=0
ST_WR=1
ST_CR=2
ST_UK=3

function PrintHelp(){
        echo "A This Program is a plug  of nagios to monitor WebServer , special for Nginx!";
        echo "This Program base on  WebServer's respond status report an emergency ";
        echo "It usefull on nginx repond 502 and 504 status";
        echo "$AUTHOR";
        echo "How to use Eg 1";
        echo "$PROGNAME --url http://www.nianw.com/index.php";
        echo "How to use Eg 2";
        echo "$PROGNAME -host www.nianw.com --url ip ;
        echo "www.nianw.com and 211.147.15.153 replace of you site and IP!"
}

while test -n "$1";do
        case "$1" in
                --help|-h)
                        PrintHelp
                        exit $ST_UK
                        ;;
                --url|-u)
                        URL=$2
                        shift
                        ;;
                --host|-H)
                        HOST=$2
                        shift
                        ;;
                *)
                        echo "fail"
                        exit $ST_UK
                        ;;
        esac
        shift
done

if [  -n "$HOSTNAME" ] ;then
        echo 1111111111
        HTTP_STATUS=`curl -s -I  "$URL" |head -n1|awk '{print $2}'`
else
        echo 2222222222
        HTTP_STATUS=`curl -s -I host:$HOSTNAME "$URL"|head -n1|awk '{print $2}'`
fi
if [ ! -n "$HTTP_STATUS" ] ;then
                echo 44444444444444
        HTTP_STATUS="CONNET_ERROR"
fi
case $HTTP_STATUS in
        200)
        echo "OK HTTP $HTTP_STATUS ";
        exit $ST_OK;
        ;;
        500|502|504|CONNET_ERROR)
        echo "CRITICAL $HTTP_STATUS $URL";
        exit $ST_CR;
        ;;
        *)
        echo "WARNING $HTTP_STATUS";
        exit $ST_WR
        ;;
esac
 

 

 

2.  /usr/local/nagios/etc/objects/command.cfg

添加一个

define command{
        command_name    u_check_nginx.sh
        command_line    $USER1$/u_check_nginx.sh --url $ARG1$ --host $ARG2$
}
 

3. /usr/local/nagios/etc/objects/services.cfg

添加一个

define service {
        host_name       yang-50
        service_description     check-50-nginx-www.nianw.com
        check_period            24x7
        normal_check_interval   2
        retry_check_interval    1
        max_check_attempts      5
        notification_period     24x7
        notification_options    w,u,c,r
        check_command           u_check_nginx.sh!http://www.nianw.com/
}

4. /usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg测试是否有错误。

5. 如果没有错误就重启服务/etc/init.d/nagios restart