RHEL6 Nagios 部署
系统环境:RHEL6 x86_64 selinux and iptables disabled
nagios 主机:192.168.0.5
mysql 主机: 192.168.0.1
一,监控本机
安装所需要软件
yum install httpd gcc make perl-ExtUtils-Embed.x86_64 -y
rpm -ivh gd-devel-2.0.35-10.el6.x86_64.rpm
 
 
useradd nagios                       运行 nagios 服务的用户
usermod -G nagios apache           使 apache 用户对 nagios 目录具有写权限,不然 web 页面操作失败
 
tar jxf nagios-cn-3.2.3.tar.bz2        解压nagios
cd nagios-cn-3.2.3
./configure –-enable-embedded-perl 编译
make all
make install
make install-init
make install-commandmode
make install-config
make install-webconf
 
yum install mysql-devel openssl-devel -y 编译所需软件包
tar zxf nagios-plugins-1.4.15.tar.gz        nagios 插件安装
cd nagios-plugins-1.4.15
./configure —with-nagios-user=nagios —with-nagios-group=nagios
--enable-extra-opts —enable-libtap —enable-perl-modules
Make && make install
 
 
vi /usr/local/nagios/etc/nagios.cfg               #nagios配置
cfg_file=/usr/local/nagios/etc/objects/hosts.cfg #新建 hosts.cfg 文件,存放主机与主机组定义
cfg_file=/usr/local/nagios/etc/objects/services.cfg #新建 services.cfg 文件,存放服务与服务组定义
#cfg_file=/usr/local/nagios/etc/objects/localhost.cfg#注释 localhost.cfg 文件
如下图:
 

 
cp -r localhost.cfg hosts.cfg
vi /usr/local/nagios/etc/objects/hosts.cfg
去掉service部分,只留下想要的的hosts部分
define host{
        use                     linux-server            ; 给网络设备用的
        host_name               Desktop5
        alias                   nagios
        address                 192.168.0.5
        2d_coords               100,200
        3d_coords               100,200,100
        }
define hostgroup{
        hostgroup_name linux-servers ; The name of the hostgroup
        alias           Linux Servers ; Long name of the group
        members         *     ; Comma separated list of hosts that belong to this group
        }
如下图:
 

cp -r localhost.cfg services.cfg
vi /usr/local/nagios/etc/objects/services.cfg
去掉hosts部分,只留下想要的的service部分
 
define servicegroup{
        servicegroup_name    系统负荷检查
        alias                  负荷检查
      members               Desktop5,进程总数,Desktop5,登录用户数,Desktop5,根分区,Desktop5,交换空间利用率
        }
define servicegroup{
        servicegroup_name           全部联通性检查
        alias                          联通性检查
        members                     Desktop5,PING
        }
 
define service{
        use                             local-service        
        host_name                      *
        service_description              PING
        check_command                 check_ping!100.0,20%!500.0,60%
        }
 
define service{                             
use                               local-service         
        host_name                       Desktop5
        service_description               根分区
        check_command                  check_local_disk!20%!10%!/
        }
 
                                                                                                                      
define service{
        use                             local-service        
        host_name                      Desktop5
        service_description              进程总数
        check_command                check_local_procs!250!400!RSZDT
        }
 
define service{
        use                              local-service        
        host_name                      Desktop5
        service_description              系统负荷
 check_command                     check_local_load!5.0,4.0,3.0!10.0,6.0,4.0
        }
define service{
        use                                local-service        
        host_name                        Desktop5
        service_description                SSH
        check_command                   check_tcp!22!1.0!10.0
        notifications_enabled              0          
}
 
/usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg
检测语法的正确性
/etc/init.d/httpd restart
/etc/init.d/nagios restart
htpasswd -c /usr/local/nagios/etc/htpasswd.users nagiosadmin #nagios 监控页面访问用户和密码
就能访问http://192.168.0.5/nagio
 

 
二.监控另外一台机器的数据库(mysql)
Yum install mysql* -y
/etc/init.d/mysql restart
Mysql
mysql>create database nagdb;                                         创建用户
mysql> grant select on nagdb.* to nagdb@'192.168.0.5';              使其具有查看权限
mysql> flush privileges;                                                刷新数库                                               
 
在nagios服务器中查看是否能够链接到mysql主机/usr/local/nagios/libexec
/usr/local/nagios/libexec/check_mysql -H 192.168.0.1 -u nagdb -d nagdb
 

在 nagios 主机上添加对 mysql 服务监控的定义
vi /usr/local/nagios/etc/objects/commands.cfg        (添加如下行)
define command{
         command_name   check_mysql
       command_line   $USER1$/check_mysql -H $HOSTADRESS$ -u nagdb -d nagdb
}
vi /usr/local/nagios/etc/objects/hosts.cfg             (添加如下行)   
define host{
       use                     linux-server
       host_name               mysql服务器
       alias                   Desktop1
       address                 192.168.0.1
       2d_coords                500,200
        3d_coords               500,200,100
}
vi /usr/local/nagios/etc/objects/services.cfg (添加如下行)
define service{
        use                           local-service
        host_name                      mysql服务器
        check_command                 check_mysql
 }
 
/usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg
/etc/init.d/httpd restart
/etc/init.d/ngios restart
就能访问http://192.168.0.5/nagio
 

 
 
 
 
 
三.Nagios 通过 NRPE 监控远程主机系统状况
 
 

 

 
远程主机设定
useradd nagios
 
tar xzf nagios-plugins-1.4.15.tar.gz
cd nagios-plugins-1.4.15
./configure —with-nagios-user=nagios --with-nagios-group=nagios --enable-extra-opts --enable-perl-modules —with-openssl --enable-libtap
 
make
make install
 
yum install xinetd -y
 
tar zxf nrpe-2.12.tar.gz
cd nrpe-2.12
./configure
make all
make install-plugin
make install-daemon
make install-daemon-config
make install-xinetd
 
vi /etc/xinetd.d/nrpe 修改如下行
only_from= 192.168.0.5                         #nagios 主机 ip 地址
 
vi /etc/services      添加如下行
nrpe                 5666/tcp                   nrpe务监听端口
 
vi /usr/local/nagios/etc/nrpe.cfg                  修改如下行
command[check_disk]=/usr/local/nagios/libexec/check_disk -w 20% -c 10% -p /
#根分区监测
service xinetd restart
netstat -antlp |grep 5666
tcp 0 0 :::5666
#看到如下输出说明 NRPE 正常启动,并确保防火墙允许此端口
:::*
LISTEN
26359/xinetd
 
 
监控主机设定
tar zxf nrpe-2.12.tar.gz
cd nrpe-2.12
./configure --with-nagios-user=nagios --with-nagios-group=nagios
make all
make install-plugin
/usr/local/nagios/libexec/check_nrpe -H 192.168.0.1
NRPE v2.12
#如看到如下输出说明连接正常
 
vi /usr/local/nagios/etc/objects/commands.cfg 添加如下行
define command{
command_name        check_nrpe
command_line         $USER1$/check_nrpe -H $HOSTADDRESS$ -c $ARG1$
}
 
vi /usr/local/nagios/etc/objects/services.cfg 添加如下行
define service{
        use                           local-service
        host_name                     mysql服务器
        check_command                 check_mysql
        service_description            mysql服务
}
define service{
       use                                generic-service
       host_name                          mysql服务器
       service_description                CPU 使用率
       check_command                      check_nrpe!check_load
}
define service{
       use                                generic-service
       host_name                          mysql服务器
       service_description                登录用户数量
       check_command                      check_nrpe!check_users
}
 
define service{
        use                               generic-service
host_name                          mysql服务器
service_description                  根使用率
check_command                     check_nrpe!check_disk
}
 
/usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg
service nagios reload
#检测无误后重载 nagios
 
 
 
Nagios 整合飞信

http://www.it-adv.net/ 下载 fetion 主程序与支持库
步骤
(1)IP:192.168.0.85:
cd ~
lftp 127.0.0.1
cd /pub/docs/nagios
get fetion cenos54X64_20101113.rar
cd /pub/docs
get rarlinux-3.8.0.tar.gz
 

cp fetion /usr/local/nagios/libexec
cd /usr/local/nagios/libexec
chmod +x fetion
chown nagios.nagios fetion
(2)执行 /usr/local/nagios/libexec/fetion
出现没有/lib/ld-linux.so
yum install /lib/ld-linux.so.2 -y
cd /usr/local/nagios/libexec/
./fetion
cd ~
tar zxf rarlinux-3.8.0.tar.gz -C /opt/
cd /opt/rar/unrar
/opt/rar/unrar x cenos54X64_20101113.rar(可能会出错,需要安装以下的包,然后重新执行此步。)yum install libstdc++.so.6
(3) cd ~
lftp 192.168.0.254
cd /pub/docs/nagios
get linuxso_20111113.tar.gz
tar zxf linuxso_20111113.tar.gz -C /usr/local/lib(将库文件放到/usr/local/ib中。ldconfig /usr/local/lib使其生效。)
cd /usr/local/lib
ls 会有4个so文件。
chmod +x *
需要的时候可能需要安装: yum install libgssapi_krb5.so.2 libz.so.1 zlib文件。
ldconfig /usr/local/lib
/usr/local/nagios/libexec/fetion 7执行成功。

(4)vi /etc/ld.so.conf.d/mysql-x86_64.conf
/usr/lib64/mysql
(5) vi fetion.conf
/usr/local/lib
ldconfig
(6) su - nagios
/usr/local/nagios/libexec/fetion --mobile=158029XXXXX --pwd=XXXXXX(飞信密码) --to=15802955XXXX --msg-utf8="hello"
然后你去/usr/local/nagios/libexec找到1509154xxxx.jpg,然后将其复制到桌面,查看其验证码,然后写上去,你就会在1509154xxxx收到一个由mobile=1509154xxxx发来的信息哦!)
你第一次用验证码,第二次就不用验证码。
实验五:
fetion 与 nagios 配置
步骤(1)cd /usr/local/nagios/libexec/
vi fetion.sh
/usr/local/nagios/libexec/fetion --mobile=158029XXXXX --pwd=qqXXXXX --to="$1" --msg-utf8="$2" > /dev/null
(2) chmod +x fetion.sh
chown ngios.nagios fetion.sh
su - nagios
cd /usr/local/nagios/libexec/
./fetion.sh 1509154xxxx "hello" (测试给1509154xxxx发送小心“hello“)
此时因为你重新调用的程序,需要重新找到验证码并输入.....
然后就可以收到信息喽......
(3) cd /usr/local/nagios/etc/objects/
vi templates.cfg

Generic contact definition template - This is NOT a real contact, just a template!
define contact{
name generic-contact ; The name of this contact template
.
.
.
service_notification_commands notify-service-by-email,notify-service-by-fetion ; send service notifications via email
host_notification_commands notify-host-by-email,notify-host-by-fetion ; send host notifications via email
..........................
}
(4) vi /usr/local/nagios/etc/objects/commands.cfg
增加以下内容:
# 'notify-host-by-fetion' command definition
define command{
command_name notify-host-by-fetion
command_line $USER1$/fetion.sh $CONTACTPAGER$ "$NOTIFICATIONTYPE$ Host Alert: $HOSTNAME$ is $HOSTSTATE$"
}

# 'notify-service-by-fetion' command definition
define command{
command_name notify-service-by-fetion
command_line $USER1$/fetion.sh $CONTACTPAGER$ "$NOTIFICATIONTYPE$: $HOSTALIAS$/$SERVICEDESC$ is $SERVICESTATE$"
}
(5)vi /usr/local/nagios/etc/objects/contacts.cfg

define contact{
.
.
.
email 997365139@qq.com ; <<***** CHANGE THIS TO YOUR EMAIL ADDRESS ******
pager 1509154xxxx(接收飞信信息的 手机号码
)
}
(6)测试:
/usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg
#校验后重新载入 nagios 配置
/etc/init.d/nagios reload
此时你若把被监控端的: service mysqld stop此时会产生报警。然后下次检测的时候你的 手机和邮箱会同时收到邮件和信息哦。。