2016年3月15日 星期二

Nagios 監控你的所有服務狀態 - Nginx 安裝 Nagios 4.1.1

之前有寫過一篇 Nagios 的安裝文,這次要在安裝一次 Nagios 的時候因為環境不同,所以寫了一篇新的 Nagios 安裝文紀錄

 

這篇將採用 CentOS 6.7,並且以 Nginx 當 WebServer,Nagios 則以編譯的方式進行安裝

 

前置準備環境;

  1. CentOS 6.x

  2. Nginx 1.8.x

  3. PHP-FPM 5.x


建立需求:

  1. 訪問根目錄 /,預設 nagios 的訪問目錄為 /nagios




 

 

安裝 Nagios by Nginx

Step.1 準備編譯環境
$ yum install gcc cpp make -y

 

Step.2 安裝 Nagios core
$ adduser -M nagios 
$ wget https://assets.nagios.com/downloads/nagioscore/releases/nagios-4.1.1.tar.gz
$ tar zxvf nagios-4.1.1.tar.gz
$ cd nagios-4.1.1
$ ./configure --prefix=/usr/local/nagios
$ make install
$ make install-init
$ make install-commandmode
$ make install-config

# nagios core:
$ ls /usr/local/nagios/
bin etc libexec sbin share var

# nagios example config:
$ ls -R /usr/local/nagios/etc/
.:
cgi.cfg nagios.cfg objects resource.cfg

./objects:
commands.cfg localhost.cfg switch.cfg timeperiods.cfg
contacts.cfg printer.cfg templates.cfg windows.cfg

建立 nagios 使用者、install-init 安裝服務設定、install-commandmode 安裝命令模式、install-config 安裝範例設定檔

 

 

Step.3 安裝 Nagios plugin
$ wget http://www.nagios-plugins.org/download/nagios-plugins-2.1.1.tar.gz 
$ tar zxvf nagios-plugins-2.1.1.tar.gz
$ cd nagios-plugins-2.1.1
$ ./configure --prefix=/usr/local/nagios
$ make && make install

$ ls /usr/local/nagios/libexec

check_http
check_ssh
..
..

 

Step.4 建立使用者驗證

為了讓 Nagios 有更好的安全性,可以使用 htpasswd 來建立驗證檔案,並且讓 Nginx 引入
$ htpasswd -c /usr/local/nagios/htpasswd.users scott 

#開啟驗證,允許scott使用者權限。
$ vim /usr/local/nagios/cgi.cfg
use_authentication=1
authorized_for_system_information=nagiosadmin,scott
authorized_for_configuration_information=nagiosadmin,scott
authorized_for_system_commands=nagiosadmin,scott
authorized_for_all_services=nagiosadmin,scott
authorized_for_all_hosts=nagiosadmin,scott
authorized_for_all_service_commands=nagiosadmin,scott
authorized_for_all_host_commands=nagiosadmin,scott

 

Step.5 準備 nagios 的站點目錄

所需要的站點內容預設都放在 /usr/local/nagios/ 底下,因為習慣問題,所以我都把他遷移到 /usr/share/nginx/html/nagios 底下
$ rsync -av /usr/local/nagios/share/ /usr/share/nginx/html/nagios/ 
$ rsync -av /usr/local/nagios/sbin /usr/share/nginx/html/nagios/cgi-bin/


  • share 是 nagios php 的內容

  • sbin 是 nagios cgi 的內容


 

Step.6 設定 nagios 的根目錄 & 權限,參考step5
$ vim /usr/local/nagios/etc/cgi.cfg 

physical_html_path=/usr/share/nginx/html/nagios
url_html_path=/

 

Step.7 設定 cgi 的起始目錄,參考step5
$ vim /usr/share/nginx/html/nagios/config.inc.php 

$cfg['cgi_base_url']='/cgi-bin';

 

Step.8 安裝 Nginx 設定檔 with spawn-fcgi

在設定 Nginx 前,必須先安裝好 Nginx 和 php-fpm,由於 nagios 有 .cgi 檔案必須執行,所以必須另外以 spawn-fcgi 來執行。

  • 安裝 spawn-fcgi


$ yum install spawn-fcgi fcgi-devel fcgi git -y 

$ cd /usr/local/src/
$ git clone git://github.com/gnosek/fcgiwrap.git
$ cd fcgiwrap
$ autoreconf -i
$ ./configure
$ make
$ make install

$ vim /etc/sysconfig/spawn-fcgi

FCGI_SOCKET=/var/run/fcgiwrap.socket
FCGI_PROGRAM=/usr/local/sbin/fcgiwrap
FCGI_USER=nginx
FCGI_GROUP=nginx
FCGI_EXTRA_OPTIONS="-M 0700"
OPTIONS="-u $FCGI_USER -g $FCGI_GROUP -s $FCGI_SOCKET -S $FCGI_EXTRA_OPTIONS -F 1 -P /var/run/spawn-fcgi2.pid -- $FCGI_PROGRAM"

$ service spawn-fcgi restart
$ chkconfig spawn-fcgi on

 

  • Nginx 設定檔


$ vim /etc/nginx/conf.d/nagios.conf 

server{
listen 80;
server_name nagios.com;
access_log /var/log/nginx/nagios_access.log;
error_log /var/log/nginx/nagios_error.log warn;

root /usr/share/nginx/html/nagios;
index index.php index.html;

location / {
auth_basic "Private Website!!";
auth_basic_user_file /usr/local/nagios/etc/htpasswd.users;
try_files $uri $uri/ /index.php;
}

location ~ \.php$ {
root /usr/share/nginx/html/nagios;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}

location ~ \.cgi$ {
gzip off;
auth_basic "Private Website!!";
auth_basic_user_file /usr/local/nagios/etc/htpasswd.users;
root /usr/share/nginx/html/nagios/cgi-bin;
rewrite ^/cgi-bin/(.*)\.cgi /$1.cgi break;
fastcgi_pass unix:/var/run/fcgiwrap.socket;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_index index.cgi;
include fastcgi_params;
}
}

在 cgi 的處理,rewrite ^/cgi-bin/(.*)\.cgi /$1.cgi break; 這一段非常重要,他會進行 rewrite 才能執行,否則你看到的都會是 403  Forbidden

 

啟動 Nagios & Nginx
$ service nagios restart 
$ service nginx restart

$ chkconfig nagios on
$ chkconfig nginx on

 

 

訪問 http://servername/,進行 htpasswd 使用者驗證之後就可以看到 nagios 的使用者介面

ScreenShot 2016-03-15 13.16.31

 

 

 

 

 

Orignal From: Nagios 監控你的所有服務狀態 - Nginx 安裝 Nagios 4.1.1

沒有留言:

張貼留言