天天看點

Cacti+Nagios完全攻略(二)整合cacti與nagios安裝部署

<b>Nagios簡介</b>

      Nagios是一個用來監視系統和網絡的開源應用軟體,它通常運作于一個主伺服器上,這個伺服器運作 Liunx 或 Unix 作業系統。Nagios 利用其衆多的插件實作對本機和遠端服務的監控,當被監控對象出現異常,Nagios 就會及時給管理人員告警。它是一個基于TCP/IP協定的軟體包,包含有nagios主程式和它的各個插件,配置非常靈活,可以監視的項目很多,也可以通過自定義 shell 腳本進行監控服務,非常适合各類企業的網絡應用。 

       Nagios功能非常強大,它可以監控服務和主機,但是他自身并不包括這部分功能的代碼,所有的監控、檢測功能都是有插件來完成的。再說報警功能,如果監控系統發現問題不能報警那就沒有意義了,是以報警也是nagios很重要的功能之一。但是,同樣的,nagios自身也沒有報警部分的代碼,甚至沒有插件,而是交給使用者或者其他相關開源項目組去完成。nagios安裝,是指基本平台,也就是nagios軟體包的安裝。它是監控體系的架構,也是所有監控的基礎。 

       打開nagios官方的文檔,會發現nagios基本上沒有什麼依賴包,隻要求系統是linux或者其他nagios支援的系統。不過如果你沒有安裝apache(http服務),那麼你就沒有那麼直覺的界面來檢視監控資訊了,是以apache姑且算是一個前提條件。 

Nagios 系統的特點主要有下面幾點:

監控主機資源和網絡服務

允許使用者通過設計實作簡單的插件來監控自己特定的服務

當被監控對象出現問題時,會及時通知管理人員

事先定義事件處理程式,當對象出現問題時自動調用對應的處理程式

通過 web 頁面來監視對象狀态,警告提示和日志檔案。

需要安裝軟體清單:

php-devel-5.1.6-23.2.el5_3.i386.rpm

php-json-ext-1.2.1.tar.bz2

cacti-plugin-0.8.7e-PA-v2.6.zip

nagios-3.2.1.tar.gz

nagios-plugins-1.4.14

ndoutils-1.4b9.tar.gz

npc-2.0.4.tar.gz

<b>Nagios安裝</b>

<a href="http://sourceforge.net/projects/nagios/files/">http://sourceforge.net/projects/nagios/files/</a>

[root@cacti-qmail soft]# tar -zxvf nagios-3.2.1.tar.gz 

[root@cacti-qmail soft]#cd nagios-3.2.1

[root@cacti-qmail nagios-3.2.1]# ./configure --prefix=/usr/local/nagios

[root@cacti-qmail nagios-3.2.1]#make all

輸入make all 得到下面具體資訊

==============================================================

*** Compile finished ***

If the main program and CGIs compiled without any errors, you

can continue with installing Nagios as follows (type 'make'

without any arguments for a list of all possible options):

 make install

     - This installs the main program, CGIs, and HTML files

    使用make install來安裝主程式,CGI和HTML檔案

 make install-init

     - This installs the init script in /etc/rc.d/init.d

    使用make install-init在/etc/rc.d/init.d安裝啟動腳本

 make install-commandmode

     - This installs and configures permissions on the

       directory for holding the external command file

    使用make install-commandmode來配置目錄權限

 make install-config

     - This installs *SAMPLE* config files in /usr/local/nagios/etc

       You'll have to modify these sample files before you can

       use Nagios. Read the HTML documentation for more info

       on doing this. Pay particular attention to the docs on

       object configuration files, as they determine what/how

       things get monitored!

    使用make install-commandmode來安裝示例配置檔案,安裝的路徑是/usr/local/nagios/etc

 make install-webconf

     - This installs the Apache config file for the Nagios

       web interface

   使用make install-webconf來安裝apache的配置檔案

=================================================================

make install在安裝這個動作之前要增加一個nagios使用者,并修改它的權限,不然會出現以下錯誤:

cd ./base &amp;&amp; make install

make[1]: Entering directory `/home/yahoon/nagios/nagios-2.9/base'

make install-basic

make[2]: Entering directory `/home/yahoon/nagios/nagios-2.9/base'

/usr/bin/install -c -m 775 -o nagios -g nagios -d /usr/local/nagios/bin

/usr/bin/install: invalid user `nagios' 非法使用者nagios

make[2]: *** [install-basic] Error 1

make[2]: Leaving directory `/home/yahoon/nagios/nagios-2.9/base'

make[1]: *** [install] Error 2

make[1]: Leaving directory `/home/yahoon/nagios/nagios-2.9/base'

make: *** [install] Error 2

=====================================================================

[root@cacti-qmail nagios-3.2.1]# useradd nagios

[root@cacti-qmail nagios-3.2.1]#mkdir /usr/local/nagios

[root@cacti-qmail nagios-3.2.1]#chown nagios.nagios /usr/local/nagios

[root@cacti-qmail nagios-3.2.1]#make install; make install-init;make install-commandmode;make install-config;make install-webconf

<b>驗證程式是否被正确安裝</b>

  切換目錄到安裝路徑(這裡是/usr/local/nagios),看是否存在 etc、bin、 sbin、 share、 var這五個目錄,如果存在則可以表明程式被正确的安裝到系統了。後表是五個目錄功能的簡要說明:

bin Nagios執行程式所在目錄,nagios檔案即為主程式

etc Nagios配置檔案位置,初始安裝完後,隻有幾個*.cfg-sample檔案

sbin Nagios Cgi檔案所在目錄,也就是執行外部指令所需檔案所在的目錄

Share Nagios網頁檔案所在的目錄

Var Nagios日志檔案、spid 等檔案所在的目錄

<b>修改nagios頁面檔案</b>

[root@cacti-qmail nagios-3.2.1]# more /etc/httpd/conf/nagios.conf

# SAMPLE CONFIG SNIPPETS FOR APACHE WEB SERVER

# Last Modified: 11-26-2005

#

# This file contains examples of entries that need

# to be incorporated into your Apache web server

# configuration file. Customize the paths, etc. as

# needed to fit your system.

ScriptAlias /nagios/cgi-bin "/usr/local/nagios/sbin"

&lt;Directory "/usr/local/nagios/sbin"&gt;

# SSLRequireSSL

   Options ExecCGI

   AllowOverride None

   Order allow,deny

   Allow from all

# Order deny,allow

# Deny from all

# Allow from 127.0.0.1

   AuthName "Nagios Access"

   AuthType Basic

<b>   AuthUserFile /usr/local/nagios/etc/htpasswd.users</b>

   Require valid-user

&lt;/Directory&gt;

Alias /nagios "/usr/local/nagios/share"

&lt;Directory "/usr/local/nagios/share"&gt;

   Options None

 <b> AuthUserFile /usr/local/nagios/etc/htpasswd.users</b>

基本上這個檔案可以直接使用,是以直接複制過去

[root@cacti-qmail nagios-3.2.1]# cp /etc/httpd/conf/nagios.conf  /var/www/html/

<b>生成nagios頁面通路賬号</b>

根據這個配置檔案中指定的AuthUserFile項,它指定的是通過Apache通路Nagios的合法使用者的帳戶名單庫檔案,需要使用htpasswd指令對這個名單庫進行使用者的添加。而預設安裝環境下在/usr/local/nagios/etc/目錄下是沒有 htpasswd.uses這個檔案的,是以需要手動添加。請特别注意,用root身份建立的htpasswd.user檔案的權限問題,應該在建立該檔案完畢後立即更改為nagios使用者nagios組的所有權

[root@cacti-qmail nagios-3.2.1]# touch /usr/local/nagios/etc/htpasswd.users

建立Nagios的網頁通路使用者帳戶檔案(這裡必須要與Nagios網頁配置檔案當中AuthUserFile指定的路徑檔案名一緻)

[root@cacti-qmail nagios-3.2.1]# /usr/bin/htpasswd -c /usr/local/nagios/etc/htpasswd.users

viong

New password:

Re-type new password:

Adding password for user viong

[root@cacti-qmail nagios-3.2.1]# cat /usr/local/nagios/etc/htpasswd.user                         

viong:HbfEis6w0gKqI

修改nagios主配置檔案 

[root@cacti-qmail nagios-3.2.1]# vi /usr/local/nagios/etc/nagios.cfg

log_file=/usr/local/nagios/var/nagios.log( 預設無nagios.log檔案,需要手動建立)

command_check_interval=15s

#command_check_interval=-1

設定指令檢查的時間間隔,将原來值-1的項注釋掉,啟用原來備用值為15s(15秒)。這項的時間間隔值請按實際需求情況設定。

[root@cacti-qmail nagios-3.2.1]# touch /usr/local/nagios/var/nagios.log   

[root@cacti-qmail nagios-3.2.1]# chown nagios.nagios /usr/local/nagios/var/nagios.log    

Nagios的CGI配置檔案                      

[root@cacti-qmail nagios-3.2.1]# vi /usr/local/nagios/etc/cgi.cfg

use_authentication=1 (預設是啟動身份驗證)

設定啟用身份驗證

authorized_for_system_information=viong

設定系統資訊的授權使用者

authorized_for_configuration_information=viong

設定配置資訊的授權使用者

authorized_for_system_commands=viong

設定系統指令的授權使用者

authorized_for_all_services=viong

設定全部監測服務的授權使用者。

authorized_for_all_hosts=viong

設定全部被監測主機的授權使用者

authorized_for_all_service_commands=viong

設定全部監測服務指令的授權用

authorized_for_all_host_commands=viong

設定全部主機指令的授權使用者

<b>測試nagios是否成功</b>

設定nagios為開機啟動

[[email protected]]#chkconfig nagios on

[[email protected]]# service nagios start

測試nagios是否成功啟動

<b>nagios-plugins插件安裝</b>

[root@cacti-qmail soft]#wget

[root@cacti-qmail soft]# tar -zxvf nagios-plugins-1.4.14.tar.gz

[root@cacti-qmail nagios-plugins-1.4.14]#./configure --prefix=/usr/local/nagios/

(注意:安裝目錄是nagios而不是nagios-plugins的原因是直接把libexec目錄的插件覆寫到nagios裡面去的)

[root@cacti-qmail nagios-plugins-1.4.14]#make;make install

[root@cacti-qmail nagios-plugins-1.4.14]# grep ^User /etc/httpd/conf/httpd.conf

将apache的運作使用者加到nagios組裡面,從httpd.conf中過濾出目前的apache運作使用者

我這裡過濾出的使用者無,如有使用者需要運作以下指令

usermod -G nagios 使用者名

<b>PHP支援JSON擴充</b>

PHP必須安裝PDO 和 JSON擴充。由于NPC使用了Ext JS,如果沒有JSON擴充,NPC的界面不會出來,隻能看到一個空白頁面。初始化PHP環境 需要用phpize指令,是以也要安裝 php-devel。

root@cacti-qmail soft]#wget http://www.aurore.net/projects/php-json/php-json-ext-1.2.1.tar.bz2

root@cacti-qmail soft]# tar jxvf php-json-ext-1.2.1.tar.bz2

[root@cacti-qmail soft]# cd php-json-ext-1.2.1

[root@cacti-qmail php-json-ext-1.2.1]# phpize

-bash: phpize: command not found

[root@cacti-qmail php-json-ext-1.2.1]# cd /misc/Server/

[root@cacti-qmail Server]# rpm -ivh php-devel-5.1.6-23.2.el5_3.i386.rpm

warning: php-devel-5.1.6-23.2.el5_3.i386.rpm: Header V3 DSA signature: NOKEY, key ID 37017186

Preparing...                ########################################### [100%]

   1:php-devel              ########################################### [100%]

[root@cacti-qmail Server]# cd /root/soft/php-json-ext-1.2.1

[root@cacti-qmail php-json-ext-1.2.1]# phpize                                      

Configuring for:

PHP Api Version:         20041225

Zend Module Api No:      20050922

Zend Extension Api No:   220051025

[root@cacti-qmail php-json-ext-1.2.1]# ./configure

 ===================================================================

如果遇到報以下錯誤,請直接yum install gcc-c++

a.configure: error: no acceptable C compiler found in $PATH

See `config.log' for more details.  請直接yum install gcc

b.configure: error: C++ preprocessor "/lib/cpp" fails sanity check   

====================================================================

[root@cacti-qmail php-json-ext-1.2.1]# make;make install

[root@cacti-qmail php-json-ext-1.2.1]# find / -name *json.so

/usr/lib/php/modules/json.so

/root/soft/php-json-ext-1.2.1/modules/json.so

/root/soft/php-json-ext-1.2.1/.libs/json.so

[root@cacti-qmail php-json-ext-1.2.1]# vi /etc/php.d/json.ini

添加以下内容

extension=json.so

儲存退出

[root@cacti-qmail php-json-ext-1.2.1]# service httpd restart

測試PHP是否支援json擴充

[root@cacti-qmail php-json-ext-1.2.1]#vi /var/www/html/test.php

&lt;?php

phpinfo();

phpinfo(INFO_GENERAL);

phpinfo(1);

?&gt;

<a target="_blank" href="http://blog.51cto.com/attachment/201003/155446719.jpg"></a>

<b></b>

<b><b>ndoutils安裝</b></b>

<b>ndoutils是将Nagios的配置及監控資訊存儲到資料庫裡,NPC通過調用ndo所存儲的資料來展現Nagios的資訊。ndoutils需要用到mysql的mysql-lib及mysql-inc,是以需要安裝mysql-devel。由于剛裝系統的時候已經裝過了.這個軟體本節就不用安裝.</b>

[root@cacti-qmail soft]# tar -zxvf ndoutils-1.4b9.tar.gz

[root@cacti-qmail soft]# cd ndoutils-1.4b9

[root@cacti-qmail ndoutils-1.4b9]#./configure

--with-mysql-inc=/usr/include/mysql --with-mysql-lib=/usr/lib/mysql --enable-mysql --with-ndo2db-user=nagios --with-ndo2db-group=nagios

[root@cacti-qmail ndoutils-1.4b9]#make

編譯完成後(不需要make install)

[root@cacti-qmail ndoutils-1.4b9]# cd src/

[root@cacti-qmail src]# ll

總計 1624

-rw-r--r-- 1 1001 1001 22480 09-15 20:04 db.c

-rw-r--r-- 1 root root 145468 03-02 01:54 dbhandlers-2x.o

-rw-r--r-- 1 root root 145444 03-02 01:54 dbhandlers-3x.o

-rw-r--r-- 1 1001 1001 169907 10-27 15:51 dbhandlers.c

-rw-r--r-- 1 root root 37872 03-02 01:54 db.o

-rwxr-xr-x 1 root root 31342 03-02 01:54 file2sock

-rw-r--r-- 1 1001 1001   5421 10-27 16:08 file2sock.c

-rw-r--r-- 1 1001 1001 11485 10-21 15:53 io.c

-rw-r--r-- 1 root root 16432 03-02 01:54 io.o

-rwxr-xr-x 1 root root 32602 03-02 01:54 log2ndo

-rw-r--r-- 1 1001 1001   6780 10-27 16:08 log2ndo.c

-rw-r--r-- 1 root root   4427 03-02 01:53 Makefile

-rw-r--r-- 1 1001 1001   4336 09-15 20:04 Makefile.in

-rwxr-xr-x 1 root root 224645 03-02 01:54 ndo2db-2x

-rwxr-xr-x 1 root root 224629 03-02 01:54 ndo2db-3x

-rw-r--r-- 1 1001 1001 57565 10-27 16:08 ndo2db.c

-rwxr-xr-x 1 root root 120333 03-02 01:54 ndomod-2x.o

-rwxr-xr-x 1 root root 128498 03-02 01:54 ndomod-3x.o

-rw-r--r-- 1 1001 1001 136855 10-27 16:08 ndomod.c

-rw-r--r-- 1 1001 1001    514 2007-01-08 protonum.c

-rwxr-xr-x 1 root root 28503 03-02 01:54 sockdebug

-rw-r--r-- 1 1001 1001   3418 10-27 16:08 sockdebug.c

-rw-r--r-- 1 1001 1001   4623 2009-07-17 utils.c

-rw-r--r-- 1 root root   8824 03-02 01:54 utils.o 

[root@cacti-qmail src]# cp ndomod-3x.o ndo2db-3x log2ndo file2sock /usr/bin/

[root@cacti-qmail src]# cd ../config

[root@cacti-qmail config]# ll

總計 64

-rw------- 1 root root 439 03-02 01:53 misccommands.cfg

-rw-r--r-- 1 1001 1001 427 09-07 04:51 misccommands.cfg.in

-rw------- 1 root root 566 03-02 01:53 nagios.cfg

-rw-r--r-- 1 1001 1001 522 09-07 04:51 nagios.cfg.in

-rw------- 1 root root 4486 03-02 02:06 ndo2db.cfg

-rw------- 1 root root 4493 03-02 01:53 ndo2db.cfg-sample

-rw-r--r-- 1 1001 1001 4489 09-15 20:08 ndo2db.cfg-sample.in

-rw------- 1 root root 5130 03-02 01:59 ndomod.cfg

-rw------- 1 root root 5130 03-02 01:53 ndomod.cfg-sample

-rw-r--r-- 1 1001 1001 5112 09-07 04:51 ndomod.cfg-sample.in

[root@cacti-qmail config]#cp ndo2db.cfg-sample ndo2db.cfg

[root@cacti-qmail config]#cp ndomod.cfg-sample ndomod.cfg

[root@cacti-qmail config]#cp ndo2db.cfg ndomod.cfg /usr/local/nagios/etc/

[root@cacti-qmail config]#cd /usr/local/nagios/etc

[root@cacti-qmail etc]# vi ndo2db.cfg

修改以下參數

socket_name=/usr/local/nagios/var/ndo.sock

db_name=cacti 

db_prefix=npc_ 

db_user=viong

db_pass=viong0623

debug_level=1

debug_file=/usr/local/nagios/var/ndo2db.debug

[root@cacti-qmail etc]# vi ndomod.cfg

output=/usr/local/nagios/var/ndo.sock

buffer_file=/usr/local/nagios/var/ndomod.tmp

<b>NPC安裝</b>

[root@cacti-qmail soft]# wget http://www.constructaegis.com/downloads/npc-2.0.4.tar.gz

[root@cacti-qmail soft]# tar -zxvf npc-2.0.4.tar.gz

[root@cacti-qmail soft]# mv npc/ /var/www/html/cacti/plugins/

[root@cacti-qmail soft]# vi /var/www/html/cacti/include/global.php

添加以下參數

$plugins= array();

$plugins[] = 'npc';

如下圖:

<a href="http://blog.51cto.com/attachment/201008/201910215.jpg" target="_blank"></a>

<b>NPC界面設定</b>

點選(console)菜單欄 user management-------&gt;admin

在realm permission---plugin management(打勾)

傳回(console)菜單欄,點選configuration--------plugin management—安裝并激活npc插件

傳回(console)菜單欄,點選configuration-------settings----NPC,修改一下三個參數

傳回(console)菜單欄,點選插件連結區-NPC

<a target="_blank" href="http://blog.51cto.com/attachment/201003/155826525.jpg"></a>

關于nagios 使用設定-後續

    本文轉自viong 51CTO部落格,原文連結:http://blog.51cto.com/viong/288278,如需轉載請自行聯系原作者