centos7 nginx環境下搭建nagios監控

準備資源(在使用wget時有的資源失效了,因此可自行下載資源備用)php

連接:https://pan.baidu.com/s/1SZFn4X8HAq2nAqVsIWE2gA 
提取碼:hsqb css

準備工做

一、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.html

 解決:yum install perl-ExtUtils-Embed -yios

二、安裝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

三、安裝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

四、下載Perl腳本

目的就是產生一個PERL的FastCGI接口,讓Nginx能夠以CGI方式處理Perl。nginx

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:無unzipweb

解決:yum install -y unzipvim

五、創建一個CGI啓動/中止腳本

這個SHELL腳本只是爲了方便管理上面的Perl腳本。腳本中的nginx爲nginx的運行用戶,請據本身的實際狀況調整。安全

注意事項:不能用root用戶執行(會提示). 要用與Nginx相同身份的用戶執行。不然可能會在Nginx Log中提示 Permision Denied。bash

#!/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監控端安裝

一、建立Nagios用戶及組,創建Nagios帳號

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

二、建立一個名爲nagcmd的用戶組,用於從web接口執行外部命令。將Nagios用戶和Nginx用戶加入組中。

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

注:上面的www是Nginx用戶所屬的組,若有不一樣請自行調整。

三、編譯安裝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配置文件

四、驗證程序是否被正確安裝

切換目錄到安裝路徑,這裏是/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–用來存放外部命令文件

五、配置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;

  }

六、編譯並安裝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

七、啓動服務

啓動前先檢查下配置文件是否正確

/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

八、安裝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被控端安裝配置(與監控端安裝類似,切勿忽略)

一、建立Nagios用戶及組,創建Nagios帳號

/usr/sbin/useradd -m -s /sbin/nologin 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

驗證程序是否被正確安裝:

ls /opt/nagios/libexec

三、安裝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

四、啓動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

五、修改NRPE配置文件,讓監控主機能夠訪問被監控主機的NRPE。(不一樣地方!)

     缺省NRPE配置文件中只容許本機訪問NRPE的Daemon

vim /opt/nagios/etc/nrpe.cfg

 
#缺省爲127.0.0.1,只能本機訪問

allowed_hosts=192.168.1.108

六、重啓nrpe的方法

kill all nrpe

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

 

 

參考:https://blog.csdn.net/cuipengchong/article/details/44589101,並添加問題。

本文分享 CSDN - 阿……莫西林。
若有侵權,請聯繫 support@oschina.cn 刪除。
本文參與「OSC源創計劃」,歡迎正在閱讀的你也加入,一塊兒分享。

相關文章
相關標籤/搜索