安装-配置

1、 开始下载:


http://prdownloads.sourceforge.net/sourceforge/nagios/nagios-4.0.6.tar.gz

如下图所示,下载free版本的。

安装前准备工作,安装一些lib包:

[root@cache-2 root]# yum -y install httpd gcc glibc glibc-common
# yum install -y gcc glibc glibc-common gd gd-devel xinetd openssl-devel


2、开始准备nagios安装环境

(1) 添加nagios运行所需要的用户和组:

#groupadd nagcmd
         useradd -m nagios
         usermod -a -G nagcmd nagios
        
把apache加入到nagcmd组,以便于在通过web Interface操作nagios时能够具有足够的权限:
# usermod -a -G nagcmd apache


3、编译安装准备

[root@cache-2 nagios-4.0.6]# ./configure --with-command-group=nagios  
--prefix=/usr/local/nagios --enable-embedded-perl  
--with-httpd-conf=/usr/local/apache2/conf  

checking for a BSD-compatible install... /usr/bin/install -c  
checking build system type... x86_64-unknown-linux-gnu  
checking host system type... x86_64-unknown-linux-gnu  
checking for gcc... gcc  
checking for C compiler default output file name... a.out  
checking whether the C compiler works... yes  
checking whether we are cross compiling... no  
.............................  
 HTML URL:  http://localhost/nagios/  
                  CGI URL:  http://localhost/nagios/cgi-bin/  
 Traceroute (used by WAP):  /bin/traceroute  
  
  
Review the options above for accuracy.  If they look okay,  
type 'make all' to compile the main program and CGIs.


4 开始make安装
4.1 make all

[root@cache-2 nagios-4.0.6]#  make all
cd ./base && make  
make[1]: Entering directory `/root/nagios-4.0.6/base'  
gcc -Wall -I.. -g -O2 -DHAVE_CONFIG_H -DNSCORE -c -o nagios.o nagios.c  
gcc -Wall -I.. -g -O2 -DHAVE_CONFIG_H -DNSCORE   -c -o broker.o broker.c  
gcc -Wall -I.. -g -O2 -DHAVE_CONFIG_H -DNSCORE   -c -o nebmods.o nebmods  
.........................  
For more information on obtaining support for Nagios, visit:  
  
       http://support.nagios.com  
  
*************************************************************  
  
Enjoy.

最小安装会报错
[root@localhost nagios]# make all
cd ./base && make
make[1]: Entering directory `/root/nagios/base'
make[1]: *** No rule to make target `../include/locations.h', needed by `broker.o'.  Stop.
make[1]: Leaving directory `/root/nagios/base'
make: *** [all] Error 2
解决办法:yum -y install perl &&./configure --with-command-group=nagios #这两个有严格的先后顺序


4.2 make install

[root@cache-2 nagios-4.0.6]#  make install
cd ./base && make install  
make[1]: Entering directory `/root/nagios-4.0.6/base'  
make install-basic  
make[2]: Entering directory `/root/nagios-4.0.6/base'  
.....................................................  
make[2]: Entering directory `/root/nagios-4.0.6/cgi'  
for file in *.cgi; do \  
        /usr/bin/strip /usr/local/nagios/sbin/$file; \  
    done


4.3 make install-config

[root@cache-2 nagios-4.0.6]# make install-config  
/usr/bin/install -c -m 775 -o nagios -g nagios -d /usr/local/nagios/etc  
/usr/bin/install -c -m 775 -o nagios -g nagios -d /usr/local/nagios/etc/objects  
/usr/bin/install -c -b -m 664 -o nagios -g nagios sample-config/nagios.cfg /usr/local/nagios/etc/nagios.cfg  
....................................................  
/usr/bin/install -c -b -m 664 -o nagios -g nagios sample-config/template-object/printer.cfg /usr/local/nagios/etc/objects/printer.cfg  
/usr/bin/install -c -b -m 664 -o nagios -g nagios sample-config/template-object/switch.cfg /usr/local/nagios/etc/objects/switch.cfg  
  
*** Config files installed ***


4.4 make install-commandmode

[root@cache-2 nagios-4.0.6]# make install-commandmode  
/usr/bin/install -c -m 775 -o nagios -g nagios -d /usr/local/nagios/var/rw  
chmod g+s /usr/local/nagios/var/rw  
  
*** External command directory configured ***  
  
Remember, these are *SAMPLE* config files.  You'll need to read  
the documentation for more information on how to actually define  
services, hosts, etc. to fit your particular needs.


4.5 make install-init

[root@cache-2 nagios-4.0.6]# make install-init  
/usr/bin/install -c -m 755 -d -o root -g root /etc/rc.d/init.d  
/usr/bin/install -c -m 755 -o root -g root daemon-init /etc/rc.d/init.d/nagios  
  
*** Init script installed ***
[root@cache-2 nagios-4.0.6]#
加到启动项里面去:
# chkconfig --add nagios
# chkconfig --level 35 nagios on
# chkconfig --list nagios


检查主机配置文件是否正确:

/usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg


5. Check安装是否正确:

5.1说明:

切换目录到安装路径(这里是/usr/local/nagios),看是否存在etc、bin、sbin、share、var 这五个目录,如果存在则可以表明程序被正确的安装到系统了。Nagios 各个目录用途说明如下:

bin    Nagios 可执行程序所在目录

etc   Nagios 配置文件所在目录

sbin  Nagios CGI 文件所在目录,也就是执行外部命令所需文件所在的目录

share        Nagios网页文件所在的目录

libexec      Nagios 外部插件所在目录

var   Nagios 日志文件、lock 等文件所在的目录

var/archives     Nagios 日志自动归档目录

var/rw      用来存放外部命令文件的目录



5.2 check

[root@cache-2 nagios-4.0.6]# cd /usr/local/nagios  
[root@cache-2 nagios]# ll  
总用量 24  
drwxrwxr-x.  2 nagios nagios 4096 5月   5 15:31 bin  
drwxrwxr-x.  3 nagios nagios 4096 5月   5 15:32 etc  
drwxrwxr-x.  2 nagios nagios 4096 5月   5 15:31 libexec  
drwxrwxr-x.  2 nagios nagios 4096 5月   5 15:31 sbin  
drwxrwxr-x. 10 nagios nagios 4096 5月   5 15:31 share  
drwxrwxr-x.  5 nagios nagios 4096 5月   5 15:32 var


5.3 安装nagios插件

[root@cache-2 ~]# wget http://nagios-plugins.org/download/nagios-plugins-2.0.tar.gz   
[root@cache-2 ~]# tar -xvf nagios-plugins-2.0.tar.gz  
[root@cache-2 ~]# cd nagios-plugins-2.0  
[root@cache-2 ~]#./configure --with-nagios-user=nagios --with-nagios-group=nagios  --prefix=/usr/local/nagios --with-mysql  
[root@cache-2 ~]# make && make install


6、 安装配置apache和php
(1)安装apache

下载 wget http://archive.apache.org/dist/httpd/httpd-2.2.23.tar.gz
 tar -xvf httpd-2.4.7.tar.gz
 cd httpd-2.4.7
 ./configure --prefix=/usr/local/apache2
make && make install


(2) 安装PHP版本

wget http://cn2.php.net/distributions/php-5.4.26.tar.gz
tar -xvf php-5.4.26.tar.gz
cd php-5.4.26
./configure --prefix=/usr/local/php --with-apxs2=/usr/local/apache2/bin/apxs
#报了个错少了个插件libxml2-devel.x86_64
make && make install


(3) 配置apache

Apache的配置文件在 /usr/local/apache2/conf/httpd.conf ,vim打开
将

User daemon
Group daemon
修改成:
User nagios
Group nagios

将
<IfModule dir_module>
  DirectoryIndex index.html
</IfModule>
修改成:
<IfModule dir_module>
  DirectoryIndex index.html index.php
    AddType application/x-httpd-php .php
</IfModule>
 
为了安全起见:nagios的web监控页面需要经过授权才能访问,这需要增加验证配置,即在httpd.conf 文件最后添加如下信息:
#setting for nagios
ScriptAlias /nagios/cgi-bin "/usr/local/nagios/sbin"
<Directory "/usr/local/nagios/sbin">
     AuthType Basic
     Options ExecCGI
     AllowOverride None
     Order allow,deny
     Allow from all
     AuthName "Nagios Access"
     #用于此目录访问身份验证的文件
     AuthUserFile /usr/local/nagios/etc/htpasswd               
     Require valid-user
</Directory>
Alias /nagios "/usr/local/nagios/share"
<Directory "/usr/local/nagios/share">
     AuthType Basic
     Options None
     AllowOverride None
     Order allow,deny
     Allow from all
     AuthName "nagios Access"
     AuthUserFile /usr/local/nagios/etc/htpasswd
     Require valid-user
</Directory>


(4) 创建apache目录验证文件

在上面的配置中,指定了目录验证文件htpasswd,下面要创建这个文件:

[root@cache-2 php-5.4.26]#  /usr/local/apache2/bin/htpasswd -c /usr/local/nagios/etc/htpasswd david
New password:
Re-type new password:
Adding password for user david

查看认证文件的内容:

[root@cache-2 php-5.4.26]#  cat /usr/local/nagios/etc/htpasswd
david:$apr1$Pf9XIJjd$m5hR2vw70MlqwrChgN7zc/

启动apache服务:

/usr/local/apache2/bin/apachectl start
service nagios start

http://xxxx.xx:xx/nagios/,可以打开了,如下所示:






六、配置Nagios

Nagios 主要用于监控一台或者多台本地主机及远程的各种信息,包括本机资源及对外的服务等。默认的Nagios 配置没有任何监控内容,仅是一些模板文件。若要让Nagios 提供服务,就必须修改配置文件,增加要监控的主机和服务,下面将详细介绍。

6.1 默认配置文件介绍

Nagios 安装完毕后,默认的配置文件在/usr/local/nagios/etc目录下。

29

每个文件或目录含义如下表所示:

文件名或目录名用途
cgi.cfg控制CGI访问的配置文件
nagios.cfgNagios 主配置文件
resource.cfg变量定义文件,又称为资源文件,在些文件中定义变量,以便由其他配置文件引用,如$USER1$
objectsobjects 是一个目录,在此目录下有很多配置文件模板,用于定义Nagios 对象
objects/commands.cfg命令定义配置文件,其中定义的命令可以被其他配置文件引用
objects/contacts.cfg定义联系人和联系人组的配置文件
objects/localhost.cfg定义监控本地主机的配置文件
objects/printer.cfg定义监控打印机的一个配置文件模板,默认没有启用此文件
objects/switch.cfg定义监控路由器的一个配置文件模板,默认没有启用此文件
objects/templates.cfg定义主机和服务的一个模板配置文件,可以在其他配置文件中引用
objects/timeperiods.cfg定义Nagios 监控时间段的配置文件
objects/windows.cfg监控Windows 主机的一个配置文件模板,默认没有启用此文件

6.2 配置文件之间的关系

在nagios的配置过程中涉及到的几个定义有:主机、主机组,服务、服务组,联系人、联系人组,监控时间,监控命令等,从这些定义可以看出,nagios各个配置文件之间是互为关联,彼此引用的。

成功配置出一台nagios监控系统,必须要弄清楚每个配置文件之间依赖与被依赖的关系,最重要的有四点:

第一:定义监控哪些主机、主机组、服务和服务组;    
第二:定义这个监控要用什么命令实现;    
第三:定义监控的时间段;    
第四:定义主机或服务出现问题时要通知的联系人和联系人组。

6.3 配置Nagios

 为了能更清楚的说明问题,同时也为了维护方便,建议将nagios各个定义对象创建独立的配置文件:

  •  创建hosts.cfg文件来定义主机和主机组

  •  创建services.cfg文件来定义服务

  •  用默认的contacts.cfg文件来定义联系人和联系人组

  •  用默认的commands.cfg文件来定义命令

  •  用默认的timeperiods.cfg来定义监控时间段

  •  用默认的templates.cfg文件作为资源引用文件


修改几个配置文件监控本机:

contacts.cfg文件

contacts.cfg是一个定义联系人和联系人组的配置文件,当监控的主机或者服务出现故障,nagios会通过指定的通知方式(邮件或者短信)将信息发给这里指定的联系人或者使用者。

define contact{
        contact_name                    David             #联系人的名称,这个地方不要有空格
        use                             generic-contact   #引用generic-contact的属性信息,其中“generic-contact”在templates.cfg文件中进行定义
        alias                           Nagios Admin
        email                           david.tang@bsmart.cn
        }
define contactgroup{
        contactgroup_name       ts                              #联系人组的名称,同样不能空格
        alias                   Technical Support               #联系人组描述
        members                 David                           #联系人组成员,其中“david”就是上面定义的联系人,如果有多个联系人则以逗号相隔
        }


templates.cfg文件

define contact{
        name                            generic-contact    ; 联系人名称
        service_notification_period     24x7               ; 当服务出现异常时,发送通知的时间段,这个时间段"24x7"在timeperiods.cfg文件中定义
        host_notification_period        24x7               ; 当主机出现异常时,发送通知的时间段,这个时间段"24x7"在timeperiods.cfg文件中定义
        service_notification_options    w,u,c,r            ; 这个定义的是“通知可以被发出的情况”。w即warn,表示警告状态,u即unknown,表示不明状态;
                                                           ; c即criticle,表示紧急状态,r即recover,表示恢复状态;
                                                           ; 也就是在服务出现警告状态、未知状态、紧急状态和重新恢复状态时都发送通知给使用者。
        host_notification_options       d,u,r                   ; 定义主机在什么状态下需要发送通知给使用者,d即down,表示宕机状态;
                                                                ; u即unreachable,表示不可到达状态,r即recovery,表示重新恢复状态。
        service_notification_commands   notify-service-by-email ; 服务故障时,发送通知的方式,可以是邮件和短信,这里发送的方式是邮件;
                                                                ; 其中“notify-service-by-email”在commands.cfg文件中定义。
        host_notification_commands      notify-host-by-email    ; 主机故障时,发送通知的方式,可以是邮件和短信,这里发送的方式是邮件;
                                                                ; 其中“notify-host-by-email”在commands.cfg文件中定义。 
        register                        0                    ; DONT REGISTER THIS DEFINITION - ITS NOT A REAL CONTACT, JUST A TEMPLATE!
        }
define host{
        name                            generic-host    ; 主机名称,这里的主机名,并不是直接对应到真正机器的主机名;
                                                        ; 乃是对应到在主机配置文件里所设定的主机名。
        notifications_enabled           1               ; Host notifications are enabled
        event_handler_enabled           1               ; Host event handler is enabled
        flap_detection_enabled          1               ; Flap detection is enabled
        failure_prediction_enabled      1               ; Failure prediction is enabled
        process_perf_data               1               ; 其值可以为0或1,其作用为是否启用Nagios的数据输出功能;
                                                        ; 如果将此项赋值为1,那么Nagios就会将收集的数据写入某个文件中,以备提取。
        retain_status_information       1               ; Retain status information across program restarts
        retain_nonstatus_information    1               ; Retain non-status information across program restarts
        notification_period             24x7            ; 指定“发送通知”的时间段,也就是可以在什么时候发送通知给使用者。
        register                        0               ; DONT REGISTER THIS DEFINITION - ITS NOT A REAL HOST, JUST A TEMPLATE!
        }
define service{
        name                            generic-service         ; 定义一个服务名称
        active_checks_enabled           1                       ; Active service checks are enabled
        passive_checks_enabled          1                       ; Passive service checks are enabled/accepted
        parallelize_check               1                       ; Active service checks should be parallelized;
                                                                ; (disabling this can lead to major performance problems)
        obsess_over_service             1                       ; We should obsess over this service (if necessary)
        check_freshness                 0                       ; Default is to NOT check service 'freshness'
        notifications_enabled           1                       ; Service notifications are enabled
        event_handler_enabled           1                       ; Service event handler is enabled
        flap_detection_enabled          1                       ; Flap detection is enabled
        failure_prediction_enabled      1                       ; Failure prediction is enabled
        process_perf_data               1                       ; Process performance data
        retain_status_information       1                       ; Retain status information across program restarts
        retain_nonstatus_information    1                       ; Retain non-status information across program restarts
        is_volatile                     0                       ; The service is not volatile
        check_period                    24x7             ; 这里的check_period告诉nagios检查服务的时间段。
        max_check_attempts              3                ; nagios对服务的最大检查次数。
        normal_check_interval           5                ; 此选项是用来设置服务检查时间间隔,也就是说,nagios这一次检查和下一次检查之间所隔的时间;
                                                         ; 这里是5分钟。
        retry_check_interval            2                ; 重试检查时间间隔,单位是分钟。
        contact_groups                  ts           ; 指定联系人组
        notification_options            w,u,c,r          ; 这个定义的是“通知可以被发出的情况”。w即warn,表示警告状态;
                                                         ; u即unknown,表示不明状态;
                                                         ; c即criticle,表示紧急状态,r即recover,表示恢复状态;
                                                         ; 也就是在服务出现警告状态、未知状态、紧急状态和重新恢复后都发送通知给使用者。
        notification_interval           10               ; Re-notify about service problems every hour
        notification_period             24x7             ; 指定“发送通知”的时间段,也就是可以在什么时候发送通知给使用者。
        register                        0                ; DONT REGISTER THIS DEFINITION - ITS NOT A REAL SERVICE, JUST A TEMPLATE!
        }
define service{
        name                            local-service           ; The name of this service template
        use                             generic-service         ; Inherit default values from the generic-service definition
        max_check_attempts              4             ; Re-check the service up to 4 times in order to determine its final (hard) state
        normal_check_interval           5             ; Check the service every 5 minutes under normal conditions
        retry_check_interval            1             ; Re-check the service every minute until a hard state can be determined
        register                        0             ; DONT REGISTER THIS DEFINITION - ITS NOT A REAL SERVICE, JUST A TEMPLATE!
        }


cgi.cfg文件

此文件用来控制相关cgi脚本,如果想在nagios的web监控界面执行cgi脚本,例如重启nagios进程、关闭nagios通知、停止nagios主机检测等,这时就需要配置cgi.cfg文件了。
由于nagios的web监控界面验证用户为david,所以只需在cgi.cfg文件中添加此用户的执行权限就可以了,需要修改的配置信息如下:

default_user_name=david
authorized_for_system_information=nagiosadmin,david  
authorized_for_configuration_information=nagiosadmin,david  
authorized_for_system_commands=david
authorized_for_all_services=nagiosadmin,david  
authorized_for_all_hosts=nagiosadmin,david
authorized_for_all_service_commands=nagiosadmin,david  
authorized_for_all_host_commands=nagiosadmin,david


重启nagios就可以了

spacer.gif






本文转载自:

http://blog.youkuaiyun.com/hanzheng260561728/article/details/51504958

http://www.cnblogs.com/mchina/archive/2013/02/20/2883404.html