安装
1.服务器端
#rpm -Uvh http://repo.webtatic.com/yum/el6/latest.rpm
#yum install php54w php54w-cli php54w-common php54w-devel php54w-gd php54w-mbstring php54w-mcrypt php54w-mysql php54w-odbc php54w-pdo php54w-xml mysql55w mysql55w-devel mysql55w-server mysql55w-libs httpd memcached php-pecl-memcache gd gd-devel
# useradd nagios -s /sbin/nologin
# mkdir /usr/local/nagios
# chown nagios:nagios /usr/local/nagios/ -R
# tar -jxvf nagios-cn-3.2.3.tar.bz2
# ./configure --prefix=/usr/local/nagios/ --with-gd-lib=/usr/lib --with-gd-inc=/usr/include --with-nagios-user=nagios --with-nagios-group=nagios --with-gd-lib=/usr/lib --with-gd-inc=/usr/include/ --with-command-group=nagios
#make all
#make install
#make install-init
#make install-commandmode
#make install-config
******#make install-webconf //如果是rpm包安装的apache,可以执行,否则报错,做如下操作即可
#vim /usr/local/apache/conf/httpd.conf
见nagios.conf
# /usr/local/apache/bin/htpasswd -c /usr/local/nagios/etc/.passwd.conf nagiosadmin //生成nagios口令密码
或者
# /usr/local/apache/bin/htpasswd -bc /usr/local/nagios/etc/.passwd.conf nagiosadmin nagiosadmin
# /usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg //检查文件是否出错
#/etc/init.d/nagios start
#/etc/init.d/httpd start
访问http://IP/nagios/ //最后的/一定加
# chmod o+w /usr/local/nagios/var/rw/nagios.cmd //更改事件通知时会用到
#/usr/local/nagios/bin/nagios -d /usr/local/nagios/etc/nagios.cfg //nagios手动重启
安装插件
# tar -zxvf nagios-plugins-1.4.16.tar.gz
# ./configure --prefix=/usr/local/nagios/ --with-nagios-user=nagios --with-nagios-group=nagios
# make && make install
#tar -zxvf nrpe-2.12.tar.gz
#./configure --prefix=/usr/local/nagios/
#make all
#make install-plugin
#make install-daemon
#make install-daemon-config
#vim /usr/local/nagios/etc/objects/commands.cfg
在最后加上
define command{
command_name check_nrpe
command_line $USER1$/check_nrpe -H $HOSTADDRESS$ -c $ARG1$
}
或者
define command{
command_name check_nrpe
command_line /usr/local/nagios/libexec/check_nrpe -H $HOSTADDRESS$ -c $ARG1$
}
# /usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -d //启动nrpe
#/usr/local/nagios/bin/nagios -d /usr/local/nagios/etc/nagios.cfg //nagios手动重启
2.被监控端
#yum install openssl openssl-devel
# useradd nagios -s /sbin/nologin
#tar -zxvf nagios-plugins-1.4.16.tar.gz -C /usr/local/src/
#./configure --prefix=/usr/local/nagios --with-nagios-user=nagios --with-nagios-group=nagios
#make
#make install
#chown nagios:nagios /usr/local/nagios/ -R
#tar -zxvf nrpe-2.12.tar.gz -C /usr/local/src/
#./configure --prefix=/usr/local/nagios/
#make all
#make install-plugin
#make install-deamon
#make install-daemon-config
#make install-xinetd
#vim /usr/local/nagios/etc/nrpe.cfg
修改如下内容,根据自己需求修改
allowed_hosts=127.0.0.1 //添加ip用逗号分隔
# vim /etc/services
在最后添加
nrpe 5666/tcp #nrpe
# /usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -d
配置
1.基本配置举例(添加监控主机和监控服务)
定义主机(服务器端)
# vim host.cfg
define host{
use linux-server
host_name webserver
alias web
# parents
address 192.168.10.132
icon_image server.gif
statusmap_image server.gd2
2d_coords 500,200
3d_coords 500,200,100
}
定义服务(服务器端)
①定义host后通过本地check_tcp监控客户端tcp端口
define service{
use local-service
host_name webserver
service_description 1080
check_command check_tcp!1080
}
①定义host后通过本地check_http加上参数监控客户端
define service{
use local-service
host_name webserver
service_description HTTP
check_command check_http!-I 192.168.10.132 -p 80 -e 403,202 //监控192.168.10.132主机的80端口,返回值为403和 202时,为ok状态
}
③定义host后通过check_nrpe远程调用客户端插件来监控客户端
define service{
use local-service
host_name webserver
service_description Login user
check_command check_nrpe!check_users
}
这里check_nrpe!check_users命令的意思就是是通过check_nrpe插件远程调用客户端nrpe.cfg里定义的check_user,所以客户端nrpe.cfg里需要定义一下check_users:
客户端
#vim /usr/local/nagios/etc/nrpe.cfg
修改如下内容,根据自己需求修改
command[check_users]=/usr/local/nagios/libexec/check_users -w 5 -c 10
command[check_load]=/usr/local/nagios//libexec/check_load -w 15,10,5 -c 30,25,20
command[check_hda1]=/usr/local/nagios//libexec/check_disk -w 20% -c 10% -p /dev/hda1
command[check_zombie_procs]=/usr/local//nagios/libexec/check_procs -w 5 -c 10 -s Z
command[check_total_procs]=/usr/local//nagios/libexec/check_procs -w 150 -c 200
2.监控网站主页
定义命令
# vim commands.cfg
添加
define command{
command_name check_index
command_line $USER1$/check_http $ARG1$
}
定义服务
# vim host.cfg
define service{
use local-service ; Name of service template to use
host_name localhost
service_description Web_index
check_command check_index!-H 192.168.0.106 -u /index.php //如果域名监控,格式是check_index!-H [url]www.testhost.test[/url] -u /index.php
}
3.监控Mysql
查看libexec目录下有没有check_mysql,如果没有,yum installed mysql-devel,在重装nagios-plugin,就会生成check_mysql
创建nagios专用数据库
mysql> create database nagios;
mysql> grant all on nagios.* to nagios@'服务端IP' identified by 'nagios';
mysql> flush privileges;
测试
# /usr/local/nagios/libexec/check_mysql -H 客户端IP -u nagios -d nagios -p nagios
Uptime: 44649 Threads: 10 Questions: 152675 Slow queries: 0 Opens: 292 Flush tables: 1 Open tables: 64 Queries per second avg: 3.419
# vim commands.cfg //定义命令
添加
#check mysql
define command{
command_name check_mysql
command_line $USER1$/check_mysql -H $HOSTADDRESS$ -u nagiosuser -d nagios -p nagiospwd
}
# vim host.cfg //定义监控服务
define service{
use local-service ; Name of service template to use
host_name webserver
service_description Mysql
check_command check_mysql
}
PS:也可以在客户端nrpe.cfg里定义check_mysql命令(前提是客户端的plugin里生成了check_mysql),然后服务端定义服务时候通过check_nrpe!check_mysql来监控客户端mysql,这样就避免了数据库的端口开放和用户远程登录带来的风险
4.监控logfile
被监控端
#wget http://labs.consol.de/wp-content/uploads/2012/02/check_logfiles-3.4.7.1.tar.gz
#tar xf check_logfiles-3.4.7.1.tar.gz
#./configure --prefix=/usr/local/nagios/ --with-nagios-user=nagios --with-nagios-group=nagios --with-seekfiles-dir=/usr/local/nagios/var/tmp/ --with-protocols-dir=/usr/local/nagios/var/tmp/ --with-perl=/usr/bin/perl --with-gzip=/bin/gzip
#make && make install
# vim /usr/local/nagios/etc/log.conf
@searches = (
{
tag => 'php_errors',
logfile => '/data/logs/php_errors.log',
rotation => 'CentOs',
criticalpatterns => [
'error',
],
},
);
#vim /usr/local/nagios/etc/nrpe.cfg
加入
command[check_logfiles]=/usr/local/nagios/libexec/check_logfiles --config=/usr/local/nagios/etc/log.conf
监控端
#vim etc/objects/commands.cfg
加入
define command{
command_name check_log
command_line $USER1$/check_nrpe -H $HOSTADDRESS$ -t $ARG1$ -c $ARG2$
}
# vim /usr/local/nagios/etc/objects/host.cfg
加入
define service{
use local-service
host_name webserver
service_description PHP_Error
check_command check_log!2!check_logfiles //超过两条报警
}
5.监控进程
插件下载地址
https://exchange.nagios.org/directory/Plugins/Operating-Systems/Linux/check_ps-2Esh/details
以监控一个java程序为例:
被监控端
把check_ps.sh放到被监控的的/usr/local/nagios/libexec/下
#vim /usr/local/nagios/etc/nrpe.cfg
command[check_UnZIP]=/usr/local/nagios/libexec/check_ps.sh -p "Linux_Sync_UnZIP.JAR"
ps:具体参数可以check_ps.sh -h查看,设置一些具体伐值来报警,我这里只是监控进程是否存在
#killall nrpe
#/usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -d
监控服务器
define service{
use local-service
host_name 10.10.10.10
service_description Linux_Sync_UnZIP
check_command check_nrpe!check_UnZIP
notifications_enabled 0
}
#/etc/init.d/nagios restart
如果想配合pnp4nagios出图的话,把下载页面的check_ps.php下载到监控服务器,放到/usr/local/pnp4nagios/share/templates目录下即可
Error: Could not open command file '/usr/local/nagios/var/rw/nagios.cmd' for update!
解决方法
# usermod -a -G nagios www
# /etc/init.d/httpd restart
# /etc/init.d/nagios restart