nagios詳細部署文檔

nagios詳細部署文檔
 
一、安裝nagios軟件
所需軟件如下所示:
nagios-3.2.3.tar.gz
nagios-plugins-1.4.15.tar.gz
nrpe-2.8.tar.gz
httpd-2.2.17.tar.gz
php-5.3.6.tar.gz
1.1 安裝nagios主程序
useradd nagios
mkdir /opt/nagios
chown nagios.nagios /opt/nagios
 
解壓縮
tar -zxvf nagios-3.2.3.tar.gz
cd nagios-3.2.3
編譯,指定安裝目錄爲/opt/nagios
./configure --prefix=/opt/nagios
make all
make install
make install-init
make install-commandmode
make install-config
 
 
1.2 安裝nagios插件
解壓縮
tar -zxvf nagios-plugins-1.4.15.tar.gz
cd nagios-plugins-1.4.15
編譯,指定路徑爲之前nagios的安裝路徑
./configure --prefix=/opt/nagios/
make
安裝
make install
ls /opt/nagios/libexec/
會顯示安裝的插件文件,即所有的插件都安裝在libexec這個目錄下
 
1.3 安裝apache
tar xzvf httpd-2.2.17.tar.gz
cd httpd-2.2.17
./configure –prefix=/opt/apache
make
make install
 
修改apache配置:
vim /opt/apache/conf/httpd.conf
將apache的運行用戶設置爲nagios
從httpd.conf中過濾出當前的apache運行用戶

grep ^User /usr/local/apache2/conf/httpd.conf

User nagios
Group nagios
將apache的主頁設置爲index.php
<IfModule dir_module>

    DirectoryIndex index.php

</IfModule>
 
增加nagios的目錄,並且訪問此目錄需要進行身份驗證
在配置文件最後增加如下內容:
#setting for nagios
ScriptAlias /nagios/cgi-bin /opt/nagios/sbin
<Directory "/opt/nagios/sbin">

    Options ExecCGI

    AllowOverride None

    Order allow,deny

    Allow from all

    AuthName "Nagios Access"

    AuthType Basic

AuthUserFile /opt/nagios/etc/htpasswd
//用於此目錄訪問身份驗證的文件

    Require valid-user

</Directory>
 
Alias /nagios /opt/nagios/share
<Directory "/opt/nagios/share">

    Options None

    AllowOverride None

    Order allow,deny

    Allow from all

    AuthName "Nagios Access"

    AuthType Basic

AuthUserFile /opt/nagios/etc/htpasswd
//用於此目錄訪問身份驗證的文件

    Require valid-user

</Directory>
修改完畢後重啓apache
apachectl restart
 
增加驗證用戶
也就是通過web訪問nagios的時候,必須要用這個用戶登陸.在這裏我們增加用戶test:密碼爲123456

/opt/apache/bin/htpasswd -mbc /opt/nagios/etc/htpasswd test 123456

查看認證文件的內容
less /opt/nagios/etc/htpasswd
test:OmWGEsBnoGpIc    前半部分是用戶名test,後面是加密後的密碼
 
 
1.4 安裝php5並與apache整合
tar xzvf php-5.3.6.tar.gz
cd php-5.3.6
./configure –prefix=/opt/php --with-apxs2=/opt/apache/bin/apxs
 
安裝問題:apxs:Error: Command failed with rc=65536
如出現上述問題請執行:cp /opt/apache/build/libtool /opt/tools/php-5.3.6/
該錯誤原因爲php自帶的libtool版本較低,make時有問題
make
make install
 
修改apache配置
LoadModule php5_module modules/libphp5.so \\這行在配置文件中如已存在,則不必添加
AddType application/x-httpd-php .php .php5      \\在配置文件最後添加
修改完畢後重啓apache
apachectl restart
 
到這裏nagios的安裝也就基本完成了,你可以通過web來訪問了.

[url]http://10.0.2.216/nagios[/url] 會彈出對話框要求輸入用戶名密碼,輸入test,密碼12345,就可以進入nagios的主頁面了。但是可以發現什麼也點不開,因爲nagios還沒啓動呢!下面的工作就是修改配置文件,增加要監控的主機和服務。

 
1.5 nagios典型配置
1.5.1 預備知識
Nagios自己定義了一套規則用於配置文件,其中最重要的概念就是”對象”----object.在Nagios裏面定義了一些基本的對象,一般用到的有:
聯繫人
contact
出了問題像誰報告?一般當然是系統管理員了
監控時間段
timeperiod
7X24小時不間斷還是週一至週五,或是自定義的其他時間段
被監控主機
host
所需要監控的服務器,當然可以是監控機自己
監控命令
command
nagios發出的哪個指令來執行某個監控,這也是自己定義的
被監控的服務
service
例如主機是否存活,80端口是否開,磁盤使用情況或者自定義的服務等
另外,多個被監控主機可以定義爲一個主機組,多個聯繫人可以被定義爲一個聯繫人組,多個服務還能定義成一個服務組
我們定義一個被監控的服務,當然就要指定被監控的主機,需要監控的時間段,要用哪個命令來完成這個監控操作,出了問題向哪個聯繫人報告
所有這些對象絕對多數都是需要我們手動定義的,這就是nagios的安裝顯得複雜的地方.其實瞭解了原理,做一遍之後餘下的工作就是複製粘貼了.下面就開始動手.
 
1.5.2 修改nagios配置文件
修改nagios的主配置文件nagios.cfg
cd /opt/nagios/etc
vi nagios.cfg
註釋行#cfg_file=/opt/nagios/etc/objects/localhost.cfg
然後把下面幾行的配置文件加以修改: 

cfg_file=/usr/local/nagios/etc /objects /contactgroups.cfg //聯繫組配置文件路徑

cfg_file=/usr/local/nagios/etc /objects /contacts.cfg       //聯繫人配置文件路徑
cfg_file=/usr/local/nagios/etc /objects /hostgroups.cfg     //主機組配置文件路徑
cfg_file=/usr/local/nagios/etc /objects /hosts.cfg          //主機配置文件路徑
cfg_file=/usr/local/nagios/etc /objects /services.cfg       //服務配置文件路徑
cfg_file=/usr/local/nagios/etc /objects /timeperiods.cfg    //監視時段配置文件路徑
注:有些文件不存在,手動創建即可

將check_external_commands=0改爲check_external_commands=1

\\這行的作用是允許在web界面下執行重啓nagios、停止主機/服務檢查等操作
將command_check_interval的值從默認的1改成command_check_interval=10s
\\根據自己的情況定這個命令檢查時間間隔,不要太長也不要太短
 
修改CGI腳本控制文件cgi.cfg
第二個要修改的配置文件cgi.cfg,它的作用是控制相關cgi腳本。先確保use_authentication=1。曾看過不少的文章,都是建議把use_authentication的值設置成”0”來取消驗證,這是一個十分糟糕的想法。接下來修改default_user_name=test ,再後面的修改在下表列出:

 authorized_for_system_information=nagiosadmin,test

authorized_for_configuration_information=nagiosadmin,test
authorized_for_system_commands=test //多個用戶之間用逗號隔開
authorized_for_all_services=nagiosadmin,test
authorized_for_all_hosts=nagiosadmin,test
authorized_for_all_service_commands=nagiosadmin,test
authorized_for_all_host_commands=nagiosadmin,test

那麼上述用戶名打那裏來的呢?是執行命令 /opt/apache/bin/htpasswd -mbc /opt/nagios/etc/htpasswd test 所生成的,這個要注意,不能隨便加沒有存在的驗證用戶,爲了安全起見,不要添加過多的驗證用戶。

 
定義監控時間段,創建配置文件timeperiods.cfg
vi timeperiods.cfg

define timeperiod{

        timeperiod_name         24x7   //時間段的名稱,這個地方不要有空格

        alias                   24 Hours A Day,7Days 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
        }
定義了一個監控時間段,它的名稱是24x7,監控的時間是每天全天24小時
 
定義聯繫人,創建配置文件contacts.cfg
vi contacts.cfg
define contact{
        contact_name                    test  //聯繫人的名稱,這個地方不要有空格
        alias                           sys admin
        service_notification_period        24x7
        host_notification_period          24x7
        service_notification_options       w,u,c,r
        host_notification_options         d,u,r
        service_notification_commands   notify-by-email
        host_notification_commands      host-notify-by-email
        email                          [email protected]
        pager                          1338757xxxx
        address1                       [email protected]
        address2                       555-555-5555
        }
創建了一個名爲test的聯繫人,下面列出其中重要的幾個選項做說明
service_notification_period     24x7
服務出了狀況通知的時間段,這個時間段就是上面在timeperiods.cfg中定義的.
host_notification_period        24x7
主機出了狀況通知的時間段, 這個時間段就是上面在timeperiods.cfg中定義的
service_notification_options    w,u,c,r
當服務出現w—報警(warning),u—未知(unkown),c—嚴重(critical),或者r—從異常情況恢復正常,在這四種情況下通知聯繫人.
host_notification_options       d,u,r
當主機出現d­­­­—當機(down),u—返回不可達(unreachable),r—從異常情況恢復正常,在這3種情況下通知聯繫人
service_notification_commands   notify-by-email
服務出問題通知採用的命令notify-by-email,這個命令是在commands.cfg中定義的,作用是給聯繫人發郵件.至於commands.cfg之後將專門介紹
host_notification_commands      host-notify-by-email
同上,主機出問題時採用的也是發郵件的方式通知聯繫人
email                           [email protected]
很明顯,聯繫的人email地址
pager                           1338757xxxx
聯繫人的手機,如果支持短信的通知的話,這個就很有用了.
alias是聯繫人別名,address是地址 意義不大.
按照上面的方式簡單的複製修改就可以創建多個聯繫人了.
 
下面就可以將多個聯繫人組成一個聯繫人組,創建文件contactgroups.cfg
vi contactgroups.cfg
define contactgroup{

        contactgroup_name               sagroup  

//聯繫人組的名稱,同樣不能空格

        alias                   System Administrators        //別名

        members                 test 

//組的成員,來自於上面定義的contacts.cfg,如果有多個聯繫人則以逗號相隔

        }

 
定義被監控主機,創建文件hosts.cfg
[root@localhost etc]# vi hosts.cfg
define host{

        host_name                       nagios-server

//被監控主機的名稱,最好別帶空格

        alias                           nagios server

        //別名

        address                         10.0.2.216

        //被監控主機的IP地址,我現在暫時先填本機的IP

        check_command                   check-host-alive

        //監控的命令check-host-alive,這個命令來自commands.cfg,用來監控主機是否存活

        max_check_attempts              5

        //檢查失敗後重試的次數

        check_period                    24x7

        //檢查的時間段24x7,同樣來自於我們之前在timeperiods.cfg中定義的

contact_groups                  sagroup

        //聯繫人組,上面在contactgroups.cfg中定義的sagroup

notification_interval           10

        //提醒的間隔,每隔10秒提醒一次

notification_period             24x7

        //提醒的週期, 24x7,同樣來自於我們之前在timeperiods.cfg中定義的

notification_options            d,u,r
//指定什麼情況下提醒,具體含義見之前contacts.cfg部分的介紹

        }

通過簡單的複製修改就可以定義多個主機了
 
與聯繫人可以組成聯繫人組一樣,多個主機也可以組成主機組.創建文件hostgrops.cfg
vi hostgroups.cfg
define hostgroup{

        hostgroup_name          sa-servers //主機組名稱

        alias                   sa Servers //別名

        members                 nagios-server

//組的成員主機,多個主機以逗號相隔,必須是上面hosts.cfg中定義的

        }

 
下面是最關鍵的了,用nagios主要是監控一臺主機的各種信息,包括本機資源,對外的服務等等.這些在nagios裏面都是被定義爲一個個的項目(nagios稱之爲服務,爲了與主機提供的服務相區別,我這裏用項目這個詞),而實現每個監控項目,則需要通過commands.cfg文件中定義的命令.
例如我們現在有一個監控項目是監控一臺機器的web服務是否正常, 我們需要哪些元素呢?最重要的有下面三點:首先是監控哪臺機,然後是這個監控要用什麼命令實現,最後就是出了問題的時候要通知哪個聯繫人?
 
定義監控的項目,也叫服務,創建services.cfg
vi services.cfg

#service definition

define service{
        host_name               nagios-server
        //被監控的主機,hosts.cfg中定義的
        service_description     check-host-alive
        //這個監控項目的描述(也可以說是這個項目的名稱),可以空格,我們這裏定義的是監控這個主機是不是存活
        check_command           check-host-alive
        //所用的命令,是commands.cfg中定義的
        max_check_attempts      5
        normal_check_interval   3
        retry_check_interval    2
        check_period            24x7
        //監控的時間段,是timeperiods.cfg中定義的
        notification_interval   10
        notification_period     24x7

        //通知的時間段, ,是timeperiods.cfg中定義的

        notification_options    w,u,c,r
        //在監控的結果是wucr時通知聯繫人,具體含義看前文.
        contact_groups          sagroup
        //聯繫人組,是contactgroups.cfg中定義的
        }
可以看到基本上所有的成員都是已經定義的.同樣的將上面的內容複製修改,就可以加上另外兩個監控項目:分別監控yahoon和dbpi是否存活
這樣整個的配置過程就結束了.雖然功能很簡單,但是已經爲以後擴展打下了良好的基礎.可以放心的告訴大家,以後的工作基本都是複製粘貼啦!!!
 
在運行nagios之前首先做測試
/opt/nagios/bin/nagios -v /opt/nagios/etc/nagios.cfg
看到下面這些信息就說明沒問題了
Total Warnings: 0
Total Errors:   0

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

如果有問題的話就可以按照輸出信息來排查,該輸出信息非常詳細,如果出錯請仔細查看該信息
 
啓動nagios
service nagios start
 

登陸[url]http://10.0.2.216 /nagios/[/url]就可以查看上面配置的監控內容了:

點左邊的Hosts
 
再點Services
 
二、監控遠程被控機:

要實現資源和服務兩個方面的監控,需要配置服務器端(nagios server)及被監控端(NRPE-nagios remote plugin executor).爲了能順利地、有條理的部署nagios 監控平臺,一個好的工作風格是在nagios server 自身實現服務和資源的監控,然後再在其他監控端部署nrpe,從nagios server 端用check_nrpe 測試通過後,再在nagios server 配置文件中逐一增加監控項目。當然如果只是監控服務而不監控主機資源,則被監控端不做任何nrpe 的安裝。爲了方便初學者更容易上手和成功,我們就從簡單的步驟開始。

 
2.1 在被監控機器安裝nrpe

1)增加用戶
useradd nagios
passwd nagios
2)
安裝nagios 插件
tar zvxf nagios-plugins-1.4.15.tar.gz
cd nagios-plugins-1.4.15
./configure --prefix=/opt/nagios
make
make install
3
)修改目錄權限
chown -R nagios:nagios /opt/nagios
4)
安裝nrpe
tar zxvf nrpe-2.8.tar.gz
cd nrpe-2.8
./configure –prefix=/opt/nagios
make all
(1)
安裝check_nrpe 這個插件
make install-plugin
(2)
安裝deamon
make install-daemon
(3)
安裝配置文件
make install-daemon-config
(4)
編輯nrpe配置文件
vim /opt/nagios/etc/nrpe.cfg
(更改如下)
allowed_hosts=127.0.0.1,192.168.10.8
默認爲allowed_hosts=127.0.0.1
5)
啓動nrpe
/opt/nagios/bin/nrpe -c /opt/nagios/etc/nrpe.cfg -d
6)
查看NRPE 是否已經啓動
netstat -nltp |grep nrpe
tcp 0 0 0.0.0.0:5666 0.0.0.0:*
LISTEN 5163/nrpe
7)
測試NRPE 是否則正常工作
之前安裝了check_nrpe 這個插件用於測試,現在就是用的時候.執行
/opt/nagios/libexec/check_nrpe -H localhost
NRPE v2.8
看到已經正確返回了NRPE 的版本信息,說明一切正常

 
2.2 監控機監控被監控機

之前已經將nagios 運行起來了,現在要做的事情是: 安裝check_nrpe 插件
在commands.cfg 中創建check_nrpe 的命令定義,因爲只有commands.cfg
中定義過的命令才能在services.cfg 中使用創建對被監控主機的監控項目
1)
安裝check_nrpe 插件
# tar -zxvf nrpe-2.8.1.tar.gz
# cd nrpe-2.8.1
# ./configure
# make all
# make install-plugin
只運行這一步就行了,因爲只需要check_nrpe 插件
2)
在apache 剛裝好了nrpe,現在我們測試一下監控機使用
check_nrpe
與被監控機運行的nrpe daemon 之間的通信.
# /opt/nagios/libexec/check_nrpe -H 10.0.2.234
NRPE v2.8
看到已經正確返回了NRPE 的版本信息,說明一切正常.
3)
在commands.cfg 中增加對check_nrpe 的定義
# vim /opt/nagios/etc/objects/commands.cfg
在最後面增加如下內容
####################################################################
# 'check_nrpe ' command definition
define command{
command_name check_nrpe
command_line $USER1$/check_nrpe -H $HOSTADDRESS$ -c $ARG1$
}
意義如下
command_name check_nrpe
定義命令名稱爲check_nrpe,在services.cfg 中要使用這個名稱.
command_line $USER1$/check_nrpe -H $HOSTADDRESS$ -c $ARG1$
這是定義實際運行的插件程序.這個命令行的書寫要完全按照check_nrpe 這個命令的用法.不知道用法的就用check_nrpe –h 查看-c 後面帶的$ARG1$參數是傳給nrpe daemon 執行的檢測命令,之前說過了它必須是被監控機nrpe.cfg中所定義的那5 條命令中的其中一條.在services.cfg 中使用check_nrpe 的時候要用!帶上這個參數
查看被控機NRPE的監控命令

cd /opt/nagios/etc
vi nrpe.cfg
找到下面這段話

# The following examples use hardcoded command arguments...

command[check_users]=/opt/nagios/libexec/check_users -w 5 -c 10

command[check_load]=/opt/nagios/libexec/check_load -w 15,10,5 -c 30,25,20

command[check_hda1]=/opt/nagios/libexec/check_disk -w 20 -c 10 -p /dev/hda1

command[check_zombie_procs]=/opt/nagios/libexec/check_procs -w 5 -c 10 -s Z

command[check_total_procs]=/opt/nagios/libexec/check_procs -w 150 -c 200

紅色部分是命令名,也就是check_nrpe 的-c參數可以接的內容,等號=後面是實際執行的插件程序(這與commands.cfg中定義命令的形式十分相似,只不過是寫在了一行).也就是說check_users就是等號後面/usr/local/nagios/libexec/check_users -w 5 -c 10的簡稱.

###########################################
舉例:
下面就可以在services.cfg 中定義對apache 主機磁盤容量的監控
define service{
host_name apache
被監控的主機名,這裏注意必須是linux 且運行着nrpe,而且必須是hosts.cfg 中定義的
service_description check-disk
監控項目的名稱
check_command check_nrpe!check_disk
監控命令是check_nrpe,是在commands.cfg 中定義的,帶的參數是check_disk,是
在nrpe.cfg 中定義的
max_check_attempts 5
normal_check_interval 3
retry_check_interval 2
check_period 24x7
notification_interval 10
notification_period 24x7
notification_options w,u,c,r
contact_groups sagroup
}
像這樣將其餘幾個監控項目加進來.

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