1.系統:CentOS 5.2
2.安裝前準備:
1).安裝好apache,GD庫(我的GD庫安裝在/usr/local/libgd)。
2).下載Nagios3.0.5,Nagios-plugins1.4.11,nrpe2.12,nsclient++
# http://www.nagios.org/download/
1. wget http://osdn.dl.sourceforge.net/sourceforge/nagios/nagios-3.0.5.tar.gz
2. wget http://osdn.dl.sourceforge.net/sourceforge/nagiosplug/nagios-plugins-1.4.11.tar.gz
複製代碼
# Nagios3.0.5,Nagios-plugins1.4.11,nrpe2.12安裝在監控服務器上。
# 去http://www.nagios.org/download/addons/下載nrpe和nsclient++插件。
# nrpe2.12安裝在Linux/Unix被監控端。
# nsclient++安裝在Windows被監控端。
3).創建帳號及組
# 創建帳號
1. /usr/sbin/useradd -m nagios
2. passwd nagios
複製代碼
# 創建組
1. /usr/sbin/groupadd nagcmd
2. /usr/sbin/usermod -a -G nagcmd nagios
3. /usr/sbin/usermod -a -G nagcmd daemon
複製代碼
# daemon爲運行apache的帳號。
3.安裝nagios
1. tar xzf nagios-3.0.5.tar.gz
2. cd nagios-3.0.5
3. ./configure --with-command-group=nagcmd --with-gd-lib=/usr/local/libgd/lib/ --with-gd-inc=/usr/local/libgd/include/
4. make all
5. # 使用make install來安裝主程序,CGI和HTML文件
6. make install
7. # 使用make install-init在/etc/rc.d/init.d安裝啓動腳本
8. make install-init
9. # 使用make install-cofig來安裝示例配置文件,安裝的路徑是/usr/local/nagios/etc.
10. make install-config
11. # 使用make install-commandmode來配置目錄權限
12. make install-commandmode
複製代碼
nagios目錄功能的簡要說明:
bin Nagios執行程序所在目錄,nagios文件即爲主程序
etc Nagios配置文件位置
sbin Nagios Cgi文件所在目錄,也就是執行外部命令所需文件所在的目錄
Share Nagios網頁文件所在的目錄
var Nagios日誌文件、spid 等文件所在的目錄
var/archives 日誌歸檔目錄
var/rw 用來存放外部命令文件
配置apache
將現面行加入apache配置文件的alias模塊<IfModule alias_module>
----------------------------------------
1. ScriptAlias /nagios/cgi-bin "/usr/local/nagios/sbin"
2.
3. <Directory "/usr/local/nagios/sbin">
4. # SSLRequireSSL
5. Options ExecCGI
6. AllowOverride None
7. Order allow,deny
8. Allow from all
9. # Order deny,allow
10. # Deny from all
11. # Allow from 127.0.0.1
12. AuthName "Nagios Access"
13. AuthType Basic
14. AuthUserFile /usr/local/nagios/etc/htpasswd.users
15. Require valid-user
16. </Directory>
17.
18. Alias /nagios "/usr/local/nagios/share"
19.
20. <Directory "/usr/local/nagios/share">
21. # SSLRequireSSL
22. Options None
23. AllowOverride None
24. Order allow,deny
25. Allow from all
26. # Order deny,allow
27. # Deny from all
28. # Allow from 127.0.0.1
29. AuthName "Nagios Access"
30. AuthType Basic
31. AuthUserFile /usr/local/nagios/etc/htpasswd.users
32. Require valid-user
33. </Directory>
複製代碼
----------------------------------------
# 創建apache目錄驗證文件
1. /usr/local/apache/bin/htpasswd -c /usr/local/nagios/etc/htpasswd.users nagiosadmin
2. New password: (輸入密碼)
3. Re-type new password: (再輸入一次密碼)
4. Adding password for user nagiosadmin
複製代碼
# 重啓apache:/usr/local/apache/bin/apachectl -k restart
# 配置nagios配置文件
vi /usr/local/nagios/etc/objects/contacts.cfg
# 將裏面的email地址改爲自己的email地址。
4.安裝Nagios插件
1. tar xzf nagios-plugins-1.4.11.tar.gz
2. cd nagios-plugins-1.4.11
3. ./configure --with-nagios-user=nagios --with-nagios-group=nagios
4. make
5. make install
複製代碼
5.啓動Nagios
# 配置機器啓動時自動啓動Nagios
1. chkconfig --add nagios
2. chkconfig nagios on
複製代碼
# 檢查Nagios配置文件
1. /usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg
複製代碼
# 啓Nnagios
1. service nagios start
複製代碼
6.SELinux及防火牆設置
1).關閉SELinux
vi /etc/selinux/config
# 將下面行設爲disabled
SELINUX=disabled
# 重啓系統
2).如果開啓防火牆,應該允許訪問apache(一般爲80端口)並允許nagios去抓取被監控機信息(一般nrpe爲5666端口)。
7.訪問Nagios服務器
http://localhost/nagios/
輸入用戶名及密碼登錄。
8.安裝nrpe插件,用來監控Linux機器
1. tar xzvf nrpe-2.12.tar.gz
2. cd nrpe-2.12
3. ./configure
4. make all
5. # 在Nagios服務器端只要安裝nrpe監控插件就行
6. make install-plugin
複製代碼
在/usr/local/nagios/etc/objects/commands.cfg中定義check_nrpe命令
vi /usr/local/nagios/etc/objects/commands.cfg
1. ########################################################################
2. #
3. # 2008.11.18 add by Stone
4. # NRPE COMMAND
5. #
6. ########################################################################
7. # 'check_nrpe ' command definition
8. define command{
9. command_name check_nrpe
10. command_line $USER1$/check_nrpe -H $HOSTADDRESS$ -c $ARG1$
11. }
複製代碼
9.在被監控服務器(Linux/unix)上安裝Nagios-plugins和nrpe
1. useradd nagios
2. tar xzvf nagios-plugins-1.4.13.tar.gz
3. cd nagios-plugins-1.4.13
4. # Nagios-plugins默認安裝到/usr/local/nagios
5. ./configure
6. make
7. make install
8. chown nagios.nagios /usr/local/nagios/
9. chown -R nagios.nagios /usr/local/nagios/libexec/
複製代碼
1. tar xzvf nrpe-2.12.tar.gz
2. cd nrpe-2.12
3. ./configure
4. make all
5. # 安裝nrpe插件,本監控端可以不裝
6. make install-plugin
7. # 安裝nrpe守護進程
8. make install-daemon
9. # 安張nrpe配置文件
10. make install-daemon-config
複製代碼
# 修改nrpe配置文件,允許Nagios監控服務器(192.168.0.19)監控
vi nrpe.cfg
# 多臺機器用逗號隔開
allowed_hosts=127.0.0.1,192.168.0.19
# 以獨立守護進程啓動nrpe,也可以使用xinetd啓動nrpe,具體清查看nrpe官方文檔。
/usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -d
# 開機自動啓動nrpe
vi /etc/rc.d/rc.local
# 加入下面行
/usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -d
# 檢查nrpe是否安裝正常
[root@wiki etc]# /usr/local/nagios/libexec/check_nrpe -H localhost
NRPE v2.12
# 返回nrpe版本說明安裝沒問題。
# 查看啓動端口
[root@wiki ~]# netstat -tunlp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:5666 0.0.0.0:* LISTEN 27387/nrpe
如果有防火牆應該開放5666端口:
iptables -I eth0 -p tcp -m tcp ?dport 5666 -j ACCEPT
***********************************************************
注意:我們需要在/usr/local/nagios/etc/nrpe.cfg中定義我們用到的監控本地資源的命令。
下面的命令是默認定義的:
1. command[check_users]=/usr/local/nagios/libexec/check_users -w 5 -c 10
2. command[check_load]=/usr/local/nagios/libexec/check_load -w 15,10,5 -c 30,25,20
3. command[check_hda1]=/usr/local/nagios/libexec/check_disk -w 20% -c 10% -p /dev/hda1
4. command[check_zombie_procs]=/usr/local/nagios/libexec/check_procs -w 5 -c 10 -s Z
5. command[check_total_procs]=/usr/local/nagios/libexec/check_procs -w 150 -c 200
複製代碼
下面的命令是自己定義的:
# 監控交換分區的使用情況,使用超過20%時爲警告狀態,超過10%時爲嚴重狀態
command[check_swap]=/usr/local/nagios/libexec/check_swap -w 20% -c 10%
# 監控根分區磁盤使用情況
command[check_disk_root]=/usr/local/nagios/libexec/check_disk -w 20% -c 10% -p /
***********************************************************
10.Nagios如何監控Linux機器
NRPE總共由兩部分組成:
(1).check_nrpe插件,運行在監控主機上。
(2).NRPE daemon,運行在遠程的linux主機上(通常就是被監控機)
按照上圖,整個的監控過程如下:
當Nagios需要監控某個遠程linux主機的服務或者資源情況時:
1).nagios會運行check_nrpe插件,我們要在nagios配置文件中告訴它要檢查什麼.
2).check_nrpe插件會通過SSL連接到遠程的NRPE daemon.
3).NRPE daemon會運行相應的nagios插件來執行檢查本地資源或服務.
4).NRPE daemon將檢查的結果返回給check_nrpe插件,插件將其遞交給nagios做處理.
注意:NRPE daemon需要nagios插件安裝在遠程被監控linux主機上,否則,daemon不能做任何的監控.
11.Nagios的配置文件
# 控制cgi訪問的配置文件
cgi.cfg
# Nagios主配置文件
nagios.cfg
# resource.cfg定義了一些變量,以便被其他文件引用,如$USER1$
resource.cfg
# objects是一個目錄,用於定義Nagios對象
objects
# servers是自己創建的一個目錄,Nagios可以加載一個目錄下面的所有配置文件(需要在nagios.cfg中配置)
servers
./objects:
# 命令定義配置文件,裏面定義的命令可以被其他文件引用
commands.cfg
# 聯繫人和聯繫人組配置文件
contacts.cfg
# 監控本地機器的配置文件
localhost.cfg
# 監控打印機的一個事例配置文件(默認未啓用)
printer.cfg
# 監控路由器的一個事例配置文件(默認未啓用)
switch.cfg
# 模板配置文件,在此可以定義模板,在其他文件中引用
templates.cfg
# 定義監控時間段的配置文件
timeperiods.cfg
# 監控Windows的一個事例配置文件(默認未啓用)
windows.cfg
./servers:
# 自己創建的主機羣組配置文件
hostgroup.cfg
# 自己創建的監控遠程Linux主機的配置文件
wiki-l-11.cfg
配置文件是怎樣引用的?
用nagios主要是監控一臺主機的各種信息,包括本機資源以及對外的服務等等.這些在nagios裏面都是被定義爲一個個的項目(nagios稱之爲服務,爲了與主機提供的服務相區別,我這裏用項目這個詞),而實現每個監控項目,則需要通過commands.cfg文件中定義的命令。
爲了不必重複定義一些項目,Nagios引入了一個模板配置文件(templates.cfg),將一些共性的屬性定義成模板,以便於多次引用。
我們現在有一個監控項目是監控一臺機器的web服務是否正常, 我們需要哪些元素呢?最重要的有下面三點:首先是監控哪臺機器,然後是這個監控要用什麼命令實現,最後就是出了問題的時候要通知哪個聯繫人。
我們首先應該在commands.cfg中定義監控遠程服務和資源的命令,以及如何發送郵件的命令。大部分監控遠程服務和資源的命令的命令通過/usr/local/nagios/libexec下的腳本實現,如ping命令爲check_ping。
/usr/local/nagios/libexec下的腳本命令的使用發法可以通過-h參數查看,如:
-----------------------------------------------------------------------------------------
[root@tech ~]# /usr/local/nagios/libexec/check_ping -h
check_ping v1991 (nagios-plugins 1.4.13)
Copyright (c) 1999 Ethan Galstad <[email protected]>
Copyright (c) 2000-2007 Nagios Plugin Development Team
<[email protected]>
Use ping to check connection statistics for a remote host.
Usage:check_ping -H <host_address> -w <wrta>,<wpl>% -c <crta>,<cpl>%
[-p packets] [-t timeout] [-4|-6]
Options:
-h, --help
Print detailed help screen
-V, --version
Print version information
-4, --use-ipv4
Use IPv4 connection
-6, --use-ipv6
Use IPv6 connection
-H, --hostname=HOST
host to ping
-w, --warning=THRESHOLD
warning threshold pair
-c, --critical=THRESHOLD
critical threshold pair
-p, --packets=INTEGER
number of ICMP ECHO packets to send (Default: 5)
-L, --link
show HTML in the plugin output (obsoleted by urlize)
-t, --timeout=INTEGER
Seconds before connection times out (default: 10)
-----------------------------------------------------------------------------------------
然後我們在contacts.cfg文件中定義聯繫人和聯繫人組,在timeperiods.cfg中定義監控時間段。最後我們在服務器監控配置文件中引用前面定義的元素來監控服務器狀態。
===============================================================================
下面引用配置文件中部分配置做說明:
vi /usr/local/nagios/etc/resource.cfg
# 定義$USER1$變量,設置插件路徑
$USER1$=/usr/local/nagios/libexec
vi /usr/local/nagios/etc/objects/commands.cfg
# 定義check-host-alive命令
define command{
command_name check-host-alive # 命令名稱
command_line $USER1$/check_ping -H $HOSTADDRESS$ -w 3000.0,80% -c 5000.0,100% -p 5
}
# 上面的$USER1$和$HOSTADDRESS$引用自已定義的配置文件。變量不需現定義才能被引用。
########################################################################
#
# 2008.11.18 add by Stone
# NRPE COMMAND
# 自己定義check_nrpe命令,此命令後接必需接一個參數,用於告訴遠程服務器上的NRPE daemon需要監控的內容,如check_swap參數爲監控遠程機器的交換分區。
########################################################################
# 'check_nrpe ' command definition
define command{
command_name check_nrpe
command_line $USER1$/check_nrpe -H $HOSTADDRESS$ -c $ARG1$
}
vi /usr/local/nagios/etc/objects/contacts.cfg
# 定義聯繫人
define contact{
contact_name nagiosadmin ; Short name of user
use generic-contact ; Inherit default values from generic-contact template (defined above)
alias Nagios Admin ; Full name of user
email [email protected] ; <<***** CHANGE THIS TO YOUR EMAIL ADDRESS ******
}
# 上面的generic-contact在templates.cfg中定義。
# 定義聯繫人組
define contactgroup{
contactgroup_name admins
alias Nagios Administrators
members nagiosadmin #在此可以加入多個聯繫人,中間用逗號隔開
}
vi /usr/local/nagios/etc/objects/timeperiods.cfg
# 定義監控的時間段
define timeperiod{
timeperiod_name 24x7 #監控所有時間段(7*24小時)
alias 24 Hours A Day, 7 Days 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 /usr/local/nagios/etc/objects/templates.cfg
# 定義generic-contact聯繫人模板,並非真正的聯繫人,真正的聯繫人在contacts.cfg中定義
define contact{
name generic-contact ; The name of this contact template
service_notification_period 24x7 ; service notifications can be sent anytime
host_notification_period 24x7 ; host notifications can be sent anytime
service_notification_options w,u,c,r,f,s ; send notifications for all service states, flapping events, and scheduled downtime events
host_notification_options d,u,r,f,s ; send notifications for all host states, flapping events, and scheduled downtime events
service_notification_commands notify-service-by-email ; send service notifications via email
host_notification_commands notify-host-by-email ; send host notifications via email
register 0 ; DONT REGISTER THIS DEFINITION - ITS NOT A REAL CONTACT, JUST A TEMPLATE!
}
------------------------------------------------------------------------------------------------------------------
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-service-by-email
服務出問題通知採用的命令notify-service-by-email,這個命令是在commands.cfg中定義的,作用是給聯繫人發郵件.
host_notification_commands notify-host-by-email
同上,主機出問題時採用的也是發郵件的方式通知聯繫人
------------------------------------------------------------------------------------------------------------------
# 定義generic-host主機模板
define host{
name generic-host ; The name of this host template
notifications_enabled 1 ; Host notifications are enabled
event_handler_enabled 1 ; Host event handler is enabled
flap_detection_enabled 1 ; Flap detection is enabled
failure_prediction_enabled 1 ; Failure prediction is enabled
process_perf_data 1 ; Process performance data
retain_status_information 1 ; Retain status information across program restarts
retain_nonstatus_information 1 ; Retain non-status information across program restarts
notification_period 24x7 ; Send host notifications at any time
register 0 ; DONT REGISTER THIS DEFINITION - ITS NOT A REAL HOST, JUST A TEMPLATE!
}
# 定義Linux主機模板
define host{
name linux-server ; The name of this host template
use generic-host ; This template inherits other values from the generic-host template
check_period 24x7 ; By default, Linux hosts are checked round the clock
check_interval 5 ; Actively check the host every 5 minutes
retry_interval 1 ; Schedule host check retries at 1 minute intervals
max_check_attempts 10 ; Check each Linux host 10 times (max)
check_command check-host-alive ; Default command to check Linux hosts
notification_period workhours ; Linux admins hate to be woken up, so we only notify during the day
; Note that the notification_period variable is being overridden from
; the value that is inherited from the generic-host template!
notification_interval 120 ; Resend notifications every 2 hours
notification_options d,u,r ; Only send notifications for specific host states
contact_groups admins ; Notifications get sent to the admins by default
register 0 ; DONT REGISTER THIS DEFINITION - ITS NOT A REAL HOST, JUST A TEMPLATE!
}
# 在nagios.cfg配置文件中開啓對/usr/local/nagios/etc/servers/中配置文件的引用。
cfg_dir=/usr/local/nagios/etc/servers
# 遠程Linux主機監控文件,如果監控多臺主機只需簡單複製修改即可。
#我們應該牢記wiki-l-11.cfg用到的命令在commands.cfg中定義,在commands.cfg中定義的命令用到/usr/local/nagios/libexec下的插件(命令)。
vi /usr/local/nagios/etc/servers/wiki-l-11.cfg
# 定義主機
define host{
use linux-server ; Name of host template to use
; This host definition will inherit all variables that are defined
; in (or inherited by) the linux-server host template definition.
host_name wiki
alias Docs
address 192.168.0.11
}
# 定義Ping遠程Linux主機
define service{
use generic-service ; Name of service template to use
host_name wiki
service_description PING
check_command check_ping!100.0,20%!500.0,60% ;check_ping命令在commands.cfg中定義,後跟兩個參數,命令及參數間用!分割。
}
#檢查遠程Linux主機根分區使用情況,check_nrpe命令必須在/usr/local/nagios/etc/objects/commands.cfg中定義(默認未定義)
define service{
use generic-service ; Name of service template to use
host_name wiki
service_description Root Partition
check_command check_nrpe!check_disk_root
}
# 檢查遠程Linux主機的登錄人數
define service{
use generic-service ; Name of service template to use
host_name wiki
service_description Current Users
check_command check_nrpe!check_users
}
# 檢查遠程Linux的主機的負載
define service{
use generic-service ; Name of service template to use
host_name wiki
service_description Current Load
check_command check_nrpe!check_load
}
# 檢查遠程Linux主機swap分區使用情況
define service{
use generic-service ; Name of service template to use
host_name wiki
service_description Swap Usage
check_command check_nrpe!check_swap
}
# 檢查遠程Linux主機的SSH服務
define service{
use generic-service ; Name of service template to use
host_name wiki
service_description SSH
check_command check_ssh
notifications_enabled 0
}
# 檢查遠程Linux主機的HTTP服務
define service{
use generic-service ; Name of service template to use
host_name wiki
service_description HTTP
check_command check_http
notifications_enabled 0
}
vi /usr/local/nagios/etc/servers/hostgroup.cfg
# 定義主機組(localhost.cfg中有類似的主機組設置,我已將其註釋掉,否則可能會有衝突)
define hostgroup{
hostgroup_name linux-servers ; The name of the hostgroup
alias Linux Servers ; Long name of the group
members localhost,wiki ; Comma separated list of hosts that belong to this group
}
#define hostgroup{
# hostgroup_name windows-servers ; The name of the hostgroup
# alias Windows Servers ; Long name of the group
# members print ; Comma separated list of hosts that belong to this group
# }
===============================================================================
# 完成監控主機配置文件的配置後使用下面命令檢查配置文件的正確性:
/usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg
# 確定無誤後重啓Nagios:
service nagios restart
使用Nagios監控Windows服務器
12.Nagios使用NSClient++監控遠程Windows主機
下載NSClient++-Win32-0.3.5.msi並安裝。
到安裝目錄打開NSC.ini文件進行修改:
在[modules]模塊,將除CheckWMI.dll和RemoteConfiguration.dll外的所有dll文件明前的註釋(;)去掉。
在[Settings]模塊可以設置一個連接密碼password=PWD,爲了簡單,在此不設密碼。設置 allowed_hosts=127.0.0.1/32,192.168.0.19,可以連接的監控服務器的地址,如果寫成192.168.0.0/24 則表示該子網內的所有機器都可以訪問;如果這個地方是空白則表示所有的主機都可以連接上來(注意在[NSClient]有allowed_hosts的同樣設置,不要設置錯了),最後不要忘記去掉前面的註釋符(;)。
運行nsclient++
NSClient++ /install
NSClient++ SysTray install
NSClient++ /start
如果有防火牆,請開放相應端口。
創建監控配置文件,使用check_nt命令監控windows系統信息(此命令默認已定義)。
Windows監控示例配置文件:
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
[root@tech etc]# cat /usr/local/nagios/etc/servers/print-w-80.cfg
###############################################################################
# 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 windows-server ; Inherit default values from a template
host_name print80 ; The name we're giving to this host
alias Print80 ; A longer name associated with the host
address 192.168.0.80 ; IP address of the host
}
###############################################################################
###############################################################################
#
# HOST GROUP DEFINITIONS
# 主機組在/usr/local/nagios/etc/servers/hostgroup.cfg中單獨配置
###############################################################################
###############################################################################
# Define a hostgroup for Windows machines
# All hosts that use the windows-server template will automatically be a member of this group
#define hostgroup{
# hostgroup_name windows-servers ; The name of the hostgroup
# alias Windows Servers ; Long name of the group
# }
###############################################################################
###############################################################################
#
# SERVICE DEFINITIONS
#
###############################################################################
###############################################################################
# Create a service for monitoring the version of NSCLient++ that is installed
# Change the host_name to match the name of the host you defined above
define service{
use generic-service
host_name print80
service_description NSClient++ Version
check_command check_nt!CLIENTVERSION
}
# 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 print80
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 print80
service_description CPU Load
check_command check_nt!CPULOAD!-l 5,80,90
}
# Create a service for monitoring
# Change the host_name to match the name of the host you defined above
define service{
use generic-service
host_name print80
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 print80
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 print80
service_description W3SVC
check_command check_nt!SERVICESTATE!-d SHOWALL -l W3SVC
}
# 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 print80
service_description Explorer
check_command check_nt!PROCSTATE!-d SHOWALL -l Explorer.exe
}
#主機組配置文件
[root@tech etc]# cat /usr/local/nagios/etc/servers/hostgroup.cfg
define hostgroup{
hostgroup_name linux-servers ; The name of the hostgroup
alias Linux Servers ; Long name of the group
members localhost,wiki ; Comma separated list of hosts that belong to this group
}
define hostgroup{
hostgroup_name windows-servers ; The name of the hostgroup
alias Windows Servers ; Long name of the group
members print80 ; Comma separated list of hosts that belong to this group
}
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
使用NDOUtils將Nagios監控信息存入數據庫
1.NDOUtils安裝需求:
Nagios正確安裝運行,版本>=2.0安裝目錄:/usr/local/nagios
MySQL正確安裝並運行,我用的是二進制安裝包,安裝目錄:/usr/local/mysql
2.安裝
下載安裝包:http://www.nagios.org/download/
# 確認DBD-mysql包安裝,如沒有安裝,請先安裝。
# 如果DBI包沒安裝DBD-mysql前需要先安裝DBI包
tar xzvf DBI-1.605.tar.gz
cd DBI-1.605
perl Makefile.PL
make
make install
tar xzvf DBD-mysql-3.0008.tar.gz
cd DBD-mysql-3.0008
unset LANG
perl Makefile.PL --libs="-L/usr/local/mysql/lib/mysql -lmysqlclient -L/usr/lib -lz " --cflags="-I/usr/local/mysql/include" --testhost=127.0.0.1 --mysql_config=/usr/local/mysql/bin/mysql_config --testuser=root --testpassword=baihe.com
make
make install
# 做好MySQL庫文件lib和include文件的連接(這點很重要)。
ln -s /usr/local/mysql/include/* /usr/include/
mkdir /usr/include/mysql
ln -s /usr/local/mysql/include/* /usr/include/mysql/
ln -s /usr/local/mysql/lib/* /usr/lib/
mkdir /usr/lib/mysql
ln -s /usr/local/mysql/lib/* /usr/lib/mysql
tar xzvf ndoutils-1.4b7.tar.gz
cd ndoutils-1.4b7
./configure --enable-mysql
# 執行完成後,注意向上看看打印出的信息,看看是否找到了MySQL的庫文件和include文件。
make
cd src/
# 我的Nagios是3.0.5,所以拷貝的是ndomod-3x.o、ndo2db-3x,如果你的是2.X.x請拷貝ndomod-2x.o、ndo2db-2x
cp ndomod-3x.o ndo2db-3x log2ndo file2sock /usr/local/nagios/bin
cd ../db
# 創建數據庫,使用root用戶,密碼是baihe.com
mysqladmin -u root -pbaihe.com create nagios
./installdb -u root -p baihe.com -d nagios
# 拷貝配置文件
cd ../config
cp ndo* /usr/local/nagios/etc/
# 修改/usr/local/nagios/etc/ndo2db.cfg文件的數據庫信息。
vi /usr/local/nagios/etc/ndo2db.cfg
vi nagios.cfg
# 複製下面內容粘貼到/usr/local/nagios/etc/nagios.cfg配置文件的#broker_module=...下面。
# Uncomment the line below if you're running Nagios 3.x
broker_module=/usr/local/nagios/bin/ndomod-3x.o config_file=/usr/local/nagios/etc/ndomod.cfg
# 修改/usr/local/nagios/etc/nagios.cfg配置文件下面參數的值爲-1(一般默認如此)。
event_broker_options=-1
# 啓動ndo2db
/usr/local/nagios/bin/ndo2db-3x -c /usr/local/nagios/etc/ndo2db.cfg
# 查看系統日誌是否有錯誤信息:
tail -30 /var/log/messages
Nov 27 14:09:26 tech nagios: ndomod: NDOMOD 1.4b7 (10-31-2007) Copyright (c) 2005-2007 Ethan Galstad ([email protected])
Nov 27 14:09:26 tech nagios: ndomod: Successfully connected to data sink. 0 queued items to flush.
Nov 27 14:09:26 tech nagios: Event broker module '/usr/local/nagios/bin/ndomod-3x.o' initialized successfully.
Nov 27 14:09:26 tech nagios: Finished daemonizing... (New PID=18848)
# 重啓Nagios
service nagios restart
# 查看Nagios日誌,看是否正常啓動。
tail -20 /usr/local/nagios/var/nagios.log
[1227766166] ndomod: NDOMOD 1.4b7 (10-31-2007) Copyright (c) 2005-2007 Ethan Galstad ([email protected])
[1227766166] ndomod: Successfully connected to data sink. 0 queued items to flush.
[1227766166] Event broker module '/usr/local/nagios/bin/ndomod-3x.o' initialized successfully.
[1227766166] Finished daemonizing... (New PID=18848)
安裝使用Nagvis插件
1.安裝需求:
1).Nagios已正常運行。
2).使用NDOUtils將Nagios監控信息存入數據庫(見NDOUtils安裝)。
3).PHP5.0以上版本,並需要安裝php-gd、php-mysql、php5-mbstring、php5-session、php5-xml模塊。
4).安裝Graphviz >= 2.14,下載http://www.graphviz.org/graphviz-rhel.repo文件放到/etc/yum.repos.d/目錄下,yum list available 'graphviz*';yum install 'graphviz*'或者安裝webdot:yum install 'webdot'。
2.Nagvis安裝
1).下載Nagivs,http://www.nagvis.org。
2).tar xvzf nagvis-1.3.x.tar.gz
3).mv nagvis /usr/local/nagios/share
4).配置nagvis
cd /usr/local/nagios/share/nagvis
cp etc/nagvis.ini.php-sample etc/nagvis.ini.php
vi etc/nagvis.ini.php
在[paths]模塊修改URL path。
在[backend_ndomy_1]模塊修改連接數據庫的一下信息(如用戶名、密碼等)。
注意:修改過的參數需要去掉前面的分號註釋符。
5).將/usr/local/nagios/share/nagvis設爲運行apache的用戶(daemon)所有。
chown daemon:nagios /usr/local/nagios/share/nagvis -R
chmod 664 /usr/local/nagios/share/nagvis/etc/nagvis.ini.php
chmod 775 /usr/local/nagios/share/nagvis/nagvis/images/maps
chmod 664 /usr/local/nagios/share/nagvis/nagvis/images/maps/*
chmod 775 /usr/local/nagios/share/nagvis/etc/maps
chmod 664 /usr/local/nagios/share/nagvis/etc/maps/*
chmod 775 /usr/local/nagios/share/nagvis/var
chmod 664 /usr/local/nagios/share/nagvis/var/*
3.訪問圖形配置工具:http://<nagiosserver>/<path-to-nagvis>/config.php
4.訪問監控圖: http://<nagiosserver>/<path-to-nagvis>/index.php?map=<mapname>
Nagios使用飛信短信報警
1.下載安裝飛信機器人
下載地址:http://www.it-adv.net/
注意:ReaHat和CentOS用戶應使用LINUX X86/32(REDHAT ES4X32)版。
其中支持庫和安裝包內容如下:
支持庫:libraryrh4x32.tar.gz
最新飛信機器人爲fetion20080522004-linrh4.tar.gz
cd /usr/local
# 將飛信解壓到/usr/local下
tar zxvf fetion20080522004-linrh4.tar.gz
mv install fetion
# 創建飛信支持庫目錄
mkdir /usr/local/fetion/lib
tar zxvf libraryrh4x32.tar.gz
cd libraryrh4x32
cp lib*so* /usr/local/fetion/lib
ln -s /usr/local/fetion/lib/libcrypto.so.0.9.7a /usr/local/fetion/lib/libcrypto.so.4
ln -s /usr/local/fetion/lib/libssl.so.0.9.7a /usr/local/fetion/lib/libssl.so.4
vi /etc/ld.so.conf
# 加入下面行
/usr/local/fetion/lib
# 保存退出,並執行ldconfig命令使配置生效。
2.測試安裝是否成功
[root@web74 fetion]# ./fetion
************************ IMPORTANT STATEMENT ************************
** **
** PLEASE DON'T USE THIS SOFTWARE TO SEND JUNK SHORT MESSAGES. **
** OTHERWISE PLEASE BEAR YOUR OWN CONSEQUENCES. **
** **
** Version:[20080522004-linrh4] **
*********************************************************************
This program is the console version of China Fetion!
It's free for personal user.
Fetion official website: http://www.fetion.com.cn/
This project website: http://www.it-adv.net/
AUTHOR:KelvinH MSN/EMAIL:[email protected]
Usage:
fetion -h
-h: help
fetion -u mobile -p pwd [-b batchfile] [-EN] [-d]
fetion -u mobile -p pwd [-b batchfile] [-EN] [-d]
-u: Fetion user account(only supports mobile phone No.)
-p: Account password
-b: Batch file name
-d: Debug on and write logs to [mobile]-debug.log
-EN: English
3.編寫發送腳本
vi /usr/local/fetion/sendsms.sh
#!/bin/sh
fetionDir=/usr/local/fetion
cd $fetionDir
DIR=`pwd`
# 設置發短信的號碼和飛信登錄密碼
user=158xxxxxxxx
pwd=xxxxxx
for phone in `cat $DIR/phonelist.txt`
do
echo "$phone" | sed '/^[ \t]*$/d' | sed 's/^[ \t]*//' | sed 's/[ \t]*$//' | grep '^1[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]'
if (($? == 0 ));then
if [[ -f $DIR/msg.txt ]];then
cat /dev/null > msg.txt
fi
phone=`echo "$phone" | sed 's/^[ \t]*//' | sed 's/[ \t]*$//'`
echo "sms $phone $1" >> $DIR/msg.txt
echo "quit" >> $DIR/msg.txt
$fetionDir/fetion -u $user -p $pwd -b $DIR/msg.txt
else
continue
fi
done
# phonelist.txt爲接收短信號碼列表,必須是發送飛信用戶的好友
# 在此控制短信接收者而不是在nagios聯繫人中,好處是修改接收者後不需要重啓nagios
# 可以使用#號註釋接收者的號碼以屏蔽接收者
vi /usr/local/nagios/fetion/phonelist.txt
# Phone NO. 1
138xxxxxxxx
# Phone NO. 2
158xxxxxxxx
# 修改飛信目錄權限(否則運行Nagios的用戶不能寫入信息內容到/usr/local/fetion/msg.txt)
chown -R nagios.nagios /usr/local/fetion
chmod +x /usr/local/fetion/sendsms.sh
chmod +x /usr/local/fetion/fetion
4.配置Nagios
vi /usr/local/nagios/etc/objects/commands.cfg
################################################################################
#
# notify-host-by-sendmsg/notify-service-by-sendmsg //定義發送飛信報警的命令
#
################################################################################
define command {
command_name notify-host-by-sendmsg
command_line /usr/local/fetion/sendsms.sh "Host $HOSTSTATE$ alert for $HOSTNAME$($HOSTADDRESS$) on $TIME$."
}
define command {
command_name notify-service-by-sendmsg
command_line /usr/local/fetion/sendsms.sh ""$TIME$":$SERVICEDESC$($HOSTADDRESS$) is $SERVICESTATE$."
}
定義聯繫人模板
define contact{
name sendmsg-contact ; The name of this contact template
service_notification_period 24x7 ; service notifications can be sent anytime
host_notification_period 24x7 ; host notifications can be sent anytime
service_notification_options w,u,c,s ; send notifications for all service states, flapping events, and scheduled downtime events
host_notification_options d,u,s ; send notifications for all host states, flapping events, and scheduled downtime events
service_notification_commands notify-service-by-sendmsg ; 使用定義的飛信報警命令
host_notification_commands notify-host-by-sendmsg ; 使用定義的飛信報警命令
register 0 ; DONT REGISTER THIS DEFINITION - ITS NOT A REAL CONTACT, JUST A TEMPLATE!
}
定義聯繫人及聯繫人組
vi /usr/local/nagios/etc/objects/contacts.cfg
define contact{
contact_name msgreceiver ; Short name of user
use sendmsg-contact ; 使用上面定義的聯繫人模板
alias MsgReceiver ; Full name of user
email
[email protected]
}
define contactgroup{
contactgroup_name admins-sendmsg
alias BaiHe Monitor
members msgreceiver
}
5.測試故障時是否能夠觸發短信報警
Nagios事件處理
我們可以使用事件處理來在任何人收到通知之前由Nagios來做一些前期故障修復。
事件處理會在下面情況觸發:
1).主機或服務處於一個軟態故障狀態時
2).主機或服務初始進入一個硬態故障時
3).主機或服務從軟態或硬態的故障狀態中初始恢復時
通過在主配置文件(nagios.cfg)中設置enable_event_handlers=1來打開全局事件處理,特定主機的和服務的事件處理可用主機和服務對象裏的event_handler_enabled域來開關。如果全局的enable_event_handlers域是關閉的,那麼特定主機的和服務的事件處理也不會運行。
事件處理命令可以用shell或是perl腳本,腳本中應該處理以下宏:
對服務的:$SERVICESTATE$、$SERVICESTATETYPE$和$SERVICEATTEMPT$;
對主機的:$HOSTSTATE$、$HOSTSTATETYPE$和$HOSTATTEMPT$。
腳本須檢測這些作爲命令行參數傳入的值,並採取必要動作來處理這些值。
事件處理命令通常是與運行於本機上的Nagios程序的權限是相同的(下面例子中Nagios服務是以nagios用戶運行的)。這可能會有問題,如果你想寫成一個用於系統服務重啓的命令,它需要有root權限才能執行一系列命令與任務。你或許會嘗使用sudo命令來實現它。
本例通過Nagios檢測遠程機器上的MySQL服務,當服務出現問題時通過Nagios的事件處理邏輯來重啓遠程機器上的MySQL服務。
1.配置在Nagios服務器(192.168.0.200)上無密碼登錄遠程機器(MySQL服務運行在上面-192.168.0.210)
[root@nagios ~]# su - nagios
[nagios@nagios ~]$ ssh-keygen -t rsa
# 下面一直回車,不要設置密碼
Generating public/private rsa key pair.
Enter file in which to save the key (/home/nagios/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/nagios/.ssh/id_rsa.
Your public key has been saved in /home/nagios/.ssh/id_rsa.pub.
The key fingerprint is:
d2:82:61:12:53:f9:53:75:77:8d:32:c0:ca:c8:20:60
[email protected]
# 將生成的密鑰拷貝到要遠程登錄的機器上
[nagios@nagios ~]$ scp .ssh/id_rsa.pub 192.168.0.210:/home/nagios/
[email protected]'s password:
id_rsa.pub 100% 233 0.2KB/s 00:00
# 在要遠程登錄的機器上配置公鑰
[nagios@nagios ~]$ ssh [email protected]
[email protected]'s password:
Last login: Sat Nov 29 22:30:55 2008 from 192.168.0.200
[nagios@nagios1 ~]$ cat id_rsa.pub >> .ssh/authorized_keys
[nagios@nagios1 ~]$ chmod 600 .ssh/authorized_keys
[nagios@nagios1 ~]$ exit
logout
Connection to 192.168.0.210 closed.
# 測試無密碼登錄
[nagios@nagios ~]$ ssh [email protected]
Last login: Sat Nov 29 22:35:27 2008 from 192.168.0.200
2.在遠程機器上配置sudo
使nagios用戶可以以root身份運行/usr/local/nagios/libexec/eventhandlers/restart-mysql腳本
[root@MySQL ~]# visudo
nagios ALL=(root) NOPASSWD:/usr/local/nagios/libexec/eventhandlers/restart-mysql
3.在遠程機器上編寫MySQL重啓腳本
[root@MySQL ~]# vi /usr/local/nagios/libexec/eventhandlers/restart-mysql
#!/bin/sh
#
# Event handler script for restarting the MySQL server on the remote machine
#
# Note: This script will only restart the MySQL server if the service is
# retried 2 times (in a "soft" state) or if the web service somehow
# manages to fall into a "hard" error state.
#
#
# What state is the MySQL service in?
case "$1" in
OK)
;;
WARNING)
;;
UNKNOWN)
;;
CRITICAL)
# Is this a "soft" or a "hard" state?
case "$2" in
SOFT)
# What check attempt are we on? We don't want to restart the MySQL server on the first
# check, because it may just be a fluke!
case "$3" in
2)
echo -n "Restarting MySQL service..."
/sbin/service mysqld restart
;;
esac
;;
HARD)
echo -n "Restarting MySQL service..."
/sbin/service mysqld restart
;;
esac
;;
esac
exit 0
上面的腳本只會在MySQL處於軟狀態,且第二次檢查出現故障時或者進入硬狀態時重啓MySQL。
4.配置Nagios服務器上的配置文件
[root@nagios ~]# cd /usr/local/nagios/etc/
# 檢查全局事件處理是否打開
[root@nagios etc]# vi nagios.cfg
enable_event_handlers=1
# 在命令配置文件中定義重啓MySQL的命令
[root@nagios etc]# vi objects/commands.cfg
# restart the service on a remote server
define command{
command_name restart-mysql
command_line /usr/bin/ssh nagios@$HOSTADDRESS$ "sudo /usr/local/nagios/libexec/eventhandlers/restart-mysql $SERVICESTATE$ $SERVICESTATETYPE$ $SERVICEATTEMPT$"
}
# 配置主機監控文件
[root@nagios etc]# vi servers/mysql.cfg
# 省略主機定義和其他服務定義
define service{
use generic-service ; Name of service template to use
host_name MySQL
service_description MySQL
check_command check_nrpe!check_mysql
notifications_enabled 1
event_handler_enabled 1
event_handler restart-mysql
}
這個腳本理論上在服務轉入硬態故障之前可以重啓MySQL服務以修復故障,這裏包含了首次重啓沒有成功的情況。須注意的是事件處理將只是第一次進入硬態緊急狀態時纔會被觸發,這將阻止Nagios在服務一直處於硬態故障狀態時反覆地重啓MySQL服務。