一、nagios监控端安装
1. nagios安装
# wget http://prdownloads.sourceforge.net/sourceforge/nagios/nagios-3.2.2.tar.gz
# tar zxvf nagios-3.2.2.tar.gz
# cd nagios-3.2.2
# useradd -m -s /bin/bash nagios
# groupadd nagios
# usermod -G nagios nagios
# groupadd nagcmd
# usermod -a -G nagcmd nagios
# usermod -a -G nagcmd www
# ./configure --prefix=/data/nagios --with-command-group=nagcmd
# make
# make all
# make install
# make install-init     # 生成init启动脚本
# make install-config     # 安装示例配置文件
# make install-commandmode     # 设置相应的目录权限
2. nagiox插件安装
# wget http://prdownloads.sourceforge.net/sourceforge/nagiosplug/nagios-plugins-1.4.15.tar.gz
# tar zxvf nagios-plugins-1.4.15.tar.gz
# cd nagios-plugins-1.4.15
# ./configure --with-nagios-user=nagios --with-nagios-group=nagios --prefix=/data/nagios
# make
# make install
3. nrpe安装
# tar zxvf nrpe-2.12.tar.gz
# cd nrpe-2.12
# ./configure
# make all
# cp src/check_nrpe /data/nagios/libexec/
4. 配置
# 加入系统服务并设为开机自动
# chkconfig --add nagios
# chkconfig nagios on
# mkdir /data/nagios/var/rw
# chown nagios.nagios /data/nagios/var/rw
# 测试配置文件可用
# /data/nagios/bin/nagios -v /data/nagios/etc/nagios.cfg
# 取消用户认证(方便调试)
# vi /data/nagios/etc/cgi.cfg
找到use_authentication=1并把值改为0
# 修改联系人邮箱
# vi /data/nagios/etc/objects/contacts.cfg
# 定义check_nrpe命令
# vi /data/nagios/etc/objects/commands.cfg
1
2
3
4
define command{
command_name check_nrpe
command_line /data/nagios/libexec/check_nrpe -H $HOSTADDRESS$ -c $ARG1$
}
# 启动服务
# service nagios start
5. nginx 配置
Nginx 的 cgi 支持参见 Nginx fastcgi perl (pl、cgi)支持
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
server {
    listen       88;
    server_name  _;
  
    location / {
        root   /data/nagios/share;
        index  index.html index.htm index.php;
    }
  
    location ~ .*.(php|php5)?$
    {
      root /data/nagios/share;
      fastcgi_pass  127.0.0.1:9000;
      fastcgi_index index.php;
      include fcgi.conf;
    }
  
    location /nagios {
        alias /data/nagios/share;
    }
  
    location /cgi-bin/images {
        alias /data/nagios/share/images;
    }
  
    location /cgi-bin/stylesheets {
        alias /data/nagios/share/stylesheets;
    }
  
    location /cgi-bin {
        alias /data/nagios/sbin;
    }
  
    location ~ .*.(cgi|pl)?$
    {
      gzip off;
      root   /data/nagios/sbin;
      rewrite ^/nagios/cgi-bin/(.*).cgi /$1.cgi break;
      fastcgi_pass  unix:/data/nginx/logs/perl-fcgi.sock;
      fastcgi_index index.cgi;
      include fcgi.conf;
      fastcgi_read_timeout   60;
    }
}
二、nagios被控端安装配置
yum install opensll-devel
1. nagios-plugins安装
# groupadd nagios
# useradd nagios -M -s /sbin/nologin -g nagios
# tar zxvf nagios-plugins-1.4.15.tar.gz
# cd nagios-plugins-1.4.15
# ./configure --prefix=/usr/local/nagios --with-nagios-user=nagios --with-nagios-gourp=nagios && make && make install
2. nrpe安装
# tar zxvf nrpe-2.12.tar.gz
# cd nrpe-2.12
# ./configure --prefix=/usr/local/nagios && make && make install
3. 配置启动
# mkdir -p /usr/local/nagios/etc
# cp sample-config/nrpe.cfg /usr/local/nagios/etc/
# vi /usr/local/nagios/etc/nrpe.cfg
修改 allowed_hosts 值为监控端的IP
# 启动nrpe
/usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -d
三、监控应用
1. 监控Squid
下载check_squid脚本,将压缩包里的check_squid解压缩到监控端的/data/nagios/libexec 里
# 修改权限
# chmod 755 /data/nagios/libexec/check_squid
# 安装脚本执行的依赖perl模块
# yum -y install perl-libwww-perl
check_squid命令参数
Usage: url urluser urlpass proxy proxyport proxyuser proxypass expectstatus
url squid dst站点域名 如:http://www.xtgly.com
urluser 目标站点需要认证登录的用户名, 符号"-"代表没有
urlpass 目标站点需要认证登录的用户密码, 符号"-"代表没有
proxy squid cache服务器的IP地址或者域名
proxyport Squid cache服务器监听的端口,默认3128
proxyuser squid cache服务器需要认证登录的用户名, 符号"-"代表没有
proxypass squid cache服务器需要认证登录的用户名, 符号"-"代表没有
expectstatus HTTP协议返回的HTTP code, 符号"2"代表从2开始
# 测试
# /data/nagios/libexec/check_squid http://www.xtgly.com - - 74.82.*.* 80 - - 2
OK - Status: 200 OK
# nagios监控端配置
# vi /data/nagios/etc/objects/commands.cfg 加入
1
2
3
4
define command {
command_name check_squid
command_line $USER1$/check_squid $ARG1$ $ARG2$ $ARG3$ $HOSTADDRESS$ $ARG4$ $ARG5$ $ARG6$ $ARG7$
}
# vi /data/nagios/etc/objects/hosts.cfg 加入
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
define host{
use                     linux-server
host_name               74.82.*.*
alias                   74.82.*.*
address                 74.82.*.*
}
define hostgroup{
hostgroup_name          CDN
alias                   CDN Servers
members                 74.82.*.*
}
define service{
use                     generic-service
host_name               74.82.*.*
service_description     Squid
check_command           check_squid!http://www.xtgly.com!-!-!80!-!-!2
notifications_enabled   0
}

2.监控mysql
添加数据库监控帐号密码

mysql> CREATE USER 'jk'@'127.0.0.1' IDENTIFIED BY 'jk';
mysql> flush privileges;

# vi /data/nagios/etc/objects/commands.cfg 加入

1
2
3
4
define command{
 command_name check_mysql
 command_line $USER1$/check_mysql -H $ARG1$ -P $ARG2$ -u $ARG3$ -p $ARG4$
}

# vi /data/nagios/etc/objects/hosts.cfg 加入

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
define host{
 use                     linux-server
 host_name               74.82.*.*
 alias                   74.82.*.*
 address                 74.82.*.*
 }
define hostgroup{
 hostgroup_name          74.82.*.*
 alias                   DirectAdmin
 members                 74.82.*.*
 }
define service{
 use                     generic-service
 host_name               74.82.*.*
 service_description     mysql
 check_command           check_mysql!74.82.*.*!3306!ptjk!ptjk3310000
 notifications_enabled   0
}

3.监控apache状态
apache服务端添加状态显示

1
2
3
4
5
SetHandler server-status
Order deny,allow
Deny from all
Allow from localhost

nagios服务端操作

# 安装终端web浏览器
# yum install lync
# 下载check_apachestatus,并放至/data/nagios/libexec目录下
# cd /data/nagios/libexec
# wget http://exchange.nagios.org/components/com_mtree/attachment.php?link_id=1376&cf_id=24
# mv check_apachestatus.pl check_apachestatus (如果nagios不在/usr/local目录下,注意修改这个脚本里的路径)
# chown nagios:nagios check_apachestatus
# chmod 755 check_apachestatus
# vi /data/nagios/etc/objects/commands.cfg 加入

1
2
3
4
define command{
command_name check_apachestatus
command_line $USER1$/check_apachestatus -H $HOSTADDRESS$ -p $ARG1$
}

# vi /data/nagios/etc/objects/hosts.cfg 加入

1
2
3
4
5
6
7
define service{
use                     generic-service
host_name               74.82.*.*
service_description     http
check_command           check_apachestatus!81
notifications_enabled   1
}

4.监控rsync
# 下载脚本check_rsync,放至/data/nagios/libexec,注意脚本里的nagios路径需要修改
# cd /data/nagios/libexec
# wget http://exchange.nagios.org/components/com_mtree/attachment.php?link_id=307&cf_id=29
# chown nagios:nagios check_rsync
# chmod 755 check_rsync
# vi /data/nagios/etc/objects/commands.cfg 加入

1
2
3
4
define command{
command_name check_rsync
command_line $USER1$/check_rsync -H $HOSTADDRESS$ -p $ARG1$
}

# vi /data/nagios/etc/objects/hosts.cfg 加入

1
2
3
4
5
6
7
define service{
use                     generic-service
host_name               74.82.*.*
service_description     rsync
check_command           check_rsync!873
notifications_enabled   1
}

5.监控nginx
# nginx服务端添加状态显示

1
2
3
4
5
6
7
8
server {
listen 84;
server_name _;
location / {
stub_status on;
access_log off;
}
}

# 下载check_nginx,并放至/data/nagios/libexec目录下
# cd /data/nagios/libexec

# wget http://www.nginxs.com/download/check_nginx
# chown nagios:nagios check_nginx
# chmod 755 check_nginx
# vi /data/nagios/etc/objects/commands.cfg 加入

1
2
3
4
define command{
command_name check_nginx
command_line $USER1$/check_nginx -U $ARG1$
}

# vi /data/nagios/etc/objects/hosts.cfg 加入

1
2
3
4
5
6
7
define service{
use                     generic-service
host_name               74.82.*.*
service_description     http
check_command           check_nginx!74.82.*.*:84
notifications_enabled   1
}

四、nagios cacti npc 整合 (注意php要支持pho-mysql)

# wget http://downloads.sourceforge.net/project/nagios/ndoutils-1.x/ndoutils-1.4b9/ndoutils-1.4b9.tar.gz?r=http%3A%2F%2Fsourceforge.net%2Fprojects%2Fnagios%2Ffiles%2Fndoutils-1.x%2Fndoutils-1.4b9%2F&ts=1304223824&use_mirror=nchc
# tar zxvf ndoutils-1.4b9.tar.gz
# cd ndoutils-1.4b9
# vi include/config.h
找到261行

 

1
2
#include
#include

修改为

1
2
#include
#include

# ./configure --enable-mysql --disable-pgsql --with-mysql=/data/mysql --with-mysql-lib=/data/mysql/lib
# make
# cp src/ndomod-3x.o /data/nagios/bin/ndomod.o
# cp src/ndo2db-3x /data/nagios/bin/ndo2db
# cp src/log2ndo /data/nagios/bin
# cp src/file2sock /data/nagios/bin
# cp config/ndomod.cfg-sample /data/nagios/etc/ndomod.cfg
# cp config/ndo2db.cfg-sample /data/nagios/etc/ndo2db.cfg
# chown -R nagios:nagios /data/nagios/
# vi /data/nagios/etc/ndomod.cf 主要修改路径,本例如下:

01
02
03
04
05
06
07
08
09
10
11
12
13
instance_name=default
output_type=tcpsocket
output=127.0.0.1
tcp_port=5668
use_ssl=0
output_buffer_items=5000
buffer_file=/data/nagios/var/ndomod.tmp
file_rotation_interval=14400
file_rotation_timeout=60
reconnect_interval=15
reconnect_warning_interval=15
data_processing_options=-1
config_output_options=2

# vi /data/nagios/etc/ndo2db.cfg 主要修改mysql帐号密码,本例如下:

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
lock_file=/data/nagios/var/ndo2db.lock
ndo2db_user=nagios
ndo2db_group=nagios
socket_type=tcp
socket_name=/data/nagios/var/ndo.sock
tcp_port=5668
use_ssl=0
db_servertype=mysql
db_host=localhost
db_port=3306
db_name=cacti
db_prefix=npc_
db_user=root
db_pass=iammysql
max_timedevents_age=1440
max_systemcommands_age=10080
max_servicechecks_age=10080
max_hostchecks_age=10080
max_eventhandlers_age=44640
max_externalcommands_age=44640
debug_level=-1
debug_verbosity=2
debug_file=/data/nagios/var/ndo2db.debug
max_debug_file_size=1000000

# vi /data/nagios/etc/nagios.cfg 添加以下内容

1
2
3
4
5
check_external_commands=1
command_check_interval=-1
event_broker_options=-1
broker_module=/data/nagios/bin/ndomod.o config_file=/data/nagios/etc/ndomod.cfg
process_performance_data=1

# ln -s /data/mysql/lib/mysql/libmysqlclient.so.16 /usr/lib/
# 启动ndo2db
# /data/nagios/bin/ndo2db -c /data/nagios/etc/ndo2db.cfg

NPC安装
# cd /tmp
# wget http://www.constructaegis.com/downloads/npc-2.0.4.tar.gz
# tar zxvf npc-2.0.4.tar.gz
# mv npc /data/nginx/html/cacti/plugins/
# chown -R www:www /data/nginx/html/
以admin登录cacti,在Plugin Management安装并启用npc插件.
进入mysql的cacti数据库,执行以下语句:

 

1
2
3
4
5
6
7
8
ALTER TABLE `npc_hostchecks` ADD COLUMN `long_output` varchar(8192) NOT NULL default '' AFTER `output`;
ALTER TABLE `npc_hoststatus` ADD COLUMN `long_output` varchar(8192) NOT NULL default '' AFTER `output`;
ALTER TABLE `npc_servicechecks` ADD COLUMN `long_output` varchar(8192) NOT NULL default '' AFTER `output`;
ALTER TABLE `npc_servicestatus` ADD COLUMN `long_output` varchar(8192) NOT NULL default '' AFTER `output`;
ALTER TABLE `npc_statehistory` ADD COLUMN `long_output` varchar(8192) NOT NULL default '' AFTER `output`;
ALTER TABLE `npc_eventhandlers` ADD COLUMN `long_output` varchar(8192) NOT NULL default '' AFTER `output`;
ALTER TABLE `npc_systemcommands` ADD COLUMN `long_output` varchar(8192) NOT NULL default '' AFTER `output`;
ALTER TABLE `npc_notifications` ADD COLUMN `long_output` varchar(8192) NOT NULL default '' AFTER `output`;