刚刚用起Nagios,把安装过程记下来,希望能对需要的朋友有帮助,在此特别感谢sery大哥的指点,安装的全过程都是参考sery大哥的blog操作的,有兴趣的朋友可以去看看, http://sery.blog.51cto.com/10037/20520非常详细。好了言归正传,咱们开动吧。
一.安装配置apache
我用的版本是 httpd-2.2.11
下载地址 http://www.onlinedown.net/soft/11529.htm
1.安装:
unzip -o httpd-2.2.11.tar.zip ;
tar -xvf httpd-2.2.11.tar.bz2 ;
cd httpd-2.2.11 ;
./configure  -prefix=/usr/local/apache;
make;
make install;
检查安装 sh /usr/local/apache/bin/apachectl -t
提示Syntax OK信息则表示安装成功.
2.配置:
编辑 /usr/local/apache/conf/httpd.conf
(1)修改Listen属性:
指定apache应用端口,这里我设置的8089,文本内容如下:
#Listen 127.0.0.1:80
Listen 8089
(2)添加如下内容至末尾:
#setting for nagios
(刚开BLOG,暂时不清楚是什么原因"Scrip t"在这里被会转义成符号".",所以下面暂时写成 " ScripAlias" 需要用的朋友别忘了加上"t",也请知道情况的朋友指点,谢谢)
ScripAlias /nagios/cgi-bin /usr/local/nagios/sbin
<Directory "/usr/local/nagios/sbin">
  Options ExecCGI
  AllowOverride None
  Order allow,deny
  Allow from all
  AuthName "Nagios Access"
  AuthType Basic
  AuthUserFile /usr/local/nagios/etc/htpasswd.users
  Require valid-user
</Directory>
Alias /nagios /usr/local/nagios/share
<Directory "/usr/local/nagios/share">
  Options None
  AllowOverride None
  Order allow,deny
  Allow from all
  AuthName "Nagios Access"
  AuthType Basic
  AuthUserFile /usr/local/nagios/etc/htpasswd.users
  Require valid-user
</Directory>

 
二.安装配置Nagios
1.安装:
我们需要下面几个包:
(1)按装nagios,顺序执行下列命令进行安装:
创建nagios用户:     useradd nagios
设置目录属主:       chown -R nagios.nagios /usr/local/nagios
tar -zxvf nagios-2.6.tar.gz
cd nagios-2.6
./configure -prefix=/usr/local/nagios
make all
make install
make commandmode
make install-config
(2)安装插件:
tar -zxvf nagios-plugins-1.4.3.tar.gz
cd nagios-plugins-1.4.3
./configure -prefix=/usr/local/nagios
make
make install
(查看目录 /usr/local/nagios/libexec/,有很多check_xx这样的文件就OK了。)
(3)配置nagios:
开启sendmail服务:   /sbin/service sendmail start
nagios配置文件目录: /usr/local/nagios/etc
(4)添加apache用户:
/usr/local/apache/bin/htpasswd -c /usr/local/nagios/etc/htpasswd blue 
然后会提示输入密码,两次输入一致即可生成。
(5)启动apache,输入地址 http://IP:8089/nagios若配置正确则提示输入用户名和密码,进入nagios管理界面;若无法找到地址,请执行命令:/usr/local/apache/bin/apachectl
-t检查apache配置。
关键步骤到了~这里偷个懒,借sery大哥的资料一用,实在是太详细了^_^

(一)修改配置文件
Nagios的置文件存放于目录/usr/local/nagios/etc,我们先将需要用到的配置文件cp为.cfg格式的,例如主配置文件:
cp /usr/local/nagios/etc/nagios.cfg-sample /usr/local/nagios/etc/nagios.cfg
我们就从这个文件开始修改。用vi编辑nagios.cfg,注释行 #cfg_file=/usr/local/nagios/etc/localhost.cfg,然后把下面几行的注释去掉:
 
cfg_file=/usr/local/nagios/etc/contactgroups.cfg //联系组配置文件路径
cfg_file=/usr/local/nagios/etc/contacts.cfg            //联系人配置文件路径
cfg_file=/usr/local/nagios/etc/hostgroups.cfg      //主机组配置文件路径
cfg_file=/usr/local/nagios/etc/hosts.cfg                 //主机配置文件路径
cfg_file=/usr/local/nagios/etc/services.cfg            //服务配置文件路径
cfg_file=/usr/local/nagios/etc/timeperiods.cfg      //监视时段配置文件路径

改check_external_commands=0为check_external_commands=1 .这行的作用是允许在web界面下执行重启nagios、停止主机/服务检查等操作。
把command_check_interval的值从默认的1改成command_check_interval=10s(根据自己的情况定这个命令检查时间间隔,不要太长也不要太短)。
主配置文件要改的基本上就是这些,通过上面的修改,发现/usr/local/nagios/etc并没有文件hosts.cfg等文件,稍后需要手动创建它们。
 
第二个要修改的配置文件是cgi.cfg,它的作用是控制相关cgi脚本。先确保use_authentication=1。接下来修改default_user_name=blue ,再后面的修改在下表列出:
 
authorized_for_system_information=nagiosadmin,blue
authorized_for_configuration_information=nagiosadmin,blue
authorized_for_system_commands=blue
authorized_for_all_services=nagiosadmin,blue
authorized_for_all_hosts=nagiosadmin,blue
authorized_for_all_service_commands=nagiosadmin,blue
authorized_for_all_host_commands=nagiosadmin,blue
上述用户名就是执行命令 /usr/local/apache/bin/htpasswd -c /usr/local/nagios/etc/htpasswd blue 所生成的,这个要注意,不能随便加没有存在的验证用户,为了安全起见
,不要添加过多的验证用户。
第3个修改的配置文件是misccommands.cfg,这个文件的主要功能是用来发送报警短信和报警邮件,对其的修改如下所示:
#host-notify-by-sms   //发送短信报警
define command {
       command_name      host-notify-by-sms
       command_line      /usr/local/bin/sms_send "Host $HOSTSTATE$ alert for $HOSTNAME$! . '$DATETIME$' " $CONTACTPAGER$
       }
 
#service notify by sms  //发送短信报警
define command {
       command_name     service-notify-by-sms
       command_line     /usr/local/bin/sms_send "'$HOSTADDRESS$' $HOSTALIAS$/$SERVICEDESC$ is $SERVICESTATE$" $CONTACTPAGER$
       }
 
主机和服务的邮件报警通知已经在文件中,不须更改。也可以把短信和邮件报警通知这些配置块写到文件commands.cfg中,效果是一样的。
 
(二)增加新的配置文件
先创建简单的配置文件timeperiods.cfg,其内容如下:
define timeperiod{
        timeperiod_name 24x7
        alias           24 Hours A Day, 7 Days A Week
        sunday           00:00-24:00
        monday          00:00-24:00
        tuesday          00:00-24:00
        wednesday    00:00-24:00
        thursday        00:00-24:00
        friday              00:00-24:00
        saturday        00:00-24:00
        }
 
第二个手动创建的配置文件是 contacts.cfg,其格式如下:
define contact {
        contact_name         test
        alias                Test
        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-by-email
        host_notification_commands     host-notify-by-email
        email                          15839414470@139.com
        pager                          15839414470
        }
(注:这里我用的139email,支持收到邮件短信通知的功能并且免费,很方便。(测试服务器或者nagios所在网络环境网络不稳定的话,一定要设置好检测规则,不然手机和邮箱就
要暴咯!我曾经一大早起来开机,收到60多条短信T_T....估计是网络不稳定检测超时或丢包)
紧接着的第三个手动创建的配置文件是contactgroups.cfg文件,这个文件是依照上一个文件contacts.cfg来的,contactgroups文件相对简单一些,其格式如下:
 
define contactgroup {
        contactgroup_name    testgroup
        alias                                test admin group
        members                        test       
}

多个成员之间用逗号做分界符,如果有更多的联系组,就依相同的格式在文件中追加余下的组。
关键的角色终于登场,这就是配置文件hosts.cfg。下面是我定义的两个主机的基本样式:
#define monitor  host
 
############################################
#                 Test  servers                                                  #
############################################
define host {
       host_name                  test01
       alias                      test server
       address                    61.x..x.49
       contact_groups             testgroup //多个联系组用逗号分隔,数据来源于contactgroups.cfg
       check_command              check-host-alive
       max_check_attempts         5
       notification_interval      10    //值可调,大小什么值合适需自己测定
       notification_period        24x7
       notification_options        d,u,r
       }
 
define host {
       host_name                  test02
       alias                      test server
       address                    202.X.24.25
       contact_groups             testgroup
       check_command             check-host-alive //down机就发报警通知
       max_check_attempts         5
       notification_interval      10
       notification_period        24x7
       notification_options        d,u,r
       }

再一个重量级的配置文件是services.cfg,没有这个文件,什么监控也没用。下面给出一个样式文件(可以根据实际情况自定义服务配置文件,然后设置nagio.cfg加载自定义的文件
即可):
#service definition
 
###########################################
#                  Test  servers service for test                         #
###########################################
define service {
        host_name             test01     //来源:hosts.cfg
        service_de.ion   check-host-alive
        check_period          24x7
        max_check_attempts    4          
        normal_check_interval 3
        retry_check_interval  2          //检测时间间隔
        contact_groups        testgroup  //来源:contactgroups.cfg
        notification_interval   10       //重复报警间隔,设置为 "0" 则不会重复报警
        notification_period     24x7
        notification_options    w,u,c,r
        check_command           check-host-alive  //检查主机是否存活
        }
define service {
        host_name             test01
        service_de.ion   check_tcp 80
        check_period          24x7
        max_check_attempts    4
        normal_check_interval 3
        retry_check_interval  2
        contact_groups        testgroup
        notification_interval   10
        notification_period     24x7
        notification_options    w,u,c,r
        check_command      check_tcp!80 //检查tcp 80端口服务是否正常
        }
define service {
        host_name             test02
        service_de.ion   check_tcp 8080
        check_period          24x7
        max_check_attempts    4
        normal_check_interval 3
        retry_check_interval  2
        contact_groups        testgroup
        notification_interval   10
        notification_period     24x7
        notification_options    w,u,c,r
        check_command      check_tcp!8080 //检查tcp 8080端口服务是否正常
        }

check_tcp与要监控的服务端口之间要用”!”做分隔符。
主机组配置文件hostgroups.cfg,这是一个可选的项目,它建立在文件hosts之上,其格式如下:
define hostgroup {
         hostgroup_name  Test
         alias           test servers
         members         test01,test02
         }
运行程序/usr/local/nagios -v /usr/local/nagios/etc/nagios.cfg来检查所有配置文件的正确性。
Nagios 2.5
Copyright (c) 1999-2006 Ethan Galstad ( http://www.nagios.org)
Last Modified: 07-13-2006
License: GPL
 
Reading configuration data...
 
Error: Could not find any host matching 'nagios-server'
Error: Could not expand member hosts specified in hostgroup (config file '/usr/local/nagios/etc/hostgroups.cfg', starting . line 2)
………………………

如果出现上面的信息,它告诉我配置文件在什么位置产生错误,提示很详细,耐心一点点修改,这个过程也会使你对配置文件相互关系更加了解。修改完成再运行命令
/usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg,如果出现下面的信息:
Total Warnings: 0
Total Errors:   0
 
Things look okay - No serious problems were detected during the pre-flight check

OK 执行命令/etc/init.d/nagios start启动nagios,大功告成!