Nagios监控案例

2018-11-29  本文已影响11人  魔力冬瓜茶

监控本机

配置步骤

定义监控命令

~]# vim /usr/local/nagios/etc/objects/commands.cfg
define command   {
        command_name    //定义监控命令的名字(自定义)
        command_line    //定义命令具体实现的功能
 }
define command{
        command_name    check_local_users
        command_line    $USER1$/check_users -w $ARG1$ -c $ARG2$
        }

define command{
        command_name    check_http
        command_line    $USER1$/check_http -I $HOSTADDRESS$ $ARG1$
        }

定义监控对象

~]# vim /usr/local/nagios/etc/objects/localhost.cfg
//指定被监控主机信息
define host{
        use                     //监控使用的模板
        host_name               //被监控主机的主机名
        alias                   //被监控主机别名
        address                 //被监控主机IP地址
        }

//定义监控对象
define service{
        use                             //监控使用的模板
        host_name                       //被监控主机的主机名
        service_description             //描述信息
        check_command                   //使用的监控命令
        }
define host{
        use                     linux-server            
        host_name               localhost
        alias                   localhost
        address                 127.0.0.1
        }

define service{
        use                             local-service
        host_name                       localhost
        service_description             Current Users
        check_command                   check_local_users!20!50 #!号参数间隔符号
        }

加载对象配置文件

~]# vim /usr/local/nagios/etc/nagios.cfg 
cfg_file=/usr/local/nagios/etc/objects/commands.cfg
cfg_file=/usr/local/nagios/etc/objects/contacts.cfg
cfg_file=/usr/local/nagios/etc/objects/timeperiods.cfg
cfg_file=/usr/local/nagios/etc/objects/templates.cfg
cfg_file=/usr/local/nagios/etc/objects/localhost.cfg

//检查配置文件是否有语法错误
~]# cd /usr/local/nagios/bin/ 
bin]# ./nagios -v /usr/local/nagios/etc/nagios.cfg 
...
Reading configuration data...
   Read main config file okay...
...
Total Warnings: 0
Total Errors:   0

配置登录页面

~]# vim /etc/httpd/conf.d/nagios.conf 
...
<Directory "/usr/local/nagios/share">
...
   AuthName "Nagios Access"
   AuthType Basic
   AuthUserFile /usr/local/nagios/etc/htpasswd.users
   Require valid-user
</Directory>

~]# htpasswd -c /usr/local/nagios/etc/htpasswd.users nagiosadmin
New password: 
Re-type new password: 
Adding password for user nagiosadmin
~]# cat /usr/local/nagios/etc/htpasswd.users 
nagiosadmin:Aey1/M3bCP3fs

重启服务并测试

~]# /etc/init.d/nagios start
Starting nagios:This account is currently not available.
 done.
~]# service httpd restart
停止 httpd:                                               [确定]
启动 httpd:                                               [确定]

监控远程服务器

NRPE工作原理

监控案例拓扑结构

监控远端主机公有数据


~]# cd /usr/local/nagios/etc/objects/
objects]# cp localhost.cfg 149-server.cfg #此文件名可自定义有标识性即可
objects]# vim 149-server.cfg 
define host{
        use                     linux-server            
        host_name               149server
        alias                   mail.tarena.com
        address                 192.168.170.149
        }
define service{
        use                             local-service         
        host_name                       149server
        service_description             PING
        check_command                   check_ping!100.0,20%!500.0,60%
        }
define service{
        use                             local-service         
        host_name                       149server
        service_description             SSH
        check_command                   check_ssh
        notifications_enabled           0
        }
define service{
        use                             local-service         
        host_name                       149server
        service_description             Current Users
        check_command                   check_local_users!2!3
        }   
objects]# vim /usr/local/nagios/etc/nagios.cfg 
cfg_file=/usr/local/nagios/etc/objects/commands.cfg
cfg_file=/usr/local/nagios/etc/objects/contacts.cfg
cfg_file=/usr/local/nagios/etc/objects/timeperiods.cfg
cfg_file=/usr/local/nagios/etc/objects/templates.cfg
cfg_file=/usr/local/nagios/etc/objects/localhost.cfg
cfg_file=/usr/local/nagios/etc/objects/149-server.cfg
objects]# cd /usr/local/nagios/bin/
bin]# ./nagios -v /usr/local/nagios/etc/nagios.cfg 
bin]# /etc/init.d/nagios restart
bin]# ping -c 4 10.128.0.28 

监控远端主机私有数据


被监控端配置

~]# useradd nagios
~]# groupadd nagcmd
~]# usermod -G nagcmd nagios
~]# tar -zxvf nagios-plugins-1.4.14.tar.gz
~]# cd nagios-plugins-1.4.14
nagios-plugins]# ./configure
nagios-plugins]# make  && make  install
~]# cd /usr/local/nagios/   //安装目录
~]# cd /usr/local/nagios/libexec/   //监控插件目录
libexec]# ls 
check_apt      check_disk_smb  check_ide_smart     check_mrtg      check_nwstat  check_sensors  check_users  ...
~]# tar -zxvf nrpe-2.12.tar.gz 
~]# cd nrpe-2.12
nrpe-2.12]# ./configure   
nrpe-2.12]# make && make install
nrpe-2.12]# make install-plugin
nrpe-2.12]# make install-daemon
nrpe-2.12]# make install-daemon-config
nrpe-2.12]# make install-xinetd
~]# 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
~]# vim /etc/xinetd.d/nrpe    
service nrpe
{
        flags           = REUSE
        socket_type     = stream
        port            = 5666
        wait            = no
        user            = nagios
        group           = nagios
        server          = /usr/local/nagios/bin/nrpe
        server_args     = -c /usr/local/nagios/etc/nrpe.cfg --inetd
        log_on_failure  += USERID
        disable         = no
        only_from       = 127.0.0.1 192.168.170.92 #Nagios服务器IP地址
}

~]# vim  /etc/services  //指定nrpe对应端口
nrpe            5666/tcp                # TCP port service nrpe

~]# /etc/init.d/xinetd restart
停止 xinetd:                                              [确定]
启动 xinetd:                                              [确定]

~]# netstat -utnalp  |  grep  :5666
tcp        0      0 0.0.0.0:5666        0.0.0.0:*        LISTEN      29488/xinetd
~]# cd /usr/local/nagios/libexec/
libexec]# ./check_nrpe -H localhost
NRPE v2.12

配置Nagios服务器

~]# tar -zxvf  nrpe-2.12.tar.gz 
~]# cd nrpe-2.12
nrpe-2.12]# ./configure 
nrpe-2.12]# make
nrpe-2.12]# make install
nrpe-2.12]# make install-plugin
nrpe-2.12]# cd /usr/local/nagios/libexec/
libexec]# ./check_nrpe -H 192.168.170.149
NRPE v2.12
~]# cd /usr/local/nagios/etc/objects
objects]# vim commands.cfg 
define command  {
        command_name    check_nrpe
        command_line  $USER1$/check_nrpe -H $HOSTADDRESS$  -c  $ARG1$
}
~]# cd /usr/local/nagios/etc/objects/
objects]# vim 149-server.cfg 
define service  {
        use                            local-service
        host_name                      149server
        service_description            149-Users
        check_command                  check_nrpe!check_users //被监控端定义的nrpe命令
        }
objects]# cd /usr/local/nagios/bin/
bin]# ./nagios -v ../etc/nagios.cfg 

bin]# /etc/init.d/nagios  restart
上一篇 下一篇

猜你喜欢

热点阅读