2.给Nginx加上对Perl的CGI的支持
(1)安装FCGI模块
tar zxvf FCGI-0.74.tar.gz
cd FCGI-0.74
perl Makefile.PL
makef
make install
3.安装FCGI-ProcManager模块
tar zxvf FCGI-ProcManager-0.24.tar.gz
cd FCGI-ProcManager-0.24
perl Makefile.PL
make
make install
4.安装IO和IO-ALL模块
tar zxvf IO-1.25.tar.gz
cd IO-1.25
perl Makefile.PL
make
make install
cd ..
tar zxvf FCGI-ProcManager-0.24.tar.gz
cd FCGI-ProcManager-0.24
perl Makefile.PL
make
make install
5.在任意目录下(此处放在/usr/local/bin)创建脚本fastcgi-wrapper.pl(脚本内容参照 https://www.linode.com/docs/websites/nginx/nginx-and-perlfastcgi-on-centos-5)
由于此脚本会在 /var/run/nginx/目录下生成一个socket文件,所以运行此脚本之前要在/var/run/下创建nginx目录,否则在运行的时候会报错。
修改脚本权限为755:chmod 755 /usr/local/bin/fastcgi-wrapper.pl
运行脚本启动perl:perl /usr/lcoal/bin/fastcgi-wrapper.pl
到/var/run/nginx/目录下如果生成perl_cgi-dispatch.sock,说明脚本运行成功了。
6.创建nagios用户,创建组nagcmd
useradd nagios
passwd nagios
groupadd nagcmd
usermod -a -G nagios
usermod -a -G nginx
7.编译安装nagios
在安装nagios之前要先装gd库:
yum install gd gd-devel
tar zxvf nagios-3.4.3.tar.gz
cd nagios-3.4.3
./configure --with-command-group=nagcmd
make
make all
make install
make install-init
make install-config
make install-commandmode
到/usr/local/nagios/目录下,如果看到bin,etc,libexec,include,sbin,share,var这几个目录就证明nagios安装成功了。
进入libexec目录我们发现里面是空的,这是由于还没有安装nagios的插件。
8.利用htpasswd工具创建密码文件
htpasswd -c /usr/local/nginx/nagiospasswd david --------------密码文件存放位置自定
提示输入密码
9.修改nagios的配置文件,使新增用户有权访问nagios
cd /usr/local/nagios/etc
vi cgi.cfg
#在一下几项添加新增的david用户
authorized_for_system_information=nagiosadmin,david
authorized_for_configuration_information=nagiosadmin,david
authorized_for_system_commands=nagiosadmin,david
authorized_for_all_services=nagiosadmin,david
authorized_for_all_hosts=nagiosadmin,david
authorized_for_all_service_commands=nagiosadmin,david
authorized_for_all_host_commands=nagiosadmin,david
10.编译安装nagios插件
nagios主程序只是提供了一个运行框架,具体的监控功能是靠nagios-plugin完成的。
tar nagios-plugins-2.0.3.tar.gz
cd nagios-plugins-2.0.3
./configure --with-nagios-user=nagios --with-nagios-group=nagios
make
make install
如果在/usr/local/nagios/libexec目录下看到所有插件则证明nagios-plugin安装成功了。
11.修改nginx的配置文件nginx.conf,以支持perl和php方式访问nagios
server
{
listen 80;
server_name 146.71.113.42;
access_log logs/access.log;
error_log logs/error.log;
index index.html index.htm index.php;
auth_basic "Nagios";
auth_basic_user_file /usr/local/nginx/nagiospasswd;
root /usr/local/nagios/share;
if ( $request_filename ~ \.(gif|png|jpg|jpeg|ico) ) {
rewrite ^/nagios/(images/.*) /$1 break;
}
if ( $request_filename ~ \.(css) ) {
rewrite ^/nagios/(stylesheets/.*) /$1 break;
}
#php configuration support
location ~ .*\.(php|php5)?$ {
root /usr/local/nagios/share;
fastcgi_pass unix:/var/run/php/php-fpm.sock;
#fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi.conf;
}
location /nagios {
alias /usr/local/nagios/share;
auth_basic "Nagios";
auth_basic_user_file /usr/local/nginx/nagiospasswd;
}
#cgi configuration support
location ~ \.(cgi|pl)?$ {
root /usr/local/nagios/sbin/;
rewrite ^/nagios/cgi-bin/(.*)\.cgi /$1.cgi$2 break;
#fastcgi_pass 127.0.0.1:8999;
fastcgi_pass unix:/var/run/nginx/perl_cgi-dispatch.sock;
fastcgi_index index.cgi;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param QUERY_STRING $query_string;
fastcgi_param REQUEST_METHOD $request_method;
fastcgi_param CONTENT_TYPE $content_type;
fastcgi_param CONTENT_LENGTH $content_length;
fastcgi_param GATEWAY_INTERFACE CGI/1.1;
fastcgi_param SERVER_SOFTWARE nginx;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
fastcgi_param REQUEST_URI $request_uri;
fastcgi_param DOCUMENT_URI $document_uri;
fastcgi_param DOCUMENT_ROOT $document_root;
fastcgi_param SERVER_PROTOCOL $server_protocol;
fastcgi_param REMOTE_ADDR $remote_addr;
fastcgi_param REMOTE_PORT $remote_port;
fastcgi_param SERVER_ADDR $server_addr;
fastcgi_param SERVER_PORT $server_port;
fastcgi_param SERVER_NAME $server_name;
fastcgi_param REMOTE_USER $remote_user;
}
}
12.启动nagios
/usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg ----------------检查配置文件是否正确,如果没有错误就可启动nagios服务
/usr/local/nagios/bin/nagios -d /usr/local/nagios/etc/nagios.cfg ----------------启动服务
/usr/local/nagios/bin/nagiostats ----------------检查nagios的运行状态
13.主控端安装nrpe
由于Nagios只能监测自己所在的主机的一些本地情况,例如,cpu负载、内存使用、硬盘使用等等。如果想要监测被监控的服务器上的这些本地情况,就要用到NRPE。NRPE(Nagios Remote Plugin Executor)是Nagios的一个扩展,它被用于被监控的服务器上,向Nagios监控平台提供该服务器的一些本地的情况。NRPE可以称为Nagios的Linux客户端。
由于NRPE是通过SSL方式在监控和被监控主机上进行数据传输的,所以必须先安装ssl相关的软件包。
主控端:
tar zxvf nrpe-2.10.tar.gz
cd nrpe-2.10
./configure
make all
make install-plugin
make install-daemon
make install-daemon-config
启动nrpe:
在被控端的配置/usr/local/nagios/etc/nrpe.cfg中
在allowed_hosts=127.0.0.1后面加上",主控端ip"
/usr/local/nagios/libexec/check_nrpe -H localhost ---------------验证是否被正确安装
16.回到主控端执行
/usr/local/nagios/libexec/check_nrpe -H 被控端ip ---------------------如果返回nrpe版本号,则说明被控端的nrpe已经配置成功。
17.通过http://ip/即可访问nagios监控页面
================================================================================
说明:在/usr/local/nagios/etc/objects目录下,有这么几个文件:
command.cfg--------------------------定义命令,这些命令都是利用/usr/local/nagios/libexec目录下的可执行程序定义的。其中command_name就是命令的名字,此名字在之后定义服务的时候使用,command_line定义具体命令的实现,均为libexec下可执行程序加上变量构成。
templates.cfg----------------------------定义各种模板,如local-service,generic-service,local-host,generic-host,这些模板之后都可以在localhost.cfg中被引用,可节省大量时间且管理方便。
timeperiods.cfg--------------------------监控时间段配置文件
contacts.cfg------------------------------定义联系人,联系人组的配置文件
printer.cfg--------------------------------定义监控打印机的配置文件模板,默认不启用此文件
switch.cfg---------------------------------定义监控路由器的配置文件模板,默认不启用此文件
windows.cfg------------------------------定义监控windows主机的配置文件模板,默认不启用此文件
localhost.cfg------------------------------定义监控本地主机的配置文件,如果监控本地主机的额外服务,需要在这里面配置
监控本机特定磁盘情况(以/dev/sda1为例)
1.在command.cfg中定义check_sda1命令,如下:
define command {
command_name check_sda1
command_line $USER1$/check_disk -w $ARG1$ -c $ARG2$ -p $ARG3$
}
2.在localhost.cfg中配置相应的服务,如下:
define service{
use local-service
host_name 192.168.136.128
service_description check_sda1
check_command check_sda1!20%!10%/dev/sda1
}
3.在nrpe.cfg中添加如下内容:
command[check_sda1]=/usr/local/nagios/libexec/check_disk -w 20% -c 10% -p /dev/sda1
4.killall掉nagios,之后重启即可。
===================================================================
监控远程主机的服务-----swap,load,users,processes
这里注意要配置三个配置文件:主控端---commands.cfg ,services.cfg ,hosts.cfg
被控端---nrpe.cfg
由于我们要监控的是远程主机,所以一定要在主控端的command.cfg中定义命令check_nrpe。
主控端commands.cfg:
define command{
command_name check_nrpe
command_line $USER1$/check_nrpe -H $HOSTADDRESS -t 30 $ARG1$
}
define command{
command_name check_swap
command_line $USER1$/check_swap -w $ARG1$ -c $ARG2$
}
define command{
command_name check_load
command_line $USER1$/check_load -w $ARG1$ -c $ARG2$
}
define command{
command_name check_users
command_line $USER1$/check_users -w $ARG1$ -c $ARG2$
}
define command{
command_name check_procs
command_line $USER1$/check_procs -w $ARG1$ -c $ARG2$ -s $ARG3$
}
主控端hosts.cfg:
define host{
use linux-server
host_name 192.168.136.129
alias 192.168.136.129
address 192.168.136.129
}
主控端services.cfg
define service{
use local-service ; Name of service template to use
host_name 192.168.136.129
service_description Swap Usage
check_command check_nrpe!check_swap!20!10
notifications_enabled 1
}
define service{
use local-service ; Name of service template to use
host_name 192.168.136.129
service_description Current Load
check_command check_nrpe!check_load!5.0,4.0,3.0!10.0,6.0,4.0
notifications_enabled 1
}
define service{
use local-service ; Name of service template to use
host_name 192.168.136.129
service_description Current Users
check_command check_nrpe!check_users!20!50
notifications_enabled 1
}
define service{
use local-service ; Name of service template to use
host_name 192.168.136.129
service_description Total Processes
check_command check_nrpe!check_procs!250!400!RSZDT
notifications_enabled 1
}
被控端nrpe.cfg
command[check_swap]=/usr/local/nagios/libexec/check_swap -w 20 -c 10
command[check_load]=/usr/local/nagios/libexec/check_load -w 15,10,5 -c 30,25,20
command[check_users]=/usr/local/nagios/libexec/check_users -w 5 -c 10
command[check_procs]=/usr/local/nagios/libexec/check_procs -w 250 -c 400 -s RSZTD
重启主控端nagios,被控端nrpe即可。
=======================================================================
被控端自定义命令,服务---------监控/dev/sda1的使用情况
主控端commands.cfg:
define command{
command_name check_sda1_client
command_line $USER1$/check_disk -w $ARG1$ -c $ARG2$ -p $ARG3$
}
主控端services.cfg:
define service{
use local-service
host_name 192.168.136.129
service_description Current sda1
check_command check_nrpe!check_sda1_client!20%!10%!/dev/sda1
notifications_enabled 1
}
被控端nrpe.cfg:
command[check_sda1_client]=/usr/local/nagios/libexec/check_disk -w 20 -c 10 /dev/sda1
主控端重启nagios,被控端重启nrpe即可。
================================================================
监控52112端口:
commands.cfg:
define command{
command_name 52112
command_line $USER1$/check_tcp -p 52112
}
services.cfg:
define service{
use local-service
host_name 192.168.136.129
service_description Check 52112
check_command check_nrpe!52112
notifications_enabled 1
}
nrpe.cfg:
command[52112]=/usr/local/nagios/libexec/check_tcp -p 52112
=================================================================
监控sda1设备IO:
nagios-plugins中默认是没有check_iostat这个工具的,可到官网下载:
http://exchange.nagios.org/directory/Plugins/Operating-Systems/Linux/check_iostat--2D-I-2FO-statistics/details
下载完之后放在分别放在主控端和被控端的libexec目录下,给予可执行权限。
commands.cfg:
define command{
command_name check_sda1_iostat
command_line $USER1$/check_iostat -d $ARG1$ -w $ARG2$ -c $ARG3$
}
services.cfg:
define service{
use local-service
host_name 192.168.136.129
service_description Check sda1 IO
check_command check_nrpe!check_sda1_iostat!sda1!1000!2000
notifications_enabled 1
}
nrpe.cfg:
command[check_sda1_iostat]=/usr/local/nagios/libexec/check_iostat -d sda1 -w 1000 -c 2000
===========================================================
监控sda2设备IO:
commands.cfg:
define command{
command_name check_sda2_iostat
command_line $USER1$/check_iostat -d $ARG1$ -w $ARG2$ -c $ARG3$
}
services.cfg:
define service{
use local-service
host_name 192.168.136.129
service_description Check sda2 IO
check_command check_nrpe!check_sda2_iostat!sda1!1000!2000
notifications_enabled 1
}
nrpe.cfg:
command[check_sda2_iostat]=/usr/local/nagios/libexec/check_iostat -d sda2 -w 1000 -c 2000
==================================================================
监控网卡流量:
到nagios官网下载
check_iftraffic_nrpe.pl
分别放到主控端和被控端的libexec目录下,并改名为check_iftraffic,赋予执行权限,修改属组和用户为nagios。
commands.cfg:
define command{
command_name check_iftraffic
command_line $USER1$/check_iftraffic -i $ARG1$ -w $ARG2$ -c $AGR3$ -b $ARG4$ -u $ARG5$
}
services.cfg:
define service{
use local-service
host_name 192.168.136.129
service_description Check eth0
check_command check_nrpe!check_iftraffic!"eth0"!50!100!100!m
notifications_enabled 1
}
nrpe.cfg:
command[check_iftraffic]=/usr/local/nagios/libexec/check_iftraffic -i eth0 -w 50 -c 100 -b 100 -u m
===============================================================================
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/29800581/viewspace-1434899/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/29800581/viewspace-1434899/