搭建nagios监控

来源:互联网 发布:数据采集控制箱 编辑:程序博客网 时间:2024/05/23 12:28
配置监控端
yum -y install gcc gcc-c++ autoconf libjpeg libjpeg-devellibpng libpng-devel freetype freetype-devel libxml2 libxml2-develzlib zlib-devel glibc glibc-devel glib2 gd gd-devel glib2-develbzip2 bzip2-devel ncurses ncurses-devel curl curl-devel openssl  openssl-devellibmcrypt-devel libtool libtool-devel

安装apache
#useradd www
#tar -jxvf httpd-2.2.24.tar.bz2
#./configure --prefix=/usr/local/apache --enable-module=so--enable-deflate=shared --enable-expires=shared--enable-rewrite=shared --enable-cache --enable-file-cahe--enable-mem-cache --enable-disk-cache --enable-static-support --enable-ssl --enable-so
#make && make install
#cp /usr/local/apache/bin/apachectl /etc/init.d/httpd
#vim /usr/local/apache/conf/httpd.conf 
#/etc/init.d/httpd start




安装PHP
# tar -zxvf php-5.3.6.tar.gz
#./configure --prefix=/usr/local/php--with-config-file-path=/usr/local/php/etc--with-apxs2=/usr/local/apache/bin/apxs --with-gd--with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib--enable-safe-mode  --enable-mbstring --with-gd --enable-gd-native-ttf --with-openssl --enable-zip  --enable-sockets 
# make && make install


编辑apache配置文件
#vim /usr/local/apache/conf/httpd.conf
找到AddType模块处添加
   AddTypeapplication/x-httpd-php-source .phps
   AddTypeapplication/x-httpd-php .php .phtml
在index.html前添加index.php


# useradd nagios -s /sbin/nologin
# mkdir /usr/local/nagios
# chown nagios:nagios /usr/local/nagios/ -R
# tar -jxvf nagios-cn-3.2.3.tar.bz2
# ./configure --prefix=/usr/local/nagios/--with-gd-lib=/usr/lib --with-gd-inc=/usr/include--with-nagios-user=nagios --with-nagios-group=nagios--with-command-group=nagios
#make all
#make install
#make install-init
#make install-commandmode
#make install-config
******#make install-webconf//如果是rpm包安装的apache,可以执行,否则报错,做如下操作即可
#vim /usr/local/apache/conf/httpd.conf
在最后添加
Alias /nagios/cgi-bin/images/"/usr/local/nagios/share/images/"
    AllowOverride None
    Options None
    Order allow,deny
    Allow from all
    AuthName "NagiosAccess"
    AuthType Basic
    AuthUserFile/usr/local/nagios/etc/.passwd.conf
    Require valid-user
ScriptAlias /nagios/cgi-bin/ "/usr/local/nagios/sbin/"
    AllowOverride None
    Options None
    Order allow,deny
    Allow from all
    AuthName "NagiosAccess"
    AuthType Basic
    AuthUserFile/usr/local/nagios/etc/.passwd.conf
    Require valid-user
Alias /nagios/ "/usr/local/nagios/share/"
    AllowOverride None
    Options None
    Order allow,deny
    Allow from all
    AuthName "NagiosAccess"
    AuthType Basic
    AuthUserFile/usr/local/nagios/etc/.passwd.conf
    Require valid-user
AddDefaultCharset utf-8 //防止部分中文乱码
# /usr/local/apache/bin/htpasswd -c/usr/local/nagios/etc/.passwd.conf nagiosadmin //生成nagios口令密码
或者
# /usr/local/apache/bin/htpasswd -bc/usr/local/nagios/etc/.passwd.conf nagiosadmin nagiosadmin

#  /usr/local/nagios/bin/nagios -v/usr/local/nagios/etc/nagios.cfg //检查文件是否出错


#/etc/init.d/nagios start
#/etc/init.d/httpd start

访问http://IP/nagios/ //别名里有/,这里就要有/

# chmod o+w /usr/local/nagios/var/rw/nagios.cmd//更改事件通知时会用到
#/usr/local/nagios/bin/nagios -d /usr/local/nagios/etc/nagios.cfg//nagios手动重启
安装插件

# tar -zxvf nagios-plugins-1.4.16.tar.gz
# ./configure--prefix=/usr/local/nagios/ 
# make && make install






#tar -zxvf nrpe-2.12.tar.gz
#./configure--prefix=/usr/local/nagios/ 
#make all
#make install-plugin
#make install-daemon
#make install-daemon-config
#make install-xinetd

#vim /usr/local/nagios/etc/objects/commands.cfg
在最后加上
define command{
       command_name check_nrpe
       command_line $USER1$/check_nrpe -H $HOSTADDRESS$-c $ARG1$
       }
或者
define command{
       command_name check_nrpe
       command_line/usr/local/nagios/libexec/check_nrpe -H $HOSTADDRESS$ -c$ARG1$
       }

# /usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg-d //启动nrpe
#/usr/local/nagios/bin/nagios -d /usr/local/nagios/etc/nagios.cfg//nagios手动重启







配置被监控的
#yum install openssl openssl-devel
# useradd nagios -s /sbin/nologin
#tar -zxvf nagios-plugins-1.4.16.tar.gz -C/usr/local/src/
#./configure --prefix=/usr/local/nagios
#make
#make install


#chown nagios:nagios /usr/local/nagios/ -R




#tar -zxvf nrpe-2.12.tar.gz -C /usr/local/src/
#./configure --prefix=/usr/local/nagios/
#make all
#make install-plugin
#make install-deamon
#make install-daemon-config
#make install-xinetd

#vim /usr/local/nagios/etc/nrpe.cfg
修改如下内容,根据自己需求修改

allowed_hosts=127.0.0.1 //添加ip用逗号分隔



command[check_users]=/usr/local/nagios//libexec/check_users -w5 -c 10
command[check_load]=/usr/local/nagios//libexec/check_load -w15,10,5 -c 30,25,20
command[check_hda1]=/usr/local/nagios//libexec/check_disk -w20% -c 10% -p /dev/hda1
command[check_zombie_procs]=/usr/local//nagios/libexec/check_procs-w 5 -c 10 -s Z
command[check_total_procs]=/usr/local//nagios/libexec/check_procs-w 150 -c 200 



# vim /etc/xinetd.d/nrpe
修改only from的IP //多个ip用逗号分隔


# vim /etc/services
在最后添加
nrpe           5666/tcp                    #nrpe


# /etc/init.d/xinetd restart //启动xinetd
# /usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg-d //启动nrpe,没生成/bin或者启动文件,可以从监控端cp一个过来
# netstat -anpt |grep 5666 //查看端口,看nrpe是否启动











添加监控主机和监控服务
例如:
# vim host.cfg


define host{
       use                  linux-server
       host_name             webserver
       alias                web
#      parents               
       address              192.168.10.132
       icon_image            server.gif
       statusmap_image       server.gd2
       2d_coords             500,200
       3d_coords             500,200,100
       }



define service{
       use                        local-service        ; Name of service template touse
       host_name                   webserver
       service_description           HTTP
       check_command                check_nrpe!check_http -I192.168.10.132 -p 80 -e 403,202 //监控192.168.10.132主机的80端口,返回值为403和  202时,为ok状态
       notifications_enabled         0
       }





报错解决方法
HTTP WARNING: HTTP/1.1 403 Forbidden - 5240 bytes in 0.070second response time
主要是监控端在检测被监控端下/var/www/html/index.html文件,编译安装的apache可定不在这个下面,所以创建一个就好了


在被监控机上#touch /var/www/html/index.html



监控网站主页

# vim commands.cfg
添加
define command{
       command_name   check_index
       command_line   $USER1$/check_http $ARG1$
       }

# vim host.cfg 
define service{
       use                        local-service        ; Name of service template touse
       host_name                   localhost
       service_description           HTTP
       check_command                check_index!-H 192.168.0.106-u /index.php //如果域名监控,格式是check_index!-H[url]www.testhost.test[/url] -u /index.php
       notifications_enabled         0
      }

监控mysql
查看libexec目录下有没有check_mysql,如果没有,yum installedmysql-devel,在重装nagios-plugin

创建nagios专用数据库
mysql> create database nagios;
Query OK, 1 row affected (0.00 sec)
      
mysql> grant all on nagios.* tonagios@'localhost' identified by 'nagios';
Query OK, 0 rows affected (0.00 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)


测试
# /usr/local/nagios/libexec/check_mysql -H localhost -u nagios-d nagios -p nagios
Uptime: 44649  Threads: 10 Questions: 152675  Slow queries:0  Opens: 292  Flush tables: 1 Open tables: 64  Queries persecond avg: 3.419



# vim commands.cfg //定义命令
在最后添加

#check mysql
define command{
command_name check_mysql
command_line $USER1$/check_mysql -H $HOSTADDRESS$ -unagiosuser -d nagios -p nagiospwd
}


# vim host.cfg //定义主机

define service{
       use                        local-service        ; Name of service template touse
       host_name                   localhost
       service_description           Mysql
       check_command                check_mysql
       notifications_enabled         0
      }




报错解决办法
1.
Can't locate RRDs.pm in @INC (@INC contains: /usr/lib64/perl5/site_perl/5.8.8/x86_64-linux-thread-multi /usr/lib/perl5/site_perl/5.8.8 /usr/lib/perl5/site_perl /usr/lib64/perl5/vendor_perl/5.8.8/x86_64-linux-thread-multi /usr/lib/perl5/vendor_perl/5.8.8 /usr/lib/perl5/vendor_perl /usr/lib64/perl5/5.8.8/x86_64-linux-thread-multi /usr/lib/perl5/5.8.8 .) at /usr/local/nagios/sbin/show.cgi line 9., referer: http://jk.ylwkj.com/nagios/cgi-bin/status.cgi?host=all

#cp -rp /usr/local/rrdtool/lib/perl/5.8.8/x86_64-linux-thread-multi/ /usr/lib64/perl5/site_perl/5.8.8/






原创粉丝点击