Nagios是一款用于監(jiān)控系統(tǒng)和網(wǎng)絡(luò)的開源應(yīng)用軟件,它的模式是服務(wù)器—客戶端,也就是說首先要在在一臺(tái)服務(wù)器上(server)部署相應(yīng)的主要套件,然后在要監(jiān)控的服務(wù)器上部署客戶端程序,這樣server會(huì)和client通信,從而監(jiān)控client端的各項(xiàng)資源。Nagios功能十分強(qiáng)大幾乎所有的項(xiàng)目都可以監(jiān)控,大到服務(wù)器的存活狀態(tài),小到服務(wù)器上的某一個(gè)服務(wù)(web)。這些功能都是通過自定義插件(或者叫做腳本)來實(shí)現(xiàn)。
當(dāng)Nagios監(jiān)控到某項(xiàng)資源發(fā)生異常會(huì)通知到用戶,你可以接入手機(jī)短信接口也可以接入郵件接口。我們可以通過web頁面來查看Nagios所監(jiān)控的各項(xiàng)資源,默認(rèn)搭建的Nagios服務(wù)器只能監(jiān)控簡(jiǎn)單的幾個(gè)項(xiàng)目,而其他服務(wù)之類的監(jiān)控項(xiàng)目都是由我們自己開發(fā)的插件來實(shí)現(xiàn)的。
nagios-3.0.5
nagios-plugins-1.4.13
nrpe-2.12.tar.gz
apache-2.2.11
// 以上軟件版本可以不一樣
安裝apache (略,請(qǐng)參考第16章中相關(guān)內(nèi)容,只需安裝,到后邊再配置)
useradd nagios
cd /usr/local/src/
wget http://syslab.comsenz.com/downloads/linux/nagios-3.0.5.tar.gz
wget http://syslab.comsenz.com/downloads/linux/nagios-plugins-1.4.13.tar.gz
wget http://syslab.comsenz.com/downloads/linux/nrpe-2.12.tar.gz
cd /usr/local/src/
tar zxvf nagios-3.0.5.tar.gz
cd nagios-3.0.5
./configure --prefix=/usr/local/nagios
make all
make install
make install-init # 把nagios做成一個(gè)運(yùn)行腳本,使nagios隨系統(tǒng)開機(jī)啟動(dòng)
make install-config # 把配置文件樣例復(fù)制到nagios的安裝目錄
make install-commandmode # 給外部命令訪問nagios配置文件的權(quán)限
chown -R nagios:nagios /usr/local/nagios
cd /usr/local/src/
tar zxvf nagios-plugins-1.4.13.tar.gz
cd nagios-plugins-1.4.13
./configure --prefix=/usr/local/nagios --with-nagios-user=nagios --with-nagios-group=nagios
make && make install
查看是否安裝成功的方法是:
ls /usr/local/nagios/libexec/ 看這個(gè)目錄下是否有插件文件
cd /usr/local/src/
tar zxvf nrpe-2.12.tar.gz
cd nrpe-2.12
./configure --enable-ssl --enable-command-args
make all
make install-plugin
make install-daemon
make install-daemon-config
vim /usr/local/apache2/conf/httpd.conf
在最后加入以下內(nèi)容:
ScriptAlias /nagios/cgi-bin /usr/local/nagios/sbin
<Directory "/usr/local/nagios/sbin/">
AllowOverride AuthConfig
Options ExecCGI
Order allow,deny
Allow from all
</Directory>
Alias /nagios/ /usr/local/nagios/share/
<Directory "/usr/local/nagios/share">
Options None
AllowOverride AuthConfig
Order allow,deny
Allow from all
</Directory>
cd /usr/local/nagios/etc/
vim cgi.cfg
把 use_authentication=1 改成 use_authentication=0 意思是不用用戶驗(yàn)證
在啟動(dòng)前先檢測(cè)一下:
/usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg
如果最后顯示如下,則說明配置沒有問題了。
Total Warnings: 0
Total Errors: 0
啟動(dòng)命令:
/etc/init.d/nagios start
或者:
/usr/local/nagios/bin/nagios -d /usr/local/nagios/etc/nagios.cfg
此時(shí),就可以訪問web頁面的nagios了,在瀏覽器中輸入:
http://IP/nagios/
看看吧。
如果你打開了web頁面,點(diǎn)擊左欄的Host Detail 會(huì)在右欄看到一行數(shù)據(jù),其中Host 名為 “l(fā)ocalhost” ,Status顯示為”up”,并且顯示為綠色,如果是其他顏色就說明你的localhost出了問題。目前只有一行數(shù)據(jù),也就是說只監(jiān)控了監(jiān)控中心(localhost)一臺(tái)主機(jī),那么如何添加其他機(jī)器被它監(jiān)控呢?這就需要在要被監(jiān)控的機(jī)器上也部署nagios軟件。
useradd nagios
cd /usr/local/src/
wget http://syslab.comsenz.com/downloads/linux/nrpe-2.12.tar.gz
tar zxvf nrpe-2.12.tar.gz
cd nrpe-2.12
./configure --enable-ssl --enable-command-args
make all
make install-plugin
make install-daemon
make install-daemon-config
cd /usr/local/src/
wget http://syslab.comsenz.com/downloads/linux/nagios-plugins-1.4.13.tar.gz
tar zxvf nagios-plugins-1.4.13.tar.gz
cd nagios-plugins-1.4.13
./configure --prefix=/usr/local/nagios --with-nagios-user=nagios --with-nagios-group=nagios
make && make install
到此就算安裝完成了,請(qǐng)查看/usr/local/nagios/目錄下是否有四個(gè)目錄分別為:bin etc libexec share 另外在libexec目錄下會(huì)有很多check_開頭的文件。如果你的機(jī)器上沒有,就請(qǐng)重新安裝吧。
vim /usr/local/nagios/etc/nrpe.cfg
找到”allowed_hosts=127.0.0.1” 改成 “allowed_hosts=127.0.0.1,10.0.4.67”
// 后邊的IP是server的IP
找到” dont_blame_nrpe=0” 改成 “dont_blame_nrpe=1”
/usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -d
添加主機(jī)當(dāng)然是要到server端(監(jiān)控中心)修改配置文件了。
cd /usr/local/nagios/etc/
vim nagios.cfg
增加內(nèi)容:
cfg_dir=/usr/local/nagios/etc/services ##定義一個(gè)目錄,以后把新增加的主機(jī)信息文件全部放到這里
mkdir /usr/local/nagios/etc/services
cd /usr/local/nagios/etc/services
vim 10.0.4.56.cfg 加入如下內(nèi)容:
define host{
use linux-server
host_name 10.0.4.56
alias 10.0.4.56
address 10.0.4.56
}
define service{
use generic-service
host_name 10.0.4.56
service_description check_ping
check_command check_ping!100.0,20%!200.0,50%
max_check_attempts 5
normal_check_interval 1
}
define service{
use generic-service
host_name 10.0.4.56
service_description check_ssh
check_command check_ssh
max_check_attempts 5
normal_check_interval 1
}
define service{
use generic-service
host_name 10.0.4.56
service_description check_http
check_command check_http
max_check_attempts 5
normal_check_interval 1
}
// 注意,這里的IP是client端的IP,監(jiān)控的項(xiàng)目有三個(gè)ping, ssh, http。其實(shí)這三個(gè)項(xiàng)目使用的腳本都為本地腳本,也就是說,即使遠(yuǎn)程主機(jī)沒有安裝nagios和nrpe同樣可以監(jiān)控這些項(xiàng)目。但是如果想監(jiān)控load,disk,等等就需要通過nrpe服務(wù)來搞定了,道理很簡(jiǎn)單,load和disk都需要登錄到遠(yuǎn)程主機(jī)上去獲得信息,而ping,ssh,http都不需要的。這個(gè)到遠(yuǎn)程主機(jī)獲取相關(guān)的信息的過程是由nrpe完成的。如果你的client上沒有啟動(dòng)nrpe服務(wù)那么我們是無法獲取遠(yuǎn)程主機(jī)的load和disk等信息的。下面筆者配置一下使用nrpe來監(jiān)控遠(yuǎn)程主機(jī)的相關(guān)項(xiàng)目。
在server端編輯/usr/local/nagios/etc/objects/commands.cfg
vim /usr/local/nagios/etc/objects/commands.cfg # 在最后面添加如下內(nèi)容
define command{
command_name check_nrpe
command_line $USER1$/check_nrpe -H $HOSTADDRESS$ -c $ARG1$
}
然后編輯10.0.4.56.cfg (還是server上)
cd /usr/local/nagios/etc/services
vim 10.0.4.56.cfg # 加入如下內(nèi)容:
define service{
use generic-service
host_name 10.0.4.56
service_description check_load
check_command check_nrpe!check_load
max_check_attempts 5
normal_check_interval 1
}
define service{
use generic-service
host_name 10.0.4.56
service_description check_disk_hda1
check_command check_nrpe!check_hda1
max_check_attempts 5
normal_check_interval 1
}
define service{
use generic-service
host_name 10.0.4.56
service_description check_disk_hda2
check_command check_nrpe!check_hda2
max_check_attempts 5
normal_check_interval 1
}
這里需要解釋一下相關(guān)的”check_command”, 先看這個(gè)” check_nrpe!check_load” 這里的check_nrpe就是上面/usr/local/nagios/etc/objects/commands.cfg中剛剛定義的,后面的check_load是在遠(yuǎn)程主機(jī)上定義的一個(gè)命令腳本。具體在哪里定義稍后介紹。為什么中間加一個(gè)”!”,這個(gè)是nagios特有的形式,無需關(guān)心。下面需要到遠(yuǎn)程主機(jī)上去定義上面用到的腳本了。
在遠(yuǎn)程主機(jī)上編輯/usr/local/nagios/etc/nrpe.cfg 文件
vim /usr/local/nagios/etc/nrpe.cfg (client上)
把” command[check_hda1]”那行改成:
command[check_hda1]=/usr/local/nagios/libexec/check_disk -w 20% -c 10% -p /dev/hda1
然后再增加一行:
command[check_hda2]=/usr/local/nagios/libexec/check_disk -w 20% -c 10% -p /dev/hda2
這里的check_hda1 和 check_hda2 都是自定義的,和server端的定義的service中的check_command對(duì)應(yīng)。也就是說,如果在server端定義了一個(gè)service(通過nrpe方式)那么必須要在客戶端上的nrpe.cfg中定義相應(yīng)的腳本。保存這個(gè)文件后,需要重新啟動(dòng)一下nrpe服務(wù)。
killall nrpe ; /usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -d (client上)
修改了配置需要重啟服務(wù)才能使添加的監(jiān)控主機(jī)生效。
/etc/init.d/nagios restart (server上)
此時(shí)再到web頁面去觀察是否多了一臺(tái)10.0.4.56
在開始,筆者就講過,我們可以自定義寫監(jiān)控腳步,從上面的例子中也可以看到監(jiān)控磁盤狀態(tài)時(shí),是根據(jù)磁盤分區(qū)來監(jiān)控的。這樣不免有些麻煩,因?yàn)槊颗_(tái)主機(jī)的磁盤分區(qū)狀況都不一樣(一樣還好),而且有多少個(gè)分區(qū)就需要定義多少個(gè)命令。所以筆者就自定義寫一個(gè)shell腳本來監(jiān)控所有的磁盤分區(qū):
vim /usr/local/nagios/libexec/check_disk.sh 寫入如下內(nèi)容:(client上)
#!/bin/bash
row=`df -h -P|wc -l`
status=0
for i in `seq 2 $row`
do
spare=`df -h -P|sed -n "$i"p|awk '{print $4}'`
use_percentage=`df -h -P|sed -n "$i"p|sed -n "s/\%//"p|awk '{print $5}'`
spare_percentage=`expr 100 - $use_percentage`
partition_name=`df -h -P|sed -n "$i"p|awk '{print $6}'`
if [ "$spare_percentage" -lt "3" ];then
echo -n "$partition_name CRITICAL ${spare_percentage}% $spare "
status[$i]=2
elif [ "$spare_percentage" -lt "5" ];then
echo -n "$partition_name WARNING! ${spare_percentage}% $spare "
status[$i]=1
else
echo -n "$partition_name OK ${spare_percentage}% $spare "
status[$i]=0
fi
done
zhuangtai=0
for j in `seq 2 $row`
do
if [ "${status[$j]}" -gt "$zhuangtai" ];then
zhuangtai=${status[$j]}
fi
done
exit $zhuangtai
chmod +x /usr/local/nagios/libexec/check_disk.sh (client上)
vim /usr/local/nagios/etc/nrpe.cfg # 加入一行:(client上)
command[check_disk]=/usr/local/nagios/libexec/check_disk.sh
保存,重啟nrpe服務(wù)
killall nrpe ; /usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -d (client上)
/usr/local/nagios/libexec/check_nrpe -H 10.0.4.56 -c check_disk (server上)
如果正常的話,會(huì)輸出一行磁盤檢測(cè)的數(shù)據(jù),否則可能會(huì)報(bào)錯(cuò)。
cd /usr/local/nagios/etc/services (server上)
vim 10.0.4.56.cfg # 加入如下內(nèi)容:
define service{
use generic-service
host_name 10.0.4.56
service_description check_disk
check_command check_nrpe!check_disk
max_check_attempts 5
normal_check_interval 1
}
/etc/init.d/nagios restart (server上)
現(xiàn)在139郵箱有順便發(fā)短信的功能,所以當(dāng)有報(bào)警時(shí),只需發(fā)送到你的139郵箱你就同樣會(huì)收到一條報(bào)警短信。這樣做的優(yōu)勢(shì)就是不用再去買短信網(wǎng)關(guān)了,節(jié)省了很大一筆錢。
vim /usr/local/nagios/etc/objects/contacts.cfg
把” email nagios@localhost” 修改成 “email 你的139郵箱”
vim /usr/local/nagios/etc/objects/templates.cfg
找到:
define service{
name generic-service
之所以看這一段,是因?yàn)樵谏厦嫣砑拥?0.0.4.56.cfg 定義了很多generic-service所以要關(guān)注這段的配置。
define service{
name generic-service
active_checks_enabled 1
passive_checks_enabled 1
parallelize_check 1
obsess_over_service 1
check_freshness 0
notifications_enabled 1
event_handler_enabled 1
flap_detection_enabled 1
failure_prediction_enabled 1
process_perf_data 1
retain_status_information 1
retain_nonstatus_information 1
is_volatile 0
check_period 24x7
max_check_attempts 3
normal_check_interval 10
retry_check_interval 2
ontact_groups admins
notification_options w,u,c,r
notification_interval 60
notification_period 24x7
register 0
}
其中有幾個(gè)參數(shù)需要你注意:
要想正確發(fā)送郵件,上面的參數(shù)得配置合理才行。
更多建議: