2 nagios 客户端软件安装及配置
2.1 軟件安裝
待監測服務器centos系統主要安裝的軟件是:nagios-plugins-1.4.16.tar.gz和nrpe-2.14.tar.gz。
其他的插件視待監測服務器需要監測的內容而定,如要監測oracle表空間,則需要安裝check_oracle_health-1.7.8.1.tar.gz、DBD-Oracle-1.64.tar.gz和DBI-1.627.tar.gz。
待監測服務器是windows系統的話,則需要安裝NSCP-0.4.1.101-Win32.msi在(32位操作系統)或NSCP-0.4.1.101-x64.msi(64位操作系統)。NSCP在以往的版本中稱為NSClient++。安裝方法也很簡單,在下面具體進行講解。
待檢測的centos5.5或相近版本的系統需要安裝nagios-plugins-1.4.16.tar.gz和nrpe-2.14.tar.gz。安裝方法如下:
一、創建用戶
# useradd nagios
二、安裝插件nagios-plugins-1.4.16.tar.gz
# tar -zxvf nagios-plugins-1.4.16.tar.gz
#cd nagios-plugins-1.4.16
#./configure --prefix=/usr/local/nagios --with-nagios-user=nagios --with-nagios-group=nagios
make && make install
chown -R nagios.nagios /usr/local/nagios/
chown -R nagios.nagios /usr/local/nagios/libexec/
三、安裝nrpe-2.14.tar.gz
1, 安裝nrpe-2.14.tar.gz
先測試openssl和openssl-devel是否已經安裝,如果沒有安裝則首先需要進行安裝。否則configure就有問題。安裝包可以在centos安裝盤中找到:
# rpm -qa|grep openssl
# lsof -i:5666 //查詢端口5666是否未被使用
#cd ..
#tar -zxvf nrpe-2.14.tar.gz
#cd nrpe-2.14
#./configure --enable-ssl --with-ssl-lib
#make all && make install-plugin && make install-daemon && make install-daemon-config
2, 修改nrpe配置文件
修改配置文件/usr/local/nagios/etc/nrpe.cfg:
#vi /usr/local/nagios/etc/nrpe.cfg
1) allow_hosts 加上nagios服務器IP地址,允許與nagios服務器進行通訊:
allowed_hosts=127.0.0.1,192.168.50.22
或使用命令:
# sed -i '/^allowed_/s/$/,192.168.50.22/' /usr/local/nagios/etc/nrpe.cfg
sed -i '/^allowed_/s/$/,192.168.50.191/' /usr/local/nagios/etc/nrpe.cfg
killall nrpe
ps -ef|grep nrpe
/usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -d
exit
向 /etc/rc.d/rc.local 文件最后增加一行空行,然后加入一行自動啟動監聽程序。然后顯示下文件內容,看看添加是否正確:
sed -i '$ a\ ' /etc/rc.d/rc.local
sed -i '$ a\/usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -d' /etc/rc.d/rc.local
more /etc/rc.d/rc.local
介紹一個常用的命令:nagios服務器上,各類似的被監測設備的cfg配置文件都可以復制使用。而service項中有hosts屬性,而且server項又很多,因此,可以使用以下命令全部替換。例如:我們將50.31_cdn1.cfg復制為50.32_cdn2.cfg后,需要將50.32_cdn2.cfg文件的host_name的50.31_cdn1全部要修改成50.32_cdn2,則可以使用以下語句:
sed -i "s/50.31_cdn1/50.32_cdn2/g" `grep 50.31_cdn1 -rl /usr/local/nagios/etc/server/50.32_cdn2.cfg`
sed -i "s/被替換字/替換后字/g" `grep 被替換字 -rl 文件路徑`
2) 增加command項,以下增加的command需要根據所在被監測服務器需要監測的項目以及被監測服務器上的環境而定。如有些服務器的根分區是/dev/sda2,有些是/dev/sda3,而有些是 /dev/mapper/VolGroup00-LogVol00 。例如:
command[check_users]=/usr/local/nagios/libexec/check_users -w 5 -c 10
command[check_load]=/usr/local/nagios/libexec/check_load -w 15,10,5 -c 30,25,20
command[check_sda2]=/usr/local/nagios/libexec/check_disk -w 20% -c 10% -p /dev/sda2
以下各小節會根據被監測服務器的具體情況進行說明。由于服務器眾多,不可能一一全部寫出,下面會盡可能多的找一些比較典型的被監測服務器和設備進行說明。
3, 啟動NRPE進程
啟動NRPE進程有兩種方法,一種是直接使用命令,本人是采用這種方法。
1) 使用命令啟動
/usr/local/nagios/etc/nrpe.cfg配置完畢后,在被監測服務器上啟動NRPE進程:
#/usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -d
如果想讓被監測服務器啟動后自動啟動這個進程,則可以將這個命令寫入/etc/rc.d/rc.local文件中。
NRPE服務啟動后,可以參考nrpe.cfg中的command命令項,在本機測試下各命令是否可以執行,如:
# /usr/local/nagios/libexec/check_nrpe -H 127.0.0.1 -c check_users
USERS OK - 3 users currently logged in |users=3;5;10;0
然后可以到nagios服務器上去測試下命令是否可用,如:
# /usr/local/nagios/libexec/check_nrpe -H 192.168.50.10 -c check_users
USERS OK - 3 users currently logged in |users=3;5;10;0
測試通過。
2) 使用xinetd服務加載
有些資料上敘述的可以使用xinetd進程加載啟動NRPE:
#cd nrpe-2.14
#make install-xinetd
在/etc/services文件中增加一行后,重啟xinetd:
#vi /etc/services
nrpe 5666/tcp # NRPE
#service xinetd restart
4, 關閉NRPE進程
如果是使用第一種命令行方式啟動的,則可以通過kill -9 進程號,或者也可以使用 #killall nrpe 命令關閉NRPE進程。
5, 測試啟動是否成功:
1) 查看端口號
netstat -at | grep nrpe
如果出現以下行,則測試通過
tcp 0 0 *:nrpe *:* LISTEN
2) 也可以使用check_nrpe命令
# /usr/local/nagios/libexec/check_nrpe -H localhost
如果出現以下信息,則測試也通過
NRPE v2.14
3) 也可以在nagios服務器上進行測試:
比如在192.168.50.22上測試192.168.50.10:
# /usr/local/nagios/libexec/check_nrpe -H 192.168.50.10
如果此時出現 NRPE v2.14 則表示成功;反之,如果出現 CHECK_NRPE: Error - Could not complete SSL handshake. 則可能50.22或者50.10上沒有啟動nrpe或者其中之一沒有安裝openssl。
2.2 數據庫192.168.50.10
2.2.1 Oracle服務器監測
1, 在安裝完nagios-plugin和nrpe插件后,我們來安裝監控oracle表空間的插件。
1) 查看是否安裝了perl:
輸入 perl -v,出現以下信息則說明已安裝。否則先安裝perl,因為這個監測腳本是使用perl編寫的。可以使用yum,也可以在centos安裝盤上找相應的rpm包:
#perl -v
This is perl, v5.8.8 built for x86_64-linux-thread-multi
Copyright 1987-2006, Larry Wall
... ... ... ...
2) 安裝 DBI-1.627.tar.gz
#tar zxvf DBI-1.627.tar.gz
cd DBI-1.627
perl Makefile.PL
make all
make install
3) 沒有報錯我們進行下一步安裝DBD-Oracle
#tar zxvf DBD-Oracle-1.64.tar.gz
cd DBD-Oracle-1.64
perl Makefile.PL
執行上述命令肯定會遇到找不到ORACLE_HOME或LD_LIBRARY_PATH錯誤:
可以先設置臨時ORACLE_HOME變量,參考oracle用戶的環境變量:
#export ORACLE_HOME=/u01/oracle/product/11.2.0/db_1
export LD_LIBRARY_PATH=$ORACLE_HOME/lib:/lib64:/usr/lib64:/usr/local/lib64:/usr/X11R6/lib64/
再執行perl Makefile.PL就OK了
make
make install
4) 最后一步安裝主角check_oracle_health
#tar zxvf check_oracle_health-1.7.8.1.tar.gz
cd check_oracle_health-1.7.8.1
./configure --prefix=/usr/local/nagios --with-nagios-user=nagios --with-nagios-group=nagios --with-mymodules-dir=/usr/local/nagios/libexec --with-mymodules-dyndir=/usr/local/nagios/libexec
make all
make install
完成后,查看/usr/local/nagios/libexec目錄下插件check_oracle_health已經有了。
2, 切換到oracle用戶,試運行一下這個插件
/usr/local/nagios/libexec/check_oracle_health --connect=你oracle的SID --user=oracle用戶 --password=oracle密碼 --mode=tnsping
或者可以把最后的--mode=tnsping換成--mode=tablespace-usage試試看是否能查看所有表空間了:
#su - oracle
$/usr/local/nagios/libexec/check_oracle_health --connect=voddb --user=sys --password=111111 --mode=tnsping
OK - connection established to voddb. 輸出OK了
$
$/usr/local/nagios/libexec/check_oracle_health --connect=voddb --user=sys --password=111111 --mode=tablespace-usage
OK - tbs USERS usage is 2.32%, tbs UNDOTBS1 usage is 0.11%,,,,,,,,,,,,后面一大堆表空間信息
如果測試的時候,報:
CRITICAL - cannot connect to voddb. install_driver(Oracle) failed: Can't load '/usr/lib64/perl5/site_perl/5.8.8/x86_64-linux-thread-multi/auto/DBD/Oracle/Oracle.so' for module DBD::Oracle: libclntsh.so.11.1: cannot open shared object file: No such file or directory at /usr/lib64/perl5/5.8.8/x86_64-linux-thread-multi/DynaLoader.pm line 230.
at (eval 13) line 3
的錯誤,則進行如下處理:
vi /etc/ld.so.conf
include ld.so.conf.d/*.conf
/u01/oracle/product/11.2.0/db_1/lib #增加這一句后執行:
#ldconfig /etc/ld.so.conf
#讓這個文件生效后,再次執行以上check_oracle_health就能通過
#/usr/local/nagios/libexec/./check_oracle_health --connect=voddb --user=sys --password=111111 --mode=tnsping
OK - connection established to voddb.
#
還有一種解決這個問題的方法是 http://blog.csdn.net/russle/article/details/4542662 :
Another solution to this problem is to modify the Makefile file (which is created when you run perl Makefile.PL ) as follows:
1.Search for the line LD_RUN_PATH=
2.Replace it with LD_RUN_PATH=my_oracle_home/lib
where my_oracle_home is, of course, the home path to your Oracle installation. In particular, the file libclntsh.so.8.0 should exist in the lib subdirectory.
Then just type make install , and all should go well.
Note that setting LD_RUN_PATH has the effect of hardcoding the path to my_oracle_home/lib in the file Oracle.so , which is generated by DBD::Oracle . This is an efficiency mechanism, so that at runtime it doesn't have to search through LD_LIBRARY_PATH or the default directories used by ld .
3, 在root用戶的.bash_profile文件中添加以下oracle_home等參數(參考oracle用戶的.bash_profile文件):
vi .bash_profile
export ORACLE_HOME=/u01/oracle/product/11.2.0/db_1
export LD_LIBRARY_PATH=$ORACLE_HOME/lib:/lib64:/usr/lib64:/usr/local/lib64:/usr/X11R6/lib64/
root重新登錄后執行
#/usr/local/nagios/libexec/./check_oracle_health --connect=voddb --user=system --password=111111 --mode=tablespace-usage
如果能夠測試出oracle系統的各個表空間的情況,則以上操作都已經通過。
如果還是不行,可以嘗試將oracle用戶的.bash_profile文件中有關oracle的參數都添加上去:
export ORACLE_BASE=/u01/oracle/
export ORACLE_HOME=$ORACLE_BASE/product/11.2.0/db_1
export PATH=$PATH:$ORACLE_HOME/bin
export ORACLE_SID=voddb
在64bit的centos5.8中還會出現以下錯誤:
則需要在 .bash_profile文件中再增加參數:
export LD_LIBRARY_PATH=$ORACLE_HOME/lib:/lib64:/usr/lib64:/usr/local/lib64:/usr/X11R6/lib64/
之后,就能成功執行:
# /usr/local/nagios/libexec/check_oracle_health --connect=voddb --user=sys --password=111111 --mode=tnsping
4, 被監控測試自己是沒問題了,如何讓監控機去調用這個腳本呢?在被監控機上的nrpe.cfg文件加入如下內容(完整的內容請看第5小點):
vi /usr/local/nagios/etc/nrpe.cfg
command[check_oracle_health]=/usr/local/nagios/libexec/check_oracle_health --connect=voddb --user=sys --password=111111 --mode=tablespace-usage
5) 保存后退出,然后我們重啟被監控機的nrpe服務
#killall nrpe
#/usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -d
6) 下面我們到nagios服務器50.22上去測試這個插件
/usr/local/nagios/libexec/check_nrpe -H 你的被監控機IP地址 -c check_oracle_health
#/usr/local/nagios/libexec/check_nrpe -H 192.168.50.10 -c check_oracle_health
這里192.168.50.10是數據庫服務器的ip地址。
如果正常,就會輸出所有的表空間使用情況。
5, 修改nrpe.cfg配置文件
#vi /usr/local/nagios/etc/nrpe.cfg
#允許與50.22進行通訊
... ...
allowed_hosts=127.0.0.1,192.168.50.22
... ...
command[check_users]=/usr/local/nagios/libexec/check_users -w 5 -c 10
command[check_load]=/usr/local/nagios/libexec/check_load -w 15,10,5 -c 30,25,20
#監測根分區目錄 / 的硬盤容量使用情況
command[check_sda2]=/usr/local/nagios/libexec/check_disk -w 20% -c 10% -p /dev/sda2
#監測oracle數據庫安裝表空間的分區目錄 /u01 的硬盤容量使用情況
command[check_sda5]=/usr/local/nagios/libexec/check_disk -w 20% -c 10% -p /dev/sda5
command[check_swap]=/usr/local/nagios/libexec/check_swap -w 20% -c 10%
command[check_zombie_procs]=/usr/local/nagios/libexec/check_procs -w 5 -c 10 -s Z
command[check_total_procs]=/usr/local/nagios/libexec/check_procs -w 300 -c 400
#監測oracle sid
command[check_oracle_sid]=/usr/local/nagios/libexec/check_oracle --db voddb
#監測oracle tns
command[check_oracle_tns]=/usr/local/nagios/libexec/check_oracle --tns voddb
#監測oracle 表空間,這里的check_oracle_tbs為經過修改的check_oracle_health。check_oracle_health為監控oracle表空間的插件
command[check_oracle_health]=/usr/local/nagios/libexec/check_oracle_tbs --connect=voddb --user=system --password=111111 --m
ode=tablespace-usage --warning=80 --critical=90
其他參數都默認。
這里的command在本機可以使用以下命令進行測試,如:
# /usr/local/nagios/libexec/check_nrpe -H 127.0.0.1 -c check_swap
# /usr/local/nagios/libexec/check_nrpe -H 127.0.0.1 -c check_oracle_health
Nagios服務器192.168.50.22服務器可以直接在shell下驗證該命令是否能夠成功執行,如:
# /usr/local/nagios/libexec/./check_nrpe -H 192.168.50.10 -c check_swap
SWAP OK - 97% free (9829 MB out of 10236 MB) |swap=9829MB;2047;1023;0;10236
#
一定要在nagios的服務器上驗證下到被監測機器的監測項是否可執行,是否能獲取到監測數據。
6, 在進行命令執行測試時,可能會出現以下問題:
1) 在數據庫機器直接使用以下命令測試能夠成功:
/usr/local/nagios/libexec/check_oracle --db voddb
/usr/local/nagios/libexec/check_oracle --tns voddb
/usr/local/nagios/libexec/check_oracle_tbs --connect=voddb --user=system --password=111111 --m
ode=tablespace-usage --warning=80 --critical=90
2) 在數據庫機器使用check_nrpe測試oracle sid 正常,但是oracle tns和tbs的兩個命令會出現以下錯誤:
[root@db1 libexec]# ./check_nrpe -H 127.0.0.1 -c check_oracle_sid
adpdb OK - 1 PMON process(es) running
[root@db1 libexec]# ./check_nrpe -H 127.0.0.1 -c check_oracle_tns
No TNS Listener on adpdb
[root@db1 libexec]# ./check_nrpe -H 127.0.0.1 -c check_oracle_health
CRITICAL - cannot connect to adpdb. ORA-12154: TNS:could not resolve the connect identifier specified (DBD ERROR: OCIServerAttach)
[root@db1 libexec]#
3) 解決方法
出現這個問題,是權限不足,將nagios加入到oinstall組,并重啟nrpe(一定要重啟下)。這樣,錯誤就能解決:
[root@db1 libexec]#usermod -a -G oinstall nagios
[root@db1 libexec]#id nagios
uid=504(nagios) gid=504(nagios) groups=504(nagios),501(oinstall)
[root@db1 libexec]# killall nrpe
[root@db1 libexec]# /usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -d
[root@db1 libexec]# ./check_nrpe -H 127.0.0.1 -c check_oracle_tns
OK - reply time 0 msec from adpdb
[root@db1 libexec]# ./check_nrpe -H 127.0.0.1 -c check_oracle_health
OK - tbs USERS usage is 20.00%
tbs UNDOTBS1 usage is 1.09%
tbs TEMP usage is 0.00%
tbs SYSTEM usage is 67.41%
tbs SYSAUX usage is 74.84%
'tbs_users_alloc'=5MB;;;0;5 'tbs_undotbs1_usage_pct'=1.09%;80;90 'tbs_undotbs1_usage'=10MB;740;832;0;925 'tbs_undotbs1_alloc'=925MB;;;0;925 'tbs_temp_usage_pct'=0.00%;80;90 'tbs_temp_usage'=0MB;819;921;0;1024 'tbs_temp_alloc'=1024MB;;;0;1024 'tbs_system_usage_pct'=67.41%;80;90 'tbs_system_usage'=690MB;819;921;0;1024 'tbs_system_alloc'=1024MB;;;0;1024 'tbs_sysaux_usage_pct'=74.84%;80;90 'tbs_sysaux_usage'=766MB;819;921;0;1024 'tbs_sysaux_alloc'=1024MB;;;0;1024 'tbs_adp_ts_usage_pct'=0.49%;80;90
[root@db1 libexec]#
注意事項:
check_oracle_health這個監測腳本,對表空間的使用率百分比計算方式是:
已經使用的表空間大小 / 最大能夠達到的表空間大小 ×100%
這里的“最大能夠達到的表空間大小”不是當前表空間的大小,而是表空間能夠擴大的最大表空間文件大小,oracle11g默認的最大表空間文件大小都是:32767MB。
以上面的system表空間為例,我們分配的表空間文件為2G,目前已經使用了1160.5M,oracle console計算出來的已經使用的百分比為56.67。
而check_oracle_health計算的方法是:1160.5M / 32767M = 3.5%
這個不是我們希望的數據,如果所有的表空間文件都自動增加到32767M,服務器都要爆掉了。我們希望的使用百分比應該是56.57,當數據使用率超過80%的時候,人為的去擴大表空間,而不是讓oracle系統自己去擴大。
因此,對check_oracle_health腳本進行了修改,將2913行的:
-- a.maxbytes bytes_max,
改成了:
a.bytes bytes_max,
將2966行的:
-- SUM(DECODE(d.autoextensible, 'YES', d.maxbytes, 'NO', d.bytes)) bytes_max,
改成了
SUM(d.bytes) bytes_max,
并將這個腳本另存為check_oracle_tbs。修改后的那一段代碼為:
if (DBD::Oracle::Server::return_first_server()->version_is_minimum("9.x")) {
@tablespaceresult = $params{handle}->fetchall_array(q{
SELECT
a.tablespace_name "Tablespace",
b.status "Status",
b.contents "Type",
b.extent_management "Extent Mgmt",
a.bytes bytes,
-- a.maxbytes bytes_max, -- modified by sandish 20130619
a.bytes bytes_max,
c.bytes_free + NVL(d.bytes_expired,0) bytes_free
FROM
(
-- belegter und maximal verfuegbarer platz pro datafile
-- nach tablespacenamen zusammengefasst
-- => bytes
............
............
UNION ALL
SELECT
d.tablespace_name "Tablespace",
b.status "Status",
b.contents "Type",
b.extent_management "Extent Mgmt",
sum(a.bytes_free + a.bytes_used) bytes, -- allocated
-- SUM(DECODE(d.autoextensible, 'YES', d.maxbytes, 'NO', d.bytes)) bytes_max, --modified by sandish 20130619
SUM(d.bytes) bytes_max,
SUM(a.bytes_free + a.bytes_used - NVL(c.bytes_used, 0)) bytes_free
FROM
sys.v_$TEMP_SPACE_HEADER a,
sys.dba_tablespaces b,
sys.v_$Temp_extent_pool c,
dba_temp_files d
WHERE
c.file_id(+) = a.file_id
and c.tablespace_name(+) = a.tablespace_name
and d.file_id = a.file_id
and d.tablespace_name = a.tablespace_name
and b.tablespace_name = a.tablespace_name
GROUP BY
b.status,
b.contents,
b.extent_management,
d.tablespace_name
ORDER BY
1
});
修改后的腳本,基本上符合了對表空間監測的要求,但對于臨時表空間的監測出來的數據都還是0。
----查找臨時表空間使用情況:
SELECT a.tablespace_name, a.BYTES total, a.bytes - nvl(b.bytes, 0) free,
nvl(b.bytes, 0)/a.BYTES *100 "used(%)"
FROM (SELECT tablespace_name, SUM (bytes) bytes FROM dba_temp_files GROUP BY tablespace_name) a,
(SELECT tablespace_name, SUM (bytes_cached) bytes FROM v$temp_extent_pool GROUP BY tablespace_name) b
WHERE a.tablespace_name = b.tablespace_name(+);
可以根據以上的查詢臨時表空間的方法修改監測腳本。
1.2.2 nagios服務器192.168.50.22上的配置
拷貝objects 文件夾中的localhost.cfg到/servers下,重命名為50.10_voddb.cfg:
#/usr/local/nagios/etc/objects/localhost.cfg -p /usr/local/nagios/etc/servers/50.10_voddb.cfg
#vi /usr/local/nagios/etc/servers/50.10_voddb.cfg
###############################################################################
###############################################################################
#
# HOST DEFINITION
#
###############################################################################
###############################################################################
# Define a host for the local machine
define host{
use linux-vod-sw ;從這個模板進行繼承,交換機為父類
host_name 50.10_voddb ;host名字,以下所有的service的host_name都要改成這個
alias voddb centos5.5 64bit ; A longer name for the server
address 192.168.50.10 ; IP address of the server
icon_p_w_picpath database.gif ; nagios map上顯示的兩張圖
statusmap_p_w_picpath database.gd2
}
#模板中的這里有hostgroup一項,這里全部刪除,我們已經創建了hostgroup.cfg文件,在hostgroup.cfg文件中,將本機的host_name加入其members就行。
###############################################################################
###############################################################################
#
# SERVICE DEFINITIONS
#
###############################################################################
###############################################################################
# Define a service to "ping" the local machine
define service{
use generic-service ; Name of service template to use
host_name 50.10_voddb
service_description PING
check_command check_ping!100.0,20%!500.0,60%
}
define service{
use generic-service ; Name of service template to use
host_name 50.10_voddb
service_description ftp
check_command check_ftp ;本機上啟動了ftp服務,也需要監測
}
# Define a service to check the number of currently logged in
# users on the local machine. Warning if > 20 users, critical
# if > 50 users.
define service{
use generic-service ; Name of service template to use
host_name 50.10_voddb
service_description Current Users
check_command check_nrpe!check_users
}
# Define a service to check the number of currently running procs
# on the local machine. Warning if > 250 processes, critical if
# > 400 users.
define service{
use generic-service ; Name of service template to use
host_name 50.10_voddb
service_description Total Processes
check_command check_nrpe!check_total_procs
}
define service{
use generic-service
host_name 50.10_voddb
service_description Current Load
check_command check_nrpe!check_load
}
# Define a service to check the swap usage the local machine.
# Critical if less than 10% of swap is free, warning if less than 20% is free
define service{
use generic-service ; Name of service template to use
host_name 50.10_voddb
service_description Swap Usage
check_command check_nrpe!check_swap
}
define service{
use generic-service
host_name 50.10_voddb
service_description Space Usage /
check_command check_nrpe!check_sda2
}
define service{
use generic-service
host_name 50.10_voddb
service_description Space Usage /u01/
check_command check_nrpe!check_sda5
}
define service{
use generic-service
host_name 50.10_voddb
service_description Zombie Processes
check_command check_nrpe!check_zombie_procs
}
define service{
use generic-service
host_name 50.10_voddb
service_description oracle_sid
check_command check_nrpe!check_oracle_sid
}
define service{
use generic-service
host_name 50.10_voddb
service_description oracle_tns
check_command check_nrpe!check_oracle_tns
}
define service{
use generic-service
host_name 50.10_voddb
service_description oracle_tablespace
check_command check_nrpe!check_oracle_health
}
最后驗證配置文件是否正確,并重啟nagios服務:
# /usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg
#service nagios start
然后可以查看nagios的ie:http://192.168.50.22/nagios/,點擊左面列表看的host,然后選擇50.10_voddb右邊的放大鏡,等幾十秒就能查看到監測信息了:
2.3 其他centos機器監測
2.3.1 被監測機器配置
我們再找一臺作為例子來說明如何監測。centos機器的監測項除了cpu負載,內存、硬盤空間,主要的監測項為進程和端口。
進程有tomcat,dhcp,dns,httpd,ftp,mysql等;端口如80,8080等。
如果要監測某個進程,我們可以通過命令先查看下系統中的進程以及它的數量;
如果要監測端口,我們可以使用命令“lsof -i:端口號”查看端口是否已經打開。
如果監測硬盤容量,可以使用df先查看下硬盤的設備文件名。
一般,我們都使用插件check_nrpe來監測。如果是自定義的監測項,需要在被監測機器中進行配置。查看下我已經配置好的一臺dhcp服務器的nrpe.cfg文件:
#[root@dhcp root]# vi /usr/local/nagios/etc/nrpe.cfg
... ...
command[check_users]=/usr/local/nagios/libexec/check_users -w 5 -c 10
command[check_load]=/usr/local/nagios/libexec/check_load -w 15,10,5 -c 30,25,20
command[check_disk]=/usr/local/nagios/libexec/check_disk -w 20% -c 10% -p /dev/mapper/VolGroup00-LogVol00
command[check_zombie_procs]=/usr/local/nagios/libexec/check_procs -w 5 -c 10 -s Z
command[check_total_procs]=/usr/local/nagios/libexec/check_procs -w 300 -c 400
command[check_swap]=/usr/local/nagios/libexec/check_swap -w 20% -c 10%
command[check_dns_proc]=/usr/local/nagios/libexec/check_procs -c 1:1 -C named
command[check_dhcpd_proc]=/usr/local/nagios/libexec/check_procs -c 1:1 -C dhcpd
這里,command[check_disk]是監測根分區的使用情況。通過df命令,查得根分區的設備文件為/dev/mapper/VolGroup00-LogVol00。
command[check_dns_proc]為監測dns服務啟是否啟動,如果有一個dns進程,則表示狀態正常,否則都是異常;
command[check_dhcpd_proc] 為監測dns服務啟是否啟動,如果有一個dhcp進程,則表示狀態正常,否則都是異常;
另外,檢查下allowed_hosts是否將nagios服務器ip寫入了,正確的應該為:
allowed_hosts=127.0.0.1,192.168.50.22
允許本機和nagios服務器通過nrpe協議進行通訊。
測試tcp端口不需要定義監測命令。修改好后重啟下nrpe:
#killall nrpe
#/usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -d
2.3.2 nagios服務器192.168.50.22上的配置
拷貝objects 文件夾中的localhost.cfg到/servers下,由于被監測機器的ip地址為192.168.50.11,我們重命名localhost.cfg為50.11_portal.cfg:
#/usr/local/nagios/etc/objects/localhost.cfg -p /usr/local/nagios/etc/servers/50.11_portal.cfg
vi /usr/local/nagios/etc/servers/50.11_portal.cfg
###############################################################################
###############################################################################
#
# HOST DEFINITION
#
###############################################################################
###############################################################################
# Define a host for the local machine
define host{
use linux-vod-sw ; Inherit default values from a template
host_name 50.11_portal ; The name we're giving to this server
alias portal centos5.5 32bit ; A longer name for the server
address 192.168.50.11 ; IP address of the server
}
###############################################################################
###############################################################################
#
# SERVICE DEFINITIONS
#
###############################################################################
###############################################################################
# Define a service to "ping" the local machine
define service{
use generic-service ; Name of service template to use
host_name 50.11_portal
service_description PING
check_command check_ping!100.0,20%!500.0,60%
}
# Define a service to check the number of currently logged in
# users on the local machine. Warning if > 20 users, critical
# if > 50 users.
define service{
use generic-service ; Name of service template to use
host_name 50.11_portal
service_description Current Users
check_command check_nrpe!check_users
}
# Define a service to check the number of currently running procs
# on the local machine. Warning if > 250 processes, critical if
# > 400 users.
define service{
use generic-service ; Name of service template to use
host_name 50.11_portal
service_description Total Processes
check_command check_nrpe!check_total_procs
}
# Define a service to check the swap usage the local machine.
# Critical if less than 10% of swap is free, warning if less than 20% is free
define service{
use generic-service ; Name of service template to use
host_name 50.11_portal
service_description Swap Usage
check_command check_nrpe!check_swap
}
# Define a service to check HTTP on the local machine.
# Disable notifications for this service by default, as not all users may have HTTP enabled.
define service{
use generic-service ; Name of service template to use
host_name 50.11_portal
service_description tomcat_portal
check_command check_tcp!8080 ;監測8080端口
}
define service{
use generic-service ; Name of service template to use
host_name 50.11_portal
service_description tomcat_ web
check_command check_tcp!8082 ;監測8082端口
}
define service{
use generic-service ; Name of service template to use
host_name 50.11_portal
service_description portal_aaa ;
check_command check_tcp!3000 ;監測3000端口
}
define service{
use generic-service
host_name 50.11_portal
service_description Space Usage/
check_command check_nrpe!check_disk
}
define service{
use generic-service
host_name 50.11_portal
service_description Current Load
check_command check_nrpe!check_load
}
define service{
use generic-service
host_name 50.11_portal
service_description Zombie Processes
check_command check_nrpe!check_zombie_procs
}
最后驗證配置文件是否正確,如果正確,則重啟nagios服務:
# /usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg
#service nagios start
然后可以刷新ie:http://192.168.50.22/nagios/,等幾十秒就能查看到監控信息了:
2.4 windows機器監測
2.4.1 客戶windows服務器配置
30幾臺服務器中,其中2、3臺的操作系統為windows2003server 32位操作系統,因此需要安裝NSCP-0.4.1.101-Win32.msi。如果是windows2008R2系統,由于是64位,因此,可以安裝NSCP-0.4.1.101-x64.msi。
安裝的時候,需要填寫nagios服務器的ip地址,允許與nagios服務器進行通訊。密碼可以填111111,或者不填。這里我們填了6個1,因此,在nagios服務器的command.cfg配置文件的check_nt命令行中需要增加 -s 111111 的參數,否則,通訊會失敗。
Enable NSCAclient不選擇。
其他的都默認,點擊下一步進行安裝。
NSClient++ 默認安裝在C:\Program Files\NSClient++,其配置文件為:nsclient.ini。如果安裝時候,密碼忘了寫,可以在安裝完后,直接修改這個配置文件。
這里,需要監測的內容除了一般的服務器的信息外,還包括的windows服務有:
mysql
sqlserver
tomcat
監測的進程有cmd.exe,explorer.exe
1.4.2 nagios服務器192.168.50.22上的配置
拷貝objects 文件夾中的windows.cfg到/servers下,重命名為50.15_win1.cfg:
#/usr/local/nagios/etc/objects/windows.cfg -p /usr/local/nagios/etc/servers/50.15_win1.cfg
#vi /usr/local/nagios/etc/servers/50.15_win1
###############################################################################
# WINDOWS.CFG - SAMPLE CONFIG FILE FOR MONITORING A WINDOWS MACHINE
#
# Last Modified: 06-13-2007
#
# NOTES: This config file assumes that you are using the sample configuration
# files that get installed with the Nagios quickstart guide.
#
###############################################################################
###############################################################################
###############################################################################
#
# HOST DEFINITIONS
#
###############################################################################
###############################################################################
# Define a host for the Windows machine we'll be monitoring
# Change the host_name, alias, and address to fit your situation
define host{
use linux-vod-sw ; Inherit default values from a template
host_name 50.15_win1 ; The name we're giving to this host
alias win2k3 ; A longer name associated with the host
address 192.168.50.15 ; IP address of the host
icon_p_w_picpath win40.gif
statusmap_p_w_picpath win40.gd2
}
###############################################################################
###############################################################################
#
# SERVICE DEFINITIONS
#
###############################################################################
###############################################################################
# Create a service for monitoring the uptime of the server
# Change the host_name to match the name of the host you defined above
define service{
use generic-service
host_name 50.15_win1
service_description Uptime
check_command check_nt!UPTIME
}
# Create a service for monitoring CPU load
# Change the host_name to match the name of the host you defined above
define service{
use generic-service
host_name 50.15_win1
service_description CPU Load
check_command check_nt!CPULOAD!-l 5,80,90
}
# Create a service for monitoring memory usage
# Change the host_name to match the name of the host you defined above
define service{
use generic-service
host_name 50.15_win1
service_description Memory Usage
check_command check_nt!MEMUSE!-w 80 -c 90
}
# Create a service for monitoring C:\ disk usage
# Change the host_name to match the name of the host you defined above
define service{
use generic-service
host_name 50.15_win1
service_description C:\ Drive Space
check_command check_nt!USEDDISKSPACE!-l c -w 80 -c 90
}
# Create a service for monitoring the W3SVC service
# Change the host_name to match the name of the host you defined above
define service{
use generic-service
host_name 50.15_win1
service_description MySQL55
check_command check_nt!SERVICESTATE!-d SHOWALL -l MySQL55
}
#windows服務名中如果帶有空格,需要用雙引號引起來
define service{
use generic-service
host_name 50.15_win1
service_description Tomcat6
check_command check_tcp!8899
}
define service{
use generic-service
host_name 50.15_win1
service_description FTP
check_command check_tcp!21
}
define service{
use generic-service
host_name 101.27_win
service_description MSSQLSERVER
check_command check_nt!SERVICESTATE!-d SHOWALL -l MSSQLFDLauncher,MSSQLSERVER,MSSQLServerOLAPService,MsDtsServer100,ReportServer,SQLWriter,SQLSERVERAGENT #需要監測多個服務,依次寫上
}
# Create a service for monitoring the Explorer.exe process
# Change the host_name to match the name of the host you defined above
define service{
use generic-service
host_name 50.15_win1
service_description Explorer
check_command check_nt!PROCSTATE!-d SHOWALL -l Explorer.exe
}
define service{
use generic-service
host_name 50.15_win1
service_description CMD
check_command check_nt!PROCSTATE!-d SHOWALL -l cmd.exe
}
監測的windows服務的服務名稱可查看windows服務中的“服務名稱”(注意不是“顯示名稱”),如下圖中為Tomcat6
修改
vi /usr/local/nagios/etc/objects/commands.cfg
# 'check_nt' command definition
define command{
command_name check_nt
command_line $USER1$/check_nt -H $HOSTADDRESS$ -p 12489 -v $ARG1$ $ARG2$ -s 111111 #把密碼加進來
}
我這里幾臺windows安裝的NSClient++密碼都是6個1。如果兩臺windows安裝的NSClient++密碼不一樣,我也想不出什么好辦法來,因此,盡量用同樣的密碼,或者都不輸入密碼。
最后驗證配置文件是否正確,并重啟nagios服務:
# /usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg
#service nagios start
然后可以刷新ie:http://192.168.50.22/nagios/,等幾十秒就能查看到監控信息了:
2.5 交換機配置
查看nagios的交換機默認監測配置文件:/usr/local/nagios/etc/object/swicth.cfg,可以看到交換機監測的常規項似乎監控端口以及端口的帶寬,我們將這兩項配置起來。
監測端口狀態使用命令check_snmp!-C public -o ifOperStatus.1 -r 1 -m RFC1213-MIB,因此需要知道需要監測的端口號的OID;
監測端口帶寬利用率使用命令check_local_mrtgtraf!/var/www/html/mrtg/192.168.50.252_1.log!AVG!1000000,1000000!5000000,5000000!10 ,這里,需要使用mrtg獲取監測端口的log文件。因此,需要在服務器端安裝mrtg包。啟動mrtg獲取端口帶寬數據日志文件,然后nagios通過分析日志文件,將帶寬數據反映到nagios的web上。
2.5.1 交換機配置
1, 啟動交換機SNMP
這里的交換機型號為思科的3650。查詢交換機SNMP是否啟動,可以通過命令查看。如果交換機已經打開了ie的配置頁面功能,也可以打開IE登錄后查看:
User Access Verification
Password:
Vod-SW>en
Password:
Vod-SW#show snmp
%SNMP agent not enabled
Vod-SW#
在交換機中啟動snmp,我們通過ie界面進行配置。打開ie,輸入“http://交換機ip地址”,輸入交換機的登錄用戶名密碼后,找到“Advanced Settings”,將SNMP選擇Enable,并輸入SNMP Read Community為public,后,點擊提交。
Vod-SW#show snmp
Chassis: FOC1509Z5JL
0 SNMP packets input
0 Bad SNMP version errors
0 Unknown community name
0 Illegal operation for community name supplied
0 Encoding errors
0 Number of requested variables
0 Number of altered variables
0 Get-request PDUs
0 Get-next PDUs
0 Set-request PDUs
0 Input queue packet drops (Maximum queue size 1000)
0 SNMP packets output
0 Too big errors (Maximum packet size 1500)
0 No such name errors
0 Bad values errors
0 General errors
0 Response PDUs
0 Trap PDUs
SNMP global trap: disabled
SNMP logging: disabled
SNMP agent enabled
Vod-SW#
2, 獲取交換機端口信息(可以在50.22上進行如下獲取OID操作)
(這里順便提一下,如果使用mrtg插件監測交換機的話,在mrtg配置成功后,可以在mrtg的html主頁面上就能知道各個端口的OID,因此,也不需要通過以下的方法獲取交換機端口的OID號。不過,很多的硬件設備還是需要自行搜索OID號,并根據硬件產商官方有關文檔進行對比甄別所要監測的OID信息。)
獲取端口信息的命令為:check_snmp!-C public -o ifOperStatus.1 -r 1 -m RFC1213-MIB。其中:
"-o ifOperStatus.1"指取出交換機的端口編號為1的OID狀態。"-r 1"選項是讓check_snmp插件檢查返回一個正常(OK)狀態,如果是在SNMP查詢結果中存在"1"(1說明交換機端口處于運行狀態)如果沒找到1就是緊急(CRITICAL)狀態。"-m RFC1213-MIB"是可選的,它告訴check_snmp插件只加載"RFC1213-MIB"庫而不是加載每個在系統里的MIB庫,這可以加快插件運行速度。
監測交換機的端口需要OID,即端口號。交換機的端口號1往往就是1,而其他的端口號就不是2,3,4等等了。可以通過以下命令獲取端口信息(信息中包含端口號):
snmpwalk -c public -v 1 -t 120 [交換機ip地址] >>switch.txt
查看switch.txt文件:
IF-MIB::ifOperStatus.1 = INTEGER: up(1)
IF-MIB::ifOperStatus.301 = INTEGER: up(1)
IF-MIB::ifOperStatus.302 = INTEGER: up(1)
IF-MIB::ifOperStatus.303 = INTEGER: up(1)
IF-MIB::ifOperStatus.304 = INTEGER: up(1)
IF-MIB::ifOperStatus.305 = INTEGER: up(1)
IF-MIB::ifOperStatus.5001 = INTEGER: up(1)
IF-MIB::ifOperStatus.10101 = INTEGER: up(1)
IF-MIB::ifOperStatus.10102 = INTEGER: up(1)
... ... ... ...
IF-MIB::ifOperStatus.10147 = INTEGER: down(2)
IF-MIB::ifOperStatus.10148 = INTEGER: down(2)
IF-MIB::ifOperStatus.10149 = INTEGER: up(1)
IF-MIB::ifOperStatus.10150 = INTEGER: up(1)
IF-MIB::ifOperStatus.10151 = INTEGER: down(2)
IF-MIB::ifOperStatus.10152 = INTEGER: down(2)
IF-MIB::ifOperStatus.10501 = INTEGER: up(1)
然后根據查到的端口對應的OID設置端口監控
這里我們可以找幾個端口號進行監測,普通的端口為1到48;49和50是聚合口,連到核心交換機上。因此我們可以監測端口1,49和50:ifOperStatus.1,ifOperStatus.10149和ifOperStatus.10150
其中,配置文件:
check_ping!200.0,20%!600.0,60%
表示:
緊急(CRITICAL)-條件是RTA大于600ms或丟包率大于等于60%;
告警(WARNING)-條件是RTA大于200ms或是丟包率大于等于20%;
正常(OK)-條件是RTA小于200ms或丟包率小于20%
check_snmp!-C public -o ifOperStatus.1 -r 1 -m RFC1213-MIB
表示:
"-o ifOperStatus.1"指出取出交換機的端口編號為1的OID狀態。"-r 1"選項是讓check_snmp插件檢查返回一個正常(OK)狀態,如果是在SNMP查詢結果中存在"1"(1說明交換機端口處于運行狀態)如果沒找到1就是緊急(CRITICAL)狀態。"-m RFC1213-MIB"是可選的,它告訴check_snmp插件只加載"RFC1213-MIB"庫而不是加載每個在系統里的MIB庫,這可以加快插件運行速度。
check_local_mrtgtraf!/var/www/html/mrtg/192.168.50.252_10150.log!AVG!10000000,10000000!50000000,50000000!10
表示:
讀取日志文件/var/www/html/mrtg/192.168.50.252_10150.log中的平均速度,當超過10M時狀態改為warning;如果超過50M狀態改為critical。
"/var/www/html/mrtg/192.168.50.252_10150.log "參數傳給check_local_mrtgtraf,意思是插件的MRTG日志文件在這個文件里讀寫,"AVG"參數的意思是取帶寬的統計平均值,"10000000, 10000000"參數是指流入的告警門限 (以字節為單位),"50000000,50000000"是輸出流量緊急狀態門限(以字節為單位),"10"是指如果MRTG日志如果超過10分鐘沒有數據 返回一個緊急狀態(應該每5分鐘更新一次)。
2.5.2 nagios服務器192.168.50.22上的配置
在交換機上打開snmp通訊就行了,大部分的工作是在nagios服務器上進行配置。
一、 MRTG插件安裝(http://www.xianren.org/net/centos-mrtg.html)
監測端口流量需要使用MRTG插件,在服務器上先安裝MRTG插件:
1, 下載MRTG安裝包
從http://oss.oetiker.ch/mrtg/pub/?M=D下載mrtg-2.17.4.tar.gz
2, 前提條件
在安裝mrtg之前,請確保您有安裝GD的libpng,zlib的包。如果尚未安裝,則先安裝這兩個包;
#rpm -qa|grep libpng
#rpm -qa|grep zlib
3, 安裝MRTG
#tar -zxvf mrtg-2.17.4.tar.gz
cd mrtg-2.17.4
./configure --prefix=/usr/local/mrtg --sysconfdir=/etc/mrtg --with-gd=/usr/local/gd2/include --with-gd-lib=/usr/local/gd2/lib --with-gd-inc=/usr/local/gd2/include --with-png=/usr/local/include --with-png-lib=/usr/local/lib --with-png-inc=/usr/local/include --with-zlib=/usr/local/zlib/include --with-zlib-lib=/usr/local/zlib/include --with-zlib-inc=/usr/local/zlib/include
如果configure出現什么warning或者error,可以嘗試下下面的configre命令:
./configure --prefix=/usr/local/mrtg --with-gd=/usr/local/gd2/ --with-gd-lib=/usr/local/gd2/lib/ --with-gd-inc=/usr/local/gd2/include/ --with-z=/usr/local/zlib/ --with-z-lib=/usr/local/zlib/lib/ --with-z-inc=/usr/local/zlib/include/ --with-png=/usr/local/lib
make
make install
到這里Mrtg已被成功的安裝到系統上了。
4, 生成cfg文件
我們要監控的交換機IP為:192.168.50.252 ,Apache主目錄是/var/www/html。將mrtg的log數據日志放在/var/www/html/mrtg/文件夾中,
首先來生成cfg文件:
#mkdir /var/www/html/mrtg/
/usr/local/mrtg/bin/cfgmaker --global 'WorkDir: /var/www/html/mrtg' --global 'Options[_]: bits,growright' --output /var/www/html/mrtg/mrtg252.cfg public@192.168.50.252
5, 修改mrtg252.cfg文件(如果以下設置已經有了,則不需要更改):
打開mrtg252.cfg,查看以下信息是否正確:
更改WorkDir為/var/www/html/mrtg
去掉Options[_]: growright, bits前面的#
并加入Language:Chinese使之支持中文(如果中文顯示有問題,比如顯示的都是四方塊,那么,可能是操作系統尚未安裝中文字符集。解決的方法是要么安裝中文字符集,要么這個配置就不需加入)
保存并退出。
6, 接著運行env LANG=C /usr/local/mrtg/bin/mrtg /var/www/html/mrtg/mrtg252.cfg
連續執行三次以上直到不再顯示錯誤為止
7, 生成頁面文件
#/usr/local/mrtg/bin/indexmaker --output=/var/www/html/mrtg/index252.html --title="192.168.50.252 Vod-Switch Traffic MRTG on linux"
8, 如果httpd已經啟動了,則可以訪問如下網頁:
http://192.168.50.22/mrtg ,(以下是經過1個星期左右時間啟動后的圖。如果是剛配置成功啟動,則圖上都沒有數據。數據會隨時間逐步生成):
9, 為了讓監測數據自動更新,還需要讓程序在后臺定時運行,
crontab -e
在其中輸入 :
*/5 * * * * /usr/bin/env LANG=C /usr/local/mrtg/bin/mrtg /var/www/html/mrtg/mrtg252.cfg --logging /var/log/mrtg252.log
即讓系統在后臺每五分鐘執行一次env LANG=C /usr/local/mrtg/bin/mrtg /var/www/html/mrtg/mrtg252.cfg進行數據刷新。
用nagios命令測試下:
# /usr/local/nagios/libexec/./check_mrtgtraf -F /var/www/html/mrtg/192.168.50.252_1.log
Traffic OK - Avg. In = 0.0 B/s, Avg. Out = 0.0 B/s|in=0.000000B/s;;;0.000000 out=0.000000B/s;;;0.000000
#
(另外一臺交換機配置也類似)
二、 nagios監測配置文件50.252_switch.cfg設置
1, 生成switch配置文件存放的路徑
#mkdir /usr/local/nagios/etc/switches
2, 在nagios.cfg文件上配置switch監控文件存放的路徑
#vi /usr/local/nagios/etc/nagios.cfg
cfg_dir=/usr/local/nagios/etc/switches
3, 配置50.252_switch.cfg
#cp -p /usr/local/nagios/etc/objects/switch.cfg /usr/local/nagios/etc/switches/50.252_switch.cfg
#vi /usr/local/nagios/etc/switches/50.252_switch.cfg
###############################################################################
# SWITCH.CFG - SAMPLE CONFIG FILE FOR MONITORING A SWITCH
#
# Last Modified: 10-03-2007
#
# NOTES: This config file assumes that you are using the sample configuration
# files that get installed with the Nagios quickstart guide.
#
###############################################################################
###############################################################################
###############################################################################
#
# HOST DEFINITIONS
#
###############################################################################
###############################################################################
# Define the switch that we'll be monitoring
define host{
use generic-switch ; Inherit default values from a template
host_name 50.252_VodSw ; The name we're giving to this switch
alias 50.252 Vod Switch ; A longer name associated with the switch
address 192.168.50.252 ; IP address of the switch
hostgroups switches ; Host groups this switch is associated with
}
#這里,交換機直接繼承了默認的generic-switch,并且在這里加入了下面定義的組switches。
###############################################################################
###############################################################################
#
# HOST GROUP DEFINITIONS
#
###############################################################################
###############################################################################
# Create a new hostgroup for switches
define hostgroup{
hostgroup_name switches ; The name of the hostgroup
alias Network Switches ; Long name of the group
}
#這里,定義了交換機的組,另外一臺交換機的配置文件中就不能再定義了,只需要在host定義中增加“hostgroups switches”就行了
###############################################################################
###############################################################################
#
# SERVICE DEFINITIONS
#
###############################################################################
###############################################################################
# Create a service to PING to switch
define service{
use generic-service ; Inherit values from a template
host_name 50.252_VodSw ; The name of the host the service is associated with
service_description PING ; The service description
check_command check_ping!200.0,20%!600.0,60% ; The command used to monitor the service
normal_check_interval 5 ; Check the service every 5 minutes under normal conditions
retry_check_interval 1 ; Re-check the service every minute until its final/hard state is determined
}
# Monitor uptime via SNMP
define service{
use generic-service ; Inherit values from a template
host_name 50.252_VodSw
service_description Uptime
check_command check_snmp!-C public -o sysUpTime.0
}
# Monitor Port 49,50 status via SNMP
define service{
use generic-service ; Inherit values from a template
host_name 50.252_VodSw
service_description Port 1 Link Status
check_command check_snmp!-C public -o ifOperStatus.1 -r 1 -m RFC1213-MIB
}
define service{
use generic-service ; Inherit values from a template
host_name 50.252_VodSw
service_description Port 49 Link Status
check_command check_snmp!-C public -o ifOperStatus.10149 -r 1 -m RFC1213-MIB
}
define service{
use generic-service ; Inherit values from a template
host_name 50.252_VodSw
service_description Port 50 Link Status
check_command check_snmp!-C public -o ifOperStatus.10150 -r 1 -m RFC1213-MIB
}
# Monitor bandwidth via MRTG logs
define service{
use generic-service ; Inherit values from a template
host_name 50.252_VodSw
service_description Port 1 Bandwidth Usage
check_command check_local_mrtgtraf!/var/www/html/mrtg/192.168.50.252_1.log!AVG!1000000,1000000!5000000,5 000000!10
}
define service{
use generic-service ; Inherit values from a template
host_name 50.252_VodSw
service_description Port 49 Bandwidth Usage
check_command check_local_mrtgtraf!/var/www/html/mrtg/192.168.50.252_10149.log!AVG!1000000,1000000!5000000,5000000!10
}
define service{
use generic-service ; Inherit values from a template
host_name 50.252_VodSw
service_description Port 50 Bandwidth Usage
check_command check_local_mrtgtraf!/var/www/html/mrtg/192.168.50.252_10150.log!AVG!1000000,1000000!5000000,5000000!10
}
define service{
use generic-service ; Inherit values from a template
host_name 50.252_VodSw
service_description CPU Load AVG5sec
check_command check_snmp!-C public -o .1.3.6.1.4.1.9.9.109.1.1.1.1.6.1 -w 50 -c 80 -u% -l "CPU load is" -m RFC1213-MIB
}
2.5.3 CPU監測
這里,單獨敘述下通過snmp監測交換機的cpu
從網上http://www.cisco.com/en/US/tech/tk648/tk362/technologies_tech_note09186a0080094a94.shtml
查到監測cpu的有關信息:
telnet到交換機,看到這臺交換機的版本是12.2(50):
同時,查詢下cpu負載:
使用MIB圖中的第一列的MID號,在192.168.50.22上監測cpu:
1) 5秒平均負載
snmpwalk -v2c -c public 192.168.50.252 .1.3.6.1.4.1.9.9.109.1.1.1.1.6.1
2) 1分鐘平均負載
snmpwalk -v2c -c public 192.168.50.252 .1.3.6.1.4.1.9.9.109.1.1.1.1.7.1
3) 5分鐘平均負載
snmpwalk -v2c -c public 192.168.50.252 .1.3.6.1.4.1.9.9.109.1.1.1.1.6.1
這三個命令都能成功執行。
再執行:
#cd /usr/local/nagios/libexec
./check_snmp -H 192.168.50.252 -o .1.3.6.1.4.1.9.9.109.1.1.1.1.6.1 -c public
SNMPv2-SMI::enterprises.9.9.109.1.1.1.1.6.1 = Gauge32: 7
./check_snmp -H 192.168.50.252 -o .1.3.6.1.4.1.9.9.109.1.1.1.1.7.1 -c public
SNMPv2-SMI::enterprises.9.9.109.1.1.1.1.7.1 = Gauge32: 10
./check_snmp -H 192.168.50.252 -o .1.3.6.1.4.1.9.9.109.1.1.1.1.8.1 -c public
SNMPv2-SMI::enterprises.9.9.109.1.1.1.1.8.1 = Gauge32: 11
對比以上的3個數據,基本上是一致的。
然后整理監測腳本:
./check_snmp -H 192.168.50.252 -o .1.3.6.1.4.1.9.9.109.1.1.1.1.7.1 -c public -P 1 -w 50 -c 80 -u% -l "CPU load is" -m:
生成監測服務,將以下腳本加入到/usr/local/nagios/etc/switches/50.252_switch.cfg中:
define service{
use generic-service ; Inherit values from a template
host_name 50.252_VodSw
service_description CPU Load AVG5sec
check_command check_snmp!-C public -o .1.3.6.1.4.1.9.9.109.1.1.1.1.6.1 -w 50 -c 80 -u% -l "CPU load is" -m RFC1213-MIB
}
最后驗證cfg文件以及重新加載nagios服務
# /usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg
#service nagios reload
2.5.4 內存監測
# 獲取內存當前使用情況(bytes),對應交換機查詢出來的Userd(b)一列
snmpwalk -v 2c -c public 192.168.50.252 1.3.6.1.4.1.9.9.48.1.1.1.5
# 獲取內存當前空閑多少(bytes),對應交換機查詢出來的Free(b)一列
snmpwalk -v 2c -c public 192.168.50.252 1.3.6.1.4.1.9.9.48.1.1.1.6
# 獲取內存歷史最大空余多少(bytes),對應交換機查詢出來的Largest (b)一列
snmpwalk -v 2c -c public 192.168.50.252 1.3.6.1.4.1.9.9.48.1.1.1.7
# snmpwalk -v 2c -c public 192.168.50.252 1.3.6.1.4.1.9.9.48.1.1.1.5
SNMPv2-SMI::enterprises.9.9.48.1.1.1.5.1 = Gauge32: 19222640
SNMPv2-SMI::enterprises.9.9.48.1.1.1.5.2 = Gauge32: 3606412
SNMPv2-SMI::enterprises.9.9.48.1.1.1.5.16 = Gauge32: 40
#
# snmpwalk -v 2c -c public 192.168.50.252 1.3.6.1.4.1.9.9.48.1.1.1.6
SNMPv2-SMI::enterprises.9.9.48.1.1.1.6.1 = Gauge32: 68694184
SNMPv2-SMI::enterprises.9.9.48.1.1.1.6.2 = Gauge32: 4782196
SNMPv2-SMI::enterprises.9.9.48.1.1.1.6.16 = Gauge32: 1048536
#
Vod-SW>show mem
Head Total(b) Used(b) Free(b) Lowest(b) Largest(b)
Processor 3480DD4 87916844 19222984 68693860 54383028 30100460
I/O 6800000 8388608 3606544 4782064 4729364 4758216
Driver te 2600000 1048576 44 1048532 1048532 1048532
--More--
最好是能夠計算出百分比,然后設置閾值進行告警。。。。如果實在沒什么辦法,可以直接監控內存Free的總數量,自己計算一個百分比:比如低于50%×87916844,為warning;低于20%×87916844,為critical。
也可以在網上查老外寫的監測腳本check_snmp_mem,如:
http://opensource.is/trac/browser/nagios-plugins/check_snmp/trunk?rev=8f404c77185fa7589fb7fbb51cd569b2344c394e&order=name
https://github.com/surcouf/nagios-snmp-plugins/blob/master/check_snmp_mem.pl
http://opensource.ok.is/trac/browser/nagios-plugins/check_snmp/check_snmp_mem.pl
對腳本的oid進行適當的修改后,就應該能使用了。
配置文件創建成功后,使用命令驗證下配置文件,如果沒有問題,則重啟nagios,然后就能查看到交換機的監測項。
另外一臺交換機配置類似。
2.6 4臺ipqam
4臺ipqam地址為192.168.50.101到104。登錄管理控制臺,可看到snmp都已經開啟。因此,可以對這些設備進行監測。
由于對這個設備監測項不是特別了解,因此,我們只對設備的簡單幾項進行監測:ping、uptime和eth0口。將交換機的配置文件復制一份進行修改。Server項改成以下三項:
define service{
use generic-service ; Inherit values from a template
host_name 50.101_ipqam ; The name of the host the service is associated with
service_description PING ; The service description
check_command check_ping!200.0,20%!600.0,60% ; The command used to monitor the service
normal_check_interval 5 ; Check the service every 5 minutes under normal conditions
retry_check_interval 1 ; Re-check the service every minute until its final/hard state is determined
}
# Monitor uptime via SNMP
define service{
use generic-service ; Inherit values from a template
host_name 50.101_ipqam
service_description Uptime
check_command check_snmp!-C public -o sysUpTime.0
}
# Monitor Port eth0 status via SNMP
define service{
use generic-service ; Inherit values from a template
host_name 50.101_ipqam
service_description eth0 Link Status
check_command check_snmp!-C public -o ifOperStatus.2 -r 1 -m RFC1213-MIB
}
#通過snapwalk查得eth0的OID為2。因此這里為ifOperStatus.2
父子設置
上面章節對父子設置簡單的介紹過,這里另起一節進行說明。
Host之間可以設置父子關系,比如交換機為父,交換機之下的服務器為子;或者表示某臺服務器依賴與另外一臺服務器。
如果將交換機設為主機,那么交換機下應該有很多的“子”服務器,此時,可以設置一個模板,如在/usr/local/nagios/etc/object/templates.cfg最后新增:
define host{
name linux-vod-sw ; Name of this template
use generic-host ; Inherit default values
check_period 24x7
check_interval 5
retry_interval 1
max_check_attempts 10
check_command check-host-alive
icon_p_w_picpath redhat.gif
statusmap_p_w_picpath redhat.gd2
parents 50.252_VodSw #使用這個模板的設備都是 50.252_VodSw 的子設備
# notification_period 24x7
# notification_interval 30
# notification_options d,r
notifications_enabled 0
contact_groups admins
register 0 ; DONT REGISTER THIS - ITS A TEMPLATE
}
然后將所有的服務器配置文件中的host定義段中的use設置為use linux-vod-sw。如:
define host{
use linux-vod-sw ; Inherit default values from a template
host_name 50.10_voddb ; The name we're giving to this server
alias voddb centos5.5 64bit ; A longer name for the server
address 192.168.50.10 ; IP address of the server
icon_p_w_picpath database.gif
statusmap_p_w_picpath database.gd2
}
MAP圖片顯示設置
圖片定義
在nagios的ie監測項Map界面,如果沒有設置好圖片,則顯示的圖片都為“?”。以下大部分都還沒有配置圖片,右面的一些服務器已經配置了圖片:
圖片文件在host定義中,如:
define host{
use linux-box-SZ-vod-sw ; Inherit default values from a template
host_name 50.10_voddb ; The name we're giving to this server
alias voddb centos5.5 64bit ; A longer name for the server
address 192.168.50.10 ; IP address of the server
icon_p_w_picpath database.gif
statusmap_p_w_picpath database.gd2
}
這里定義了兩個變量icon_p_w_picpath和statusmap_p_w_picpath。statusmap_p_w_picpath在上圖中進行顯示,文件名后綴為.gd2。
如果鼠標移動到某個服務器上,又會顯示一個框,這個框中的左上角圖片為icon_p_w_picpath參數指定的圖片文件名:
圖片參數包括(摘錄網上的解釋):
icon_p_w_picpath : 可以使用GIF、PNG或者是JPG文件,推薦圖像尺寸為40x40
icon_p_w_picpath_alt: 即為HTML語法中關于p_w_picpath元素的ALT屬性值
vrml_p_w_picpath : Nagios提供使用VRML語言繪制三維圖像功能,這里不要使用透明圖片(PNG, GIF),最好使用JPG
statusmap_p_w_picpath: 提供給statusmap CGI腳本使用的圖片,服務器要支持GD,這個文件的類型為gd2
(icon_p_w_picpath_alt,vrml_p_w_picpath尚未研究是怎么用的。)
圖片配置完后的nagios Map應該是這樣的:
上面的Map圖中,有些圖是nagios自帶的,有些圖是我用windows的畫圖版畫出40×40像素的圖后,通過下一小節的方法轉成gd2。
Gd2圖片文件生成
http://www.net527.com/caozuoxitong/Linux/6374.html
可以通過以下方法將png或者gif生成gd2文件
1, 可以從廠商下載visio或者PPT的圖標庫,一般可以得到JPG格式的文件,調整大小到40x40。
This variable is used to define the name of an p_w_picpath that should be associated with this host in the statusmap CGI. You can specify a JPEG, PNG, and GIF p_w_picpath if you want, although I would strongly suggest using a GD2 format p_w_picpath, as other p_w_picpath formats will result in a lot of wasted CPU time when the statusmap p_w_picpath is generated. GD2 p_w_picpaths can be created from PNG p_w_picpaths by using the pngtogd2 utility supplied with Thomas Boutell's gd library. The GD2 p_w_picpaths should be created in uncompressed format in order to minimize CPU load when the statusmap CGI is generating the network map p_w_picpath. The p_w_picpath will look best if it is 40x40 pixels in size.
以上意思是推薦是使用GD2圖片文件,因為其消耗系統資源少。不能使用壓縮的GD2圖片文件。可以使用Thomas Boutell's gd library的pngtogd2從png生成GD2文件。圖片建議使用40×40像素。
2, 將pnp轉成GD2
下載編譯、安裝GD庫
gd-progs-2.0.33-9.4.el5_4.2.i386.rpm
#rpm -ivh gd-progs-2.0.33-9.4.el5_4.2.i386.rpm
# pngtogd2 -h
Usage: pngtogd2 filename.png filename.gd2 cs fmt
where cs is the chunk size
fmt is 1 for raw, 2 for compressed
# giftogd2 -h
Usage: giftogd2 filename.gif filename.gd2 cs fmt
where cs is the chunk size
fmt is 1 for raw, 2 for compressed
#./pngtogd2 /usr/local/nagios/share/p_w_picpaths/logos/H3cSwitch.png H3cSwitch.gd2 cs 1
cp H3cSwitch.gd2 /usr/local/nagios/share/p_w_picpaths/logos/
3, 定義define hostextinfo,define serviceextinfo使用自定義的圖標
define hostextinfo{
host_name host_name
notes note_string
notes_url url
action_url url
icon_p_w_picpath p_w_picpath_file
icon_p_w_picpath_alt alt_string
vrml_p_w_picpath p_w_picpath_file
statusmap_p_w_picpath p_w_picpath_file
2d_coords x_coord,y_coord
3d_coords x_coord,y_coord,z_coord
}
define serviceextinfo{
host_name host_name
service_description service_description
notes note_string
notes_url url
action_url url
icon_p_w_picpath p_w_picpath_file
icon_p_w_picpath_alt alt_string
}
define hostextinfo{
host_name H3C-S5100
# notes_url http://webserver/hostinfo.pl?host=you_can_edit_this
icon_p_w_picpath H3cSwitch.png
# icon_p_w_picpath_alt
vrml_p_w_picpath H3cSwitch.png
statusmap_p_w_picpath H3cSwitch.gd2
# 2d_coords 100,250
# 3d_coords 100.0,50.0,75.0
}
----------------本章完--------------------
轉載于:https://blog.51cto.com/sandjj/1297228
總結
以上是生活随笔為你收集整理的2 nagios 客户端软件安装及配置的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: AcWing 1902. 马拉松(枚举)
- 下一篇: 企业两化融合申报知识