Nagios3.2安装详解
1.Nagios简介
Nagios是一款遵循GPLv2的开源网络监控软件,可用来监控指定的多种系统的主机、服务,并可在它们的工作状态发生变化时通知管理员。它的特性包括:
1)监控网络服务(HTTP、POP3、SMTP、PING、MySQL等)
2)监控主机资源(磁盘空间利用率、内存利用率、CPU负载等)
3)简洁的插件设计接口,使得用户可以轻松开发所需的检测脚本
4)并行服务模式
5)轻松描述网络结构,并且能够区辨“宕机”和“主机不可达”
6)通过邮件或用户自定义的方式将主机或服务的工作状态变化情况通知给管理员
7)自动日志滚动
8) 支持以冗余方式进行主机监控
9)可以通过web方式直观的查看当前网络状态、通知和问题历史、日志文件等等,此组件为可选.
Nagios通常由一个主程序(Nagios)、一个插件程序(Nagios-plugins)和四个可选的
ADDON(NRPE、NSCA、NSClient++和NDOUtils)组成。Nagios的监控工作都是通过插件实现的,因此,Nagios和Nagios-plugins是服务器端工作所必须的组件。而四个ADDON中,NRPE用来在监控的远程Linux/Unix主机上执行脚本插件以实现对这些主机资源的监控;NSCA用来让被监控的远程Linux/Unix 主机主动将监控信息发送给Nagios服务器(这在冗余监控模式中特别要用到);NSClient++是用来监控Windows主机时安装在Windows主机上的组件;而NDOUtils则用来将Nagios的配置信息和各event产生的数据存入数据库,以实现这些数据的快速检索和处理。这四个ADDON(附件)中,NRPE和NSClient++工作于客户端,NDOUtils工作于服务器端,而NSCA则需要同时安装在服务器端和客户端。
目前,Nagios只能安装在Linux系统主机上,其编译需要用到gcc。同时,如果打算使用web界面的管理工具的话,还需要有apache服务器和GD图形库的支持。
2.安装前的准备工作
2.1解决安装Nagios的依赖关系
Nagios基本组件的运行依赖于httpd、gcc和gd。可以通过以下命令来检查nagios所依赖的rpm包是否已经完全安装:
# rpm -q httpd gcc glibc glibc-common gd gd-devel
对于缺少的rpm包,您可以挂载光盘直接使用rpm命令进行安装。如果不想总是被rpm的依赖关系所困扰,您可以通过yum结合本地源来实现rpm包的管理。对于本地源的建立,可以参考笔者的另外一篇关于yum的文章。
说明:以上软件包您也可以通过编译源代码的方式安装,只是后面许多要用到的相关文件的路径等需要按照您的源代码安装时的配置逐一修改。
2.2添加nagios运行所需要的用户和组
# groupadd nagcmd
# useradd -m nagios
# usermod -a -G nagcmd nagios
把apache加入到nagcmd组,以便于在通过web Interface操作nagios时能够具有足够的权限:# usermod -a -G nagcmd apache
3.编译安装nagios
# tar zxf nagios-3.1.2.tar.gz
# cd nagios-3.1.2
# ./configure --with-command-group=nagcmd (为了后面suexec省事可加上
-prefix=/var/www/nagios,后面nagios的路径就成为/var/www/nagios)
# make all
# make install
# make install-init
# make install-config
# make install-commandmode
为email指定您想用来接收nagios警告信息的邮件地址,默认是本机的nagios用户:
# vi /usr/local/nagios/etc/objects/contacts.cfg
email nagios@localhost #这个是默认设置
在httpd的配置文件目录(conf.d)中创建Nagios的Web程序配置文件:
# make install-webconf
创建一个登录nagios web程序的用户,这个用户帐号在以后通过web登录nagios认证时所用:# htpasswd -c /usr/local/nagios/etc/https://www.sodocs.net/doc/3515127159.html,ers nagiosadmin
以上过程配置结束以后需要重新启动httpd:
# service httpd restart
4.编译、安装nagios-plugins
nagios的所有监控工作都是通过插件完成的,因此,在启动nagios之前还需要为其安装官方提供的插件。
# tar zxf nagios-plugins-1.4.13.tar.gz
# cd nagios-plugins-1.4.13
# ./configure --with-nagios-user=nagios --with-nagios-group=nagios
--enable-redhat-pthread-workaround
# make
# make install
5.配置并启动Nagios
(1)把nagios添加为系统服务并将之加入到自动启动服务队列:
# chkconfig --add nagios
# chkconfig nagios on
(2)检查其主配置文件的语法是否正确:
# /usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg
(3)如果上面的语法检查没有问题,接下来就可以正式启动nagios服务了:
# service nagios start
(4)配置selinux
如果您的系统开启了selinux服务,则默认为拒绝nagios web cgi程序的运行。您可以通过下面的命令来检查您的系统是否开启了selinux:
#getenforce
如果上面命令的结果显示开启了selinux服务,您可以通过下面的命令暂时性的将其关闭:
#setenforce 0
如果您想在以后完全关闭selinux,可以通过编辑/etc/sysconfig/selinux文件,将其中的selinux后面的值“force”修改为“disable”即可。
当然,您也可以通过以下方式将nagios的CGI程序运行于SELinux/targeted模式而不用关闭
selinux:
# chcon -R -t httpd_sys_content_t /usr/local/nagios/sbin
# chcon -R -t httpd_sys_content_t /usr/local/nagios/share
(5)配置WEB接口:
编辑apache 的httpd.conf:
ScriptAlias /nagios/cgi-bin "/usr/local/nagios/sbin"
Options ExecCGI
AllowOverride None
Order allow,deny
Allow from all
AuthName "Nagios Access"
AuthType Basic
AuthUserFile /usr/local/nagios/etc/https://www.sodocs.net/doc/3515127159.html,ers
Require valid-user
Alias /nagios "/usr/local/nagios/share"
Options None
AllowOverride None
Order allow,deny
Allow from all
AuthName "Nagios Access"
AuthType Basic
AuthUserFile /usr/local/nagios/etc/https://www.sodocs.net/doc/3515127159.html,ers
Require valid-user
(6)修改配置文件
Nagios的主配置文件是nagios.cfg,我们就从这个文件开始修改。用vi编辑nagios.cfg,注释行#cfg_file=/usr/local/nagios/etc/localhost.cfg,然后把下面几行的注释去掉:
cfg_file=/usr/local/nagios/etc/objects/command.cfg
cfg_file=/usr/local/nagios/etc/objects/contactgroups.cfg //联系组配置文件路径
cfg_file=/usr/local/nagios/etc/objects/contacts.cfg //联系人配置文件路径
cfg_file=/usr/local/nagios/etc/objects/hostgroups.cfg //主机组配置文件路径
cfg_file=/usr/local/nagios/etc/objects/hosts.cfg //主机配置文件路径
cfg_file=/usr/local/nagios/etc/objects/services.cfg //服务配置文件路径
cfg_file=/usr/local/nagios/etc/objects/timeperiods.cfg //监视时段配置文件路径
改check_external_commands=0为check_external_commands=1 这行的作用是允许执行在web界面下重启nagios、停止主机/服务检查等操作。把command_check_interval的值从默认的1改成command_check_interval=10s(根据自己的情况定这个命令检查时间间隔,不要太长也不要太短)。主配置文件要改的基本上就是这些,通过上面的修改,发现/usr/local/nagios/etc并没有文件hosts.cfg等一干文件,怎么办?稍后手动创建它们。
第2个要修改的配置文件是cgi.cfg,它的作用是控制相关cgi脚本。先确保use_authentication=1。曾看过不少的文章,都是建议把use_authentication的值设置成”0”来取消验证,这是一个十分糟糕的想法。接下来修改default_user_name=sery ,再后面的修改在下表列出:
authorized_for_system_information=nagiosadmin,sery
authorized_for_configuration_information=nagiosadmin,sery
authorized_for_system_commands=nagiosadmin,sery //多个用户之间用逗号隔开authorized_for_all_services=nagiosadmin,sery
authorized_for_all_hosts=nagiosadmin,sery
authorized_for_all_service_commands=nagiosadmin,sery
authorized_for_all_host_commands=nagiosadmin,sery
那么上述用户名打那里来的呢?是执行命令/usr/local/apache/bin/htpasswd –c
/usr/local/nagios/etc/htpasswd sery 所生成的,这个要注意,不能随便加没有存在的验证用户,为了安全起见,不要添加过多的验证用户。
第3个修改的配置文件是commands.cfg,这个文件的主要功能是用来发送报警短信和报警邮件,对其的修改如下所示:
#host-notify-by-sms //发送短信报警
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 //发送短信报警
define command {
command_name service-notify-by-sms
command_line /usr/local/bin/sms_send
"'$HOSTADDRESS$'$HOSTALIAS$/$SERVICEDESC$ is $SERVICESTATE$" $CONTACTPAGER$
}
主机和服务的邮件报警通知已经在文件中,不须更改。也可以把短信和邮件报警通知这些配置块写到文件commands.cfg中,效果是一样的。
本人的环境是发送特定的标题到特定的邮箱,邮箱就会push到手机,所以,就只需要在command.cfg 下面修改相应的'notify-by-email'和'host-notify-by-email'就行
增加新的配置文件
先创建简单的配置文件timeperiods.cfg,其内容如下:
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
}
第二个手动创建的配置文件是contacts.cfg,其格式如下:
# In this simple config file, a single contact will receive all alerts.
define contact{
contact_name nagiosadmin
alias nagiosadmin
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-notify-by-sms 如果是通过别的方式报警可以添加
host_notification_commands host-notify-by-email,host-notify-by-sms email kernel@https://www.sodocs.net/doc/3515127159.html,
pager 136811234567
}
define contact {
contact_name sery
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 notify-by-email
host_notification_commands host-notify-by-email
haishi24@https://www.sodocs.net/doc/3515127159.html,
pager 133********
}
上面的文件定义了2个联系人,如果有更多联系人的话,照这个格式在后面追加即可。服务通知选项(service_notification_options)与主机通知选项(host_notification_options)的几个选项在这里说明一下:w-warning , u-unknown,c-critical,r-recovery;d-down,u-unreachable,注意一下,主机报警和服务报警有些差异。
紧接着的第三个手动创建的配置文件是contactgroups.cfg文件,这个文件是依照上一个文件
contacts.cfg来的,contactgroups文件相对简单一些,其格式如下:
define contactgroup {
contactgroup_name nagios
alias system administrator group
members nagiosadmin,sery
}
多个成员之间用逗号做分界符,如果有更多的联系组,就依相同的格式在文件中追加余下的组。
配置文件hosts.cfg:
#define monitor host
##################################################### ############
# HOSTS #
##################################################### ############
# Generic host definition template - This is NOT a real host, just a template!(模版)define host{
name linux-server
notifications_enabled 1
event_handler_enabled 1
flap_detection_enabled 1
failure_prediction_enabled 1
process_perf_data 1
retain_status_information 1
retain_nonstatus_information 1
notification_period 24x7
register 0
}
##################################################### ############
# web17 #
##################################################### ############
define host{
use linux-server (上面模版定义的主机名)
host_name web17
alias web17
address 10.0.0.74
check_command check-host-alive
max_check_attempts 5
notification_interval 60
notification_period 24x7
contact_groups nagios
notification_options d,u,r
}
更多的主机依此格式逐个追加进来。为了以后维护方便,尽可能在文件中使用易读的注释(如本例# web17 #)。
配置文件services.cfg:
#service definition
##################################################### #########
# SERVICES #
##################################################### #########
# Generic service definition template - This is NOT a real service, just a template!(模版)define service{
name linux-service
active_checks_enabled 1
passive_checks_enabled 1
parallelize_check 1
obsess_over_service 1
check_freshness 0
notifications_enabled 1
event_handler_enabled 1
flap_detection_enabled 1
failure_prediction_enabled 1
process_perf_data 1
retain_status_information 1
retain_nonstatus_information 1
is_volatile 0
register 0
}
##################################################### #########
# web17 #
##################################################### #########
# Define a service to "ping" the remote machine
define service{
use linux-service
host_name web17
service_description CURRENT LOAD
check_period 24x7
max_check_attempts 5
normal_check_interval 1
retry_check_interval 1
contact_groups nagios
notification_interval 60
notification_period 24x7
notification_options w,u,c,r
check_command check_nrpe!check_load!30,25,20!40,35,30
}
# Define a service to check the disk space of the all partition
# on the remote machine. Warning if
define service{
use linux-service
host_name web17
service_description disk_var
check_period 24x7
max_check_attempts 5
normal_check_interval 30
retry_check_interval 10
contact_groups nagios
notification_interval 60
notification_period 24x7
notification_options w,u,c,r
check_command check_nrpe!check_disk_var!20%!10%
}
更多的主机及服务依此格式逐个追加进来。
主机组配置文件hostgroups.cfg,这是一个可选的项目,它建立在文件hosts之上,其格式如下:define hostgroup {
hostgroup_name web server
alias web server
members web17 //本例用了一个主机
}
运行程序/usr/local/nagios –v /usr/local/nagios/etc/nagios.cfg来检查所有配置文件的正确性。运行完毕将在输出尾部出现:
Total Warnings: 0
Total Errors: 0
Things look okay - No serious problems were detected during the pre-flight check
如果出现错误,肯定是cfg配置文件的问题,自己调试解决就可以了(不过,好像比较繁琐)
验证通过以后,就可以执行命令/usr/local/nagios –d /usr/local/nagios/etc/nagios.cfg 把nagios作为守护进程。然后用ps –aux | grep nagios 看进程是否处于运行状态。到这一步,nagios 服务基本上算是配置完毕。做hosts.cfg、services.cfg等配置时,可以运用一些小技巧来减少出错的概率:如先定义少许的主机、服务,待校验无误后再追加。
在验证下
/usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg
没有出错的话!重新启动nagios和apache
(7)通过web界面查看nagios:
http://your_nagios_IP/nagios
登录时需要指定前面设定的web认证帐号和密码。
6.安装NRPE守护进程
# tar xzf nrpe-2.12.tar.gz
# cd nrpe-2.12
编译NRPE插件。
# ./configure -prefix= /usr/local/nagios
# make all
安装NRPE插件,守护进程,守护进程和配置文件.
# make install-plugin
# make install-daemon
# make install-daemon-config
安装NRPE守护进程作为一个服务xinetd 项
# make install-xinetd
编辑在/ etc / xinetd.d / nrpe文件,添加的IP地址监测服务器到only_from 。
# only_from = 127.0.0.1
添加以下条目NRPE守护进程在/ etc /services 档案中以下内容。
# Nrpe 5666/tcp # NRPE
加入到Services文件中
# echo "nrpe 5666/tcp # NRPE" >> /etc/services 重起xinetd 服务。
# service xinetd restart
测试本地NRPE daemon 服务。
看看进程是否工作正常...
查看nrpe守护下运行xinetd 。
# netstat -at | grep nrpe
输出了这个命令应该表现出这样的:
# tcp 0 0 *:nrpe *:* LISTEN
检查以确保NRPE daemon为正常。要做到这一点,运行check_nrpe插件安装测试。
# /usr/local/nagios/libexec/check_nrpe -H localhost
应该返回一个字符串,告诉您的是哪个版本的NRPE安装,
就像这样:
NRPE v2.12
配置:
由于nrpe外构组件,所以必须在commands.cfg中定义(我这里的nagios版本是3.2和2.x的版本有些不一样)
# [root@localhost etc]# vi /usr/local/nagios/etc/objects/commands.cfg
最下面添加
#check nrpe
define command{
command_name check_nrpe
command_line $USER1$/check_nrpe -H $HOSTADDRESS$ -c $ARG1$
}
执行/usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -d