naigos監控伺服器主機及服務狀態
使用nagios進行伺服器狀態臨控,以前一直用cacti,之前也曾看到nagios的介紹
。這兩日配置和整理一下nagios,希望幫助做系統管理的兄弟們,因為監控是我們最不可
以缺少的一項工作。
閑話少說,下面開始操作,以下面操作都經過自己操作並投入使用,並且參考下
面連接的文章,在此表示感謝。。
參考
http://blog.csdn.net/mayabin/archive/2006/08/07/1033103.aspx
http://blog.chinaunix.net/u/19540/showart_197158.html
http://blog.chinaunix.net/u/10668/showart_254777.html
http://www.2007boys.cn/html/167jishuwenzhang/jishuwenzhang/20070605/1964_5.ht
ml
簡單介紹nagios
nagios是一款開源的監控軟體,能夠對網路內的主機和硬體設備進行狀態監控。
功能:
1. 狀態監控
2. 預警報告
3. 集中,規模化管理
4. web可視化顯示方式
5. 自定義shell腳本或插件實現更多的功能
http://www.nagios.org 官方網站
nagios的實現方式:nagios採用分佈-集中的管理模式。在nagios伺服器上安裝nagios主
程序,在被監控主機上安裝nagios代理程序。通過nagios主程序和nagios代理程序之間
的通訊,監視主機狀態。主程序和代理程序之間有兩種工作方式,一種是主程序--
>Active-->代理程序;另一種是主程序-->passive-->代理程序;顧名思義,是以一方向
另一方主動提出通訊需求來劃分的。
所需軟體:
系統:Redhat AS 4
nagios-2.9.tar.gz
nagios-plugins-1.4.9.tar.gz
nrpe-2.8.1.tar.gz
這三個軟體都可以官方網站下載http://www.nagios.org/download/
imagepak-base.tar.gz
下載地址:
http://download.chinaunix.net/download.php?id=9332&ResourceID=4709
實現要求:在一台機器上面監控多台運行主機狀態,所以安裝步驟為首先安裝監控機器
,再配置監控機器,最後安裝被監控端軟體。
一.安裝監控中心
1.nagios 安裝
tar zxf nagios-2.9.tar.gz
useradd nagios
cd nagios
./configure --prefix=/usr/local/nagios --with-gd-lib=/usr/local/lib --with-
gd-inc=/usr/local/include
make all
make install
make install-init
make install-commandmode
make install-config
chown nagios.nagios /usr/local/nagios
2.安裝nagios-plugins
tar zxf nagios-plugins.tar.gz
cd nagios-plugins
./configure --prefix=/usr/local/nagios-plugins
make all
make install
mv /usr/local/nagios-plugins/libexec /usr/local/nagios/
(在redhat系統上面安裝可能出現configure時,到這裡checking for redhat spopen
problem...就不動了,所以需要在configure時再加上這個 --enable-redhat-pthread-
workaround )
3安裝imagepak-base.tar.gz
tar zxf imagepak-base.tar.gz
解壓以後是base目錄
cp -r base /usr/local/nagios/share/images/logos/
4安裝nrpe-2.8.1.tar.gz
tar zxf nrpe-2.8.1.tar.gz
cd nrpe-2.81
./configure --enable-ssl --enable-command-args
make all
cp src/check_nrpe /usr/local/nagios/libexec
vi /usr/local/nagios/etc/checkcommands.cfg
#添加下面內容
define command{
command_name check_nrpe
command_line /usr/local/nagios/libexec/check_nrpe -H $HOSTADDRESS$ -c
$ARG1$
}
到此監控中心所需軟體安裝完畢,接下來進行配置nagios
二. 配置nagios
1.配置apache
在httpd.conf中追加
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>
2.配置nagios
cd /usr/loca/nagios/etc/
把這裡.cfg-sample文件配置文件模板,全部重命名為.cfg
如:cp nagios.cfg-sample nagios.cfg
然後檢查配置文件是否出錯
/usr/local/nagios/bin/nagios –v /usr/local/nagios/etc/nagios.cfg
出現Total Warnings: 0
Total Errors: 0
vi /usr/local/nagios/etc/cgi.cfg
use_authentication=1改為use_authentication=0,即不用驗證.
啟動後台進程把nagios作為守護進程:
/usr/local/nagios/bin/nagios -d /usr/local/nagios/etc/nagios.cfg
/etc/init.d/nagios start
http://ip/nagios/ 這樣可以打開web頁面
三、在被監控機器上面安裝nrpe和plugin
1、安裝nrpe
tar zxf nrpe-2.8.1.tar.gz
cd nrpe-2.8.1
./configure --enable-ssl --enable-command-args
make all
mkdir -p /usr/local/nagios/etc
mkdir /usr/local/nagios/bin
mkdir /usr/local/nagios/libexec
useradd nagios
chown -R nagios.nagios /usr/local/nagios/
cp sample-config/nrpe.cfg /usr/local/nagios/etc/
cp src/nrpe /usr/local/nagios/bin/
chmod 755 /usr/local/nagios/bin/nrpe
/usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -d
啟動nrpe 埠為5666 用netstat -an |grep 5666 查看埠是否已經監聽
2、安裝插件
tar zxf nagios-plugins-1.4.9.tar.gz
cd nagios-plugins
./configure --prefix=/usr/local/nagios
make all
make install
在上面全部安裝完畢之後,開始定義配置文件,下面配置全部都是臨控端進行。
在nagios安裝完畢之後,主要是調好配置文件。
cd /usr/local/nagios/etc/ 為配置文件。
nagios.cfg為主配置文件,增加新的.cfg配置文件,需要在nagios.cfg裡面添加才可以。
localhost.cfg 定義了本機一些監控配置等,創建新的或其它配置文件可以參考此文件
。
創建下面幾個cfg配置文件在/usr/local/nagios/etc裡面
touch contactgroups.cfg contacts.cfg hostgroups.cfg hosts.cfg services.cfg
timeperiods.cfg
在對上面相關幾個文件添加內容后,還是需要在nagios.cfg中,添加對應文件。如:我
在hosts.cfg 添加了對192.168.0.1這台機器,那在nagios中就要有這樣一句:
cfg_file=/usr/local/nagios/etc/hosts.cfg
添加要監控的主機到配置文件中,有兩種方法。一種就是將這台機器要監控的內容分別
寫入hosts.cfg contacts.cfg service.cfg 文件中,然後要在nagios.cfg添加好對應的
cfg文件路徑。
另一種方法,就是生成一個單獨的ip.cfg文件,之後把要監控的內容寫入這一個文件就
可以了。
我是生成了一個單獨IP文件,如果有多台機就生成個IP文件
例子:1,添加監控本機的web服務。
vi nagios/etc/localhost.cfg
添加
define service{
use local-service
host_name localhost
service_description Current http
check_command check_http!100.0,20%!500.0,60%
}
這配置就是參考上面的PING監控改的,如果要添加其它服務同理。
之後重新啟動nagios ,打開web頁面5分鐘后,就可以看到本機apache的狀態了。
2.添加192.168.0.128這台遠程機器
vi 192.168.0.128.cfg 創建新文件
define host{
use generic-host ; Name of host template to use
host_name test_nrpe
alias client
address 192.168.0.128
check_command check-host-alive
max_check_attempts 1
check_period 24x7
notification_interval 120
notification_period 24x7
notification_options d,r
contact_groups ghbadmin
}
define service{
use generic-service ; Name of service template to use
host_name test_nrpe
service_description apache
is_volatile 0 #類似聲音警告功能關閉
check_period 24x7 #監控期限為24X7
max_check_attempts 1 #最大重試次數
normal_check_interval 1 #標準檢測時間間隔 1分鐘
retry_check_interval 1 #重試時間間隔
contact_groups admins #聯繫組
notification_options w,u,c,r # w,u,c,r 發生這四種情況時,進行通告。
notification_interval 960 # 通告間隔
notification_period 24x7 #通告過期時間
check_command check_http!100.0,20%!500.0,60%
}
文件裡面第一部為監控主機是否存活,第二部分為監控apache服務,如果死掉了,則通過
ghbadmin 這個組來發送郵件,而且這個ghbadmin郵件聯繫組是在哪裡定義的呢,我是在
localhost.cgf 文件定義的,這樣就不用在其它配置文件再次寫入了。
vi localhost.cfg
define contactgroup{
contactgroup_name ghbadmin
alias Nagios Administrators
members ghbspecial
}
define contact{
contact_name ghbspecial
alias Nagios Admin
service_notification_period 24x7
host_notification_period 24x7
service_notification_options w,u,c,r
host_notification_options d,r
service_notification_commands notify-by-email
host_notification_commands host-notify-by-email
email ghb@aaa.com
}
上面配置完畢之後,記得要修一下nagios.cf文件,把192.168.0.128.cfg 添加進去
cfg_file=/usr/local/nagios/etc/192.168.0.128.cfg
重新啟動nagios /etc/init.d/nagios restart
關掉服務,或關掉主機,就可以通過web頁面看到狀態了,並且很快就會收到郵件了,如
果想收到簡訊,可以到網易等申請簡訊提醒業務。
如果想用郵件功能,那麼nagios所在伺服器上面一定要開啟郵件服務功能。
到此nagios監控配置完畢。
[ 本帖最後由 ghbspecial 於 2007-7-5 17:55 編輯 ]
《解決方案》
好東西,收了.
支持一下
《解決方案》
恩 我就是在redhat上 安nagios-plugins的時候出現checking for redhat spopen problem...了不動了。。。
感謝LZ提供資料參考
《解決方案》
open source好東西
《解決方案》
感謝指點
《解決方案》