################################################################################初始化环境(默认环境已经安装好mysql mysql-server mysql-devel以及development tools)yum install gd gd-devel gcc gcc-c++ autoconf libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel zlib zlib-devel glibc glibc-devel glib2 glib2-devel bzip2 bzip2-devel ncurses ncurses-devel curl curl-devel e2fsprogs e2fsprogs-devel krb5-devel libidn libidn-devel openssl httpd openssl-devel bison bison-devel readline readline-devel make apr* autoconf automake curl-devel gcc gcc-c++ openssl openssl-devel pcre-devel keyutils perl compat* mpfr cpp glibc libgomp libstdc++-devel ppl cloog-ppl keyutils-libs-devel libcom_err-devel libsepol-devel libselinux-devel libXpm* freetype libjpeg* libpng* ncurses* libtool* libxml2 libxml2-devel patch libevent lighttpd-fastcgi php* cmake############################################################################添加nginx使用组与账号groupadd wwwuseradd -g www www -s /bin/false安装nginxtar zxvf nginx-1.4.3.tar.gzcd nginx-1.4.3./configure --prefix=/usr/local/webserver/nginx --user=www --group=www --with-file-aio --with-http_stub_status_module --with-http_gzip_static_module --with-http_flv_module --with-http_ssl_module --with-http_ssl_modulemake && make install制作nginx启动脚本vim /etc/rc.d/init.d/nginx#!/bin/sh## nginx - this script starts and stops the nginx daemon# chkconfig: - 85 15# description: Nginx is an HTTP(S) server, HTTP(S) reverse# proxy and IMAP/POP3 proxy server# processname: nginx# config: /usr/local/webserver/nginx/conf/nginx.conf# config: /etc/sysconfig/nginx# pidfile: /usr/local/webserver/nginx/logs/nginx.pid# Source function library.. /etc/rc.d/init.d/functions# Source networking configuration.. /etc/sysconfig/network# Check that networking is up.[ "$NETWORKING" = "no" ] && exit 0nginx="/usr/local/webserver/nginx/sbin/nginx"prog=$(basename $nginx)NGINX_CONF_FILE="/usr/local/webserver/nginx/conf/nginx.conf"[ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginxlockfile=/var/lock/subsys/nginxmake_dirs() {# make required directoriesuser=`nginx -V 2>&1 | grep "configure arguments:" | sed 's/[^*]*--user=([^ ]*).*/1/g' -`options=`$nginx -V 2>&1 | grep 'configure arguments:'`for opt in $options; doif [ `echo $opt | grep '.*-temp-path'` ]; thenvalue=`echo $opt | cut -d "=" -f 2`if [ ! -d "$value" ]; then# echo "creating" $valuemkdir -p $value && chown -R $user $valuefifidone}start() {[ -x $nginx ] || exit 5[ -f $NGINX_CONF_FILE ] || exit 6make_dirsecho -n $"Starting $prog: "daemon $nginx -c $NGINX_CONF_FILEretval=$?echo[ $retval -eq 0 ] && touch $lockfilereturn $retval}stop() {echo -n $"Stopping $prog: "killproc $prog -QUITretval=$?echo[ $retval -eq 0 ] && rm -f $lockfilereturn $retval}restart() {configtest || return $?stopsleep 1start}reload() {configtest || return $?echo -n $"Reloading $prog: "killproc $nginx -HUPRETVAL=$?echo}force_reload() {restart}configtest() {$nginx -t -c $NGINX_CONF_FILE}rh_status() {status $prog}rh_status_q() {rh_status >/dev/null 2>&1}case "$1" instart)rh_status_q && exit 0$1;;stop)rh_status_q || exit 0$1;;restart|configtest)$1;;reload)rh_status_q || exit 7$1;;force-reload)force_reload;;status)rh_status;;condrestart|try-restart)rh_status_q || exit 0;;*)echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"exit 2esac给nginx启动脚本添加权限并加入启动列表chmod 775 /etc/rc.d/init.d/nginxchkconfig nginx onservice nginx restartservice php-fpm startchkconfig php-fpm on更改php默认时区vim /etc/php.inidate.timezone = PRC配置www.conf将账号和组设置为wwwvim /etc/php-fpm.d/www.confuser = wwwgroup = www建立网站文件存放目录mkdir -p /data0/wwwchown www.www /data0/www/ -Rchmod 700 /data0/www/ -R编辑nginx的配置文件vim /usr/local/webserver/nginx/conf/nginx.conf#####################################################################################安装cacti所需组件yum install net-snmp net-snmp-utils net-snmp-devel rrdtool rrdtool-perl rrdtool-devel安装cactitar zxvf cacti-0.8.8b.tar.gzmv cacti-0.8.8b /data0/www/cactichown www.www /data0/www/ -Rchmod 700 /data0/www/ -R建立cacti任务计划crontab -u www -eMAILTO=""*/5 * * * * /usr/bin/php /data0/www/cacti/poller.php &>/dev/null导入cacti数据库,并建立cactiuser账号mysql -uroot -pcreate table cactigrant all on cacti.* to 'cactiuser'@'localhost' identified by '111111' with grant option;flush privileges;mysql -uroot -p111111 cacti < /data0/www/cacti/cacti.sql修改cacti配置文件,修改mysql数据库相关参数vim /data0/www/cacti/include/config.php修改php线程目录属性chown root.www /var/lib/php/session/修改nginx配置文件将cacti和nagios以及php,fastcgi加入,加入后访问cacti为http://IP/cacti ,访问nagios为:http://IP/nagios 并且nagios使用htpasswd账号。vim /usr/local/webserver/nginx/conf/nginx.confuser www www;worker_processes 2;worker_cpu_affinity 01 10;#error_log logs/error.log;#error_log logs/error.log notice;#error_log logs/error.log info;#pid logs/nginx.pid;worker_rlimit_nofile 10240;events {use epoll;worker_connections 10240;}http {include mime.types;default_type application/octet-stream;#log_format main '$remote_addr - $remote_user [$time_local] "$request" '# '$status $body_bytes_sent "$http_referer" '# '"$http_user_agent" "$http_x_forwarded_for"';#access_log logs/access.log main;sendfile on;tcp_nopush on;#keepalive_timeout 0;keepalive_timeout 30;tcp_nodelay on;fastcgi_buffers 2 256k;fastcgi_buffer_size 128k;fastcgi_busy_buffers_size 256k;fastcgi_temp_file_write_size 256k;proxy_connect_timeout 100;proxy_read_timeout 30;proxy_buffer_size 8k;proxy_buffers 8 64k;proxy_busy_buffers_size 128k;proxy_temp_file_write_size 128k;gzip on;gzip_min_length 1k;gzip_buffers 4 16k;gzip_http_version 1.0;gzip_comp_level 2;gzip_types text/plain text/javascript application/x-javascript text/css application/xml;gzip_vary on;# server {# listen 80;# server_name localhost;# #charset koi8-r;# #access_log logs/host.access.log main;# location / {# root /data0/www;# index index.php index.html index.htm;# } #error_page 404 /404.html; # redirect server error pages to the static page /50x.html ## error_page 500 502 503 504 /50x.html;# location = /50x.html {# root /data0/www;# } # proxy the PHP scripts to Apache listening on 127.0.0.1:80 # #location ~ \.php$ { # proxy_pass http://127.0.0.1; #} # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 ## location ~ \.php$ {# root html;# fastcgi_pass 127.0.0.1:9000;# fastcgi_index index.php;# fastcgi_param SCRIPT_FILENAME /data0/www$fastcgi_script_name;# include fastcgi_params;# } # deny access to .htaccess files, if Apache's document root # concurs with nginx's one # #location ~ /\.ht { # deny all; #}# }server { listen 80; server_name localhost; index index.html index.htm index.php; root /data0/www; location ~ .*\.(php|php5)?$ { fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME /data0/www$fastcgi_script_name; include fastcgi_params; } location /nagios { auth_basic "Nagios Access"; auth_basic_user_file /usr/local/nagios/etc/htpasswd.users; } location /nginx_status { stub_status on; access_log off; } location ~ .*\.(cgi|pl)?$ { gzip off; root /usr/local/nagios/sbin; rewrite ^/nagios/cgi-bin/(.*)\.cgi /$1.cgi break; fastcgi_pass unix:/usr/local/webserver/nginx/logs/perl-fcgi.sock; fastcgi_param SCRIPT_FILENAME /usr/local/nagios/sbin$fastcgi_script_name; fastcgi_index index.cgi; fastcgi_read_timeout 60; fastcgi_param REMOTE_USER $remote_user; include fastcgi_params; auth_basic "Nagios Access"; auth_basic_user_file /usr/local/nagios/etc/htpasswd.users; } } # another virtual host using mix of IP-, name-, and port-based configuration # #server { # listen 8000; # listen somename:8080; # server_name somename alias another.alias; # location / { # root html; # index index.html index.htm; # } #} # HTTPS server # #server { # listen 443; # server_name localhost; # ssl on; # ssl_certificate cert.pem; # ssl_certificate_key cert.key; # ssl_session_timeout 5m; # ssl_protocols SSLv2 SSLv3 TLSv1; # ssl_ciphers HIGH:!aNULL:!MD5; # ssl_prefer_server_ciphers on; # location / { # root html; # index index.html index.htm; # } #}}###################################################################################建立nagios使用的账号groupadd nagcmduseradd -m nagiosusermod -a -G nagcmd nagios将nginx安装使用的账号www也加到该组usermod -a -G nagcmd www安装nagiostar zxvf nagios-3.5.1.tar.gz修改cd nagios-3.5../configure --with-command-group=nagcmd --enable-event=brokermake allmake installmake install-initmake install-configmake install-commandmode 填写管理员邮件地址vim /usr/local/nagios/etc/objects/contacts.cfgmake install-webconf建立nagios访问账号htpasswd -c /usr/local/nagios/etc/htpasswd.users nagiosadmin安装nagios-pluginstar zxvf nagios-plugins-1.5.tar.gzcd nagios-plugins-1.5./configure --with-nagios-user=nagios --with-nagios-group=nagiosmakemake install加入启动列表chkconfig --add nagioschkconfig nagios on/usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfgservice nagios start将nagios页面路径链接到网站文件存放路径ln -s /usr/local/nagios/share/ /data0/www/nagioschown -R www.www /data0/www/nagiosNagios依赖PHP环境和perl环境。由于Nginx不支持Perl的CGI,这里就需要先来搭建Perl环境。让nginx支持fcgi安装FCGI模块wget http://search.cpan.org/CPAN/authors/id/F/FL/FLORA/FCGI-0.73.tar.gztar FCGI-0.73.tar.gztar -zxvf FCGI-0.73.tar.gzcd FCGI-0.73perl Makefile.PLmakemake install安装FCGI-ProcManager模块wget http://search.cpan.org/CPAN/authors/id/G/GB/GBJK/FCGI-ProcManager-0.19.tar.gztar -zxvf FCGI-ProcManager-0.19.tar.gzcd FCGI-ProcManager-0.19perl Makefile.PLmakemake install安装IO和IO::ALL模块wget http://search.cpan.org/CPAN/authors/id/G/GB/GBARR/IO-1.25.tar.gztar -zxvf IO-1.25.tar.gzcd IO-1.25perl Makefile.PLmakemake installcdwget http://search.cpan.org/CPAN/authors/id/I/IN/INGY/IO-All-0.41.tar.gztar -zxvf IO-All-0.41.tar.gzcd IO-All-0.41perl Makefile.PLmakemake installcd下载Perl脚本,这个脚本的目的就是产生一个PERL的FastCGI接口,让Nginx可以以CGI方式处理Perl。wget http://www.mike.org.cn/wp-content/uploads/2011/07/perl-fcgi.ziplsunzip perl-fcgi.zipmv perl-fcgi.pl /usr/local/webserver/nginx/chmod 755 /usr/local/webserver/nginx/perl-fcgi.pl建立一个CGI启动/停止脚本,注意pidfile和dir路径。vim /etc/init.d/perl-fcgi#!/bin/bash# chkconfig: - 99 15# description: perl-fcgi# processname: perl-fcgi# pidfile: /usr/local/webserver/nginx/logs/perl-fcgi.pid# Source function library.. /etc/rc.d/init.d/functions# Source networking configuration.. /etc/sysconfig/network# Check that networking is up.[ "$NETWORKING" = "no" ] && exit 0#set -xdir=/usr/local/webserver/nginx stop (){#pkill -f $dir/perl-fcgi.plkill $(cat $dir/logs/perl-fcgi.pid)rm $dir/logs/perl-fcgi.pid 2>/dev/nullrm $dir/logs/perl-fcgi.sock 2>/dev/nullecho "stop perl-fcgi done"} start (){rm $dir/now_start_perl_fcgi.sh 2>/dev/nullrm $dir/logs/perl-fcgi.pid 2>/dev/nullrm $dir/logs/perl-fcgi.sock 2>/dev/nullchown www.www $dir/logsecho "$dir/perl-fcgi.pl -l $dir/logs/perl-fcgi.log -pid $dir/logs/perl-fcgi.pid -S $dir/logs/perl-fcgi.sock" >>$dir/now_start_perl_fcgi.sh chown www.www $dir/now_start_perl_fcgi.shchmod u+x $dir/now_start_perl_fcgi.sh sudo -u www $dir/now_start_perl_fcgi.shecho "start perl-fcgi done"} case $1 instop)stop;;start)start;;restart)stopstart;;esac给脚本赋予权限chmod 755 /etc/init.d/perl-fcgichkconfig --add perl-fcgichkconfig perl-fcgi on由于启动脚本里使用了sudo,所以当服务器重启并加载这个服务脚本时会提示需要一个tty,导致fcgi不能随服务器一起启动起来,所以要修改/etc/sudoersvisudoDefaults requiretty,修改为 Defaults:www !requiretty 表示仅 www 用户不需要控制终端。启动脚本,正常情况下在/usr/local/webserver/nginx/logs下生成perl-fcgi.sock这个文件,如果没有生成,请检查下上面的步聚。service perl-cgi startservice php-fpm restart注意:nagios在nginx+fcgi下会出现在web界面下设置主机信息的时候出错502,这是需要到nagios源代码目录的cgi目录,找到cmd.c文件,搜索post,只有一个,修改为get,然后执行make cleanmake allmake install-cgisservice nagios restart#################################################################################################安装ndoutils,该插件可以将nagios监控结果保存到mysql中tar -zxvf ndoutils-1.5.2.tar.gzcd ndoutils-1.5.2./configure --enable-mysql --with-ndo2db-user=nagios --with-ndo2db-group=nagiosmake将源目录下的相关文件考到相应位置并修改权限cp src/{ndomod-3x.o,ndo2db-3x,log2ndo,file2sock} /usr/bin/cp config/ndo2db.cfg-sample /usr/local/nagios/etc/ndo2db.cfgcp config/ndomod.cfg-sample /usr/local/nagios/etc/ndomod.cfgchown nagios.nagios /usr/local/nagios/etc/ndo*chmod 664 /usr/local/nagios/etc/ndo*修改ndo2db配置文件vim /usr/local/nagios/etc/ndo2db.cfg socket_type=tcp db_host=localhost db_port=3306 db_name=cacti db_prefix=npc_ db_user=cactiuser db_pass=111111 debug_level=1修改ndomod配置文件vim /usr/local/nagios/etc/ndomod.cfg output_type=tcpsocket output=127.0.0.1将ndo2db启动脚本放到系统目录下cp daemon-init /etc/init.d/ndo2db编辑该启动脚本vim /etc/init.d/ndo2db Ndo2dbBin=/usr/bin/ndo2db-3x修改启动脚本权限并启动 chmod +x /etc/init.d/ndo2dbchkconfig --add ndo2dbchkconfig ndo2db onservice ndo2db start修改nagios配置文件,让nagios支持ndomod这个broker。vim /usr/local/nagios/etc/nagios.cfg broker_module=/usr/bin/ndomod-3x.o config_file=/usr/local/nagios/etc/ndomod.cfg event_broker_options=-1 重启nginxservice nginx restart安装cacti的npc插件tar -zxvf npc-2.0.4.tar.gzmv npc/ /data0/www/cacti/plugins/vim /data0/www/cacti/include/global.php $plugins = array(); $plugins[]='npc';重置下cacti插件目录权限chown www.www -R /data0/www/cacti/plugins/登陆cacti,安装npc插件配置 npcConsole -> Settings -> npc钩选Remote CommandsNagios Command File Path: /usr/local/nagios/var/rw/nagios.cmdNagios URL: http://yourserver/nagios添加执行权限chmod +x /usr/local/nagios/var/rw/nagios.cmd mysql修改表结构,否则npc无数据alter table npc_eventhandlers add long_output TEXT NOT NULL default '' after output;alter table npc_hostchecks add long_output TEXT NOT NULL default '' after output;alter table npc_hoststatus add long_output TEXT NOT NULL default '' after output;alter table npc_notifications add long_output TEXT NOT NULL default '' after output;alter table npc_servicechecks add long_output TEXT NOT NULL default '' after output;alter table npc_servicestatus add long_output TEXT NOT NULL default '' after output;alter table npc_statehistory add long_output TEXT NOT NULL default '' after output;alter table npc_systemcommands add long_output TEXT NOT NULL default '' after output;service php-fpm restartservice cgi restartservice nginx restartservice nagios restartservice ndo2db restart#########################################################################################安装nrpe(nagios监控linux服务器,需要让被监控的linux服务器安装nrpe+nagiosplugins,nagiosplugins由于该服务器已经安装,所以不需再动)nrpe依靠xinetd服务器,先安装xinetdtar -zxvf nrpe-2.15.tar.gzcd nrpe-2.15./configure --with-nrpe-user=nagios --with-nrpe-group=nagios --with-nagios-user=nagios --with-nagios-group=nagios --enable-ssl --enable-command-argsmake allmake install-pluginmake install-daemonmake install-daemon-configvim /etc/init.d/nrpe#!/bin/sh## chkconfig: - 99 15# processname: nrpe# Source function libraryif [ -f /etc/rc.d/init.d/functions ]; then. /etc/rc.d/init.d/functionselif [ -f /etc/init.d/functions ]; then. /etc/init.d/functionselif [ -f /etc/rc.d/functions ]; then. /etc/rc.d/functionsfi# Source networking configuration.. /etc/sysconfig/network# Check that networking is up.[ ${NETWORKING} = "no" ] && exit 0NrpeBin=/usr/local/nagios/bin/nrpeNrpeCfg=/usr/local/nagios/etc/nrpe.cfgLockFile=/var/lock/subsys/nrpe# See how we were called.case "$1" in start) # Start daemons. echo -n "Starting nrpe: " daemon $NrpeBin -c $NrpeCfg -d echo touch $LockFile ;; stop) # Stop daemons. echo -n "Shutting down nrpe: " killproc nrpe echo rm -f $LockFile ;; restart) $0 stop $0 start ;; status) status nrpe ;; *) echo "Usage: nrpe {start|stop|restart|status}" exit 1esacexit 0赋予执行权限,加入启动列表,启动nrpechmod +x /etc/init.d/nrpechkconfig --add nrpechkconfig nrpe onservice nrpe restart测试nrpe/usr/local/nagios/libexec/check_nrpe -H 127.0.0.1 -c check_load nagios监控nginx建立check_nginx命令脚本vim /usr/local/nagios/libexec/check_nginx (注意修改里面各变量路径)#!/bin/shPROGNAME=`basename $0`VERSION="Version 1.0,"AUTHOR="2009, Mike Adolphs (http://www.matejunkie.com/)"ST_OK=0ST_WR=1ST_CR=2ST_UK=3hostname="localhost"port=80path_pid=/usr/local/webserver/nginx/logsname_pid="nginx.pid"status_page="nginx_status"output_dir=/tmppid_check=1secure=0print_version() { echo "$VERSION $AUTHOR"}print_help() { print_version $PROGNAME $VERSION echo "" echo "$PROGNAME is a Nagios plugin to check whether nginx is running." echo "It also parses the nginx's status page to get requests and" echo "connections per second as well as requests per connection. You" echo "may have to alter your nginx configuration so that the plugin" echo "can access the server's status page." echo "The plugin is highly configurable for this reason. See below for" echo "available options." echo "" echo "$PROGNAME -H $hostname -P 80 -p $path_pid -n $name_pid " echo " -s nginx_statut -o /tmp [-w INT] [-c INT] [-S] [-N]" echo "" echo "Options:" echo " -H/--hostname)" echo " Defines the hostname. Default is: localhost" echo " -P/--port)" echo " Defines the port. Default is: 80" echo " -p/--path-pid)" echo " Path where nginx's pid file is being stored. You might need" echo " to alter this path according to your distribution. Default" echo " is: /var/run" echo " -n/--name_pid)" echo " Name of the pid file. Default is: nginx.pid" echo " -N/--no-pid-check)" echo " Turn this on, if you don't want to check for a pid file" echo " whether nginx is running, e.g. when you're checking a" echo " remote server. Default is: off" echo " -s/--status-page)" echo " Name of the server's status page defined in the location" echo " directive of your nginx configuration. Default is:" echo " nginx_status" echo " -o/--output-directory)" echo " Specifies where to write the tmp-file that the check creates." echo " Default is: /tmp" echo " -S/--secure)" echo " In case your server is only reachable via SSL, use this" echo " this switch to use HTTPS instead of HTTP. Default is: off" echo " -w/--warning)" echo " Sets a warning level for requests per second. Default is: off" echo " -c/--critical)" echo " Sets a critical level for requests per second. Default is:" echo " off" exit $ST_UK}while test -n "$1"; do case "$1" in -help|-h) print_help exit $ST_UK ;; --version|-v) print_version $PROGNAME $VERSION exit $ST_UK ;; --hostname|-H) hostname=$2 shift ;; --port|-P) port=$2 shift ;; --path-pid|-p) path_pid=$2 shift ;; --name-pid|-n) name_pid=$2 shift ;; --no-pid-check|-N) pid_check=0 ;; --status-page|-s) status_page=$2 shift ;; --output-directory|-o) output_dir=$2 shift ;; --secure|-S) secure=1 ;; --warning|-w) warning=$2 shift ;; --critical|-c) critical=$2 shift ;; *) echo "Unknown argument: $1" print_help exit $ST_UK ;; esac shiftdoneget_wcdiff() { if [ ! -z "$warning" -a ! -z "$critical" ] then wclvls=1 if [ ${warning} -gt ${critical} ] then wcdiff=1 fi elif [ ! -z "$warning" -a -z "$critical" ] then wcdiff=2 elif [ -z "$warning" -a ! -z "$critical" ] then wcdiff=3 fi}val_wcdiff() { if [ "$wcdiff" = 1 ] then echo "Please adjust your warning/critical thresholds. The warning \must be lower than the critical level!" exit $ST_UK elif [ "$wcdiff" = 2 ] then echo "Please also set a critical value when you want to use \warning/critical thresholds!" exit $ST_UK elif [ "$wcdiff" = 3 ] then echo "Please also set a warning value when you want to use \warning/critical thresholds!" exit $ST_UK fi}check_pid() { if [ -f "$path_pid/$name_pid" ] then retval=0 else retval=1 fi}get_status() { if [ "$secure" = 1 ] then wget --no-check-certificate -q -t 3 -T 3 \http://${hostname}:${port}/${status_page} -O ${output_dir}/nginx-status.1 sleep 1 wget --no-check-certificate -q -t 3 -T 3 \http://${hostname}:${port}/${status_page} -O ${output_dir}/nginx-status.2 else wget -q -t 3 -T 3 http://${hostname}:${port}/${status_page} \-O ${output_dir}/nginx-status.1 sleep 1 wget -q -t 3 -T 3 http://${hostname}:${port}/${status_page} \-O ${output_dir}/nginx-status.2 fi stat_output1=`stat -c %s ${output_dir}/nginx-status.1` stat_output2=`stat -c %s ${output_dir}/nginx-status.2` if [ "$stat_output1" = 0 -o "$stat_output2" = 0 ] then echo "UNKNOWN - Local copy/copies of $status_page is empty." exit $ST_UK fi}get_vals() { tmp1_reqpsec=`grep '^ ' ${output_dir}/nginx-status.1|awk '{print $3}'` tmp2_reqpsec=`grep '^ ' ${output_dir}/nginx-status.2|awk '{print $3}'` reqpsec=`expr $tmp2_reqpsec - $tmp1_reqpsec` tmp1_conpsec=`grep '^ ' ${output_dir}/nginx-status.1|awk '{print $2}'` tmp2_conpsec=`grep '^ ' ${output_dir}/nginx-status.2|awk '{print $2}'` conpsec=`expr $tmp2_conpsec - $tmp1_conpsec` reqpcon=`echo "scale=2; $reqpsec / $conpsec" | bc -l` if [ "$reqpcon" = ".99" ] then reqpcon="1.00" fi}do_output() { output="nginx is running. $reqpsec requests per second, $conpsec \connections per second ($reqpcon requests per connection)"}do_perfdata() { perfdata="'reqpsec'=$reqpsec 'conpsec'=$conpsec 'conpreq'=$reqpcon"}# Here we go!get_wcdiffval_wcdiffif [ ${pid_check} = 1 ]then check_pid if [ "$retval" = 1 ] then echo "There's no pid file for nginx. Is nginx running? Please \also make sure whether your pid path and name is correct." exit $ST_CR fifiget_statusget_valsdo_outputdo_perfdataif [ -n "$warning" -a -n "$critical" ]then if [ "$reqpsec" -ge "$warning" -a "$reqpsec" -lt "$critical" ] then echo "WARNING - ${output} | ${perfdata}" exit $ST_WR elif [ "$reqpsec" -ge "$critical" ] then echo "CRITICAL - ${output} | ${perfdata}" exit $ST_CR else echo "OK - ${output} | ${perfdata} ]" exit $ST_OK fielse echo "OK - ${output} | ${perfdata}" exit $ST_OKfi赋给权限chown nagios.nagios /usr/local/nagios/libexec/check_nginxchmod +x /usr/local/nagios/libexec/check_nginx编辑nrpe配置文件vim /usr/local/nagios/etc/nrpe.cfg command[check_nginx]=/usr/local/nagios/libexec/check_nginx -w 15000 -c 20000测试 /usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -d /usr/local/nagios/libexec/check_nginx -H 127.0.0.1 -P 80 -p /usr/local/webserver/nginx/logs/ -n nginx.pid -s nginx_status -o /tmp/ -w 15000 -c 20000 编辑nginx配置文件,让nginx能够提供状态值页面,上面给的配置文件已经加过,在此只做参考。vim /usr/local/webserver/nginx/conf/nginx.conf location /nginx_status { stub_status on; access_log off; } 编辑vim /usr/local/nagios/etc/objects/commands.cfg,将nginx命令加入 # 'check_nginx' command definitiondefine command{ command_name check_nginxstatus command_line $USER1$/check_nginx -H $ARG1$ -P $ARG2$ -p $ARG3$ -n $ARG4$ -o $ARG5$ -w $ARG6$ -c $ARG7$ } 编辑vim /usr/local/nagios/etc/objects/localhost.cfg,将nginx服务导入(注意模板使用的是generic-service) define service{ use generic-service host_name localhost service_description Nginx check_command check_nginxstatus!ip!80!/usr/local/webserver/nginx/logs!nginx.pid!nginx_status!/tmp!15000!20000 notifications_enabled 1 } 重启nginx service nginx restart 重启nagios service nagios restart 链接nginx默认首页,否则nagios里的http会warning。 ln -s /usr/local/webserver/nginx/html/index.html /data0/www/index.html ln -s /usr/local/webserver/nginx/html/50x.html /data0/www/50x.html 配置系统的邮件由个人常用的邮箱转发,这样nagios就可以用你的个人邮箱发送报警通知了。 vim /etc/mail.rc