xxxmenger 发表于 2019-1-15 13:48:43

Nagios部署

       Nagios配置详解

一、nagios简介
1、nagios基本介绍
官方网站:http://www.nagios.org/
   Nagios是一款用于系统和网络监控的应用程序。它可以在你设定的条件下对主机和服务进行监控,在状态变差和变好的时候给出告警信息。

2、Nagios更进一步的特征
1.监控网络服务(SMTP、POP3、HTTP、NNTP、PING等);
2.监控主机资源(处理器负荷、磁盘利用率等);
3.简单地插件设计使得用户可以方便地扩展自己服务的检测方法;
4.并行服务检查机制;
5.具备定义网络分层结构的能力,用"parent"主机定义来表达网络主机间的关系,这种关系可被用来发现和明晰
6.当服务或主机问题产生与解决时将告警发送给联系人(通过EMail、短信、用户定义方式);
7.具备定义事件句柄功能,它可以在主机或服务的事件发生时获取更
8.自动的日志回滚;
9.可以支持并实现对主机的冗余监控;
10.可选的WEB界面用于查看当前的网络状态、通知和故障历史、日志文件等;
Nagios通常由一个主程序(Nagios)、一个插件程序(Nagios-plugins)和四个可选的ADDON(NRPE、NSCA、
NSClient++和NDOUtils)组成。Nagios的监控工作都是通过插件实现的,因此,Nagios和Nagios-plugins是服务
二、nagios监控部署
1、下载所需软件包
Nagios主程序
1.nagios.3.2.0.tar.gz Nagios的主程序
#wget http://prdownloads.sourceforge.net/sourceforge/nagios/nagios.3.2.0.tar.gz
Nagios 的插件
#wget http://prdownloads.sourceforge.net/sourceforge/nagiosplug/nagios-plugins-1.4.13.tar.gz
Nagios代理检测程序
#wget http://prdownloads.sourceforge.net/sourceforge/nagios/nrpe-2.12.tar.gz
# ls
# ls
nagios-3.2.0.tar.gz nagios-plugins-1.4.13.tar.gz nrpe-2.12.tar.gz
安装依赖包
#yum -y install httpd php gcc glibc glibc-common gd gd-devel

2、创建Nagios账户和组
# useradd -m nagios
#groupadd nagcmd
#usermod -a -G nagcmd nagios
#usermod -a -G nagcmd apache

3、编译安装Nagios
# tar xvf nagios-3.2.0.tar.gz -C /usr/local/src/
# cd /usr/local/src/nagios-3.2.0/
# ./configure --with-command-group=nagcmd --with-nagios-user=nagios --with-nagios-group=nagios
# make all
# make install
# make install-init (生成init启动脚本)
# make install-config   (生成init启动脚本)
# make install-commandmode (设置相应的权限)
# make install-webconf (生成Apache配置文件nagios.conf)

4、为Nagios设置web验证密码
# htpasswd -c /usr/local/nagios/etc/htpasswd.users nagiosadmin
注:第一次添加用时要使用“-c”参数,以后再添加用户就不需在使用“-c”。

5、设置Nagios开机启动
# chkconfig --add nagios
# chkconfig nagios on

6、修改selinux设置
两种方法:
(1) 直接关闭selinux
# cat /etc/sysconfig/selinux
SELINUX=disabled
# setenforce 0
(2)根据selinux提示的结果设置selinux
chcon -R -t httpd_sys_content_t /usr/local/nagios/share/
chcon -R -t httpd_sys_content_t /usr/local/nagios/sbin/

三、安装Nagios插件Nagios-plugin
Nagios-plugin插件提供了很多监控的模版,也可以自己手写。
# tar xvf nagios-plugins-1.4.14.tar.gz -C /usr/local/src/
# cd /usr/local/src/nagios-plugins-1.4.14/
# ./configure --prefix=/usr/local/nagios --with-nagios-user=nagios--with-nagios-group=nagios
# make && make install

四、安装nrpe
1、Nagios监控的方式
    第一部分是主机外监控,比如:主机是否存活,WEB服务是否正常,MySQL服务是否正常等内容,再主机外通过访问
其端口即可得知。这些监控命令再安装 nagios-plugins-1.4.13.tar.gz时已经生成了,再/usr/local/nagios/libexec
目录下。
    第二部分是主机内监控,比如:要监控服务器的进程、磁盘使用等功能。这些功能的实现要依靠nrpe了,nrpe的工
作模式是C/S模式,在被监控主机中,开启nrpe监听,当听到监控服务器上所发出的命令,让它检查该服务器上的硬盘
使用信息时,它就会执行,并把信息传回,监控服务器。

2、安装nrpe
# tar xvf nrpe-2.12.tar.gz -C /usr/local/src/
# cd /usr/local/src/nrpe-2.12/
# ./configure
# make all
# make install-plugin
# make install-daemon
# make install-daemon-config
# make install-xinetd

3、配置nrpe
# vim /etc/xinetd.d/nrpe
# default: on
# description: NRPE (Nagios Remote Plugin Executor)
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.0.10 192.168.0.9
}
在only_from 添加要监控的主机的IP地址,中间以空格隔开。

4、添加端口
# vim /etc/services最后
nrpe 5666/tcp #nrpe

5、重新启动xinetd服务
# service xinetd restart
# netstat -anplt | grep 5666 | grep -v grep
tcp      0      0 0.0.0.0:5666                0.0.0.0:*                   LISTEN      23187/xinetd   

6、修改配置文件commands.cfg加入对nrpe的支持
# vi /usr/local/nagios/etc/objects/commands.cfg
#nrpe set
define command{
command_name check_nrpe
command_line /usr/local/nagios/libexec/check_nrpe -H $HOSTADDRESS$ -c $ARG1$
}
还可自行添加例如:
#ntp set
define command{
command_name check_ntp
command_line /usr/local/nagios/libexec/check_ntp -H $HOSTADDRESS
}

7、nrpe监控的模版
# vim /usr/local/nagios/etc/nrpe.cfg
(默认有下面四个命令)
command=/usr/local/nagios/libexec/check_users -w 5 -c 10
command=/usr/local/nagios/libexec/check_load -w 15,10,5 -c 30,25,20
command=/usr/local/nagios/libexec/check_disk -w 20% -c 10% -p /dev/hda1
command=/usr/local/nagios/libexec/check_procs -w 5 -c 10 -s Z
command=/usr/local/nagios/libexec/check_procs -w 150 -c 200
可以添加,例如:
command=/usr/local/nagios/libexec/check_swap -w 20% -c 10%
command=/usr/local/nagios/libexec/check_mem -w 80% -c 90%
command=/usr/local/nagios/libexec/check_gwapp_cm
command=/usr/local/nagios/libexec/check_gwapp_cu
command=/usr/local/nagios/libexec/check_gwapp_cnn

8、如何使用nrpe监控
在services.cfg里面添加服务“check_command check_nrpe!check_load
”即可,例:
define service {
host_name Nagios-Server
service_description check_load
check_period 24x7
max_check_attempts 4
normal_check_interval 3
retry_check_interval 2
contact_groups sagroup
notification_interval 10
notification_period 24x7
notification_options w,u,c,r
check_command check_nrpe!check_load
}

五、检测主机是否存活
1、配置文件概述
如果安装上面的步骤,安装完成后,配置文件在安装时放在了/usr/local/nagios/etc/目录下
# cd /usr/local/nagios/
#l1
-rw-rw-r-- 1 nagios nagios 11408 08-30 11:55 cgi.cfg (CGI配置文件)
-rw-r--r-- 1 root root 26 08-30 11:56 htpasswd.users (Apache的验证密码文件)
-rw-rw-r-- 1 nagios nagios 43776 08-30 11:55 nagios.cfg (主配置文件)
drwxrwxr-x 2 nagios nagios 4096 08-30 11:55 objects (对象定义文件目录)
-rw-rw---- 1 nagios nagios 1340 08-30 11:55 resource.cfg (资源配置文件)
我们修要修改的的是nagios.cfg 和 objects 目录下的文件,来检测主机是否存活。

2、主配置文件nagios.cfg的配置
主配置文件的内容很多,对于这个版本,我们需要修改和添加的主要是对象配置文件,即:cfg_file=
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/contactgroups.cfg (添加此行 联系人组定义文件)
cfg_file=/usr/local/nagios/etc/objects/hosts.cfg (添加此行 主机定义文件)
cfg_file=/usr/local/nagios/etc/objects/hostgroups.cfg (添加此行 主机组定义文件)
cfg_file=/usr/local/nagios/etc/objects/services.cfg (添加此行 服务定义文件)
cfg_file=/usr/local/nagios/etc/objects/timeperiods.cfg (时间周期定义文件)
cfg_file=/usr/local/nagios/etc/objects/templates.cfg
# Definitions for monitoring the local (Linux) host
#cfg_file=/usr/local/nagios/etc/objects/localhost.cfg (注释掉此行)

3、主机定义文件的配置
# vi objects/hosts.cfg
define host{
            host_name Nagios-Server
            alias Nagios Server
            address 192.168.0.11
            check_command check-host-alive
            check_interval 5
            retry_interval 1
            max_check_attempts 5
            check_period 24x7
            process_perf_data 0
            retain_nonstatus_information 0
            contact_groups sagroup
            notification_interval 30
            notification_period 24x7
            notification_options d,u,r
          }
define host{
            host_name Linux-Server
            alias Test Server
            address 192.168.0.10
            check_command check-host-alive
            check_interval 5
            retry_interval 1
            max_check_attempts 5
            check_period 24x7
            process_perf_data 0
            retain_nonstatus_information 0
            contact_groups sagroup
            notification_interval 30
            notification_period 24x7
            notification_options d,u,r
          }
define host{
            host_name Win-Server
            alias Test Server
            address 192.168.0.9
            check_command check-host-alive
            check_interval 5
            retry_interval 1
            max_check_attempts 5
            check_period 24x7
            process_perf_data 0
            retain_nonstatus_information 0
            contact_groups sagroup
            notification_interval 30
            notification_period 24x7
            notification_options d,u,r
          }

4、主机组定义文件的配置
# vi objects/hostgroups.cfg
define hostgroup {
hostgroup_name System-Admin
alias system Admin
members Nagios-Server,Linux-Server,Win-Server
}
在“members”后面添加以逗号分割。

5、服务定义文件的配置
# vi objects/services.cfg
#--------------------------Nagios-Server----------------------------------#
define service {
host_name Nagios-Server
service_description check-host-alive
check_period 24x7
max_check_attempts 4
normal_check_interval 3
retry_check_interval 2
contact_groups sagroup
notification_interval 10
notification_period 24x7
notification_options w,u,c,r
check_command check-host-alive
}
define service {
host_name Nagios-Server
service_description check-ssh
check_period 24x7
max_check_attempts 4
normal_check_interval 3
retry_check_interval 2
contact_groups sagroup
notification_interval 10
notification_period 24x7
notification_options w,u,c,r
check_command check_ssh
}
define service {
host_name Nagios-Server
service_description check_load
check_period 24x7
max_check_attempts 4
normal_check_interval 3
retry_check_interval 2
contact_groups sagroup
notification_interval 10
notification_period 24x7
notification_options w,u,c,r
check_command check_nrpe!check_load
}
define service {
host_name Nagios-Server
service_description check_mem
check_period 24x7
max_check_attempts 4
normal_check_interval 3
retry_check_interval 2
contact_groups sagroup
notification_interval 10
notification_period 24x7
notification_options w,u,c,r
check_command check_nrpe!check_mem
}
define service {
host_name Nagios-Server
service_description check_users
check_period 24x7
max_check_attempts 4
normal_check_interval 3
retry_check_interval 2
contact_groups sagroup
notification_interval 10
notification_period 24x7
notification_options w,u,c,r
check_command check_nrpe!check_users
}
define service {
host_name Nagios-Server
service_description check_total_procs
check_period 24x7
max_check_attempts 4
normal_check_interval 3
retry_check_interval 2
contact_groups sagroup
notification_interval 10
notification_period 24x7
notification_options w,u,c,r
check_command check_nrpe!check_total_procs
}
define service {
host_name Nagios-Server
service_description check_zombie_procs
check_period 24x7
max_check_attempts 4
normal_check_interval 3
retry_check_interval 2
contact_groups sagroup
notification_interval 10
notification_period 24x7
notification_options w,u,c,r
check_command check_nrpe!check_zombie_procs
}
#--------------------------Linux-Server----------------------------------#
define service {
host_name Linux-Server
service_description check-host-alive
check_period 24x7
max_check_attempts 4
normal_check_interval 3
retry_check_interval 2
contact_groups sagroup
notification_interval 10
notification_period 24x7
notification_options w,u,c,r
check_command check-host-alive
}
define service {
host_name Linux-Server
service_description check-ssh
check_period 24x7
max_check_attempts 4
normal_check_interval 3
retry_check_interval 2
contact_groups sagroup
notification_interval 10
notification_period 24x7
notification_options w,u,c,r
check_command check_ssh
}
define service {
host_name Linux-Server
service_description check_load
check_period 24x7
max_check_attempts 4
normal_check_interval 3
retry_check_interval 2
contact_groups sagroup
notification_interval 10
notification_period 24x7
notification_options w,u,c,r
check_command check_nrpe!check_load
}
define service {
host_name Linux-Server
service_description check_mem
check_period 24x7
max_check_attempts 4
normal_check_interval 3
retry_check_interval 2
contact_groups sagroup
notification_interval 10
notification_period 24x7
notification_options w,u,c,r
check_command check_nrpe!check_mem
}
define service {
host_name Linux-Server
service_description check_users
check_period 24x7
max_check_attempts 4
normal_check_interval 3
retry_check_interval 2
contact_groups sagroup
notification_interval 10
notification_period 24x7
notification_options w,u,c,r
check_command check_nrpe!check_users
}
define service {
host_name Linux-Server
service_description check_total_procs
check_period 24x7
max_check_attempts 4
normal_check_interval 3
retry_check_interval 2
contact_groups sagroup
notification_interval 10
notification_period 24x7
notification_options w,u,c,r
check_command check_nrpe!check_total_procs
}
define service {
host_name Linux-Server
service_description check_zombie_procs
check_period 24x7
max_check_attempts 4
normal_check_interval 3
retry_check_interval 2
contact_groups sagroup
notification_interval 10
notification_period 24x7
notification_options w,u,c,r
check_command check_nrpe!check_zombie_procs
}

6、联系人定义文件配置
define contact{
contact_name nagiosadmin
use generic-contact
alias Nagios Admin
service_notification_period 24x7
host_notification_period 24x7
service_notification_options w,u,c,r
host_notification_options d,u,r
service_notification_commands notify-service-by-email这个命令在command.cfg中定义
host_notification_commands notify-host-by-email      这个命令在command.cfg中定义
email 187101*****@139.com
}

7、联系组定义文件配置
# vi objects/contactgroups.cfg
define contactgroup {

contactgroup_name sagroup
alias system administrator group
members nagiosadmin
}

8、修改目录的所有者
# chown -R nagios:nagios objects/

9、检测配置文件是否正确
# /usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg
Total Warnings: 0
Total Errors: 0
如果都是0说明配置成功了,如果有错误,可根据提示信息查找相关错误。

10、启动Nagios服务
# service httpd start
# service nagios start
在浏览器输入网址:http://192.168.0.11/nagios/

六、Nagios监控Linux客户端的配置
1、添加Nagios用户
# useradd -s /sbin/nologin nagios

2、安装Nagios插件nagios-plugin
Nagios-plugin插件提供了很多监控的模版,也可以自己手写。
# tar xvf nagios-plugins-1.4.14.tar.gz -C /usr/local/src/
# cd /usr/local/src/nagios-plugins-1.4.14/
# ./configure --prefix=/usr/local/nagios --with-nagios-user=nagios--with-nagios-group=nagios
# make && make install

3、安装nrpe
# tar xvf nrpe-2.12.tar.gz -C /usr/local/src/
# cd /usr/local/src/nrpe-2.12/
# ./configure
# make all
# make install-plugin
# make install-daemon
# make install-daemon-config
# make install-xinetd

4、配置npre
# vim /etc/xinetd.d/nrpe
# default: on
# description: NRPE (Nagios Remote Plugin Executor)
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.0.10 192.168.0.9
}
在only_from 添加监控服务器主机的IP地址。

5、添加端口
# vim /etc/services最后
nrpe 5666/tcp #nrpe

6、重新启动xinetd服务
# service xinetd restart
# netstat -anplt | grep 5666 | grep -v grep
tcp      0      0 0.0.0.0:5666                0.0.0.0:*                   LISTEN      23187/xinetd   

7、修改文件的所有者
# chown -R nagios:nagios /usr/local/nagios
# /etc/init.d/xinetd restart

七、Nagios监控Windows客户端
1、监控服务器的配置
对与Nagios监控服务器不需要什么其它的配置,添加services.cfg即可。
监控Windows 2003 Server的配置 例:
#-------------------------Win-Server----------------------------------------#
define service {
host_name Win-Server
service_description System Load
check_period 24x7
max_check_attempts 4
normal_check_interval 3
retry_check_interval 2
contact_groups sagroup
notification_interval 10
notification_period 24x7
notification_options w,u,c,r
check_command check_nt!CPULOAD!-l 5,80,90
}
define service {
host_name Win-Server
service_description Memory Usage
check_period 24x7
max_check_attempts 4
normal_check_interval 3
retry_check_interval 2
contact_groups sagroup
notification_interval 10
notification_period 24x7
notification_options w,u,c,r
check_command check_nt!MEMUSE!-w 80 -c 90
}
define service {
host_name Win-Server
service_description Disk-C Usage
check_period 24x7
max_check_attempts 4
normal_check_interval 3
retry_check_interval 2
contact_groups sagroup
notification_interval 10
notification_period 24x7
notification_options w,u,c,r
check_command check_nt!USEDDISKSPACE!-l c -w 80 -c 90
}
define service {
host_name Win-Server
service_description Disk-D Usage
check_period 24x7
max_check_attempts 4
normal_check_interval 3
retry_check_interval 2
contact_groups sagroup
notification_interval 10
notification_period 24x7
notification_options w,u,c,r
check_command check_nt!USEDDISKSPACE!-l d -w 80 -c 90
}

2、监控客户端的配置
安装NSClient++
http://sourceforge.net/projects/nscplus/
下载后,双击运行即可,安装时会提示输入Nagios服务器的IP地址,安装完成后选中启动服务即可。
file:///C:/DOCUME~1/ADMINI~1/LOCALS~1/Temp/ksohtml/wps_clip_image-20862.png
注:红色方块内填写Nagios监控服务器的IP地址。

3、效果图
file:///C:/DOCUME~1/ADMINI~1/LOCALS~1/Temp/ksohtml/wps_clip_image-8418.png
file:///C:/DOCUME~1/ADMINI~1/LOCALS~1/Temp/ksohtml/wps_clip_image-32331.png
八、nagios配置信息讲解
1、Nagios主机配置
1.主机的配置:故名思议把需要监控的主机放在该文件里,在Nagios的监控界面上就会存在这些主机,当然,还有一
些其它的设置:

define host{
             host_name Nagios-Server #设置主机的名字,该名字会出现在hostgroups.cfg和services.cfg中。
             alias Nagios Server   #一个别名而已
             address 192.168.0.11   #被监控主机的IP地址
             check_command check-host-alive#检查的命令
             check_interval 1      #检查的时间间隔
             retry_interval 1
             max_check_attempts 5
             check_period 24x7       #检查的时间段,一般的服务器都是7x24
             process_perf_data 0
             retain_nonstatus_information 0
             contact_groups sagroup#设置联系人组,当该主机出现了报警信息,就发信息给这个组
             notification_interval 10#提醒的时间间隔,这个单位是分钟哦,看过一些文档写是秒,用够就知道了。
             notification_period 24x7   #提醒的时间段
             notification_options d,u,r
}

2、主机组的配置
   分组是很有必要的,可以根据操作系统类型,功能、区域等等很多进行有效的分组,才能达到预期的目的。
define hostgroup {
                      hostgroup_name System-Admin#组名字,可以根据应用调整
                      alias system Admin
                      members Nagios-Server,Linux-Server,Win-Server

#组成员用逗号隔开,这里的组成员是hosts.cfg定义的“host_name”这个的值。

3、服务的配置
服务的配置是很重要的,也是配置最多的地方,对与每个主机要监控的服务,都在这个文件中列出,
还好的是并不复杂,只需修改“host_name”、“service_description、check_command”等一些字段。

define service {
               host_name         Nagios-Server         #主机名字,hosts.cfg中定义的
               service_description check-host-alive    #所监控服务的描述,至于怎么描述合理,就只有发挥你的主   
                                                      观能动性了。
               check_period 24x7   #检查的时间段
               max_check_attempts 4
               normal_check_interval 3
               retry_check_interval 2
               contact_groups sagroup
               notification_interval 10
               notification_period 24x7   #提醒的时间段
               notification_options w,u,c,r#在这四种情况下,通知联系人。
               check_command check-host-alive   #检查的命令,在command.cfg中定义
                }
    w—报警(warning)
    u—未知(unkown)
    c—严重(critical)
    r—从异常情况恢复正常

4、联系人的配置
联系人,顾名思义就是管理员了,根据值班的时间定义了。

    define contact{
                   contact_name nagiosadmin#联系人的名字
                   alias Nagios Admin   #别名
                   service_notification_period 24x7#服务报警的时间段
                   host_notification_period 24x7   #主机报警的时间段
                   service_notification_options w,u,c,r #同上,就是在这四种情况下报警。
                   host_notification_options d,u,r#同上。
                   service_notification_commands notify-service-by-email#服务报警发消息的命令,在command.cfg中定义。
                   host_notification_commands notify-host-by-email#主机报警发消息的命令,在command.cfg中定义。
                   email 187101*****@139.com#给谁发email呢。
            }

5、联系人组配置
联系组,定义哪些联系人分为一个组。

define contactgroup {   
                  contactgroup_name sagroup    #组名称
                  alias system administrator group#组别名
                  members nagiosadmin#组成员
      }




页: [1]
查看完整版本: Nagios部署