安装apache+php  可以选择tar包或者直接rpm包,我这里偷懒用yum来安装apache+php。

yum install httpd* php* -y


将以下内容追加到httpd.conf文件的末尾:
    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/nagios.password
    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/nagios.password
    Require valid-user
</directory>


###############################################################################

创建nagios的管理用户和组

#useradd nagios -s /sbin/nologin

[nagios]#passswd nagios                                #################创建用户nagios

[nagios]#passwd nagios                                   #################设置用户nagios密码

[nagios]#groupadd nagcmd                              #################创建nagcmd组

[nagios]#usermod -a -G nagcmd nagios       #################将nagios添加到nagcmd组中

##############################################################################

安装nagios

[nagios]#tar xvzf nagios-3.2.1.tar.gz           ###############解压nagios的压缩包

[nagios]#cd nagios-3.2.1                           ##################进入解压的目录

[nagios]#./configure --prefix=/usr/local/nagios --with-command-group=nagcmd  ###编译路径为/usr/loca/nagios 组为nagcmd


[nagios]#make all  #############根据提示编译

[nagios]#make install

[nagios]#make install-init

[nagios]#make install-config                        #########安装的路径是/usr/local/nagios/etc
[nagios]#make install-commandmode      ###########配置目录权限

安装Nagios-plugins


[nagios]#tar xvzf nagios-plugins-1.4.14.tar.gz          ############解压plugins压缩包

[nagios]#./configure --prefix=/usr/local/nagios --with-nagios-user=nagios --with-nagios-group=nagios  ####编译安装路径为/usr/local/nagios  user为nagios group为nagios。


[nagios]# make && make install  #################编译 安装

下面开始配置nagios

[nagios]# cd /usr/local/nagios/etc/   ######进入此目录

[nagios]# vim 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

# Definitions for monitoring the local (Linux) host
#cfg_file=/usr/local/nagios/etc/objects/localhost.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/contactgroups.cfg
cfg_file=/usr/local/nagios/etc/objects/services.cfg


[nagios]# vim cgi.cfg
 authorized_for_system_information=nagiosadmin
 authorized_for_configuration_information=nagiosadmin
 authorized_for_system_commands=nagiosadmin
 authorized_for_all_services=nagiosadmin
 authorized_for_all_hosts=nagiosadmin
 authorized_for_all_service_commands=nagiosadmin
 authorized_for_all_host_commands=nagiosadmin

在这里默认的用户是“nagiosadmin”可以通过浏览器操纵nagios服务的关闭、重启等各种操作。 这里可以不用修改,后面创建用户的时候可以直接创建用户为nagiosadmin。


[nagios]#vim /usr/local/nagios/etc/object/hosts.cfg
define host {
       host_name                   a.b.com      ###########被监控IP 或 域名
       alias                        tomas             ###########这个是别名
       address                     1.1.1.1            ##########被监控的IP罗
       contact_groups               tomasgroup           ##########这个在contactgroups.cfg添加的
       check_command                check-host-alive
       check_period                 24x7  
       max_check_attempts             5                ###########  失败后重试次数
       notification_interval         10         ########每隔多久提醒一次,我觉得这是分钟意思             
       notification_period           24x7              
       notification_options          d,u,r           ###########什么情况下提醒 d = down 机
       }

[nagios]#vim /usr/local/nagios/etc/objects/hostgroups.cfg
      define hostgroup {
      hostgroup_name  tomas
      alias           tomas
      members        1.1.1.1        多台被监控机用“,”隔开。             
      }

vim /usr/local/nagios/etc/objects/contacts.cfg

shit +g 在最后加入

define contact {
     contact_name         userA
     alias                system administrator
     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
     host_notification_commands     notify-host-by-email
     email                          root@a.b.com
    }

define contact {
     contact_name         userB
     alias                system administrator
     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
     host_notification_commands     notify-host-by-email
     email                          alex@a.b.com
}

达到触发条件后就会发邮件给root与alex这2个ID userA和userB, 注意USERA.USERB为不同的邮箱。(小技巧在这可以设置139邮箱保定手机,实现短信的功能,当然也可以用飞信)

vim /usr/local/nagios/etc/objects/contactgroups.cfg

define contactgroup {
      contactgroup_name    tomasgroup  #########定义个组名将USERA USERB 添加到这个组
      alias                system administrator group
      members             userA,userB
      }


vim /usr/local/nagios/etc/objects/services.cfg

define service {
host_name 1.1.1.1
service_description PING
check_period        24x7
notification_interval       3
normal_check_interval       1
max_check_attempts          3
retry_check_interval        1
check_command check_ping!100.0,20%!500.0,60%  ##############   通过ping监控
contact_groups         tomasgroup
                      }

define service {
host_name 1.1.1.1
max_check_attempts          5
check_period            24x7
notification_interval       10
normal_check_interval       1
retry_check_interval        1
service_description HTTP
check_command check_http                         ##############监控Apache
notification_period    24x7
notification_options    w,u,c,r
contact_groups         tomasgroup
                       }
(小提示留心的朋友可能发现了check_command check_http 其实这些命令都在/usr/local/nagios/libexec/ 下可以都尝试下)


###########################################################################################
最后用下面的命令检查一下配置参数是否正确
/usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg
如果出现:
Total Warnings: 0
Total Errors:   0
恭喜您配置正确.................

创建:
htpasswd -c /usr/local/nagios/etc/nagios.password nagiosadmin     ##############创建nagiosadmin (创建这个账号的最大好处不用修改cgi.cfg文件,这也是好多文档中为什么要用这个用户)
检查是否成功创建:
tail /usr/local/nagios/etc/nagios.password 查看是否有nagiosadmin:*************
自此nagios配置完成

最后重启nagios Apache 
service nagios restart
service httpd restart