################################################################################
初始化环境(默认环境已经安装好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 www
useradd -g www www -s /bin/false
安装nginx
tar zxvf nginx-1.4.3.tar.gz
cd 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_module
make && 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 0
nginx="/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/nginx
lockfile=/var/lock/subsys/nginx
make_dirs() {
# make required directories
user=`nginx -V 2>&1 | grep "configure arguments:" | sed 's/[^*]*--user=([^ ]*).*/1/g' -`
options=`$nginx -V 2>&1 | grep 'configure arguments:'`
for opt in $options; do
if [ `echo $opt | grep '.*-temp-path'` ]; then
value=`echo $opt | cut -d "=" -f 2`
if [ ! -d "$value" ]; then
# echo "creating" $value
mkdir -p $value && chown -R $user $value
fi
fi
done
}
start() {
[ -x $nginx ] || exit 5
[ -f $NGINX_CONF_FILE ] || exit 6
make_dirs
echo -n $"Starting $prog: "
daemon $nginx -c $NGINX_CONF_FILE
retval=$?
echo
[ $retval -eq 0 ] && touch $lockfile
return $retval
}
stop() {
echo -n $"Stopping $prog: "
killproc $prog -QUIT
retval=$?
echo
[ $retval -eq 0 ] && rm -f $lockfile
return $retval
}
restart() {
configtest || return $?
stop
sleep 1
start
}
reload() {
configtest || return $?
echo -n $"Reloading $prog: "
killproc $nginx -HUP
RETVAL=$?
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" in
start)
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 2
esac
给nginx启动脚本添加权限并加入启动列表
chmod 775 /etc/rc.d/init.d/nginx
chkconfig nginx on
service nginx restart
service php-fpm start
chkconfig php-fpm on
更改php默认时区
vim /etc/php.ini
date.timezone = PRC
配置www.conf将账号和组设置为www
vim /etc/php-fpm.d/www.conf
user = www
group = www
建立网站文件存放目录
mkdir -p /data0/www
chown www.www /data0/www/ -R
chmod 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
安装cacti
tar zxvf cacti-0.8.8b.tar.gz
mv cacti-0.8.8b /data0/www/cacti
chown www.www /data0/www/ -R
chmod 700 /data0/www/ -R
建立cacti任务计划
crontab -u www -e
MAILTO=""
*/5 * * * * /usr/bin/php /data0/www/cacti/poller.php &>/dev/null
导入cacti数据库,并建立cactiuser账号
mysql -uroot -p
create table cacti
grant all on cacti.* to 'cactiuser'@'localhost'>
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.conf
user 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 nagcmd
useradd -m nagios
usermod -a -G nagcmd nagios
将nginx安装使用的账号www也加到该组
usermod -a -G nagcmd www
安装nagios
tar zxvf nagios-3.5.1.tar.gz
修改
cd nagios-3.5.
./configure --with-command-group=nagcmd --enable-event=broker
make all
make install
make install-init
make install-config
make install-commandmode
填写管理员邮件地址
vim /usr/local/nagios/etc/objects/contacts.cfg
make install-webconf
建立nagios访问账号
htpasswd -c /usr/local/nagios/etc/htpasswd.users nagiosadmin
安装nagios-plugins
tar zxvf nagios-plugins-1.5.tar.gz
cd nagios-plugins-1.5
./configure --with-nagios-user=nagios --with-nagios-group=nagios
make
make install
加入启动列表
chkconfig --add nagios
chkconfig nagios on
/usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg
service nagios start
将nagios页面路径链接到网站文件存放路径
ln -s /usr/local/nagios/share/ /data0/www/nagios
chown -R www.www /data0/www/nagios
Nagios依赖PHP环境和perl环境。由于Nginx不支持Perl的CGI,这里就需要先来搭建Perl环境。
让nginx支持fcgi
安装FCGI模块
wget http://search.cpan.org/CPAN/authors/id/F/FL/FLORA/FCGI-0.73.tar.gz
tar FCGI-0.73.tar.gz
tar -zxvf FCGI-0.73.tar.gz
cd FCGI-0.73
perl Makefile.PL
make
make install
安装FCGI-ProcManager模块
wget http://search.cpan.org/CPAN/authors/id/G/GB/GBJK/FCGI-ProcManager-0.19.tar.gz
tar -zxvf FCGI-ProcManager-0.19.tar.gz
cd FCGI-ProcManager-0.19
perl Makefile.PL
make
make install
安装IO和IO::ALL模块
wget http://search.cpan.org/CPAN/authors/id/G/GB/GBARR/IO-1.25.tar.gz
tar -zxvf IO-1.25.tar.gz
cd IO-1.25
perl Makefile.PL
make
make install
cd
wget http://search.cpan.org/CPAN/authors/id/I/IN/INGY/IO-All-0.41.tar.gz
tar -zxvf IO-All-0.41.tar.gz
cd IO-All-0.41
perl Makefile.PL
make
make install
cd
下载Perl脚本,这个脚本的目的就是产生一个PERL的FastCGI接口,让Nginx可以以CGI方式处理Perl。
wget http://www.mike.org.cn/wp-content/uploads/2011/07/perl-fcgi.zip
ls
unzip perl-fcgi.zip
mv 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 -x
dir=/usr/local/webserver/nginx
stop ()
{
#pkill -f $dir/perl-fcgi.pl
kill $(cat $dir/logs/perl-fcgi.pid)
rm $dir/logs/perl-fcgi.pid 2>/dev/null
rm $dir/logs/perl-fcgi.sock 2>/dev/null
echo "stop perl-fcgi done"
}
start ()
{
rm $dir/now_start_perl_fcgi.sh 2>/dev/null
rm $dir/logs/perl-fcgi.pid 2>/dev/null
rm $dir/logs/perl-fcgi.sock 2>/dev/null
chown www.www $dir/logs
echo "$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.sh
chmod u+x $dir/now_start_perl_fcgi.sh
sudo -u www $dir/now_start_perl_fcgi.sh
echo "start perl-fcgi done"
}
case $1 in
stop)
stop
;;
start)
start
;;
restart)
stop
start
;;
esac
给脚本赋予权限
chmod 755 /etc/init.d/perl-fcgi
chkconfig --add perl-fcgi
chkconfig perl-fcgi on
由于启动脚本里使用了sudo,所以当服务器重启并加载这个服务脚本时会提示需要一个tty,导致fcgi不能随服务器一起启动起来,所以要修改/etc/sudoers
visudo
Defaults requiretty,修改为 Defaults:www !requiretty 表示仅 www 用户不需要控制终端。
启动脚本,正常情况下在/usr/local/webserver/nginx/logs下生成perl-fcgi.sock这个文件,如果没有生成,请检查下上面的步聚。
service perl-cgi start
service php-fpm restart
注意:nagios在nginx+fcgi下会出现在web界面下设置主机信息的时候出错502,这是需要
到nagios源代码目录的cgi目录,找到cmd.c文件,搜索post,只有一个,修改为get,然后执行
make clean
make all
make install-cgis
service nagios restart
#################################################################################################
安装ndoutils,该插件可以将nagios监控结果保存到mysql中
tar -zxvf ndoutils-1.5.2.tar.gz
cd ndoutils-1.5.2
./configure --enable-mysql --with-ndo2db-user=nagios --with-ndo2db-group=nagios
make
将源目录下的相关文件考到相应位置并修改权限
cp src/{ndomod-3x.o,ndo2db-3x,log2ndo,file2sock} /usr/bin/
cp config/ndo2db.cfg-sample /usr/local/nagios/etc/ndo2db.cfg
cp config/ndomod.cfg-sample /usr/local/nagios/etc/ndomod.cfg
chown 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/ndo2db
chkconfig --add ndo2db
chkconfig ndo2db on
service 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
重启nginx
service nginx restart
安装cacti的npc插件
tar -zxvf npc-2.0.4.tar.gz
mv 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插件
配置 npc
Console -> Settings -> npc
钩选Remote Commands
Nagios Command File Path: /usr/local/nagios/var/rw/nagios.cmd
Nagios 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 restart
service cgi restart
service nginx restart
service nagios restart
service ndo2db restart
#########################################################################################
安装nrpe(nagios监控linux服务器,需要让被监控的linux服务器安装nrpe+nagiosplugins,nagiosplugins由于该服务器已经安装,所以不需再动)
nrpe依靠xinetd服务器,先安装xinetd
tar -zxvf nrpe-2.15.tar.gz
cd nrpe-2.15
./configure --with-nrpe-user=nagios --with-nrpe-group=nagios --with-nagios-user=nagios --with-nagios-group=nagios --enable-ssl --enable-command-args
make all
make install-plugin
make install-daemon
make install-daemon-config
vim /etc/init.d/nrpe
#!/bin/sh
#
# chkconfig: - 99 15
# processname: nrpe
# Source function library
if [ -f /etc/rc.d/init.d/functions ]; then
. /etc/rc.d/init.d/functions
elif [ -f /etc/init.d/functions ]; then
. /etc/init.d/functions
elif [ -f /etc/rc.d/functions ]; then
. /etc/rc.d/functions
fi
# Source networking configuration.
. /etc/sysconfig/network
# Check that networking is up.
[ ${NETWORKING} = "no" ] && exit 0
NrpeBin=/usr/local/nagios/bin/nrpe
NrpeCfg=/usr/local/nagios/etc/nrpe.cfg
LockFile=/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 1
esac
exit 0
赋予执行权限,加入启动列表,启动nrpe
chmod +x /etc/init.d/nrpe
chkconfig --add nrpe
chkconfig nrpe on
service 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/sh
PROGNAME=`basename $0`
VERSION="Version 1.0,"
AUTHOR="2009, Mike Adolphs (http://www.matejunkie.com/)"
ST_OK=0
ST_WR=1
ST_CR=2
ST_UK=3
hostname="localhost"
port=80
path_pid=/usr/local/webserver/nginx/logs
name_pid="nginx.pid"
status_page="nginx_status"
output_dir=/tmp
pid_check=1
secure=0
print_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> 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> 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
shift
done
get_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_wcdiff
val_wcdiff
if [ ${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
fi
fi
get_status
get_vals
do_output
do_perfdata
if [ -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
fi
else
echo "OK - ${output} | ${perfdata}"
exit $ST_OK
fi
赋给权限
chown nagios.nagios /usr/local/nagios/libexec/check_nginx
chmod +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 definition
define 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
运维网声明
1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网 享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com