原文地址:http://www.ritto.cn/2008/12/129.html
他的博客上記錄了好多安裝日記,有空的朋友可以去瞅瞅。
我還沒(méi)有看,一會(huì)看完了了給他提點(diǎn)意見(jiàn)。^___^
有顏色和粗體部分是我看完后加的。
注意:這篇筆記里面的軟件盡量安裝所給的版本。
1.安裝Nagios
useradd nagios
mkdir /usr/local/nagios
chown nagios.nagios /usr/local/nagios
tar zxvf nagios-2.9.tar.gz
cd nagios-2.9
./configure --prefix=/usr/local/nagios --with-cgiurl=CGIURL --with-htmurl=HTMURL --with-nagios-user=nagios --with-nagios-group=nagios
CGIURL 和HTMURL替換成自己的。
如果沒(méi)有錯(cuò)的話(huà),繼續(xù)下面的步驟。
//配置nagios
make all //編譯nagios
make install //安裝主要的程序,CGI及HTML文件
make install-init //在/etc/rc.d/init.d安裝啟動(dòng)腳本
make install-commandmode //給外部命令訪問(wèn)nagios配置文件的權(quán)限
make install-config //將配置文件的例子復(fù)制到nagios的安裝目錄
驗(yàn)證程序是否被正確安裝
看是否存在etc,bin,sbin,share,var五個(gè)目錄
bin Nagios執(zhí)行程序所在目錄,nagios文件即為主程序
etc Nagios配置文件位置
sbin Nagios cgi文件所在目錄,執(zhí)行外部命令所需文件所在的目錄
share Nagios網(wǎng)頁(yè)文件所在的目錄
var Nagios日志文件,spid等文件所在的目錄
2.安裝插件
tar zxvf nagios-plugins-1.4.9.tar.gz
cd nagios-plugins-1.4.9
./configure --prefix=/usr/local/nagios/
make
make install
ls /usr/local/nagios/libexec/ //會(huì)顯示安裝的插件文件
3.修改apache配置
Apache 的目的是可以在WEB上更加人性化的查看監(jiān)控結(jié)果,其他的WEB服務(wù)器也可以。
grep ^User /usr/local/apache2/conf/httpd.conf
User daemon
usermod -G nagios daemon
此處替換成自己的。比如我的機(jī)器上是apache
usermod -G nagios apache 把a(bǔ)pache和nagios 都放到nagios 組中。
vi /usr/local/apache2/conf/httpd.conf //在下面加入如下內(nèi)容
#Setting for nagios
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/apachectl -t //檢查配置文件是否正確
httpd -t
/usr/local/apache2/bin/htpasswd -c /usr/local/nagios/etc/htpasswd ritto
cat /usr/local/nagios/etc/htpasswd //查看認(rèn)證文件內(nèi)容
/usr/local/apache2/bin/apachectl start //啟動(dòng)apache
service httpd restart
4.配置Nagios
Nagios自定義了一套規(guī)則用于配置文件,在nagios里面定義了一些基本對(duì)象,如:
聯(lián)系人 contact 出了問(wèn)題像誰(shuí)報(bào)告?一般當(dāng)然是系統(tǒng)管理員了
監(jiān)控時(shí)間段 timeperiod 7X24小時(shí)不間斷還是周一至周五,或是自定義的其他時(shí)間段
被監(jiān)控主機(jī) host 所需要監(jiān)控的服務(wù)器,當(dāng)然可以是監(jiān)控機(jī)自己
監(jiān)控命令 command nagios 發(fā)出的哪個(gè)指令來(lái)執(zhí)行某個(gè)監(jiān)控,這也是自己定義的
被監(jiān)控的服務(wù) service 例如主機(jī)是否存活,80是否開(kāi),磁盤(pán)使用情況或自定義的服務(wù)等
另:多個(gè)被監(jiān)控主機(jī)可以定義為一個(gè)主機(jī)組
多個(gè)聯(lián)系人可以被定義為一個(gè)聯(lián)系人組
多個(gè)服務(wù)也可以被定義為一個(gè)服務(wù)組
將配置文件改名:
cd /usr/local/nagios/etc/
tar zcvf bak.tar.gz *
mv cgi.cfg-sample cgi.cfg
mv commands.cfg-sample commands.cfg
mv localhost.cfg-sample localhost.cfg
mv nagios.cfg-sample nagios.cfg
mv resource.cfg-sample resource.cfg
修改Nagios的配置文件:
------------------------------------------------------------------------
vi nagios.cfg //修改nagios的主配置文件
#cfg_file=/usr/local/nagios//etc/localhost.cfg //注釋此行 37
//將下面幾行的注釋去掉
cfg_file=/usr/local/nagios/etc/contactgroups.cfg //聯(lián)系組配置文件路徑
cfg_file=/usr/local/nagios/etc/contacts.cfg //聯(lián)系人配置文件路徑
cfg_file=/usr/local/nagios/etc/hostgroups.cfg //主機(jī)組配置文件路徑
cfg_file=/usr/local/nagios/etc/hosts.cfg //主機(jī)配置文件路徑
cfg_file=/usr/local/nagios/etc/services.cfg //服務(wù)配置文件路徑
cfg_file=/usr/local/nagios/etc/timeperiods.cfg //監(jiān)視時(shí)段配置文件路徑
check_external_commands=0 //將 0 改成 1,允許在web界面下執(zhí)行重啟Nagios
command_check_interval=15s //改成 10s, 命令檢查時(shí)間間隔
------------------------------------------------------------------------
配置相關(guān)CGI腳本。
vi cgi.cfg //修改cgi腳本控制文件
use_authentication=1 //確保值為 1
default_user_name=ritto //修改為認(rèn)證用戶(hù)
//后面修改內(nèi)容如下:
authorized_for_system_information=nagiosadmin,test
authorized_for_configuration_information=nagiosadmin,test
authorized_for_system_commands=test //多個(gè)用戶(hù)之間用逗號(hào)隔開(kāi)
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
-------------------------------------------------------------------------
關(guān)于所有命令的配置都在commands.cfg里面。
vi misccommands.cfg //主要功能是用于發(fā)送報(bào)警短信和報(bào)警郵件
#host-notify-by-sms //發(fā)送短信報(bào)警
define command {
command_name host-notify-by-sms
command_line /usr/local/bin/sms_send "Host $HOSTSTATE$ alert for $HOSTNAME$! on '$DATETIME$' " $CONTACTPAGER$
}
#service notify by sms //發(fā)送短信報(bào)警
define command {
command_name service-notify-by-sms
command_line /usr/local/bin/sms_send "'$HOSTADDRESS$' $HOSTALIAS$/$SERVICEDESC$ is $SERVICESTATE$" $CONTACTPAGER$
}
-----------------------------------------------------------------------------
vi timeperiods.cfg //定義監(jiān)控時(shí)間段,名稱(chēng)是24*7,監(jiān)控時(shí)間是全天24小時(shí)
define timeperiod{
timeperiod_name 24x7 //時(shí)間段的名稱(chēng),這個(gè)地方不要有空格
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
}
-----------------------------------------------------------------------------
vi contacts.cfg //定義聯(lián)系人
define contact {
contact_name sa //不要有空格
alias system administrator
service_notification_period 24x7
host_notification_period 24x7
service_notification_options w,u,c,r
host_notification_options d,u,r
service_notification_commands service-notify-by-sms,service-notify-by-email //命令讀配置miscommands.cfg
host_notification_commands host-notify-by-email,host-notify-by-sms //命令讀配置miscommands.cfg
email monitor@wswtek.com
pager 13297949944
}
define contact {
contact_name ritto
alias system administrator
service_notification_period 24x7
host_notification_period 24x7
service_notification_options w,u,c,r
host_notification_options d,u,r
service_notification_commands service-notify-by-sms,service-notify-by-email
host_notification_commands host-notify-by-email,host-notify-by-sms
email ritto.zhao@wswtek.com
pager 13297949944
}
//如果不需要手機(jī)報(bào)警的話(huà),則改成如下:
service_notification_commands notify-by-email
host_notification_commands host-notify-by-email
//上面的文件定義了2個(gè)聯(lián)系人,如果有更多聯(lián)系人的話(huà),照這個(gè)格式在后面追加即可。
//服務(wù)通知選項(xiàng)(service_notification_options)
//與主機(jī)通知選項(xiàng)(host_notification_options)的幾個(gè)選項(xiàng)在這里說(shuō)明一下:
//w-warning 報(bào)警 , u-unknown 未知, c-critical 嚴(yán)重 , r-recovery 從異常情況恢復(fù)正常 ; d-down 關(guān)機(jī)了,
//u-unreachable,注意一下,主機(jī)報(bào)警和服務(wù)報(bào)警有些差異。
-----------------------------------------------------------------------------
vi contactgroups.cfg //將多個(gè)聯(lián)系人定義一個(gè)聯(lián)系人組
define contactgroup{
contactgroup_name sagroup
alias System Administrators
members ritto,sa
}
-----------------------------------------------------------------------------
vi hosts.cfg //定義被監(jiān)控主機(jī)
#define monitor host
############################################
# Wangjing IDC servers #
############################################
define host {
host_name nagios-server
alias nagios server
address 192.168.4.226
contact_groups sagroup //多個(gè)聯(lián)系組用逗號(hào)分隔,數(shù)據(jù)來(lái)源于contactgroups.cfg
check_command check-host-alive //這個(gè)命令來(lái)自commands.cfg,用來(lái)監(jiān)控主機(jī)是否存活
max_check_attempts 5 //檢查失敗后重試的次數(shù)
notification_interval 10 //提醒的時(shí)間,每隔10秒提醒一次
notification_period 24x7 //提醒的周期,24*7,來(lái)自之前timeperiods.cfg中定義的
notification_options d,u,r //指定什么情況下提醒,來(lái)自contacts.cfg中定義的
}
define host {
host_name mail12.supertalent.com
alias nagios test client
address 192.168.4.41
contact_groups sagroup
check_command check-host-alive
max_check_attempts 5
notification_interval 10
notification_period 24x7
notification_options d,u,r
}
------------------------------------------------------------------------------
vi hostgroups.cfg //將多個(gè)主機(jī)定義一個(gè)主機(jī)組
define hostgroup{
hostgroup_name sa-servers //主機(jī)組名稱(chēng)
alias sa Servers //別名
members nagios-server //組的成員主機(jī),多個(gè)主機(jī)以逗號(hào)相隔,必須是上面hosts.cfg中定義的
}
------------------------------------------------------------------------------
vi services.cfg //定義監(jiān)控的服務(wù)
#service definition
###############################################
# Wangjing IDC servers service for host-live #
###############################################
define service{
host_name nagios-server //要監(jiān)控的主機(jī), 必須是hosts.cfg 中定義的
service_description check-host-alive //定義的是監(jiān)控這個(gè)主機(jī)是不是存活
//給監(jiān)控項(xiàng)目起個(gè)名字,任意起都可以,如check ftp
check_command check-host-alive //所用的命令,是commands.cfg中定義的
//所用的命令,必須是commands.cfg中定義的
check_period 24x7 //監(jiān)控的時(shí)間段,是timeperiods.cfg中定義的
max_check_attempts 5
normal_check_interval 3
retry_check_interval 2
contact_groups sagroup //聯(lián)系人組, contactgroups.cfg中定義的
notification_interval 10
notification_period 24x7 //通知的時(shí)間段, ,是timeperiods.cfg中定義的
notification_options w,u,c,r //在監(jiān)控的結(jié)果是wucr時(shí)通知聯(lián)系人,具體含義看前文.
}
define service {
host_name mail12.supertalent.com
service_description check_tcp 80
check_period 24x7
max_check_attempts 4
normal_check_interval 3
retry_check_interval 2
contact_groups sagroup
notification_interval 10
notification_period 24x7
notification_options w,u,c,r
check_command check_tcp!80 //檢查tcp 80端口服務(wù)是否正常
}
//書(shū)寫(xiě)時(shí)要注意的是,check_tcp與要監(jiān)控的服務(wù)端口之間要用”!”做分隔符。如果服務(wù)太多,以考慮用腳本來(lái)生成。
-----------------------------------------------------------------------------
確保這一部的warning 和 error 為0,則繼續(xù)下一步。
/usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg //檢查所有配置文件的正確性
/usr/local/nagios/bin/nagios -d /usr/local/nagios/etc/nagios.cfg //作為守護(hù)進(jìn)程后臺(tái)啟動(dòng)Nagios
echo "/usr/local/nagios/bin/nagios -d /usr/local/nagios/etc/nagios.cfg" >> /etc/rc.local //開(kāi)機(jī)自動(dòng)運(yùn)行
-----------------------------------------------------------------------------
使用命令和插件監(jiān)控更多信息
/usr/local/nagios/libexec //插件默認(rèn)的安裝路徑
./check_disk -w 10% -c 5% / //檢查根分區(qū)的使用情況,若剩余10%以下,為警告狀態(tài)(warning)
若剩余 5%以下,為嚴(yán)重狀態(tài)(critical)
先監(jiān)控一臺(tái)主機(jī)
在hosts.cfg中定義主機(jī)名 --- 在services.cfg中定義監(jiān)控內(nèi)容
------------------------------------------------------------------------------
使用NRPE監(jiān)控Linux上的"本地信息"
對(duì)系統(tǒng)為L(zhǎng)inux的主機(jī)進(jìn)行如下監(jiān)控: CPU負(fù)載,磁盤(pán)容量,登陸用戶(hù)數(shù),總進(jìn)程數(shù),僵尸進(jìn)程數(shù),swap分區(qū)使用情況
在被監(jiān)控主機(jī)上.
useradd nagios
passwd nagios
tar zxvf nagios-plugins-1.4.9.tar.gz
cd nagios-plugins-1.4.9
./configure --prefix=/usr/local/nagios
make
make install
chown nagios.nagios /usr/local/nagios
chown -R nagios.nagios /usr/local/nagios/libexec/
至于NRPE的作用,下面的博客講的很詳細(xì)了。
http://yahoon.blog.51cto.com/13184/41893
tar zxvf nrpe-2.8.1.tar.gz
cd nrpe-2.8.1
./configure //NRPE port: 5666
make all
make install-plugin
之前說(shuō)過(guò)監(jiān)控機(jī)需要安裝check_nrpe這個(gè)插件,被監(jiān)控機(jī)并不需要,我們?cè)谶@里安裝它是為了測(cè)試的目的
make install-daemon //安裝daemon
make install-daemon-config //安裝配置文件
ls /usr/local/nagios/
bin/ etc/ libexec/ share/ //現(xiàn)在nagios目錄會(huì)有4個(gè)目錄了
如果機(jī)器上沒(méi)有xinetd,就yum 安裝一個(gè)。
將NRPE daemon作為xinetd下的一個(gè)服務(wù)運(yùn)行
yum -y install xinetd
service xinetd start
chkconfig --level 3 xinetd on
make install-xinetd //安裝xinetd腳本
編輯這個(gè)腳本
vi /etc/xinetd.d/nrpe
only_from = 127.0.0.1 192.168.4.226 //在后面增加監(jiān)控主機(jī)的地址,以空格間隔
增加nrpe服務(wù)
vi /etc/services
nrpe 5666/tcp # nrpe //增加這一行
重啟xinetd服務(wù)
service xinetd restart
netstat -at | grep nrpe //查看NRPE是否已經(jīng)啟動(dòng)
netstat -an | grep 5666 //查看5666端口是否被監(jiān)聽(tīng)
如果以上都沒(méi)有任何結(jié)果,重復(fù)檢查一下上面添加的東西是否正確!
vi /etc/sysconfig/iptables //增加一條5666的端口
-A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 5666 -j ACCEPT
在被監(jiān)控機(jī)器上測(cè)試一下NRPE是否正常工作。
/usr/local/nagios/libexec/check_nrpe -H 127.0.0.1 //測(cè)試NRPE是否正常工作
NRPE v2.8.1 //正常的結(jié)果會(huì)顯示當(dāng)前NRPE的版本號(hào)
/usr/local/nagios/libexec/check_nrpe –h查看這個(gè)命令的用法
可以看到用法是check_nrpe –H 被監(jiān)控的主機(jī) -c要執(zhí)行的監(jiān)控命令
注意:-c后面接的監(jiān)控命令必須是nrpe.cfg文件中定義的.也就是NRPE daemon只運(yùn)行nrpe.cfg中所定義的命令查看NRPE的監(jiān)控命令
cd /usr/local/nagios/etc
vi 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_hda1]=/usr/local/nagios/libexec/check_disk -w 20 -c 10 -p /dev/hda1
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 150 -c 200
[***]中是命令名,也就是check_nrpe的-c參數(shù)可以接的內(nèi)容,=后面是實(shí)際執(zhí)行的插件程序。
比如,我現(xiàn)在要監(jiān)控硬盤(pán)的情況,加入以下內(nèi)容:
command[check_hda1]=/usr/local/nagios/libexec/check_disk -w 20 -c 10 -p /dev/sda1
command[check_hda3]=/usr/local/nagios/libexec/check_disk -w 20 -c 10 -p /dev/sda3
所以以上nrpe.cfg配置的命令用法如下:
[root@ytt etc]# check_nrpe -H127.0.0.1 -c check_hda1
DISK OK - free space: /boot 77 MB (82% inode=99%);| /boot=16MB;78;88;0;98
[root@ytt etc]# check_nrpe -H127.0.0.1 -c check_hda3
DISK OK - free space: / 134960 MB (97% inode=99%);| /=3322MB;145789;145799;0;145809
/usr/local/nagios/libexec/check_nrpe -H 127.0.0.1 -c check_users //檢測(cè)登陸用戶(hù)數(shù)
/usr/local/nagios/libexec/check_nrpe -H 127.0.0.1 -c check_load //CPU負(fù)載
/usr/local/nagios/libexec/check_nrpe -H 127.0.0.1 -c check_zombie_procs //僵尸進(jìn)程
/usr/local/nagios/libexec/check_nrpe -H 127.0.0.1 -c check_total_procs //總進(jìn)程數(shù)
//check_load -w 15,10,5 -c 30,25,20
//在unix里面負(fù)載的均值通常表示是1分鐘,5分鐘,15分鐘內(nèi)平均有多少進(jìn)程處于等待狀態(tài)
//當(dāng)1分鐘多于15個(gè)進(jìn)程等待,5分鐘多于10個(gè),15分鐘多于5個(gè)則為warning狀態(tài)
//當(dāng)1分鐘多于30個(gè)進(jìn)程等待,5分鐘多于25個(gè),15分鐘多于20個(gè)則為critical狀態(tài)
-------------------------------------------------------------------------------------
在運(yùn)行Nagios的監(jiān)控主機(jī)上
因?yàn)橹耙呀?jīng)將Nagios運(yùn)行起來(lái)了,現(xiàn)在要做的是:
安裝check_nrpe插件
在commands.cfg中創(chuàng)建check_nrpe中的命令,只有在commands.cfg中定義過(guò)的命令才能在services.cfg中使用
創(chuàng)建對(duì)被監(jiān)控主機(jī)的監(jiān)控項(xiàng)目
tar zxvf nrpe-2.8.1.tar.gz
cd nrpe-2.8.1
./configure
make all
make install-plugin //只運(yùn)行這一步即可,只需要check_nrpe插件
這一步,記得在被監(jiān)控機(jī)器上的
/etc/xinetd.d/nrpe里面加上監(jiān)控機(jī)器的IP地址!
/usr/local/nagios/libexec/check_nrpe -H 192.168.4.30
NRPE v2.8.1 //測(cè)試一下監(jiān)控機(jī)使用check_nrpe與被監(jiān)控機(jī)運(yùn)行的nrpedaemon之間的通信
//看到已經(jīng)返回了正確的NRPE的版本信息,說(shuō)明一切正常
在commands.cfg中增加對(duì)check_nrpe的定義
vi /usr/local/nagios/etc/commands.cfg
#################################################################
# 2008.12.4 by ritto
#################################################################
# 'check_nrpe' command definition
define command{
command_name check_nrpe
command_line $USER1$/check_nrpe -H $HOSTADDRESS$ -c $ARG1$
}
//command_name check_nrpe 定義命令名稱(chēng)為check_nrpe, 在services.cfg中要使用這個(gè)名稱(chēng)
//command_line $USER1$/check_nrpe -H $HOSTADDRESS$ -c $ARG1$ 定義實(shí)際運(yùn)行的插件程序
接下來(lái)可以在services.cfg中定義對(duì)主機(jī)CPU負(fù)載的監(jiān)控。
注意機(jī)器必須是LINUX系統(tǒng),并且安裝了NRPE.
vi services.cfg
define service {
host_name mail1
service_description check-load
check_period 24x7
max_check_attempts 4
normal_check_interval 3
retry_check_interval 2
contact_groups sagroup
notification_interval 10
notification_period 24x7
notification_options w,u,c,r
check_command check_nrpe!check_load
}
----------------------------------------------------------------------
在被監(jiān)控機(jī)上增加check_swap命令的定義
vi /usr/local/nagios/etc/nrpe.cfg
增加下面這一行
command[check_swap]=/usr/local/nagios/libexec/check_swap -w 20% -c 10%
//如果在被監(jiān)控機(jī)上是以daemon運(yùn)行的nrpe,則需要手動(dòng)重啟
//如果在被監(jiān)控機(jī)上是以xinetd運(yùn)行的,則不需要
----------------------------------------------------------------------
在監(jiān)控機(jī)上增加這個(gè)監(jiān)控項(xiàng)目:
vi /etc/services.cfg
define service {
host_name mail1
service_description check-swap
check_period 24x7
max_check_attempts 4
normal_check_interval 3
retry_check_interval 2
contact_groups sagroup
notification_interval 10
notification_period 24x7
notification_options w,u,c,r
check_command check_nrpe!check_swap
}
------------------------------------------------------------------------------
建議再檢查一下配置是否正確再啟動(dòng)服務(wù)!
所有配置都已經(jīng)修改好,現(xiàn)在重啟Nagios,釘?shù)鬘agios進(jìn)程,再重啟,過(guò)一會(huì)就可以看到畫(huà)面了
killall nagios
/usr/local/nagios/bin/nagios -d /usr/local/nagios/etc/nagios.cfg
或使用
/etc/init.d/nagios restart/start/stop/status
如查報(bào)錯(cuò),則可能是腳本路徑設(shè)置錯(cuò)誤,
vi /etc/init.d/nagios
將prefix=/usr/local/nagios改為安裝的目錄/etc/init.d/nagios
參考學(xué)習(xí)地址:
http://sery.blog.51cto.com/10037/20520
http://yahoon.blog.51cto.com/13184/41778