一、创建nagios用户和用户组。

[root@localhost ~]#useradd -s /sbin/nologin nagios
[root@localhost ~]#mkdir /usr/local/nagios
[root@localhost ~]#chown -R nagios:nagios /usr/local/nagios

二、编译安装nagios

[root@localhost ~]# tar -zxvf nagios-3.5.0.tar.gz
[root@localhost ~]# cd nagios
[root@localhost nagios]#./configure --prefix=/usr/local/nagios
[root@localhost ~]# tar -zxvf nagios-3.5.0.tar.gz
[root@localhost ~]# cd nagios
[root@localhost nagios]#./configure --prefix=/usr/local/nagios
[root@localhost nagios]#make all
[root@localhost nagios]#make install
[root@localhost nagios]# make install-init #在/etc/rc.d/init.d目录下创建nagios启动脚本
[root@localhost nagios]# make install-config #安装示例配置文件
[root@localhost nagios]# make install-commandmode #配置目录权限

三、nagios目录对应功能说明:

   bin:可执行程序所在目录

   etc:配置文件所在目录

   sbin:cgi文件所在目录,也就是执行外部命令所需文件所在目录

   share:网页文件所在目录

   libexec:外部插件所在目录

   var:日志文件,lock等文件所在目录。修改日志文件时间格式,修改拷贝的文件(perl -i -pe '($t) = ($_ =~ m/^\[(\d+)\]/); $nice=scalar localtime $t; s/^\[(\d+)\]/[$nice]/'  nagios.log)

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

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

四、安装nagios插件。

[root@localhost ~]# tar -zxvf nagios-plugins-1.4.16.tar.gz
[root@localhost ~]# yum -y install openssl*
[root@localhost ~]# cd nagios-plugins-1.4.16
[root@localhost nagios-plugins-1.4.16]# ./configure --prefix=/usr/local/nagios
[root@localhost nagios-plugins-1.4.16]# make
[root@localhost nagios-plugins-1.4.16]# make install

五、安装并配置httpd和php

[root@localhost ~]# yum -y install php httpd
[root@localhost ~]# vim /etc/httpd/conf/httpd.conf
User nagios
Group nagios
DirectoryIndex index.html index.html.var index.php
AddType application/x-httpd-php .php
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>
[root@localhost ~]# htpasswd -cm /usr/local/nagios /etc/htpasswd USER
[root@localhost ~]# service httpd restart
[root@localhost ~]# service nagios restart
#访问http://ip/nagios 输入用户/密码就可以看到nagios管理界面

六、安装插件NRPE

[root@localhost ~]# tar -zxvf nrpe-2.14.tar.gz
[root@localhost ~]# cd nrpe-2.14
[root@localhost nrpe-2.14]# ./configure
[root@localhost nrpe-2.14]# make all
[root@localhost nrpe-2.14]# make install-plugin
[root@localhost ~]# cd /usr/local/nagios/etc
[root@localhost etc]# mkdir monitor
[root@localhost etc]# cp -R objects/* monitor/
[root@localhost etc]# vim monitor/commands.cfg
define command{
        command_name check_nrpe
        command_line $USER1$/check_nrpe -H $HOSTADDRESS$ -c $ARG1$
        }
[root@localhost libexec]# ./check_nrpe -H 192.168.1.1 #检查与被监控对象的连通性

七、定义被监控主机和服务。

[root@localhost ~]# vim /usr/local/nagios/etc/cgi.cfg #将用户名修改为nagios界面登录的用户名
default_user_name=USER
[root@localhost ~]# vim /usr/local/nagios/etc/nagios.cfg
cfg_dir=/usr/local/nagios/etc/monitor  #注释掉cfg_file开头的行
[root@localhost ~]# vim /usr/local/nagios/etc/monitor/contacts.cfg  #需要指定联系人才能实现监控
define contact{
        contact_name                    USER   #必须改为http验证用户  
        use                             generic-contact
        alias                           Nagios Admin   
        email                           admin@domain.com
        }
define contactgroup{
        contactgroup_name       admins
        alias                   Nagios Administrators
        members                 USER
        }
[root@localhost ~]# service postfix restart #重启邮件服务
[root@localhost ~]# vim /usr/local/nagios/etc/monitor/hosts.cfg #定义主机
define host{
        use       windows-server   #引用模板windows-server的属性信息
        host_name test-server      #主机命名
        alias     test             #别名
        address   ip_address       #ip地址
        }
define hostgroup{                  #必须定义组
        hostgroup_name  windows-servers
        alias           windows Servers
        members         test-server
        }
[root@localhost ~]# vim /usr/local/nagios/etc/monitor/services.cfg #定义服务
define service{
        use                      local-service
        host_name                test-server
        service_description      EXAMPLE
        check_command            check-host-alive
        }
[root@localhost ~]# cd /sbin
[root@localhost sbin]# ln -s /usr/local/nagios/bin/nagios nagios
[root@localhost sbin]# nagios -v /usr/local/nagios/etc/nagios.cfg #检查主配置文件

八、客户端安装nrpe和nagios-plugin。

[root@localhost ~]# useradd nagios
[root@localhost ~]# yum -y install openssl
[root@localhost ~]# tar -zxvf nagios-plugins-1.4.16.tar.gz
[root@localhost ~]# cd nagios-plugins-1.4.16
[root@localhost nagios-plugins-1.4.16]# ./configure --prefix=/usr/local/nagios
[root@localhost nagios-plugins-1.4.16]# make&make install
check_http.c: In function ‘process_arguments’:
check_http.c:312: error: ‘ssl_version’ undeclared (first use in this function)
check_http.c:312: error: (Each undeclared identifier is reported only once
check_http.c:312: error: for each function it appears in.)
[root@localhost ~]# make clean  #清除掉之前make的残余信息
[root@localhost ~]# ./configure  --prefix=/usr/local/nagios
[root@localhost ~]# make&make install
[root@localhost ~]# chown nagios:nagios /usr/local/nagios
[root@localhost ~]# chown -R nagios:nagios /usr/local/nagios/libexec/
[root@localhost ~]# tar -zxvf nrpe-2.14.tar.gz
[root@localhost ~]# cd nrpe-2.14
[root@localhost nrpe-2.14]# ./configure
[root@localhost nrpe-2.14]# make all
[root@localhost nrpe-2.14]# make install-plugin
[root@localhost nrpe-2.14]# make install-daemon
[root@localhost nrpe-2.14]# make install-daemon-config
[root@localhost nrpe-2.14]# make install-xinetd
[root@localhost ~]# 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.1.10 #指定监控主机的IP
}
[root@localhost ~]# vim /etc/services
nrpe            5666/tcp                # nrpe
[root@localhost ~]# service xinetd restart
[root@localhost ~]# /usr/local/nagios/libexec/check_nrpe -H localhost #测试本地nrpe daemon是否正常
[root@localhost ~]# vim /usr/local/nagios/etc/nrpe.cfg #添加监控服务
command[check_http]=/usr/local/nagios/libexec/check_http -I 127.0.0.1 -w 5 -c 10
#以下在服务端添加
[root@localhost ~]# vim /usr/local/nagios/etc/monitor/services.cfg
define service{
        use                      local-service
        host_name                test-linux
        service_description      linux
        check_command            check_nrpe!check_http
        }

九、配置Fetion短信报警。

[root@localhost ~]# tar -zxvf fetion20101113.tar.gz
[root@localhost ~]# cd fetion20101113
[root@localhost ~]# mv fetion /bin   #剪切主程序到/bin
[root@localhost ~]# mkdir /usr/local/fetion
[root@localhost ~]# mv fetion20101113/* /usr/local/fetion/
[root@localhost ~]# vim /etc/ld.so.conf.d/fetion.conf
/usr/local/fetion     #指向Fetion的库文件目录
[root@localhost ~]# ldconfig  #重新读取库文件
[root@localhost ~]# fetion --help   #测试命令是否可执行
[root@localhost ~]# yum -y install libgcc_s.so.1 #安装缺失的库文件
[root@localhost ~]# fetion --mobile=10086 --pwd=123456 --to=10010 --msg-utf8=Test --debug  #发送测试短信,接收人必须是飞信好友,首次发送需填写验证码:/bin/10086.jpg
[root@nagios ~]# vim /usr/local/nagios/etc/monitor/commands.cfg  #修改commands配置文件
# 'notify-host-by-fetion' command definition
define command{
          command_name    notify-host-by-fetion
          command_line    /bin/fetion --mobile=10086 --pwd=123456 --to=$CONTACTPAGER$ --msg-utf8="***** Nagios *****\n\nNotification Type: $NOTIFICATIONTYPE$\nHost: $HOSTNAME$\nState: $HOSTSTATE$\nAddress: $HOSTADDRESS$\nInfo: $HOSTOUTPUT$\n\nDate/Time: $LONGDATETIME$\n"
                }
# 'notify-service-by-fetion' command definition
define command{
        command_name    notify-service-by-fetion
        command_line    /bin/fetion --mobile=10086 --pwd=123456 --to=$CONTACTPAGER$ --msg-utf8="***** Nagios *****\n\nNotification Type: $NOTIFICATIONTYPE$\n\nService: $SERVICEDESC$\nHost: $HOSTALIAS$\nAddress: $HOSTADDRESS$\nState: $SERVICESTATE$\n\nDate/Time: $LONGDATETIME$\n\nAdditional Info:\n\n$SERVICEOUTPUT$\n"
                }
[root@nagios ~]# vim /usr/local/nagios/etc/monitor/contacts.cfg   #修改联系人配置文件
define contact{
        contact_name                    Tom
        use                             generic-contact
        alias                           Monitor user
        email                           Tom@gmail.com
        service_notification_commands   notify-service-by-fetion
        host_notification_commands      notify-host-by-fetion
        pager                           10086   #手机号码
        }
define contact{
        contact_name                    Tony
        use                             generic-contact
        alias                           Monitor user
        email                           Tony@gmail.com
        service_notification_commands   notify-service-by-fetion
        host_notification_commands      notify-host-by-fetion
        pager                           10010
        }
define contactgroup{
        contactgroup_name       admins
        alias                   Nagios Administrators
        members                 Tom,Tony
        }
[root@localhost ~]#