基于Apache搭建Nagios图形监控

基于apache 的稍微简单一点么?实验一下子就OK了。。。

环境:

System:

[root@losnau etc]# cat /etc/issue
Red Hat Enterprise Linux Server release 6.5 (Santiago)
Kernel \r on an \m

IP:192.168.1.5

 防火墙关闭(service iptabes stop): Firewall is not running.
 vim /etc/sysconfig/selinux  SELINUX=disabled

 

要监控服务:监控本机CPu负载、当天用户登录数量、是否开启80端口、是否活动、/分区使用情况、swap分区使用情况、总进程数


实验开始:

安装基础套件
[root@dep5 ~]# yum install -y gcc glibc glibc-common gd gd-devel xinetd openssl-devel

安装Apache和PHP

[root@dep5 ~]# yum install -y httpd* php

创建Nagios用户和组
[root@dep5 ~]# useradd -s /sbin/nologin nagios
[root@dep5 ~]# mkdir /usr/local/nagios
[root@dep5 ~]# chown nagios:nagios /usr/local/nagios/

编译安装Nagios
[root@dep5 ~]# wget http://prdownloads.sourceforge.net/sourceforge/nagios/nagios-3.4.3.tar.gz
[root@dep5 ~]# tar zxvf nagios-3.4.3.tar.gz
[root@dep5 ~]# cd nagios
[root@dep5 nagios]# ./configure --prefix=/usr/local/nagios --with-nagios-user=nagios --with-nagios-group=nagios
[root@dep5 nagios]# make all && make install && make install-init && make install-commandmode && make install-config && chkconfig --add nagios && chkconfig --level 35 nagios on && chkconfig --list nagios

检测是否安装正确
# cd /usr/local/nagios
 
(查看是否有/usr/local/nagios/etc、bin、sbin、share、var、libexec这六个目录,如果有,表示安装成功)

安装Nagios插件
#wget http://pkgs.fedoraproject.org/repo/pkgs/nagios-plugins/nagios-plugins-1.4.16.tar.gz/862f5e44fb5bc65ce7e5d86d654d4da0/nagios-plugins-1.4.16.tar.gz

[root@dep5 ~]# tar zxvf nagios-plugins-1.4.16.tar.gz
[root@dep5 ~]# cd nagios-plugins-1.4.16
[root@dep5 nagios-plugins-1.4.16]# ./configure --prefix=/usr/local/nagios
[root@dep5 nagios-plugins-1.4.16]# make && make install


4.7、安装NRPE


[root@dep5 ~]# wget http://prdownloads.sourceforge.net/sourceforge/nagios/nrpe-2.13.tar.gz
[root@dep5 ~]# tar zxvf nrpe-2.13.tar.gz
[root@dep5 ~]# cd nrpe-2.13
[root@dep5 nrpe-2.13]# ./configure
[root@dep5 nrpe-2.13]# make all
[root@dep5 nrpe-2.13]# make install-plugin
主监控机需要安装check_nrpe 这个插件,被监控机并不需要,我们在这里安装它只是为了测试目的。

安装配置Apache和PHP
Apache 和Php 不是安装nagios 所必须的,但是nagios提供了web监控界面,通过web监控界面可以清晰的看到被监控主机、资源的运行状态,因此,安装一个web服务是很必要.
[root@dep5 ~] usermod -G nagios apache       #则会出现notifications for this service have been disabled
[root@dep5 ~] touch /etc/httpd/conf.d/nagios.conf    创建nagios.conf web配置文件
[root@dep5 ~] vim /etc/httpd/conf.d/nagios.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/htpasswd.users
#            //用于此目录访问身份验证的文件 
     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.users
     Require valid-user
</Directory>

# htpasswd -c  /usr/local/nagios/etc/htpasswd.users nagiosadmin  加入认证
# grep nagiosadmin /usr/local/nagios/etc/cgi.cfg  验证

在IE里面输入http://192.168.1.5/nagios
输入账号nagiosadmin 密码就是你刚刚改过的密码


完了么?不不不,前面只是让Apache跟Ngios建立联系

重要的现在才刚刚开始呢?

 

[root@losnau ~]# /usr/local/nagios/etc/objects

[root@losnau objects]# mv localhost.cfg localhost.cfg.bak #备份一下,万一以后用得着呢?

[root@losnau objects]# vim localhost.cfg

添加一下内容

define host{
        use                     linux-server            ; Name of host template to use
                                                        ; This host definition will inherit all variables that are defined
                                                        ; in (or inherited by) the linux-server host template definition.
        host_name               localhost
        alias                   localhost
        address                 127.0.0.1
        }
define hostgroup{
        hostgroup_name  linux-servers ; The name of the hostgroup
        alias           Linux Servers ; Long name of the group
        members         localhost     ; Comma separated list of hosts that belong to this group
        }
;==================================================================================================
;check ping
define service{
        use                             local-service         ; Name of service template to use
        host_name                       localhost
        service_description             PING
        check_command                   check_ping!100.0,20%!500.0,60%
        notifications_enabled           1
        max_check_attempts              3
        normal_check_interval           3
        retry_check_interval            2
        check_period                    24x7
        notification_interval           5
        notification_period             24x7
        notification_options            w,u,c,r
        contact_groups                  admins
        }
;check / space
define service{
        use                             local-service         ; Name of service template to use
        host_name                       localhost
        service_description             / Parttion
        check_command                   check_local_disk!20%!10%!/
        notifications_enabled           1
        max_check_attempts              3
        normal_check_interval           3
        retry_check_interval            2
        check_period                    24x7
        notification_interval           5
        notification_period             24x7
        notification_options            w,u,c,r
        contact_groups                  admins

        }
;check /opt space
define service{
        use                             local-service         ; Name of service template to use
        host_name                       localhost
        service_description             /opt Parttion
        check_command                   check_local_disk!30%!15%!/opt
        notifications_enabled           1
        max_check_attempts              3
        normal_check_interval           3
        retry_check_interval            2
        check_period                    24x7
        notification_interval           5
        notification_period             24x7
        notification_options            w,u,c,r
        contact_groups                  admins
        }
;check users
define service{
        use                             local-service         ; Name of service template to use
        host_name                       localhost
        service_description             Current Users
        check_command                   check_local_users!20!50
        notifications_enabled           1
        max_check_attempts              3
        normal_check_interval           3
        retry_check_interval            2
        check_period                    24x7
        notification_interval           5
        notification_period             24x7
        notification_options            w,u,c,r
        contact_groups                  admins
        }
;check Toal Processes
define service{
        use                             local-service         ; Name of service template to use
        host_name                       localhost
        service_description             Total Processes
        check_command                   check_local_procs!250!400!RSZDT
        notifications_enabled           1
        max_check_attempts              3
        normal_check_interval           3
        retry_check_interval            2
        check_period                    24x7
        notification_interval           5
        notification_period             24x7
        notification_options            w,u,c,r
        contact_groups                  admins
        }
;check cpu load
define service{
        use                             local-service         ; Name of service template to use
        host_name                       localhost
        service_description             Current Load
        check_command                   check_local_load!5.0,4.0,3.0!10.0,6.0,4.0
        notifications_enabled           1
        max_check_attempts              3
        normal_check_interval           3
        retry_check_interval            2
        check_period                    24x7
        notification_interval           5
        notification_period             24x7
        notification_options            w,u,c,r
        contact_groups                  admins
        }
;check swap space
define service{
        use                             local-service         ; Name of service template to use
        host_name                       localhost
        service_description             Swap Usage
        check_command                   check_local_swap!20!10
        notifications_enabled           1
        max_check_attempts              3
        normal_check_interval           3
        retry_check_interval            2
        check_period                    24x7
        notification_interval           5
        notification_period             24x7
        notification_options            w,u,c,r
        contact_groups                  admins

        }
;check ssh
define service{
        use                             local-service         ; Name of service template to use
        host_name                       localhost
        service_description             SSH
        check_command                   check_ssh
        notifications_enabled           1
        max_check_attempts              3
        normal_check_interval           3
        retry_check_interval            2
        check_period                    24x7
        notification_interval           5
        notification_period             24x7
        notification_options            w,u,c,r
        contact_groups                  admins
        }

检查配置文件是否正确
[root@losnau objects]# /usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg

启动nagios
[root@losnau objects]# /etc/init.d/nagios restart

启动nagios
[root@losnau objects]# /etc/init.d/httpd  restart

打开浏览器输入:192.168.1.5/nagios

输入登录的帐号密码

若出现以下界面,恭喜你成了搭建了简单的基于Apache的Ngios监控

 

 

 

上面那个只是监控本机,监控多台,还有警报那些还没弄好,慢慢来嘛,有待学习

原理那些请参考:http://www.cnblogs.com/mchina/archive/2013/02/20/2883404.html

这里就不再重复啦,上面那个博客貌似给出的更加详细一些,貌似自己做的时候出了一点小错误,不过已经从坑里爬出来了,哈哈



 

posted on 2016-08-30 19:42  losbyday  阅读(293)  评论(0编辑  收藏  举报

导航