Nagios配置学习手记-续-完整版
时间:2006-06-03 来源:chenl
Nagios配置学习手记-续-完整版
2005.06.01
[email protected]
|
说明
本文是上一篇文章<Nagios配置学习手记>的后续版本。主要内容为对command(plugins)/host/service的配置及对主机/应用监控具体实现。其中内容包括:
配置了commands/hosts/services实例;
采用从源码编译方式安装;
添加了Nrpe支持,对远程主机的本地资源进行监控;
采用统一的Notify接口方式,添加了多种通知方式的实现,包括Smtp方式的Email、QQ;
MSN/SMS方式在本文没有讨论。SMS虽然内部通过http方式实现,但不具有通用性;Msn实现方式可以参照QQ。QQ使用的是TextQQ(一个采用LumaQQ核心的QQ项目)。关于Msn,还没有找到比较好的Linux下支持CLI操作的软件,如果有谁知道合适的,希望推荐。
主机操作系统为AS3U5。
本文主要参考Nagios的官方文档Nagios_2_0_Docs.pdf而成。
以下文中所列出的配置文件及脚本,皆在我的系统的真实环境测试通过,可以经过简单修改后使用。但在此生命,并不保证在所有的环境下皆能正常运行。本文档的所有权归chenl([email protected])所有,使用GPL发布,可以自由拷贝,转载,转载时请保持文档的完整性,严禁用于任何商业用途。
一、前言
关于本文档前一部分的相关信息,请参阅:
http://www.cublog.cn/u/1589/showart.php?id=114110 ,或
http://blog.donews.com/chnl/archive/2006/05/17/873481.aspx
二、安装
关于安装部分,上篇文章已经作了介绍。所不同的是,本次多了nrpe,并且采用源码方式。如果安装出现问题,可以参考http://www.novell.com/coolsolutions/feature/16723.html 。
(一)Nagios端
1、安装文件列表
我的系统中默认已经安装有gd。如果系统中没有gd存在,需要先安装gd。
nagios-2.3.1.tar.gz
nagios-plugins-1.4.3.tar.gz
2、安装账号及目录
安装建议在操作系统中建立(useradd)Nagios单独的系统账号, 本例中使用nagios用户,其主目录为/home/cl/nagios。其中:
nagios的安装目录为: /home/cl/nagios/dist
plugins的安装目录为: /home/cl/nagios/dist/libexec
要确认nagios对所有这些目录有读写的权限。另外,要保证httpd Server的账号对nagios的cgi及html有读及执行的权限,比较常规的做法是把httpd Server的用户,如apache,加入到nagios组中,并设置相应的权限。
如果需要Nagios在开机的时候自动启动,则需要把相应的init脚本copy至/etc/init.d目录下,改名为nagios,并执行chkconfig –add nagios命令来添加(Redhat系统),具体请参阅这方面的文档。
3、其他
Notify脚本的部分需要Java/Expect的环境支持。QQ通知部分,需要Jdk1.5及expect支持。email通知部分,Jdk1.4以上版本即可。
(二)Nrpe端
1、nrpe的安装
此例中,Nrpe安装使用的文件版本为nrpe-2.5.1.tar.gz,与Nagios端的安装一样,Nrpe需要建立自己的操作系统用户,为了统一管理,仍然使用nagios,其主目录为/home/cl/nagios。其中:
nrpe的安装目录为: /home/cl/nagios/nrpe
要确认nagios对所有这些目录有读写的权限。
如果需要Nrpe在开机的时候自动启动,则需要把相应的init脚本放copy至/etc/init.d目录下,改名为nrpe,并执行chkconfig –add nrpe命令来添加(Redhat系统),具体请参阅这方面的文档。
三、配置
将会被讨论的配置文件列表为:
Nagios:
nagios.cfg minimal.cfg resource.cfg cgi.cfg commands.cfg hosts.cfg services.cfg
Httpd(Apache):
nagios.conf .htaccess htpasswd.users
Nrpe:
nrce.cfg
部分修改过的重要配置将被标红处理。
(一)Nagios部分
此例中,Nagios安装后配置文件所在的目录为/home/cl/nagios/dist/etc。
1、nagios.cfg
这是Nagios的主配置文件。大部分的全局配置都在这个文件中,其他配置文件可以利用cfg_file指令来引用。
本例中nagios.cfg文件内容如下:
log_file=/home/cl/nagios/dist/var/nagios.log
#以下四行,为自定义的Command/Hosts/Services及minimal.cfg文件,将会在下面讨论。
cfg_file=/home/cl/nagios/dist/etc/commands.cfg
cfg_file=/home/cl/nagios/dist/etc/hosts.cfg
cfg_file=/home/cl/nagios/dist/etc/services.cfg
cfg_file=/home/cl/nagios/dist/etc/minimal.cfg
object_cache_file=/home/cl/nagios/dist/var/objects.cache
resource_file=/home/cl/nagios/dist/etc/resource.cfg
status_file=/home/cl/nagios/dist/var/status.dat
nagios_user=nagios
nagios_group=nagios
check_external_commands=0
command_check_interval=-1
command_file=/home/cl/nagios/dist/var/rw/nagios.cmd
comment_file=/home/cl/nagios/dist/var/comments.dat
downtime_file=/home/cl/nagios/dist/var/downtime.dat
lock_file=/home/cl/nagios/dist/var/nagios.lock
temp_file=/home/cl/nagios/dist/var/nagios.tmp
event_broker_options=-1
log_rotation_method=d
log_archive_path=/home/cl/nagios/dist/var/archives
use_syslog=1
log_notifications=1
log_service_retries=1
log_host_retries=1
log_event_handlers=1
log_initial_states=0
log_external_commands=1
log_passive_checks=1
service_inter_check_delay_method=s
max_service_check_spread=30
service_interleave_factor=s
host_inter_check_delay_method=s
max_host_check_spread=30
max_concurrent_checks=0
service_reaper_frequency=10
auto_reschedule_checks=0
auto_rescheduling_interval=30
auto_rescheduling_window=180
sleep_time=0.25
#如果自定义的command或者notify的脚本运行时间比较长,则需要把Timeout修改的大一些。
service_check_timeout=60
host_check_timeout=30
event_handler_timeout=30
notification_timeout=60
ocsp_timeout=5
perfdata_timeout=5
retain_state_information=1
state_retention_file=/home/cl/nagios/dist/var/retention.dat
retention_update_interval=60
use_retained_program_state=1
use_retained_scheduling_info=0
interval_length=60
use_aggressive_host_checking=0
execute_service_checks=1
accept_passive_service_checks=1
execute_host_checks=1
accept_passive_host_checks=1
enable_notifications=1
enable_event_handlers=1
process_performance_data=0
obsess_over_services=0
check_for_orphaned_services=0
check_service_freshness=1
service_freshness_check_interval=60
check_host_freshness=0
host_freshness_check_interval=60
aggregate_status_updates=1
status_update_interval=15
enable_flap_detection=0
low_service_flap_threshold=5.0
high_service_flap_threshold=20.0
low_host_flap_threshold=5.0
high_host_flap_threshold=20.0
date_format=us
p1_file=/home/cl/nagios/dist/bin/p1.pl
illegal_object_name_chars=`~!$%^&*|'"<>?,()=
illegal_macro_output_chars=`~$&|'"<>
use_regexp_matching=0
use_true_regexp_matching=0
admin_email=nagios
admin_pager=pagenagios
daemon_dumps_core=0
2、minimal.cfg
此配置文件中,主要设定了有关Notify的参数及Timeperiod等。本来在其中配置的commands/hosts/services等都放在了其他单独的文件中。
内容如下:
define timeperiod{
timeperiod_name 24x7
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
}
define command{
command_name notify-by-email
command_line /home/cl/nagios/dist/libexec/alert_sender.sh '** $NOTIFICATIONTYPE$ alert - $HOSTALIAS$/$SERVICEDESC$ is $SERVICESTATE$ **' '***** Nagios *****\n\nNotification Type: $NOTIFICATIONTYPE$\n\nService: $SERVICEDESC$\nHost: $HOSTALIAS$\nAddress: $HOSTADDRESS$\nState: $SERVICESTATE$\n\nDate/Time: $LONGDATETIME$\n\nAdditional Info:\n\n$SERVICEOUTPUT$' >>/home/cl/nagios/dist/libexec/alert_sender.log 2>&1 &
}
define command{
command_name host-notify-by-email
command_line /home/cl/nagios/dist/libexec/alert_sender.sh 'Host $HOSTSTATE$ alert for $HOSTNAME$!' '***** Nagios *****\n\nNotification Type: $NOTIFICATIONTYPE$\nHost: $HOSTNAME$\nState: $HOSTSTATE$\nAddress: $HOSTADDRESS$\nInfo: $HOSTOUTPUT$\n\nDate/Time: $LONGDATETIME$\n' >>/home/cl/nagios/dist/libexec/alert_sender.log 2>&1 &
}
define contact{
contact_name nagios-admin
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 [email protected]
}
define contactgroup{
contactgroup_name admins
alias Nagios Administrators
members nagios-admin
}
在两个Notify的command中,我们都调用了同一个脚本/home/cl/nagios/dist/libexec/alert_sender.sh。
注意,脚本引用的所有的参数一定要用单引号”' “引起来。
另外,在contact中定义的email地址其实已经没有实际意义,因为Notify的脚本调用的是自己的配置文件。
3、resource.cfg
此配置文件中,主要定义资源相关的选项,本例中,只定义了plugins所在的目录位置。
内容如下:
$USER1$=/home/cl/nagios/dist/libexec
4、cgi.cfg
此配置文件中,定义了cgi相关的选项,诸如配置文件位置,HTML文件位置,URL,系统安全性等。
内容如下:
main_config_file=/home/cl/nagios/dist/etc/nagios.cfg
physical_html_path=/home/cl/nagios/dist/share
url_html_path=/nagios
show_context_help=0
#以下修改配置CGI需要认证,且用户nagios用于全部权限。
use_authentication=1
authorized_for_system_information=nagios
authorized_for_configuration_information=nagios
authorized_for_system_commands=nagios
authorized_for_all_services=nagios
authorized_for_all_hosts=nagios
authorized_for_all_service_commands=nagios
authorized_for_all_host_commands=nagios
default_statusmap_layout=5
default_statuswrl_layout=4
ping_syntax=/bin/ping -n -U -c 5 $HOSTADDRESS$
refresh_rate=90
5、commands.cfg
此文件中为nagios进行系统检测所使用的command。每个命令,都可以跟-h参数来获得使用说明。
除了默认的命令,用户可以根据自己的需要进行相应的开发。开发的规范,可以参阅Nagios的官方文档。
内容如下:
define command{
command_name check-host-alive
command_line $USER1$/check_ping -H $HOSTADDRESS$ -w 99,99% -c 100,100% -p 1
}
define command{
command_name check_ping
command_line $USER1$/check_ping -H $HOSTADDRESS$ -w $ARG1$ -c $ARG2$ -p 5
}
define command{
command_name check_local_disk
command_line $USER1$/check_disk -w $ARG1$ -c $ARG2$ -p $ARG3$
}
define command{
command_name check_local_users
command_line $USER1$/check_users -w $ARG1$ -c $ARG2$
}
define command{
command_name check_local_procs
command_line $USER1$/check_procs -w $ARG1$ -c $ARG2$
}
define command{
command_name check_local_load
command_line $USER1$/check_load -w $ARG1$ -c $ARG2$
}
define command{
command_name check_dns
command_line $USER1$/check_dns -H www.yahoo.com -s $HOSTADDRESS$
}
define command{
command_name check_ftp
command_line $USER1$/check_ftp -H $HOSTADDRESS$
}
define command{
command_name check_hpjd
command_line $USER1$/check_hpjd -H $HOSTADDRESS$ -C public
}
define command{
command_name check_http
command_line $USER1$/check_http -H $HOSTADDRESS$ -u $ARG1$ -w $ARG2$ -c $ARG3$
}
define command{
command_name check_nntp
command_line $USER1$/check_nntp -H $HOSTADDRESS$
}
define command{
command_name check_pop
command_line $USER1$/check_pop -H $HOSTADDRESS$ -w $ARG1$ -c $ARG2$
}
define command{
command_name check_smtp
command_line $USER1$/check_smtp -H $HOSTADDRESS$ -w $ARG1$ -c $ARG2$
}
define command{
command_name check_tcp
command_line $USER1$/check_tcp -H $HOSTADDRESS$ -p $ARG1$
}
define command{
command_name check_telnet
command_line $USER1$/check_tcp -H $HOSTADDRESS$ -p 23
}
define command{
command_name check_udp
command_line $USER1$/check_udp -H $HOSTADDRESS$ -p $ARG1$
}
#为了安全原因,我的nrpe没有跑在默认端口,因此在此指定端口为7866。
define command{
command_name check_nrpe
command_line $USER1$/check_nrpe -H $HOSTADDRESS$ -p 7866 -c $ARG1$
}
以check_smtp命令为例,“$USER1$/check_smtp -H $HOSTADDRESS$ -w $ARG1$ -c $ARG2$“ 中,$HOSTADDRESS$在使用中将被替换为引用主机的IP地址,”-w $ARG1$ -c $ARG2$”将会被替换为warning及critical的阀值。这样在hosts/services中配置使用命令,将会简单的多。
6、hosts.cfg
此配置文件中,定义了所有将会被监控的主机。
内容如下:
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
register 0 ; DONT REGISTER THIS DEFINITION - ITS NOT A REAL HOST, JUST A TEMPLATE!
}
define host{
use generic-host ; Name of host template to use
host_name www
alias www
address 202.106.196.45
check_command check-host-alive
max_check_attempts 10
notification_interval 120
notification_period 24x7
notification_options d,r
contact_groups admins
}
define host{
use generic-host ; Name of host template to use
host_name clhost
alias clhost
address 202.106.196.159
check_command check-host-alive
max_check_attempts 10
notification_interval 120
notification_period 24x7
notification_options d,r
contact_groups admins
}
define host{
use generic-host ; Name of host template to use
host_name mlserver
alias mlserver
address 202.106.196.60
check_command check-host-alive
max_check_attempts 10
notification_interval 120
notification_period 24x7
notification_options d,r
contact_groups admins
}
define host{
use generic-host ; Name of host template to use
host_name cncw
alias cncw
address 202.106.196.61
check_command check-host-alive
max_check_attempts 10
notification_interval 120
notification_period 24x7
notification_options d,r
contact_groups admins
}
define host{
use generic-host ; Name of host template to use
host_name db
alias db
address 202.106.196.146
check_command check-host-alive
max_check_attempts 10
notification_interval 120
notification_period 24x7
notification_options d,r
contact_groups admins
}
define host{
use generic-host ; Name of host template to use
host_name bjmc
alias bjmc
address 202.106.196.58
check_command check-host-alive
max_check_attempts 10
notification_interval 120
notification_period 24x7
notification_options d,r
contact_groups admins
}
define host{
use generic-host ; Name of host template to use
host_name bjlt
alias bjlt
address 202.106.196.157
check_command check-host-alive
max_check_attempts 10
notification_interval 120
notification_period 24x7
notification_options d,r
contact_groups admins
}
define host{
use generic-host ; Name of host template to use
host_name www-i
alias www-i
address 10.10.0.104
check_command check-host-alive
max_check_attempts 10
notification_interval 120
notification_period 24x7
notification_options d,r
contact_groups admins
}
define host{
use generic-host ; Name of host template to use
host_name clhost-i
alias clhost-i
address 10.10.0.101
check_command check-host-alive
max_check_attempts 10
notification_interval 120
notification_period 24x7
notification_options d,r
contact_groups admins
}
define host{
use generic-host ; Name of host template to use
host_name mlserver-i
alias mlserver-i
address 10.10.0.102
check_command check-host-alive
max_check_attempts 10
notification_interval 120
notification_period 24x7
notification_options d,r
contact_groups admins
}
define host{
use generic-host ; Name of host template to use
host_name cncw-i
alias cncw-i
address 10.10.0.112
check_command check-host-alive
max_check_attempts 10
notification_interval 120
notification_period 24x7
notification_options d,r
contact_groups admins
}
define host{
use generic-host ; Name of host template to use
host_name db-i
alias db-i
address 10.10.0.103
check_command check-host-alive
max_check_attempts 10
notification_interval 120
notification_period 24x7
notification_options d,r
contact_groups admins
}
define host{
use generic-host ; Name of host template to use
host_name bjmc-i
alias bjmc-i
address 10.10.0.111
check_command check-host-alive
max_check_attempts 10
notification_interval 120
notification_period 24x7
notification_options d,r
contact_groups admins
}
define host{
use generic-host ; Name of host template to use
host_name bjlt-i
alias bjlt-i
address 10.10.0.113
check_command check-host-alive
max_check_attempts 10
notification_interval 120
notification_period 24x7
notification_options d,r
contact_groups admins
}
define host{
use generic-host ; Name of host template to use
host_name gateway
alias gateway
address 202.106.196.1
check_command check-host-alive
max_check_attempts 10
notification_interval 120
notification_period 24x7
notification_options d,r
contact_groups admins
}
define hostgroup{
hostgroup_name Servers-e
alias External Servers
members cncw,mlserver,db,www,clhost,bjmc,bjlt
}
define hostgroup{
hostgroup_name Servers-i
alias Internal Servers
members cncw-i,mlserver-i,db-i,www-i,clhost-i,bjmc-i,bjlt-i
}
在配置中,可以使用模板(use generic-host)的形式减少配置的工作量。各个主机也可以根据具体情况,配置max_check_attempts notification_interval 等选项。而hostgroup的定义,可以使得相类似的主机,在同一视图中得到体现。
7、services.cfg
此配置文件中,定义了所有将会被监控的服务。
内容如下:
define service{
name generic-service ; The 'name' of this service template
active_checks_enabled 1 ; Active service checks are enabled
passive_checks_enabled 1 ; Passive service checks are enabled/accepted
parallelize_check 1 ; Active service checks should be parallelized (disabling this can lead to major performance problems)
obsess_over_service 1 ; We should obsess over this service (if necessary)
check_freshness 0 ; Default is to NOT check service 'freshness'
notifications_enabled 1 ; Service notifications are enabled
event_handler_enabled 1 ; Service 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
register 0 ; DONT REGISTER THIS DEFINITION - ITS NOT A REAL SERVICE, JUST A TEMPLATE!
}
define service{
use generic-service ; Name of service template to use
host_name cncw,mlserver,db,www,clhost,bjmc,bjlt,cncw-i,mlserver-i,db-i,www-i,clhost-i,bjmc-i,bjlt-i,gateway
service_description PING
is_volatile 0
check_period 24x7
max_check_attempts 1
normal_check_interval 1
retry_check_interval 1
contact_groups admins
notification_options w,u,c,r
notification_interval 960
notification_period 24x7
check_command check_ping!100.0,20%!500.0,60%
}
define service{
use generic-service ; Name of service template to use
host_name mlserver-i
service_description Root Partition
is_volatile 0
check_period 24x7
max_check_attempts 4
normal_check_interval 5
retry_check_interval 1
contact_groups admins
notification_options w,u,c,r
notification_interval 960
notification_period 24x7
check_command check_local_disk!20%!10%!/
}
define service{
use generic-service ; Name of service template to use
host_name mlserver-i
service_description Current Users
is_volatile 0
check_period 24x7
max_check_attempts 4
normal_check_interval 5
retry_check_interval 1
contact_groups admins
notification_options w,u,c,r
notification_interval 960
notification_period 24x7
check_command check_local_users!20!50
}
define service{
use generic-service ; Name of service template to use
host_name mlserver-i
service_description Total Processes
is_volatile 0
check_period 24x7
max_check_attempts 4
normal_check_interval 5
retry_check_interval 1
contact_groups admins
notification_options w,u,c,r
notification_interval 960
notification_period 24x7
check_command check_local_procs!250!400
}
define service{
use generic-service ; Name of service template to use
host_name mlserver-i
service_description Current Load
is_volatile 0
check_period 24x7
max_check_attempts 4
normal_check_interval 5
retry_check_interval 1
contact_groups admins
notification_options w,u,c,r
notification_interval 960
notification_period 24x7
check_command check_local_load!5.0,4.0,3.0!10.0,6.0,4.0
}
define service{
use generic-service ; Name of service template to use
host_name mlserver,mlserver-i
service_description Check Pop
is_volatile 0
check_period 24x7
max_check_attempts 4
normal_check_interval 5
retry_check_interval 1
contact_groups admins
notification_options w,u,c,r
notification_interval 960
notification_period 24x7
check_command check_pop!3!5
}
define service{
use generic-service ; Name of service template to use
host_name mlserver,mlserver-i
service_description Check Smtp
is_volatile 0
check_period 24x7
max_check_attempts 4
normal_check_interval 5
retry_check_interval 1
contact_groups admins
notification_options w,u,c,r
notification_interval 960
notification_period 24x7
check_command check_smtp!3!5
}
define service{
use generic-service ; Name of service template to use
host_name mlserver, mlserver-i
service_description Check Http
is_volatile 0
check_period 24x7
max_check_attempts 4
normal_check_interval 5
retry_check_interval 1
contact_groups admins
notification_options w,u,c,r
notification_interval 960
notification_period 24x7
check_command check_http!/!1!3
}
define service{
use generic-service ; Name of service template to use
host_name www,www-i
service_description Check Http
is_volatile 0
check_period 24x7
max_check_attempts 4
normal_check_interval 5
retry_check_interval 1
contact_groups admins
notification_options w,u,c,r
notification_interval 960
notification_period 24x7
check_command check_http!/!1!3
}
define service{
use generic-service ; Name of service template to use
host_name clhost,clhost-i
service_description Check Http
is_volatile 0
check_period 24x7
max_check_attempts 4
normal_check_interval 5
retry_check_interval 1
contact_groups admins
notification_options w,u,c,r
notification_interval 960
notification_period 24x7
check_command check_http!/sms/tv!1!3
}
define service{
use generic-service ; Name of service template to use
host_name cncw-i,db-i,www-i,clhost-i,db-i
service_description Check Nrpe Users
is_volatile 0
check_period 24x7
max_check_attempts 4
normal_check_interval 5
retry_check_interval 1
contact_groups admins
notification_options w,u,c,r
notification_interval 960
notification_period 24x7
check_command check_nrpe!check_users
}
define service{
use generic-service ; Name of service template to use
host_name cncw-i,db-i,www-i,clhost-i,db-i
service_description Check Nrpe Load
is_volatile 0
check_period 24x7
max_check_attempts 4
normal_check_interval 5
retry_check_interval 1
contact_groups admins
notification_options w,u,c,r
notification_interval 960
notification_period 24x7
check_command check_nrpe!check_load
}
define service{
use generic-service ; Name of service template to use
host_name cncw-i,db-i,www-i,clhost-i,db-i
service_description Check Nrpe Procs
is_volatile 0
check_period 24x7
max_check_attempts 4
normal_check_interval 5
retry_check_interval 1
contact_groups admins
notification_options w,u,c,r
notification_interval 960
notification_period 24x7
check_command check_nrpe!check_total_procs
}
define service{
use generic-service ; Name of service template to use
host_name cncw-i,db-i,www-i,clhost-i,db-i
service_description Check Nrpe Zombie Procs
is_volatile 0
check_period 24x7
max_check_attempts 4
normal_check_interval 5
retry_check_interval 1
contact_groups admins
notification_options w,u,c,r
notification_interval 960
notification_period 24x7
check_command check_nrpe!check_zombie_procs
}
define service{
use generic-service ; Name of service template to use
host_name cncw-i,db-i,www-i,clhost-i,db-i
service_description Check Nrpe Root Partition
is_volatile 0
check_period 24x7
max_check_attempts 4
normal_check_interval 5
retry_check_interval 1
contact_groups admins
notification_options w,u,c,r
notification_interval 960
notification_period 24x7
check_command check_nrpe!check_diskR
}
define service{
use generic-service ; Name of service template to use
host_name cncw-i,db-i,www-i,clhost-i,db-i
service_description Check Nrpe Usr Partition
is_volatile 0
check_period 24x7
max_check_attempts 4
normal_check_interval 5
retry_check_interval 1
contact_groups admins
notification_options w,u,c,r
notification_interval 960
notification_period 24x7
check_command check_nrpe!check_diskU
}
define service{
use generic-service ; Name of service template to use
host_name cncw-i,db-i,www-i,clhost-i,db-i
service_description Check Nrpe Home Partition
is_volatile 0
check_period 24x7
max_check_attempts 4
normal_check_interval 5
retry_check_interval 1
contact_groups admins
notification_options w,u,c,r
notification_interval 960
notification_period 24x7
check_command check_nrpe!check_diskH
}
define service{
use generic-service ; Name of service template to use
host_name cncw-i,db-i,www-i,clhost-i,db-i
service_description Check Nrpe Var Partition
is_volatile 0
check_period 24x7
max_check_attempts 4
normal_check_interval 5
retry_check_interval 1
contact_groups admins
notification_options w,u,c,r
notification_interval 960
notification_period 24x7
check_command check_nrpe!check_diskV
}
define service{
use generic-service ; Name of service template to use
host_name cncw-i,db-i,www-i,clhost-i,db-i
service_description Check Nrpe Backup Partition
is_volatile 0
check_period 24x7
max_check_attempts 4
normal_check_interval 5
retry_check_interval 1
contact_groups admins
notification_options w,u,c,r
notification_interval 960
notification_period 24x7
check_command check_nrpe!check_diskB
}
define service{
use generic-service ; Name of service template to use
host_name cncw-i,db-i,www-i,clhost-i,db-i
service_description Check Nrpe Data Partition
is_volatile 0
check_period 24x7
max_check_attempts 4
normal_check_interval 5
retry_check_interval 1
contact_groups admins
notification_options w,u,c,r
notification_interval 960
notification_period 24x7
check_command check_nrpe!check_diskD
}
define servicegroup{
servicegroup_name MailServices
alias MailServices
members mlserver,Check Pop,mlserver,Check Smtp,mlserver,Check Http
}
define servicegroup{
servicegroup_name WebServices
alias WebServices
members mlserver,Check Http,www,Check Http,clhost,Check Http
}
同hosts的配置一样,services也可以使用模板(use generic-service )的形式减少配置的工作量。各个Service也可以根据具体情况,配置max_check_attempts notification_interval 等选项。而servicegroup的定义,可以使得相类似的服务,在同一视图中得到体现。
以服务Check Http为例,其命令(check_command) check_http!/!1!3将会被解释为 check -H HostIP -u / -w 1 -c 3,就是对主机的缺省页面(/)进行访问,Warning的阀值为1s,Critical的阀值为3s。
另外check_nrpe中所是使用的命令,不是在本地的commands.cfg中配置的。这些命令,都是远程nrpe主机上的,需要在下面讨论的nrpe.cfg中配置。
(二)Httpd(Apache)部分
此例中使用RedHat默认安装的Httpd Server。
1、nagios.conf
此文件配置cgi/html文件的目录及相关的认证选项等。只要将此文件放置在/etc/httpd/conf.d目录下,Apache就会自动加载并处理该文件。
内容如下:
ScriptAlias /nagios/cgi-bin /home/cl/nagios/dist/sbin
<Directory "/home/cl/nagios/dist/sbin">
AllowOverride AuthConfig
Options ExecCGI
Order allow,deny
Allow from all
</Directory>
Alias /nagios /home/cl/nagios/dist/share
<Directory "/home/cl/nagios/dist/share">
Options None
AllowOverride AuthConfig
Order allow,deny
Allow from all
</Directory>
2、.htaccess
此文件为web认证的配置文件,需要放在Nagios html(home/cl/nagios/dist/sbin)/cgi(home/cl/nagios/dist/share)所在的目录下。
内容如下:
AuthName "Nagios Access"
AuthType Basic
AuthUserFile /home/cl/nagios/dist/etc/htpasswd.users
require valid-user
3、htpasswd.users
此文件为web认证的用户名及密码,其文件的位置需要与.httaccess文件中配置的一致,并且可以被httpd所访问。文件采用以下的命令生成:htpasswd -c /home/cl/nagios/dist/etc/htpasswd.users nagios,之后输入nagios的密码就可以了。
内容如下:
nagios:xxxxxxxxxxx
(三)Nrpe部分
此例中,Nrpe安装后配置文件所在的目录为/home/cl/nagios/nrpe/etc。
1、nrpe.cfg
此配置文件,配置了所有nrpe Deamon所需要的环境及check_nrpe中所引用的命令。
pid_file=/home/cl/nagios/nrpe/var/run/nrpe.pid
server_port=7866
nrpe_user=nagios
nrpe_group=nagios
allowed_hosts=127.0.0.1,10.0.0.102,202.106.196.60
dont_blame_nrpe=0
debug=0
command_timeout=60
command[check_users]=/home/cl/nagios/nrpe/libexec/check_users -w 5 -c 10
command[check_load]=/home/cl/nagios/nrpe/libexec/check_load -w 15,10,5 -c 30,25,20
command[check_diskR]=/home/cl/nagios/nrpe/libexec/check_disk -w 500 -c 200 -p /
command[check_diskH]=/home/cl/nagios/nrpe/libexec/check_disk -w 500 -c 200 -p /home
command[check_diskU]=/home/cl/nagios/nrpe/libexec/check_disk -w 500 -c 200 -p /usr
command[check_diskV]=/home/cl/nagios/nrpe/libexec/check_disk -w 500 -c 200 -p /var
command[check_diskB]=/home/cl/nagios/nrpe/libexec/check_disk -w 1000 -c 500 -p /backup
command[check_diskD]=/home/cl/nagios/nrpe/libexec/check_disk -w 1000 -c 500 -p /data
command[check_zombie_procs]=/home/cl/nagios/nrpe/libexec/check_procs -w 5 -c 10 -s Z
command[check_total_procs]=/home/cl/nagios/nrpe/libexec/check_procs -w 150 -c 200
用户为nagios,端口采用的是7866,没有采用默认端口。allowed_hosts参数配置的,允许对自己访问的主机列表为:127.0.0.1,10.0.0.102,202.106.196.60,其中Nagios端的IP地址必须在此之内,否则check_nrpe命令的访问将会被拒绝。
四、Notify脚本
Notify脚本包括三部分:Email通知部分,QQ通知部分,和控制脚本alert_sender.sh。
(一)Email通知部分
此部分包含一个Java jar文件EmailSend.jar和两个配置文件,EmailNotify.cfg及dest.addr。
1、EmailSend.jar
这个文件是我以前写过的一个发送测试Email的Java程序,此次刚好利用了。此程序需要Jdk1.4以上版本的支持,使用方法为:
java -jar EmailSend.jar MtaAddr SrcMailAddr DestMailAddrFile Subject SrcFile SendTimes [strPass]"
如果有人对此文件感兴趣,可以直接用Jad反编译后察看,非常简单的(因为我本身对Java并不熟悉),只是对SMTP Session的操作,或者也可以Email联系我,[email protected] 。
2、EmailNotify.cfg
本文件EmailSend的配置文件。
内容如下:
MtaAddr=mail.163.com
Pass=passwd
DestMailAddrFile=dest.addr
SendTimes=1
Email_Notify="Enable
3、dest.addr
本文件的内容是EmailSend是调用的目的邮件地址。
内容如下:
(二)QQ通知部分
此部分包含TextQQ的程序文件;两个配置文件,QqNotify.cfg及dest.qq;两个脚本文件qq.sh及qq.exp。
其中qq.exp需要Expect环境支持。
1、TextQQ
TextQQ是一个采用LumaQQ核心的Text-Based Qq项目。TextQQ最新版本需要JDK1.5版本支持。
TextQQ在使用前需要更新其配置文件config.ini中的qqno及qqpass字段。详细情况请访问其官方网站。
2、QqNotify.cfg及dest.qq
QqNotify.cfg是TextQQ的配置文件。
内容如下:
Qq_Nofity="Enable"
DestQQFile=dest.qq
dest.qq是Notify目标的qq号码。
内容如下:
31222343
55668789
3、qq.sh及qq.exp
qq.sh是执行TextQQ的一个脚本。主要是为了qq.exp的方便执行。
其内容如下:
#!/bin/sh
cd /home/cl/nagios/dist/libexec/im/qq
./tq.s
qq.exp,是一个执行TextQQ,并发送相关的Notify信息到指定QQ号码Expect脚本。因为TextQQ的执行是一个交互的过程,例如,我们必须要等到TextQQ提示登陆成功之后,才可以发送消息。Expect脚本的主要功能就在这里。更详细的信息,请访问Expect的官方网站,http://expect.nist.gov/。
内容如下:
#!/usr/bin/expect
set qq "[lindex $argv 0]"
set sub "[lindex $argv 1]"
set cont "[lindex $argv 2]"
send_user "Start...\n"
spawn /home/cl/nagios/dist/libexec/im/qq/qq.sh
expect ">"
send "/m $qq Begin:---- $sub ---- $cont ----End. \n"
expect ">"
send "\n"
send "/q \n"
expect "press any key to continue..."
send "\n"
send_user "It's Finished.\n
(三)alert_sender.sh部分
所有的诸如Email/QQ等通知方式,都是通过此脚本的调用来实现。alert_sender.sh根据配置文件来决定是否采用Email/QQ或者其他的Notify方式。
1、脚本内容
其内容如下:
#!/bin/sh
#########################################################################
#
# File: alert_sender.sh
# Description: This script send the alert to appointed person with email, sms or other way.
# Language: GNU Bourne-Again SHell
# Version: 1.5
# Date: 2005-05-29
# First Version write for the Bash Srcipt Monitor System, SysMon:
# Date: 2005-09-16
# Corp.: Chenlei
# Author: [email protected]
#
#########################################################################
############################
#
# Exit values:
# ------------
# 0 successful
# Others failur
#
# ----------------------------------------------------------------------
# These are Parameters from external
#
# 1: Subject
# 2: Alert Info File
Usage="$0 Subject AlertInfo."
if [ $# -ne 2 ]; then
echo "$Usage"
exit -1
fi
. /etc/profile
Subject="$1"
AlertInfo="$2"
ProgDir=`dirname $0`
. $ProgDir/email/EmailNotify.cfg
. $ProgDir/im/qq/QqNofiy.cfg
# .$ProgDir/sms/SmsNofiy.cfg
#####
#####
if [ "$Email_Notify" = "Enable" ]; then
if [ -z "$MtaAddr" ]; then
MtaAddr=mail.163.com
fi
if [ -z "$SrcMailAddr" ]; then
fi
if [ -z "$DestMailAddrFile" ]; then
DestMailAddrFile=$ProgDir/email/dest.addr
fi
if [ -z $SendTimes ]; then
SendTimes=1
fi
cd $ProgDir/email
EAlertInfoFile=/home/cl/nagios/dist/libexec/email/log/TempFile.eml
/usr/bin/printf "%b" "$AlertInfo" > $EAlertInfoFile
if [ -z $Pass ]; then
/usr/java/bin/java -jar EmailSend.jar "$MtaAddr" "$SrcMailAddr" "$DestMailAddrFile" "$Subject" "$EAlertInfoFile" "$SendTimes"
else
/usr/java/bin/java -jar EmailSend.jar "$MtaAddr" "$SrcMailAddr" "$DestMailAddrFile" "$Subject" "$EAlertInfoFile" "$SendTimes" "$Pass"
fi
cd -
echo `date` >> $ProgDir/email/log/OutFile.eml
cat $EAlertInfoFile >>$ProgDir/email/log/OutFile.eml
echo `#################################`>>$ProgDir/email/log/OutFile.eml
fi
#####
# IM
#####
if [ "$Im_Nofity" = "Enable" ]; then
cd $ProgDir/im/qq
IAlertInfoFile=/home/cl/nagios/dist/libexec/im/qq/log/TempFile.msg
/usr/bin/printf "%b" "$AlertInfo" |grep -v ^$ > $IAlertInfoFile
Msg=""
while read Record
do
Msg="$Msg $Record"
done < $IAlertInfoFile
while read QQ
do
./qq.exp $QQ "$Subject" "$Msg" > /home/cl/nagios/dist/libexec/im/qq/log/qq.out
done < ./dest.qq
cd -
echo `date` >> $ProgDir/im/qq/log/OutFile.msg
echo $Msg >>$ProgDir/im/qq/log/OutFile.msg
echo `#################################`>>$ProgDir/im/qq/log/OutFile.msg
fi
#####
# Sms
#####
2、简单说明
脚本的第一部分,对传递参数作了判断,并初始化了系统环境;第二部分是Email通知部分,执行EmailSend.jar文件来实现;第三部分是QQ通知部分使用了一个while循环语句来读取dest.qq中的记录(此处有待改善,需要把判断每行记录是否为QQ号码形式),并调用qq.exp来发送。
五、启动
1、Nrpe
Nrpe可以用使用以下的命令来启动:
nrpe -c NrpeConfigFile -d或
/etc/init.d/nrpe start来启动。
2、Nagios
Nagios在启动之前,可以使用命令nagios -v NagiosConfigFile来检验配置文件,如果无误,则可以使用以下的命令启动:
nagios -d NagiosConfigFile
或
/etc/init.d/nagios start
并使用以下的命令来追踪日志文件:
tail -f NagiosHome/var /nagios.log
出现的错误,大多都和不正确的配置有关,查一下官方文档,一般都可以解决。
3、Httpd(Apache)
Nagios需要httpd支持,因此需要启动httpd。Http的启动可以使用以下的命令:
/etc/init.d/httpd start
六、使用及测试
1、登陆
打开浏览器之后,输入主机的Nagios主机ip加上nagios的路径即可,如果你的Nagios主机ip是192.168.200.172,那么输入http://192.168.200.172/nagios就可以了。
如下图所示:
只要输入正确的用户名/密码,即可登陆。
2、功能截图
以下是一些页面的截图:
3、测试
可以通过修改一些主机的IP地址或修改Warning及Critical的阀值等形式,让Nagios产生Alert及Notification,来验证Nagios的展示及告警通知等功能。
附录:
文档列表:
以上文中所列出的配置文件及脚本,皆在我的系统的真实环境测试通过,可以经过简单修改后使用。但在此生命,并不保证在所有的环境下皆能正常运行。
Nagios部分:
nagios.cfg
minimal.cfg
resource.cfg
cgi.cfg
commands.cfg
hosts.cfg
services.cfg
Httpd部分:
nagios.confg
.htaccess
htpasswd.users
Nrpe部分:
nrpe.cfg
Notify部分:
EmailSend.jar
EmailNotify.cfg
dest.addr
TextQQ
QqNotify.cfg
dest.qq
qq.sh
qq.exp
alert_sender.sh