centos7 nginx环境下搭建nagios监控

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/github_38854224/article/details/89374078

准备资源(在使用wget时有的资源失效了,所以可自行下载资源备用)

链接:https://pan.baidu.com/s/1SZFn4X8HAq2nAqVsIWE2gA 
提取码:hsqb 

准备工作

1、FCGI模块 安装

wget http://search.cpan.org/CPAN/authors/id/F/FL/FLORA/FCGI-0.73.tar.gz

tar xvzf FCGI-0.73.tar.gz

cd FCGI-0.73

perl Makefile.PL

make

make install

问题1:使用perl报错(Can't locate ExtUtils/Embed.pm in @INC (@INC contains: /usr/local/lib64/perl5 /usr/local/share/perl5 /usr/lib64/perl5/vendor_perl /usr/share/perl5/vendor_perl /usr/lib64/perl5 /usr/share/perl5 .).BEGIN failed--compilation aborted.

 解决:yum install perl-ExtUtils-Embed -y

2、安装FCGI-ProcManager模块

wget http://search.cpan.org/CPAN/authors/id/G/GB/GBJK/FCGI-ProcManager-0.19.tar.gz
tar xvzf FCGI-ProcManager-0.19.tar.gz
cd FCGI-ProcManager-0.19
perl Makefile.PL
make
make install

3、安装IO和IO::ALL模块

wget http://search.cpan.org/CPAN/authors/id/G/GB/GBARR/IO-1.25.tar.gz
tar zxvf IO-1.25.tar.gz
cd IO-1.25
perl Makefile.PL
make
make install


  

wget http://search.cpan.org/CPAN/authors/id/I/IN/INGY/IO-All-0.41.tar.gz
tar zxvf IO-All-0.41.tar.gz
cd IO-All-0.41
perl Makefile.PL
make
make install

4、下载Perl脚本

目的就是产生一个PERL的FastCGI接口,让Nginx可以以CGI方式处理Perl。

wget http://www.mike.org.cn/wp-content/uploads/2011/07/perl-fcgi.zip
unzip perl-fcgi.zip
cp perl-fcgi.pl /opt/nginx/


chmod 755 /opt/nginx/perl-fcgi.pl

问题1:无unzip

解决:yum install -y unzip

5、建立一个CGI启动/停止脚本

这个SHELL脚本只是为了方便管理上面的Perl脚本。脚本中的nginx为nginx的运行用户,请据自己的实际情况调整。

注意事项:不能用root用户执行(会提示). 要用与Nginx相同身份的用户执行。否则可能会在Nginx Log中提示 Permision Denied。

#!/bin/bash
#set -x
dir=/opt/nginx

stop ()
{
#pkill  -f  $dir/perl-fcgi.pl
kill $(cat $dir/logs/perl-fcgi.pid)
rm $dir/logs/perl-fcgi.pid 2>/dev/null
rm $dir/logs/perl-fcgi.sock 2>/dev/null
echo "stop perl-fcgi done"
}

start ()
{
rm $dir/now_start_perl_fcgi.sh 2>/dev/null
chown nginx.nginx $dir/logs
echo "$dir/perl-fcgi.pl -l $dir/logs/perl-fcgi.log -pid $dir/logs/perl-fcgi.pid -S $dir/logs/perl-fcgi.sock" >>$dir/now_start_perl_fcgi.sh
chown nginx.nginx $dir/now_start_perl_fcgi.sh
chmod u+x $dir/now_start_perl_fcgi.sh
sudo -u nginx $dir/now_start_perl_fcgi.sh
echo "start perl-fcgi done"
}

case $1 in
stop)
stop
;;
start)
start
;;
restart)
stop
start
;;
esac

chmod 755 /opt/nginx/start_perl_cgi.sh

/opt/nginx/start_perl_cgi.sh start   //启动脚本

注意:正常情况下在/opt/nginx/logs下生成perl-fcgi.sock这个文件,如果没有生成,请检查下上面的步聚。

安装Nagios

安装前准备

安装的机器上必须有一个WEB服务,本文是在Nginx环境上安装的。

下载nagios主程序和相关插件程序包(资源失效的话用上面备用资源)

wget http://prdownloads.sourceforge.net/sourceforge/nagios/nagios-3.2.3.tar.gz
wget http://prdownloads.sourceforge.net/sourceforge/nagiosplug/nagios-plugins-1.4.15.tar.gz
wget http://prdownloads.sourceforge.net/sourceforge/nagios/nrpe-2.12.tar.gz

安装GD库(Nagios中的statusmap和trends模块必须)(yum 资源中没有这两个模块,如果在安装php时,可在那时加上gd库安装)

apt-get install libgd2-noxpm libgd2-noxpm-dev/yum install libgd2-noxpm libgd2-noxpm-dev

Nagios监控端安装

1、创建Nagios用户及组,建立Nagios账号

/usr/sbin/useradd -m -s /sbin/nologin nagios

2、创建一个名为nagcmd的用户组,用于从web接口执行外部命令。将Nagios用户和Nginx用户加入组中。

groupadd nagcmd
usermod -a -G nagcmd nagios
usermod -a -G nagcmd nginx 

注:上面的www是Nginx用户所属的组,如有不同请自行调整。

3、编译安装Nagios

tar zxvf nagios-3.2.3.tar.gz

cd nagios-3.2.3

./configure --with-command-group=nagcmd --prefix=/opt/nagios

make

make all
make install
make install-init
make install-config
make install-commandmode

#这里是在Nginx下运行Nagios,这一步就不用做了
make install-webconf

注:

make install 用于安装主要的程序、CGI及HTML文件
make install-init 用于生成init启动脚本
make install-config 用于安装示例配置文件
make install-commandmode 用于设置相应的目录权限
make install-webconf 用于安装Apache配置文件

4、验证程序是否被正确安装

切换目录到安装路径,这里是/usr/local/nagios,看是否存在etc、bin、 sbin、 share、 var这五个目录,如果存在则可以表明程序被正确的安装到系统了。

ls /opt/nagios/

bin/     etc/     sbin/    share/   var/

注:

bin–Nagios执行程序所在目录,其中的nagios文件即为主程序。
etc–Nagios配置文件位置
sbin–Nagios cgi文件所在目录,也就是执行外部命令所需文件所在的目录
Share–Nagios网页文件所在的目录
var–Nagios日志文件、spid 等文件所在的目录
var/archives–日志归档目录
var/rw–用来存放外部命令文件

5、配置NGINX

1)、配置Nagios Web界面登陆帐号及密码

htpasswd -c /usr/local/nagios/etc/nagiospasswd
mike

htpasswd(这个工具由Apache安装包所提供),可在线生成需要加密数据

    nginx环境下 

     a)、访问http://www.4webhelp.net/us/password.php生成需要加密数据

     b)、创建加密验证文件

vim /opt/nagios/etc/nagiospasswd

#加入生成的加密数据,冒号前是用户名,后面是加密后的密码

nagios:25JB.R7mXY96o

    c)、修改Nagios配置文件,给新增的用户增加访问权限

vim /opt/nagios/etc/cgi.cfg

#以下几项中分别加入新增的用户,多用户用逗号分隔。

authorized_for_system_information=nagiosadmin,nagios
authorized_for_configuration_information=nagiosadmin,nagios
authorized_for_system_commands=nagiosadmin,nagios
authorized_for_all_services=nagiosadmin,nagios
authorized_for_all_hosts=nagiosadmin,nagios
authorized_for_all_service_commands=nagiosadmin,nagios
authorized_for_all_host_commands=nagiosadmin,nagios

2)、修改NGINX配置,以支持WEB方式访问Nagios

       方法1:以http://ip/nagios方式访问

       在WEB主目录下创建一个软链

ln -s  /opt/nagios/share/ /opt/nginx/html/nagios

     nginx.conf配置片断如下

 server {
        listen  8080;
        server_name    news.ci123.com;
        access_log     /dev/null;
        root /opt/nginx/html;
        index index.php;
        location ~ .*\.(php|php5)?$  {
                fastcgi_pass  127.0.0.1:9000;
                fastcgi_index index.php;
                include fastcgi.conf;
                }
        location ~ .*\.(htm|html|gif|jpg|jpeg|png|bmp|swf|ioc|rar|zip|txt|flv|mid|doc|ppt|pdf|xls|mp3|wma)$ {
                expires      30d;
                }
        location ~ .*\.(js|css)?$ {
                expires      1h;
                }
        location /status {
                stub_status on;
                }
        location ~ .*\.(cgi|pl)?$ {
                auth_basic "Nagios Access";
                auth_basic_user_file /opt/nagios/etc/nagiospasswd;//密码文件
                gzip off;
                root   /opt/nagios/sbin;
                rewrite ^/nagios/cgi-bin/(.*)\.cgi /$1.cgi break;
                fastcgi_pass  unix:/opt/nginx/logs/perl-fcgi.sock;
                fastcgi_param SCRIPT_FILENAME /opt/nagios/sbin$fastcgi_script_name;
                fastcgi_index index.cgi;
                fastcgi_param  REMOTE_USER        $remote_user;
                fastcgi_param AUTH_USER           $remote_user;
                fastcgi_param HTTP_ACCEPT_LANGUAGE en_US;
                include fastcgi_params;
                fastcgi_read_timeout   60;
                }
        }

注:如果你的fastcgi的配置文件中没有配置REMOTE_USER参数,一定要在nginx.conf中加上下面这个fastcgi的参数定义。

fastcgi_param  REMOTE_USER        $remote_user;

如果没有这个fastcgi的参数定义,Nagios就不能正确验证你的登陆信息。网上大多数文章解决Nginx下Nagios登陆验证失败的方法都是在Nagios的cgi.cfg配置文件(nagios/etc/cgi.cfg)中关掉验证(use_authentication=0)或设置一个缺省的登陆用户(default_user_name=test),这两种方法都是不安全的。

方法二:以http://ip方式访问

server
  {
    listen       80;
    server_name  192.168.1.108;
    index index.html index.htm index.php;
    root  /opt/nagios/share;
    auth_basic "Nagios Access";
    auth_basic_user_file /opt/nagios/etc/nagiospasswd;
    location ~ .*\.(php|php5)?$
    {     
      #fastcgi_pass  unix:/tmp/php-cgi.sock;
      fastcgi_pass  127.0.0.1:9000;
      fastcgi_index index.php;
      include fcgi.conf;
    }
    location ~ .*\.(cgi|pl)?$
    {
    gzip
    off;
    root   /opt/nagios/sbin;
    rewrite ^/nagios/cgi-bin/(.*)\.cgi /$1.cgi break;
    fastcgi_pass  unix:/opt/webserver/nginx/logs/perl-fcgi.sock;
    fastcgi_param SCRIPT_FILENAME /opt/nagios/sbin$fastcgi_script_name;
    fastcgi_index index.cgi;
    fastcgi_read_timeout   60;
    fastcgi_param  REMOTE_USER        $remote_user;
    include fcgi.conf;
    auth_basic "Nagios Access";
    auth_basic_user_file /opt/nagios/etc/nagiospasswd;
    }

    location /nagios
    {
    alias
    /opt/nagios/share;
    auth_basic "Nagios Access";
    auth_basic_user_file /opt/nagios/etc/nagiospasswd;
    }

    log_format  wwwlogs  '$remote_addr - $remote_user [$time_local] "$request" '

               '$status $body_bytes_sent "$http_referer" '

               '"$http_user_agent" $http_x_forwarded_for';

    access_log  /data1/logs/access_www.log  wwwlogs;

  }

6、编译并安装Nagios插件

由于Nagios主程序只是提供一个运行框架,其具体监控是靠运行在其下的插件完成的,所以Nagios插件是必须安装的。

tar zxvf nagios-plugins-1.4.15.tar.gz

cd nagios-plugins-1.4.15

./configure --with-nagios-user=nagios --with-nagios-group=nagios --prefix=/opt/nagios

make

make install

验证Nagios插件是否正确安装,显示安装的插件文件,即所有的插件都安装在libexec这个目录下。

ls /opt/nagios/libexec

7、启动服务

启动前先检查下配置文件是否正确

/opt/nagios/bin/nagios -v /opt/nagios/etc/nagios.cfg


Nagios Core 3.4.1
Copyright (c) 2009-2011 Nagios Core Development Team and Community Contributors
Copyright (c) 1999-2009 Ethan Galstad
Last Modified: 05-11-2012
License: GPL

Website: http://www.nagios.org
Reading configuration data...
   Read main config file okay...
Processing object config file '/opt/nagios/etc/objects/commands.cfg'...
Processing object config file '/opt/nagios/etc/objects/contacts.cfg'...
Processing object config file '/opt/nagios/etc/objects/timeperiods.cfg'...
Processing object config file '/opt/nagios/etc/objects/templates.cfg'...
Processing object config file '/opt/nagios/etc/objects/localhost.cfg'...
   Read object config files okay...

Running pre-flight check on configuration data...

Checking services...
        Checked 9 services.
Checking hosts...
        Checked 1 hosts.
Checking host groups...
        Checked 1 host groups.
Checking service groups...
        Checked 0 service groups.
Checking contacts...
        Checked 1 contacts.
Checking contact groups...
        Checked 1 contact groups.
Checking service escalations...
        Checked 0 service escalations.
Checking service dependencies...
        Checked 0 service dependencies.
Checking host escalations...
        Checked 0 host escalations.
Checking host dependencies...
        Checked 0 host dependencies.
Checking commands...
        Checked 25 commands.
Checking time periods...
        Checked 5 time periods.
Checking for circular paths between hosts...
Checking for circular host and service dependencies...
Checking global event handlers...
Checking obsessive compulsive processor commands...
Checking misc settings...

Total Warnings: 0
Total Errors:   0

Things look okay - No serious problems were detected during the pre-flight check

如果没有报错,可以启动Nagios服务

/opt/nagios/bin/nagios -d /opt/nagios/etc/nagios.cfg

查看Nagios运行状态

/opt/nagios/bin/nagiostats

8、安装NRPE

由于Nagios只能监测自己所在的主机的一些本地情况,例如,cpu负载、内存使用、硬盘使用等等。如果想要监测被监控的服务器上的这些本地情况,就要用到NRPE。NRPE(Nagios Remote Plugin Executor)是Nagios的一个扩展,它被用于被监控的服务器上,向Nagios监控平台提供该服务器的一些本地的情况。NRPE可以称为Nagios的Linux客户端。

由于NRPE是通过SSL方式在监控和被监控主机上进行数据传输的,所以必须先安装ssl相关的软件包。

yum install libssl-dev libssl0.9.8

编译安装NRPE

tar zxvf nrpe-2.12.tar.gz

cd nrpe-2.12

./configure --prefix=/opt/nagios

make all
make install-plugin
make install-daemon
make install-daemon-config

注:监控主机上只需要make install-plugin这一步就可以了。监控机上只要有一个check_nrpe插件用于连接被监控端nrpe的daemon就行了。

启动NRPE

/opt/nagios/bin/nrpe -c /opt/nagios/etc/nrpe.cfg -d

验证NRPE是否正确安装(如果成功,会返回NRPE的版本号)

[root@test2-5 conf]# /opt/nagios/libexec/check_nrpe -H localhost

NRPE v2.13

Nagios被控端安装配置(与监控端安装相似,切勿忽略)

1、创建Nagios用户及组,建立Nagios账号

/usr/sbin/useradd -m -s /sbin/nologin nagios

2、编译并安装Nagios插件

tar zxvf nagios-plugins-1.4.15.tar.gz

cd nagios-plugins-1.4.15

./configure --with-nagios-user=nagios --with-nagios-group=nagios --prefix=/opt/nagios

make

make install

验证程序是否被正确安装:

ls /opt/nagios/libexec

3、安装NRPE

tar zxvf nrpe-2.12.tar.gz

cd nrpe-2.12

./configure --prefix=/opt/nagios

make all
make install-plugin
make install-daemon
make install-daemon-config

4、启动NRPE

/opt/nagios/bin/nrpe -c /opt/nagios/etc/nrpe.cfg -d

验证NRPE是否正确安装(如果成功,会返回NRPE的版本号)

[root@test2-5 conf]# /opt/nagios/libexec/check_nrpe -H localhost

NRPE v2.13

5、修改NRPE配置文件,让监控主机可以访问被监控主机的NRPE。(不同地方!)

     缺省NRPE配置文件中只允许本机访问NRPE的Daemon

vim /opt/nagios/etc/nrpe.cfg

 
#缺省为127.0.0.1,只能本机访问

allowed_hosts=192.168.1.108

6、重启nrpe的方法

kill all nrpe

/opt/nagios/bin/nrpe -c /opt/nagios/etc/nrpe.cfg -d

参考:https://blog.csdn.net/cuipengchong/article/details/44589101,并添加问题。

猜你喜欢

转载自blog.csdn.net/github_38854224/article/details/89374078