Nagios的安裝部署和與Cacti的整合

本文檔基於CENTOS5.2系統和Nagios實現網管監控功能,並與Cacti實現整合。

Nagios是一款開源的免費網絡監視工具,其功能強大,靈活性強。能有效監控Windows、Linux和Unix的主機狀態,交換機路由器等網絡設置,打印機等。本文檔主要實現nagios監控windows主機,nagios監控windows系統有三種實現方式:SNMP、NSClient++、NRPE,後面兩種方式都需要在windows上安裝agent,因目前服務器上都開啓了SNMP,所以本文檔只介紹使用SNMP方式來監控Windows。
第一部分:Nagios的安裝
1.安裝基礎支持套件和添加用戶
nagios需要一些基礎支持套件才能運行,如httpd;gcc;glibc;glibc-common;gd;gd-devel庫等。

創建一個用戶組名爲nagcmd 用於從web執行外部命令:將nagios與apache用戶加入到這個用戶主:

#yum install httpd

#yum install gcc

#yum install glibc glibc-common

#yum install gd gd-devel

#/usr/sbin/useradd -m nagios 添加一個名爲nagios的用戶用以專門跑nagios

#passwd nagios 設置密碼

#/usr/sbin/groupadd nagcmd 添加nagcmd用戶組,用以通過web頁面提交外部控制命令

#/usr/sbin/usermod -a -G nagcmd nagios 將nagios用戶加入nagcmd組

#/usr/sbin/usermod -a -G nagcmd apache 將apache用戶加入nagcmd組

2.編譯與安裝nagios

#mkdir ~/downloads #cd ~/downloads #wget http://osdn.dl.sourceforge.net/sourceforge/nagios/nagios-3.0.6.tar.gz #wget http://osdn.dl.sourceforge.net/sourceforge/nagiosplug/nagios-plugins-1.4.13.tar.gz #cd ~/downloads #tar xzf nagios-3.0.6.tar.gz #cd nagios-3.0.6 #./configure --with-command-group=nagcmd #make all #make install #make install-init #make install-config #make install-commandmode
這時nagios基本已經安裝完成,默認安裝後的配置文件用於啓動nagios是沒有問題的。 #vi usr/local/nagios/etc/objects/contacts.cfg
修改nagiosadmin這行其中的郵件地址爲你的email地址,以將報警郵件發到你的郵箱 #make install-webconf 安裝nagios的web接口 #htpasswd -c /usr/local/nagios/etc/htpasswd.users nagiosadmin
設置登陸web界面時HTTP驗證的賬號密碼 #service httpd restart 啓動apache

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

Bin

Nagios執行程序所在目錄,nagios文件即爲主程序

etc

Nagios配置文件位置,初始安裝完後,只有幾個*.cfg-sample文件

3與2區別 對象配置文件都在etc/objects目錄下

sbin

Nagios Cgi文件所在目錄,也就是執行外部命令所需文件所在的目錄

Share

Nagios網頁文件所在的目錄

Var

Nagios日誌文件、spid 等文件所在的目錄

var/archives

Empty directory for the archived logs

var/rw

Empty directory for the external command file

3. 編譯並安裝nagios-plugins
nagios-plugins是nagios官方提供的一套插件程序,nagios監控主機的功能其實都是通過執行插件程序來實現的。

#cd ~/downloads

#tar xzf nagios-plugins-1.4.13.tar.gz

#cd nagios-plugins-1.4.13

#./configure --with-nagios-user=nagios --with-nagios-group=nagios

#make

#make install
安裝插件,安裝後所有插件命令將被安裝到/usr/local/nagios/libexec 目錄下

4. 驗證:

ls /usr/local/nagios/libexec

會顯示安裝的插件文件,即所有的插件都安裝在libexec這個目錄下

3.2.5 配置WEB接口

方法一:直接在安裝nagios時 make install‐webconf

創建一個nagiosadmin的用戶用於Nagios的WEB接口登錄。記下你所設置的登錄口令,一會兒你會用到它。

htpasswd ‐c /usr/local/nagios/etc/htpasswd.users nagiosadmin

重啓Apache服務以使設置生效。

service httpd restart

方法二:在httpd.conf最後添加如下內容: #setting for nagios 20090325

#setting by bbs.linuxtone.org

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

<Directory "/usr/local/nagios/sbin">

Options ExecCGI

AllowOverride None

Order allow,deny

Allow from all

AuthName "Nagios Access"

AuthType Basic

AuthUserFile /usr/local/nagios/etc/htpasswd

//用於此目錄訪問身份驗證的文件

Require valid-user

</Directory>

Alias /nagios /usr/local/nagios/share

<Directory "/usr/local/nagios/share">

Options None

AllowOverride None

Order allow,deny

Allow from all

AuthName "Nagios Access"

AuthType Basic

AuthUserFile /usr/local/nagios/etc/htpasswd

//用於此目錄訪問身份驗證的文件

Require valid-user

</Directory>

/usr/local/apache2/bin/htpasswd ‐c /usr/local/nagios/etc/htpasswd test

New password: (輸入12345)

Re‐type new password: (再輸入一次密碼)

Adding password for user test

查看認證文件的內容

less /usr/local/nagios/etc/htpasswd

test:OmWGEsBnoGpIc 前半部分是用戶名test,後面是加密後的密碼

本例添加的是test 用戶名,需要改 cgi.cfg 配置文件,允許test用戶 authorized_for_system_information=test

authorized_for_configuration_information=test

authorized_for_system_commands=test

authorized_for_all_services=test

authorized_for_all_hosts=nagiosadmin,test

authorized_for_all_service_commands=test

authorized_for_all_host_commands=test

如果有多用戶,請用逗號格開

4.安裝nagios-snmp-plugins
nagios-snmp-plugins是一套用Perl編寫的通過SNMP方式監控主機的插件程序。

#cd ~/downloads

#wget http://nagios.manubulon.com/nagios-snmp-plugins.1.1.1.tgz

#tar xzf nagios-snmp-plugins.1.1.1.tgz

#cd nagios_plugins

配置check_snmp_int.pl這些插件的使用時需要配置cpan,CPAN是Comprehensive Perl Archive Network的縮寫.。它是一個巨大的Perl軟件收藏庫,收集了大量有用的Perl模塊(modules)及其相關的文件。這裏主要是使用Perl-Net-SNMP模塊。有兩種方式安裝:
A)通過CPAN來安裝

#perl -MCPAN -e shell

cpan&gt; install Net::SNMP

B) 手工安裝

首先去官方網站www.cpan.org下載以下幾個模塊

Crypt::DES

Digest::MD5

Digest::SHA1

Digest::HMAC

Net::SNMP

下載後對於每個模塊依次按照下面的方式安裝

#tar zxf <module>.tar.gz <module>表示模塊名,具體請按上面提到的模塊替換

#cd <module> <module>表示模塊名,具體請按上面提到的模塊替換

#perl Makefile.pl

#make test

#make install

注意:Net::SNMP模塊必須在最後安裝。至此Net::SNMP手動安裝完畢

#./install.sh
執行nagios-snmp-plugins安裝腳本,執行之後會將插件命令安裝到/usr/local/nagios/libexec下

5.其他設置

至此nagios基本已經安裝完畢,但這時還不能馬上啓動nagios,需要以下設置。

#chkconfig --add nagios 將nagios添加到服務中

#chkconfig nagios on 設置服務爲自啓動

#/usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg
檢測nagios的配置是否正確,在後面配置nagios過程中我們爲了檢測配置的是否正確需要不斷執行該命令來檢查配置文件。

#service nagios start 啓動nagios

需要注意的是,Centos默認打開了selinux並且運行於強制安全模式,這將導致在打開nagios的web界面時會出現Internet Server Error的錯誤。

#getenforce 查看是否運行於強制模式,結果爲1表示是

#setenforce 0 更改selinux運行於寬容模式

但是這個設置重啓後就會失效,如需要重啓後保持該設置需要修改/etc/sysconfig/selinux,將其中的SELINUX= enforcing更改爲SELINUX= permissive並重啓系統。當然你也可以改成disable禁用selinux。

也可以不更改selinux的運行模式,解決辦法爲:

#chcon -R -t httpd_sys_content_t /usr/local/nagios/sbin/

#chcon -R -t httpd_sys_content_t /usr/local/nagios/share/

6.實現MSN報警
要實現MSN報警需要使用phpmsnclass,這是一個用php實現的msn機器人程序。

#cd ~/downloads
#wget http://phpmsnclass.googlecode.com/files/phpmsnclass_1.9.7z

下載phpmsnclass,此文件爲7zip壓縮格式,需要安裝7z

#wget http://nchc.dl.sourceforge.net/sourceforge/p7zip/p7zip_4.65_x86_linux_bin.tar.bz2

#tar jxvf p7zip_4.65_x86_linux_bin.tar.bz2

#cd p7zip_4.65

#./install.sh 安裝7z

#cd -

#7z e phpmsnclass_1.9.7z 解壓文件

phpmsnclass需要php支持,安裝php,這裏爲了方便就直接完成LAMP環境的安裝

#yum -y install mysql-server 安裝mysql數據庫,後面安裝cacti需要
由於安全原因請將mysql設置爲只監聽在127.0.0.1上,並刪除mysql庫中默認的除了root@localhost外的所有用戶,相關設置請見以前的關於postfix安裝中的設置。

#yum install -y php php-cli php-xcache php-mysql php-mbstring php-gd php-pgsql php-mcrypt php-mhash php-xml php-bcmath 安裝php相關模塊

msn報警有2種方式,一種是ls息,另外一個是以msn機器人的形式發送,將消息存儲在目錄內。這裏採用第二種。

#cd phpmsnclass

#mkdir /var/spool/msnbot

#mkdir /var/spool/msnbot/log

#mkdir /var/spool/msnbot/spool

下面把msnbot.php, config.php和msn.class.php拷貝到/var/spool/msnbot/

#cp msnbot.php config.php msn.class.php /var/spool/msnbot

#chmod 777 /var/spool/msnbot/spool
#chmod o+t /var/spool/msnbot/spool
這個機器人的工作原理是如果要發送消息,就調用php生成消息文件到
/var/spool/msnbot/spool目錄中,發送程序檢查到該目錄有新文件就會模擬msn登陸和發信將信息發送出去。

#chmod +x /var/spool/msnbot/msnbot.php

#vi config.php
在配置文件中設置MSN的賬號和密碼(以後報警通過這個MSN發出)

#cp msnbot.sh /etc/init.d/

#chmod +x msnbot.sh 加上執行權限

#vi msnbot.sh

在啓動函數裏執行的php文件前要加上php (php後有空格)

#./msnbot.sh start 啓動MSN機器人

下面發個消息測試一下

#php msnsendmsg.php [email protected] "this is a test2"
[email protected]的MSN賬號發送測試信息
注意需要將[email protected]和config.php裏設置的MSN賬號相互加爲好友

如果成功接收到信息說明配置正確,此步驟到此結束,後面的能讓Nagios使用MSN進行報警的功能需要到Nagios裏設置了。

如果需要支持離線消息即當你在離線狀態下也能接收到機器人的信息,需要使用MSNP15協議,修改相關發信的vi 文件將聲明新類的地方new MSN的第一個參數改爲MSNP15

第二部分 nagios的配置
nagios目錄結構比較清晰,安裝後下面一共有bin etc libexec sbin share var幾個目錄,其中配置文件都存放在etc目錄下,bin下存放的是nagios的相關命令,sbin下存放的是通過web方式外部執行的cgi,libexec存放的是所有插件,而var則存放的是log和pid文件等。而實現監控都是依靠執行插件來實現的。
要實現監控,需要在nagios中定義一個service,在這個service中指定監控對象和監控命令以及報警機制等。Nagios的配置關係可以按照下圖來做說明:
nagios的配置也比較清晰明瞭,etc目錄下默認有objects目錄和一些配置文件,其中objects裏放的是主配置文件nagios.cfg包含進去的配置文件。在nagios.cfg中既可以指定單獨包含一個cfg,也可以指定一個包含目錄,即該目錄下所有的cfg文件都會包含進來。
首先我們配置下主配置文件,爲了目錄的清晰我們自己建立一些目錄來存放相關的配置文件,比如建立commands目錄存放命令,建立services目錄存放服務,建立hosts目錄存放主機,在nagios.cfg文件中找到cfg_file的部分,下面是一個示例,實際部署的時候可以按照你自己的情況去設置:

cfg_file=/usr/local/nagios/etc/objects/commands.cfg //監事腳本配置

cfg_file=/usr/local/nagios/etc/objects/contacts.cfg //聯繫人/組配置文件

cfg_file=/usr/local/nagios/etc/objects/timeperiods.cfg//監事時段配置文件

cfg_file=/usr/local/nagios/etc/objects/templates.cfg //監事服務,報警設置

cfg_file=/usr/local/nagios/etc/objects/localhost.cfg//本機配置文件

cfg_file=/usr/local/nagios/etc/objects/switch.cfg//監事路由

cfg_dir=/usr/local/nagios/etc/services 包含配置目錄,目錄下所有cfg文件將被包含;下同

cfg_dir=/usr/local/nagios/etc/hosts

cfg_dir=/usr/local/nagios/etc/commands

cfg_dir=/usr/local/nagios/etc/switches

cfg_dir=/usr/local/nagios/etc/routers

nagios.cfg其他地方可以暫不調整,該文件中具體參數請參考附件中的配置文件中文註釋
這裏先說明一下,監控都是依靠插件去完成的,舉個例子說明,如我們要監控60.222.111.83這臺服務器的虛擬內存,當達到70%的時候狀態爲警告,達到90%的時候爲嚴重。這個依靠check_snmp_storage.pl這個插件來完成。

[root@localhost etc]# /usr/local/nagios/libexec/check_snmp_storage.pl -H 60.222.111.83 -C mypublic -2 -m "^Virtual Memory$" -w 70 -c 90

Virtual Memory: 21%used(531MB/2472MB) (<70%) : OK

其中-H參數表示主機地址,-C參數表示SNMP團體名稱,-2表示使用SNMP v2協議,-w爲warning(警告)狀態的閾值,-c表示爲critical(嚴重)狀態的閾值
要用nagios實現監控某主機,就是要實現用nagios調用這樣的命令即可。
配置resource.cfg,文件內容:

[root@localhost etc]# cat resource.cfg |grep -v '#'| sed /^$/d 查看resource.cfg配置

$USER1$=/usr/local/nagios/libexec

$USER7$=-C mypublic -2

這裏其實是定義兩個變量$USER1$和$USER7$,因爲在後面定義command的時候插件的路徑是絕對路徑,爲了節省輸入直接可以用$USER1$代替/usr/local/nagios/libexec。而SNMP插件的命令行中的參數需要設置SNMP相關的信息,爲節省輸入這裏用$USER7$來代替
-C mypublic -2。大家可對照上面檢測60.222.111.83的虛擬內存的例子結合來加以理解。
下面我們來定義一個command用以監控,仍然以在nagios中實現監控60.222.111.83這臺主機的虛擬內存爲例說明。爲了目錄結構的清晰,我們將command的定義都放到commands目錄下。在commands下建立一個cfg後綴的文件,針對此示例的command定義內容爲

define command{

command_name check_snmp_storage

command_line $USER1$/check_snmp_storage.pl -H $HOSTADDRESS$ $USER7$ $ARG1$ -w $ARG2$ -c $ARG3$

}

其中的$USER1$和$USER7$即爲我們在上面的resource.cfg中定義的兩個變量,$HOSTADDRESS$爲我們在下面要定義的主機。
在hosts目錄下建立一個cfg後綴的文件來存放主機配置。

define host{

use windows-server 定義使用的模板

host_name web83 定義主機名爲web83

alias web server on 111.83 主機別名

address 60.222.111.83 主機IP地址

hostgroups linuxtoneweb 將該主機歸到linuxtoneweb這個組,如果要歸到多個組裏,用逗號分隔組名

}

在定義主機的時候我們將83歸到了linuxtoneweb這個組,下面來定義這個組名。可以直接在hosts下建立一個定義組名的cfg文件。

define hostgroup{

hostgroup_name linuxtoneweb

alias linuxtone web servers

}

這樣就定義了一個linuxtoneweb組,將主機歸爲一個組有兩種方法,一種是我們這種在主機的定義中去設置,另外一種是在組的設置中定義members。如下:

define hostgroup{

hostgroup_name linuxtoneweb

alias linuxtone web servers
members web83 設置該組的成員,需要是在host中定義的主機名,多個成員請用逗號分隔

}

在定義主機分組裏面我們還可以用hostgroup_members定義下級分組,十分方便。
OK,上面我們完成了命令、主機的定義,下面我們就要進入nagios監控某個對象的關鍵設置,nagios的監控都是靠定義一個service來實現,這裏我們定義一個service來實現監控83的虛擬內存。在services目錄下建立一個cfg文件。

define service {

hostgroup_name linuxtone,linuxtoneweb,database 定義監控對象

name memory 設置服務名

service_description check memory 服務描述

check_period 24x7 監控週期設置

max_check_attempts 4 最大檢測嘗試次數

normal_check_interval 3 正常檢測間隔時間

retry_check_interval 2 重試檢測間隔時間

contact_groups admins 報警聯繫組

notification_interval 10 通知間隔

notification_period 24x7 通知週期設置

notification_options w,u,c,r 定義什麼狀態時報警

check_command check_snmp_storage!-m "^Virtual Memory$"!70!90

}

這樣我們完成對一個service的定義,定義監控對象我們這裏是定義了3個組,即屬於這3個組的所有主機都會檢測虛擬內存(注意:組必須在其他文件中事先定義好)。當然你也可以用host_name針對主機作爲檢測對象(多主機用逗號分隔)。監控週期和報警週期我們設置的都是24x7,這個會在後面去定義。定義報警狀態中的w表示warning,u表示unknown,c表示critial,r表示recovery(即恢復後是否發送通知);報警選項一般生產環境下設置w,c,r即可。最後我們看到實現監控的主體部分
check_command check_snmp_storage!-m "^Virtual Memory$"!70!90
定義實現這個檢測的命令,check_snmp_storage即爲我們上面在commands裏定義的命令名,

!表示帶入的參數,這裏有三個!表示帶入三個參數到command裏,每個參數之間用!分隔,這裏表示第一個參數爲-m “^Virtual Memory$”,第二個參數爲70,第三個參數爲90。下面我們回頭去看看check_snmp_storage這個command的定義:
command_line $USER1$/check_snmp_storage.pl -H $HOSTADDRESS$ $USER7$ $ARG1$ -w $ARG2$ -c $ARG3$
這裏的$ARG1$即表示參數1,$ARG2即表示參數2,$ARG3$即表示參數3
而$HOSTADDRESS$是我們定義的檢測對象,它的值就是我們的主機IP地址;$USER1$和$USER7$爲在resource.cfg裏的定義。下面我們將參數值代入這個command裏,最後實現的效果是:
/usr/local/nagios/libexec/check_snmp_storage.pl –H 60.222.111.83 –C mypublic -2 –m “^Virtual Memory$” –w 70 –c 90
而這個命令就是我們實現監控83的虛擬內存的命令!
設置3個參數是爲了靈活的針對不同主機的調用,如果你對不同的主機設置的閾值不同,只需要對不同的主機定義不同的service即可,command是可以通用的!而這裏的參數1也是爲了實現不同的功能而設置的,這個腳本還可以檢測物理內存,如果我們把參數1的值設置爲”^Phisical Memory$”就可以針對物理內存進行檢測!定義command的時候參數的個數的設定是你可以根據需要設置的。只需要在後面service中能將參數值代到command中最終能實現你要檢測的命令即可。到此爲止你可以感受到nagios的靈活性了吧。

到這裏我們基本實現了nagios的監控功能,下面我們繼續來看看其他設置。在定義host的時候我們有個主機模板的設置,這個設置的定義在object/template.cfg中定義的,這是一個系統自帶的配置文件,關於我們所使用的Windows-Server的模板定義內容爲:

# Windows host definition template - This is NOT a real host, just a template!

define host{

name windows-server ; The name of this host template

use generic-host ; Inherit default values from the generic-host template

check_period 24x7 ; By default, Windows servers are monitored round the clock

check_interval 5 ; Actively check the server every 5 minutes

retry_interval 1 ; Schedule host check retries at 1 minute intervals

max_check_attempts 10 ; Check each server 10 times (max)

check_command check-host-alive ; Default command to check if servers are "alive"

notification_period 24x7 ; Send notification out at any time - day or night

notification_interval 30 ; Resend notifications every 30 minutes

notification_options d,r ; Only send notifications for specific host states

contact_groups admins ; Notifications get sent to the admins by default

hostgroups windows-servers ; Host groups that Windows servers should be a member of

register 0 ; DONT REGISTER THIS - ITS JUST A TEMPLATE

}

objects目錄下的contact.cfg中定義的是報警聯繫人。

define contact{

contact_name nagiosadmin ; Short name of user

use generic-contact ; Inherit default values from generic-contact template (defined above)

alias Nagios Admin ; Full name of user

email [email protected] ; &lt;&lt;***** CHANGE THIS TO YOUR EMAIL ADDRESS ******

address1 [email protected] ;這裏是個自定義的變量設置,定義了一個MSN地址用以接收MSN報警信息,聯繫人的自定義變量只能使用address1-address6;通過這個設置你可以實現多種報警方式,如電話,手機短信等,通過在這裏設置你的電話,手機號,然後到報警命令定義裏定義一個命令即可。

}

其使用了generic-contact這個聯繫模板,關於這個模板templates.cfg裏的定義內容爲:

define contact{

name generic-contact ; The name of this contact template

service_notification_period 24x7 ; service notifications can be sent anytime

host_notification_period 24x7 ; host notifications can be sent anytime

service_notification_options w,u,c,r,f,s ; send notifications for all service states, flapping events, and scheduled downtime events

host_notification_options d,u,r,f,s ; send notifications for all host states, flapping events, and scheduled downtime events

service_notification_commands notify-service-by-email,notify-service-by-msn ; send service notifications via email

host_notification_commands notify-host-by-email,notify-host-by-msn ; send host notifications via email

register 0 ; DONT REGISTER THIS DEFINITION - ITS NOT A REAL CONTACT, JUST A TEMPLATE!

}

上面我在該配置文件中增加了notify-service-by-msn和notify-service-by-msn的兩種報警命令
而關於報警的週期設置在objects下的timeperiods.cfg中,如我們上面在service裏定義的7x24的報警週期設置內容爲:

define timeperiod{

timeperiod_name 24x7

alias 24 Hours A Day, 7 Days A Week

sunday 00:00-24:00

monday 00:00-24:00

tuesday 00:00-24:00

wednesday 00:00-24:00

thursday 00:00-24:00

friday 00:00-24:00

saturday 00:00-24:00

}

通過這裏我們可以靈活設置報警時間以和報警方式結合,如週末和休息時間設置報警爲發送短信和郵件,工作時間設置爲發送郵件和MSN報警方式。
下面我們來看關於報警命令的設置,在objects下的command.cfg裏定義的爲

define command{

command_name notify-host-by-email

command_line /usr/bin/printf "%b" "***** Nagios *****\n\nNotification Type: $NOTIFICATIONTYPE$\nHost: $HOSTNAME$\nState: $HOSTSTATE$\nAddress: $HOSTADDRESS$\nInfo: $HOSTOUTPUT$\n\nDate/Time: $LONGDATETIME$\n" | /bin/mail -s "** $NOTIFICATIONTYPE$ Host Alert: $HOSTNAME$ is $HOSTSTATE$ **" $CONTACTEMAIL$

}

# 'notify-service-by-email' command definition

define command{

command_name notify-service-by-email

command_line /usr/bin/printf "%b" "***** Nagios *****\n\nNotification Type: $NOTIFICATIONTYPE$\n\nService: $SERVICEDESC$\nHost: $HOSTALIAS$\nAddress: $HOSTADDRESS$\nState: $SERVICESTATE$\n\nDate/Time: $LONGDATETIME$\n\nAdditional Info:\n\n$SERVICEOUTPUT$" | /bin/mail -s "** $NOTIFICATIONTYPE$ Service Alert: $HOSTALIAS$/$SERVICEDESC$ is $SERVICESTATE$ **" $CONTACTEMAIL$

}

# 'notify-host-by-msn' command definition

define command{

command_name notify-host-by-msn

command_line php /usr/local/nagios/share/msn/msnsendmsg.php $CONTACTADDRESS1$ "***** Nagios *****\n\nNotification Type: $NOTIFICATIONTYPE$\nHost: $HOSTNAME$\nState: $HOSTSTATE$\nAddress: $HOSTADDRESS$\nInfo: $HOSTOUTPUT$\n\nDate/Time: $LONGDATETIME$\n"

}

# 'notify-service-by-msn' command definition

define command{

command_name notify-service-by-msn

command_line php /usr/local/nagios/share/msn/msnsendmsg.php $CONTACTADDRESS1$ "***** Nagios *****\n\nNotification Type: $NOTIFICATIONTYPE$\n\nService: $SERVICEDESC$\nHost: $HOSTALIAS$\nAddress: $HOSTADDRESS$\nState: $SERVICESTATE$\n\nDate/Time: $LONGDATETIME$\n\nAdditional Info:\n\n$SERVICEOUTPUT$"

}

定義了4種報警命令,後面兩種msn報警是我添加的;郵件報警是使用系統自帶的sendmail來發送郵件的(注意要啓動sendmail服務),而msn報警是使用我們上面所配置的msn機器人發送的報警信息。
至此,整個nagios的監控和報警機制瞭解完畢。注意我們在配置過程中的任何改動都記得要去通過/usr/local/nagios/bin/nagios –c /usr/local/nagios/etc/nagios.cfg以檢查配置文件是否正確,如果正確則可以重啓nagios加載新配置(service nagios restart)

7:nagios 使用FetionRobot短信/msn在線報警

1、安裝fetion 官網地址:http://www.it‐adv.net/ 支持庫安裝: 需要Glibc2.4以上的版本

2、安裝過程 下載包地址 LINUX X86/32(REDHAT ES4X32):支持庫 最新程序fetion20080522004‐linrh4.tar.gz 其中支持庫和安裝包內容如下:

# tar zxvf libraryrh4x32.tar.gz libACE‐5.6.5.so libACE_SSL‐5.6.5.so libcrypto.so.0.9.7a libssl.so.0.9.7a # tar zxvf fetion20080522004‐linrh4.tar.gz

我將fetion放在/../nagios/libexec/sms

把支持庫複製到/usr/lib/目錄下,並作軟鏈接如下:

cp lib*so* /usr/lib/ ln ‐s /usr/lib/libcrypto.so.0.9.7a /usr/lib/libcrypto.so.4 ln ‐s /usr/lib/libssl.so.0.9.7a /usr/lib/libssl.so.4

設定lib庫配置文件

#vi /etc/ld.so.conf #增加一條 /usr/lib/ #保存退出後,執行 #ldconfig

2.測試安裝

#cd install # ./fetion ‐h

Usage: fetion ‐h

‐h: help

fetion ‐u mobile ‐p pwd [‐b batchfile] [‐EN] [‐d]

fetion ‐u mobile ‐p pwd [‐b batchfile] [‐EN] [‐d]

‐u: Fetion user account(only supports mobile phone No.)

‐p: Account password

‐b: Batch file name

‐d: Debug and write logs to [mobile]‐debug.log

‐EN: English

3、實際測試 測試帳號:13888888888 密碼:123456 (如果密碼中含有特殊字符,請使用單引號)

如果發送成功。那麼fetion安裝過程就OK了

3.與nagios組合及perl腳本

3、與nagios 組合及perl腳本

fetion 組合nagios

1、Fetion以後臺方式啓動:nohup /usr/local/nagios/libexec/sms/fetion ‐u 13888888888 ‐p 123456 >/dev/null 2&gt;1 &

2、寫一個perl腳本

Sms.pl (/../nagios/libexec/sms/sms.pl)

#!/usr/bin/perl ‐w

use strict;

use Data::Dumper;

use LWP::UserAgent;

my $mesg = shift;

$mesg =~ s/\//|/g;

my @number = (接收手機號,接收手機號);

foreach my $number ( @number ) {

#print "send sms to $number \n";

my $send_sms_url = "http://localhost:40000/API/buddy/sms/" . $number . "/" . $mesg . '?rand=0.7394060082921254';

my $request = HTTP::Request‐&gt;new(GET =&gt; $send_sms_url);

my $ua = LWP::UserAgent‐&gt;new;

$ua‐&gt;timeout( 5 );

$ua‐&gt;agent("Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.2; .NET CLR 1.1.4322; Maxthon 2.0)");

my $response = $ua‐&gt;request($request);

my $content = $response‐&gt;content();

}

4.nagios配置

contacts.cfg添加:

define contact{

contact_name sms‐members

use sms‐mail‐contact

alias Nagios Admin SMS

pager 138888888888

}

define contactgroup{

contactgroup_name admins‐and‐lijs

alias Nagios Administrators

members sms‐members

}

修改報警選項:templates.cfg

define contact{

name generic‐contact

service_notification_period 24x7

host_notification_period 24x7

service_notification_options w,u,c,r,f,s

host_notification_options d,u,r,f,s

service_notification_commands notify‐service‐by‐email; service‐notify‐by‐sms

host_notification_commands notify‐host‐by‐email

register 0

}

define contact{

name sms‐mail‐contact

service_notification_period 24x7

host_notification_period 24x7

service_notification_options w,u,c,r,f,s

host_notification_options d,u,r,f,s

service_notification_commands service‐notify‐by‐sms

host_notification_commands notify‐host‐by‐email

register 0

}

修改調用腳本:commands.cfg

#host‐notify‐by‐sms

define command {

command_name host‐notify‐by‐sms

command_line /usr/bin/printf "%b" "***** Nagios *****\n\nNotification Type: $NOTIFICATIONTYPE$\n\nHost: $HOSTNAME$\nState: $HOSTSTATE$\nWAN_IP: $HOSTALIAS$\nLAN_IP: $HOSTADDRESS$\nInfo: $HOSTOUTPUT$\n\nDate/Time: $LONGDATETIME$\n" | /usr/local/nagios/libexec/sms.pl 710880931 "** $NOTIFICATIONTYPE$ Host Alert: $HOSTNAME$ is $HOSTSTATE$ **" $CONTACTEMAIL$

}

#service notify by sms

define command {

command_name service‐notify‐by‐sms

command_line /usr/local/nagios/libexec/sms/sms.pl '$NOTIFICATIONTYPE$ $HOSTNAME$ $SERVICEDESC$ is $SERVICESTATE$ info: $SERVICEOUTPUT$'

}

Nagios PNP配置繪圖

參考本人以前的文檔:http://bbs.linuxtone.org/thread‐1235‐1‐1.html

1、PNP 安裝見官網:http://www.pnp4nagios.org/pnp/install

2、zlib gd rrdtool 可以使用yum 安裝

wget http://packages.sw.be/rpmforge‐release/rpmforge‐release‐0.3.6‐1.el5.rf.i386.rpm

rpm ‐ivh rpmforge‐release‐0.3.6‐1.el5.rf.i386.rpm

yum –y install zlib gd gd‐devel rrdtool

3.修改PNP

修改PNP配置文件,開啓日誌 默認路徑在:/usr/local/nagios/var/process_perfdata.cfg LOG_FILE = /usr/local/nagios/var/perfdata.log

#

# Loglevel 0=silent 1=normal 2=debug

#

LOG_LEVEL = 0 #本例設置爲2 使用debug模式

4、太陽圖標配置文件修改

參照官網:http://www.pnp4nagios.org/pnp/webfe

本例3.X有一個小區別就是加太陽圖標的時候 是在/usr/local/nagios/etc/objects/templates.cfg server選項及local選項,

添加紅色部分

‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐

define service{

name generic‐service

action_url /nagios/pnp/index.php?host=$HOSTNAME$&srv=$SERVICEDESC$

process_perf_data 1

‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐ define service{ name local‐service use generic‐service

action_url /nagios/pnp/index.php?host=$HOSTNAME$

process_perf_data 1

5.nagios主配置文件修改

service_perfdata_command=process‐service‐perfdata

修改command.cfg 配置文件 # 'process‐service‐perfdata' command definition

define command{

command_name process‐service‐perfdata

command_line /usr/bin/printf

"%b" "$LASTSERVICECHECK$\t$HOSTNAME$\t$SERVICEDESC$

\t$SERVICESTATE$\t$SERVICEATTEMPT$\t$SERVICESTATETYPE$\

t$SERVICEEXECUTIONTIME$\t$SERVICELATENCY$\t$SERVICEOUTPUT$

\t$SERVICEPERFDATA$\n" &gt;&gt; /usr/local/nagios/var/service‐perfdata.out

}

define command {

command_name process‐service‐perfdata

command_line /usr/local/nagios/libexec/process_perfdata.pl

}

重新加載nagios配置文件即可。如有問題注意看權限及日誌提示。

第三部分 安裝cacti
Cacti的運行需要AMP(Apache + MySQL + PHP), RRDTool , net-snmp , AMP環境我們在上面已經配置。

安裝rrdtool,rrdtool不能直接通過yum安裝,可以加入Dag RPM Repository以便讓yum能找到rrdtool

#wget http://dag.wieers.com/rpm/packages/rpmforge-release/rpmforge-release-0.3.6-1.el5.rf.i386.rpm

#rpm -Uvh rpmforge-release-0.3.6-1.el5.rf.i386.rpm

#yum install rrdtool

安裝net-snmp

#yum –y install net-snmp net-snmp-utils

安裝cacti
#cd ~/downloads
#wget http://www.cacti.net/downloads/cacti-0.8.7d.tar.gz
#tar zxvf cacti-0.8.7d.tar.gz
#mkdir –p /usr/local/wwwroot
#cp –rf cacti-0.8.7.d /usr/local/wwwroot/cacti
下面開始導入數據庫,mysql數據庫的基本安全配置這裏不做說明,請參考以前的postfix郵件安裝文檔中的mysql設置部分。
#mysql –u root –p
mysql&gt;create database cacti; 創建一個數據庫供cacti使用
mysql&gt;use cacti;

Mysql&gt;source /usr/local/cacti/cacti.sql 導入mysql數據庫
Mysql&gt;grant all privileges on cacti.* to cacti@localhost identified by ‘cactipass’;
添加一個數據庫賬號cacti 密碼爲cactipass 用以訪問cacti庫
Mysql&gt;flush privileges; 刷新權限信息

配置cacti數據庫信息
#cd cacti
#vi include/config.php

1. $database_type = "mysql";

2. $database_default = "cacti";

3. $database_hostname = "localhost";

4. $database_username = "cactiuser";

5. $database_password = "cacti";

配置cacti自動運行:
#crontab -e

1. */5 * * * * /opt/php/bin/php /opt/htdocs/www/cacti/poller.php &gt; /dev/null 2&gt;&1

Spine更有效率可以選擇性安裝

Spine更有效率可以選擇性安裝

配置cacti:
訪問cacti的web地址,就會顯示安裝界面
http://nagios.c1gstudio.com/cacti/

1.選擇"新的安裝"

2.配置路徑(以系統中實際安裝路徑爲準)
/usr/local/rrdtool/bin/rrdtool
/opt/php/bin/php
/usr/local/bin/snmpwalk
/usr/local/bin/snmpget
/usr/local/bin/snmpbulkwalk
/usr/local/bin/snmpgetnext

3.默認用戶名及密碼
admin/admin
接着輸入新的密碼

4.進入cacti後臺
設置相應版本及驗證碼
settings-&gt;General
RRDTool Utility Version = RRDTool1.3.X
SNMP Version = 2
SNMP Community = 你的驗證碼(privatepass )
save

5.查看報表
點graphs選項5分鐘後應該可以看到4個報表.
memory usage
load average
logged in users
processers

6.創建磁盤監控
在首頁點擊Create graphs然後在Data Query欄目中勾上需要監控的分區保存.

以上完成了cacti安裝,下面安裝插件
在安裝插件前需先安裝"插件框架"

安裝插件框架:
1.解壓

1. cd /opt/htdocs/www

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

3. chown -R www:website cacti-plugin-arch/

4. chmod -R 0775 cacti-plugin-arch

2.導入數據庫結構

1. mysql -u cacti -p cacti<cacti-plugin-arch/pa.sql

3.覆蓋文件
方法一:覆蓋

1. cp –rf cacti-plugin-arch/files-0.8.7e/* /opt/htdocs/www/cacti

方法二:打補丁

1. cp cacti-plugin-arch/cacti-plugin-0.8.7e-PA-v2.6.diff /opt/htdocs/www/cacti/

2. cd /opt/htdocs/www/cacti/

3. patch -p1 -N &lt;cacti-plugin-0.8.7e-PA-v2.6.diff

我用方法一

4.配置插件的數據庫信息
#cd /opt/htdocs/www/cacti/
#vi include/global.php
#插件數據庫需再配置一下

1. $database_type = "mysql";

2. $database_default = "cacti";

3. $database_hostname = "localhost";

4. $database_username = "cactiuser";

5. $database_password = "cacti";

5.配製插件

1. #我的cacti安裝在域下的cacti目錄,這裏也需修改

2. $config['url_path'] = '/cacti/';

3.

4. #爲後面安裝npc增加變量

5. $plugins[] = 'npc';

注:0.8.7g開始上面兩變量改放在includes/config.php中,如果參照以前的教程會找不到了囧
參見Change Log
http://forums.cacti.net/viewtopic.php?t=38492

Apache設置
#vi /etc/http/conf.d/cacti.conf 編輯cacti站點的配置文件,文件內容如下:

Alias /cacti "/usr/local/wwwroot/cacti"

&lt;Directory "/usr/local/wwwroot/cacti">

Options FollowSymLinks MultiViews

AllowOverride None

Order allow,deny

Allow from all

</Directory>
修改cacti的配置文件

#vi /usr/local/wwwroot/cacti/include/config.php
修改其中的數據庫連接信息,設置好數據庫主機地址,用戶,密碼信息。
至此安裝完成,訪問http://ip/cacti/install根據提示安裝cacti (ip換成你對應的主機ip地址)
接下來安裝cacti的插件管理
#cd ~/download/

#wget http://mirror.cactiusers.org/downloads/plugins/cacti-plugin-0.8.7d-PA-v2.4.zip
#unzip cacti-plugin-0.8.7d-PA-v2.4.zip
#mysql –u root –p cacti&lt;pa.sql 導入sql到cacti庫中
#cd cd files-0.8.7d/
#cp –rf * /usr/local/wwwroot/cacti 複製相關文件到cacti下

第四部分 整合cacti和nagios
整合cacti和nagios是利用了cacti的一個插件nagios for cacti,它的原理是將nagios的數據通過ndo2db導入到mysql數據庫(cacti的庫中),然後cacti讀取數據庫信息將nagios的結果展示出來。
1.安裝ndoutils

首先需要安裝ndoutils以將nagios的數據能導入到mysql數據庫中。
#yum -y install mysql-devel 安裝mysql開發包以編譯ndoutils

#wget http://nchc.dl.sourceforge.net/sourceforge/nagios/ndoutils-1.4b7.tar.gz

#tar zxvf ndoutils-1.4b7.tar.gz
#cd ndoutils

#./configure --enable-mysql --disable-pgsql --with-mysql-lib=/usr/lib/mysql

#make

#cp config/ndomod.cfg /usr/local/nagios/etc

修改nagios主配置文件
#vi /usr/local/nagios/etc/nagios.cfg
添加以下內容
check_external_commands=1

command_check_interval=-1

event_broker_options=-1

broker_module=/usr/local/nagios/bin/ndomod.o config_file=/usr/local/nagios/etc/ndomod.cfg

process_performance_data=1

添加的內容至此結束

#cd src

#cp ndomod-3x.o nod2db-3x log2ndo file2sock /usr/local/nagios/bin

#cp src/ndo2db-3x /usr/local/nagios/bin/ndo2db

#cp config/ndo2db.cfg /usr/local/nagios/etc
修改配置文件ndocmd.cfg和ndo2db.cfg,這裏我的配置文件內容爲:
[root@localhost downloads]# cat /usr/local/nagios/etc/ndomod.cfg |grep -v '^#'|sed /^$/d

instance_name=default

output_type=tcpsocket

output=127.0.0.1

tcp_port=5668

output_buffer_items=5000

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

file_rotation_interval=14400

file_rotation_timeout=60

reconnect_interval=15

reconnect_warning_interval=15

data_processing_options=-1

config_output_options=2

[root@localhost downloads]# cat /usr/local/nagios/etc/ndo2db.cfg |grep -v '^#'|sed /^$/d

ndo2db_user=nagios

ndo2db_group=nagios

socket_type=tcp

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

tcp_port=5668

db_servertype=mysql

db_host=127.0.0.1

db_port=3306

db_name=cacti

db_prefix=npc_

db_user=cacti

db_pass=cacti2008

max_timedevents_age=1440

max_systemcommands_age=10080

max_servicechecks_age=10080

max_hostchecks_age=10080

max_eventhandlers_age=44640

debug_level=1

debug_verbosity=1

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

max_debug_file_size=1000000
#/usr/local/nagios/bin/ndo2db -c /usr/local/nagios/etc/ndo2db.cfg 啓動ndo2db

2.安裝php-json
npc展示部分用到json,需要在php中安裝php-json的支持,centos 5下的php 版本默認是5.1.6 不帶json支持,無法簡單地通過yum install php5-json方式來安裝。

#cd ~/downloads
#wget http://www.aurore.net/projects/php-json/php-json-ext-1.2.0.tar.bz2

#tar xvjf php-json-ext-1.2.0.tar.bz2

#cd

#phpize 編譯前初始化php環境(注:如果要使用phpize首先安裝php-devel)

#./configure

#make

#make install

#vi /etc/php.d/json.ini
extension=php_json.so

#/usr/sbin/apache2ctl graceful 重啓apache

爲了驗證是否成功可使用phpinfo查看是否已支持json

3.安裝npc

#cd ~/downloads
#wget http://www.assembla.com/spaces/npc/documents/aUjAwmdW8r3BuPab7jnrAJ/download?filename=npc-2.0.0b.166.tar.gz
#mv npc /usr/local/wwwroot/cacti/plugins/
啓用cacti的插件功能,以admin用戶登陸cacti,在console中的user management裏對admin的用戶權限進行編輯,勾選上Plugin Management,然後到插件管理中心安裝並啓用NPC即可。

至此完成nagios和cacti的整合完畢!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章