免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 6314 | 回复: 8
打印 上一主题 下一主题

Nagios配置实例文档 [复制链接]

论坛徽章:
3
CU大牛徽章
日期:2013-03-14 14:14:08CU大牛徽章
日期:2013-03-14 14:14:26CU大牛徽章
日期:2013-03-14 14:14:29
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2010-12-17 15:00 |只看该作者 |倒序浏览
本帖最后由 comcn2 于 2010-12-17 21:45 编辑

nagios基本安装配置介绍.pdf (583.55 KB, 下载次数: 226)
NAGIOS 基本安装配置介绍
作者:不死鸟
QQ 群:19481624
特点介绍
安装配置介绍
1、nagios 安装
2、nagios-plguin 安装
3、nagios.cfg、cgi.cfg、创建web 验证用户
4、contacts.cfg、host.cfg、services.cfg、timeperiod.cfg、command
本章的主要内容用一个例子进行讲解
1、主要环境如下:
主机名 操作系统 IP 作用
nagios.tianway.net RHEL 5 192.168.0.1 监控机
Web1.tianway.net RHEL5 192.168.0.2 被监控机
Web2.tianway.net RHEL 5 192.168.0.3 被监控机
Web3.tianway.net RHEL 5 192.168.0.4 被监控机
2、监控内容
主机名 要监控的服务
nagios.tianway.net 是否活动
是否开启ssh
磁盘使用情况
web1.tianway.net 是否活动
是否开启ssh
磁盘使用情况
是否开启web
cpu 负载
swap 分区使用情况
主进程数
蓝色表示外部服务,即不需要登陆到服务器即可进行检测的服务,绿色字体表示内部服务,必
须登陆到服务器才能获取到相关的信息。
3、架构图
当前登陆用户
僵尸进程数
web2.tianway.net 是否活动
是否开启ssh
是否开启web
磁盘使用情况
cpu 负载
swap 分区使用情况
主进程数
当前登陆用户
僵尸进程数
web3.tianway.net 是否活动
是否开启ssh
是否开启web
磁盘使用情况
cpu 负载
swap 分区使用情况
主进程数
当前登陆用户
僵尸进程数
第一节 nagios 基本安装
软件获取
[root@nagios ~]#wget http://www.nagios.com/naigos.3.6.tar.gz
解压缩
[root@nagios ~]# tar xzvf nagios-3.0.6.tar.gz
[root@nagios ~]# cd nagios-3.0.6
编译
[root@nagios nagios-3.0.6]# ./configure --prefix=/usr/local/nagios/
输出如下信息:
*** Configuration summary for nagios 3.0.6 12-01-2008 ***:
General Options:
-------------------------
Nagios executable: nagios
Nagios user/group: nagios,nagios
Command user/group: nagios,nagios
Embedded Perl: no
Event Broker: yes
Install ${prefix}: /usr/local/nagios/
Lock file: ${prefix}/var/nagios.lock
Check result directory: ${prefix}/var/spool/checkresults
Init directory: /etc/rc.d/init.d
Apache conf.d directory: /etc/httpd/conf.d
Mail program: /bin/mail
Host OS: linux-gnu
Web Interface Options:
------------------------
HTML URL: http://localhost/nagios/
CGI URL: http://localhost/nagios/cgi-bin/
Traceroute (used by WAP): /bin/traceroute
Review the options above for accuracy. If they look okay,
type 'make all' to compile the main program and CGIs.
根据输出信息的提示执行:
[root@nagios nagios-3.0.6]# make all
输出如下信息:
*** Compile finished ***
If the main program and CGIs compiled without any errors, you
can continue with installing Nagios as follows (type 'make'
without any arguments for a list of all possible options):
安装nagios 主程序、CGI 和HTML 文件
make install
- This installs the main program, CGIs, and HTML files
安装nagios 的启动脚本,目录是/etc/rc.d/init.d
make install-init
- This installs the init script in /etc/rc.d/init.d
安装外部命令使用的目录,并配置目录权限
make install-commandmode
- This installs and configures permissions on the
directory for holding the external command file
安装样板配置文件,位于/usr/local/nagios//etc 目录下
make install-config
- This installs *SAMPLE* config files in /usr/local/nagios//etc
You'll have to modify these sample files before you can
use Nagios. Read the HTML documentation for more info
on doing this. Pay particular attention to the docs on
object configuration files, as they determine what/how
things get monitored!
安装apache 配置文件,以便能够通过web 页面访问nagios
make install-webconf
- This installs the Apache config file for the Nagios
web interface
*** Support Notes *******************************************
If you have questions about configuring or running Nagios,
please make sure that you:
- Look at the sample config files
- Read the HTML documentation
- Read the FAQs online at http://www.nagios.org/faqs
before you post a question to one of the mailing lists.
Also make sure to include pertinent information that could
help others help you. This might include:
- What version of Nagios you are using
- What version of the plugins you are using
- Relevant snippets from your config files
- Relevant error messages from the Nagios log file
For more information on obtaining support for Nagios, visit:
http://www.nagios.org/support/
*************************************************************
Enjoy.
根据输出结果依次执行如下命令:
[root@nagios nagios-3.0.6]# make install
cd ./base && make install
make[1]: Entering directory `/root/nagios-3.0.6/base'
make install-basic
make[2]: Entering directory `/root/nagios-3.0.6/base'
/usr/bin/install -c -m 775 -o nagios -g nagios -d /usr/local/nagios//bin
/usr/bin/install: invalid user `nagios'
make[2]: *** [install-basic] Error 1
make[2]: Leaving directory `/root/nagios-3.0.6/base'
make[1]: *** [install] Error 2
make[1]: Leaving directory `/root/nagios-3.0.6/base'
make: *** [install] Error 2
可以看出出错原因是没有nagios 用户,我们现在创建nagios 用户,并重新进行安装
[root@nagios nagios-3.0.6]# useradd nagios
[root@nagios nagios-3.0.6]# make install
执行完毕,如下提示:*** Main program, CGIs and HTML files installed ***
You can continue with installing Nagios as follows (type 'make'
without any arguments for a list of all possible options):
make install-init
- This installs the init script in /etc/rc.d/init.d
make install-commandmode
- This installs and configures permissions on the
directory for holding the external command file
make install-config
- This installs sample config files in /usr/local/nagios//etc
make[1]: Leaving directory `/root/nagios-3.0.6'
执行make install-init 安装脚本文件
[root@nagios nagios-3.0.6]# make install-init
/usr/bin/install -c -m 755 -d -o root -g root /etc/rc.d/init.d
/usr/bin/install -c -m 755 -o root -g root daemon-init /etc/rc.d/init.d/nagios
*** Init script installed ***
执行make install-commandmode 安装外部命令的目录,并配置权限
[root@nagios nagios-3.0.6]# make install-commandmode
/usr/bin/install -c -m 775 -o nagios -g nagios -d /usr/local/nagios//var/rw
chmod g+s /usr/local/nagios//var/rw
*** External command directory configured ***
执行make install-config 安装nagios 使用的配置文件样板,你可以根据自己的需求对这些文件
进行修改然后直接使用,这些文件位于/usr/local/nagios//etc 目录下
/usr/bin/install -c -m 775 -o nagios -g nagios -d /usr/local/nagios//etc
/usr/bin/install -c -m 775 -o nagios -g nagios -d /usr/local/nagios//etc/objects
/usr/bin/install -c -b -m 664 -o nagios -g nagios sample-config/nagios.cfg
/usr/local/nagios//etc/nagios.cfg
/usr/bin/install -c -b -m 664 -o nagios -g nagios sample-config/cgi.cfg
/usr/local/nagios//etc/cgi.cfg
/usr/bin/install -c -b -m 660 -o nagios -g nagios sample-config/resource.cfg
/usr/local/nagios//etc/resource.cfg
/usr/bin/install -c -b -m 664 -o nagios -g nagios
sample-config/template-object/templates.cfg
/usr/local/nagios//etc/objects/templates.cfg
/usr/bin/install -c -b -m 664 -o nagios -g nagios
sample-config/template-object/commands.cfg
/usr/local/nagios//etc/objects/commands.cfg
/usr/bin/install -c -b -m 664 -o nagios -g nagios
sample-config/template-object/contacts.cfg
/usr/local/nagios//etc/objects/contacts.cfg
/usr/bin/install -c -b -m 664 -o nagios -g nagios
sample-config/template-object/timeperiods.cfg
/usr/local/nagios//etc/objects/timeperiods.cfg
/usr/bin/install -c -b -m 664 -o nagios -g nagios
sample-config/template-object/localhost.cfg
/usr/local/nagios//etc/objects/localhost.cfg
/usr/bin/install -c -b -m 664 -o nagios -g nagios
sample-config/template-object/windows.cfg
/usr/local/nagios//etc/objects/windows.cfg
/usr/bin/install -c -b -m 664 -o nagios -g nagios
sample-config/template-object/printer.cfg
/usr/local/nagios//etc/objects/printer.cfg
/usr/bin/install -c -b -m 664 -o nagios -g nagios
sample-config/template-object/switch.cfg /usr/local/nagios//etc/objects/switch.cfg
*** Config files installed ***
Remember, these are *SAMPLE* config files. You'll need to read
the documentation for more information on how to actually define
services, hosts, etc. to fit your particular needs.
执行make install-webconf,为nagios 安装a
pache 的配置文件,完成后就可以支持nagios 的web 页面登陆,从而通过web 页面对nagios
进行操作
[root@nagios nagios-3.0.6]# make install-webconf
/usr/bin/install -c -m 644 sample-config/httpd.conf /etc/httpd/conf.d/nagios.conf
*** Nagios/Apache conf file installed ***
至此nagios 安装完毕,做一个简单的检查工作,看看在/usr/local/nagios 目录下是否存在如下子
目录
bin nagios 执行程序所在目录,nagios 文件即为主程序
etc nagios 配置文件所在目录
sbin nagios cgi 文件所在目录,也就是执行外部命令所需文件所在的目录
share nagios 网页文件所在的目录
var nagios 日志文件、spid 等文件所在的目录
var/archive 用于存放存档日志的空目录
var/rw 为外部命令保留的空目录
第二节 插件安装
1、获取软件
[root@nagios ~]# wget http://www.nagios.org/nagios-plugins-1.4.13.tar.gz
2、插件安装
解压缩
[root@nagios ~]# tar xzvf nagios-plugins-1.4.13.tar.gz
[root@nagios ~]# cd nagios-plugins-1.4.13
编译并指定安装路径
[root@nagios nagios-plugins-1.4.13]# ./configure --prefix=/usr/local/nagios
config.status: creating po/Makefile
--with-apt-get-command:
--with-ping6-command: /bin/ping6 -n -U -w %d -c %d %s
--with-ping-command: /bin/ping -n -U -w %d -c %d %s
--with-ipv6: yes
--with-mysql: no
--with-openssl: yes
--with-gnutls: no
--enable-extra-opts: no
--with-perl: /usr/bin/perl
--enable-perl-modules: no
--with-cgiurl: /nagios/cgi-bin
--with-trusted-path: /bin:/sbin:/usr/bin:/usr/sbin
安装
make &&make install
查看/usr/local/nagios/libexec/目录,会显示安装的插件,所有的插件都在这个目录下
[root@nagios nagios-plugins-1.4.13]# ls /usr/local/nagios/libexec/
check_apt check_ide_smart check_nt check_ssh
check_breeze check_ifoperstatus check_ntp check_ssmtp
check_by_ssh check_ifstatus check_ntp_peer check_swap
check_clamd check_imap check_ntp_time check_tcp
check_cluster check_ircd check_nwstat check_time
check_dhcp check_jabber check_oracle check_udp
check_dig check_ldap check_overcr check_ups
check_disk check_ldaps check_ping check_users
check_disk_smb check_load check_pop check_wave
check_dns check_log check_procs library_linux32
check_dummy check_mailq check_real negate
check_file_age check_mrtg check_rpc urlize
check_flexlm check_mrtgtraf check_sensors utils.pm
check_ftp check_nagios check_simap utils.sh
check_http check_nntp check_smtp
check_icmp check_nntps check_spop
把apache 用户加入nagios 组,如果省略这一步会在后续的操作中产生一些问题
[root@nagios #usermod -G nagios apache
基本安装过程结束,下一节进行一个简单的服务监控配置
第三节基本配置
本章的目标是完成nagios 的基本配置并实现对web 服务和ssh 服务的监控
1、实现apache 的身份验证,登陆nagios 的虚拟目录
查看/etc/httpd/conf.d/nagios.cfg 文件
[root@nagios conf.d]# cat nagios.conf
# SAMPLE CONFIG SNIPPETS FOR APACHE WEB SERVER
# Last Modified: 11-26-2005
#
# This file contains examples of entries that need
# to be incorporated into your Apache web server
# configuration file. Customize the paths, etc. as
# needed to fit your system.
ScriptAlias /nagios/cgi-bin "/usr/local/nagios//sbin"
<Directory "/usr/local/nagios//sbin">
# SSLRequireSSL
Options ExecCGI
AllowOverride None
Order allow,deny
Allow from all
AuthName "Nagios Access"
AuthType Basic
AuthUserFile /usr/local/nagios//etc/htpasswd.users
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.users
Require valid-user
</Directory>
文中粗体字部分指明了验证文件的路径,我们需要手工来生成这个密码文件,并在其中创建
用户及密码
[root@nagios ~]# htpasswd /usr/local/nagios/etc/htpasswd.users nagiosadmin
New password:
Re-type new password:
现在apache 基本配置完成,我们可以在web 页面登陆nagios 了
使用浏览器打开http://nagios.tianway.net/nagios,出现如下登陆界面
输入用户名 nagiosadmin 和密码,登陆之后就可以看到如下界面了
很快我们就发现什么都打不开。呵呵,nagios 还没有启动呢。
2、修改nagios 配置文件
对nagios 进行配置需要了解一个概念object(对象),nagios 使用object 来对各种信息进行组织,nagios 中的包含如下obeject:
obeject 含义 用途
contact 联系人 主要包含用那种方式进行报警,哪一种情况下报警,报警的时间段
contactgroup 联系人组 通过联系人组来对联系人进行组织
timeperiod 监控时间段 7X24 小时不间断还是周一至周五,或是自定义的其他时间段
host 被监控主机 需要监控的服务器
hostgroup 主机组 多个host 组成一个主机组
command 监控命令 定义nagios 可以调用的命令
service 被监控的服

定义被监控的服务,如web 服务
servicegroup 服务组 多个被监控的服务组成一个服务组
需要用到的配置文件
配置文件名称 作用 位置
nagios.cfg 主配置文件,用于设置关键的参数 /usr/local/nagios/etc/nagios.cfg
cgi.cfg 和cgi 相关的设置 /usr/local/nagios/etc/cgi.cfg
resource.cfg 资源配置文件,包含很少的参数设置,如
$USER1$=/usr/local/nagios/libexec
/usr/local/nagios/etc/resource.cfg
command.cfg 命令配置文件,只有在这个文件中定义过的
命令才能被nagios 调用
/usr/local/nagios/etc/obeject/command.cfg
contacts.cfg 对联系人和联系人组进行定义 /usr/local/nagios/etc/object/contacts.cfg
templates.cfg 模板文件,内含各种模板 /usr/local/nagios/etc/object/templates.cfg
timeperiods.cf
g
时间配置文件定义时间段 /usr/local/nagios/etc/object/timeperiods.cfg
services.cfg 定义监控的服务(该文件需手工生成) /usr/local/nagios/etc/object/services.cfg
1) 修改主配置文件nagios.cfg
[root@nagios etc]# vi nagios.cfg
把cfg_file=/usr/local/nagios/etc/objects/localhost.cfg 注释掉
确认如下行未被注释掉
cfg_file=/usr/local/nagios/etc/objects/commands.cfg //命令配置文件路径
cfg_file=/usr/local/nagios/etc/objects/contacts.cfg //联系人配置文件路径
cfg_file=/usr/local/nagios/etc/objects/timeperiods.cfg //时间段配置文件路径
cfg_file=/usr/local/nagios/etc/objects/templates.cfg //模板文件路径
cfg_file=/usr/local/nagios/etc/objects/services.cfg //监视服务器配置文件路径,此行手工添加,原文件中并不存在
cfg_file=/usr/local/nagios/etc/objects/hosts.cfg //主机配置文件路径,此行手工添加,原文件中并不存在
2)修改cgi 控制文件cgi.cfg,确认如下内容一致
authorized_for_system_information=nagiosadmin
authorized_for_configuration_information=nagiosadmin
authorized_for_system_commands=nagiosadmin
authorized_for_all_services=nagiosadmin
authorized_for_all_hosts=nagiosadmin
authorized_for_all_service_commands=nagiosadmin
authorized_for_all_host_commands=nagiosadmin
看到这里可能会产生一个疑问,nagiosadmin 这个用户是哪里来的?是这样的,这个用户目前并不存在,需要我们手工来进行创建。如何增
加用户会在后边介绍。
3)查看apache 配置文件/etc/httpd/conf.d/nagios.conf
[root@nagios conf.d]# vi nagios.conf
# SAMPLE CONFIG SNIPPETS FOR APACHE WEB SERVER
# Last Modified: 11-26-2005
#
# This file contains examples of entries that need
# to be incorporated into your Apache web server
# configuration file. Customize the paths, etc. as
# needed to fit your system.
ScriptAlias /nagios/cgi-bin "/usr/local/nagios/sbin"
<Directory "/usr/local/nagios/sbin">
# SSLRequireSSL
Options ExecCGI
AllowOverride None
Order allow,deny
Allow from all
# Order deny,allow
# Deny from all
# Allow from 127.0.0.1
AuthName "Nagios Access"
AuthType Basic
AuthUserFile /usr/local/nagios/etc/htpasswd.users //验证文件的路径
Require valid-user
</Directory>
Alias /nagios "/usr/local/nagios/share"
<Directory "/usr/local/nagios/share">
# SSLRequireSSL
Options None
AllowOverride None
Order allow,deny
Allow from all
# Order deny,allow
# Deny from all
# Allow from 127.0.0.1
AuthName "Nagios Access"
AuthType Basic
AuthUserFile /usr/local/nagios/etc/htpasswd.users //验证文件的路径
Require valid-user
</Directory>
AuthUserFile /usr/local/nagios/etc/htpasswd.users 指定web 服务器用户验证文件的路径,前文提到的用户nagiosadmin 的信息就存放
在这个文件中。这个文件默认是不存在的,我们需要手工来创建它。
3)用户管理
[root@nagios etc]# htpasswd -c /usr/local/nagios/etc/htpasswd.users nagiosadmin //创建第一个用户
New password:
Re-type new password:
Adding password for user nagiosadmin
[root@nagios etc]# htpasswd /usr/local/nagios/etc/htpasswd.users nagios //创建第二个用户
New password:
Re-type new password:
Adding password for user nagios
[root@nagios etc]# cat htpasswd.users //查看已经创建的用户
nagiosadmin:dICKCZltUuRus
nagios:48aygCWwPxaU2
-c 表示创建htpasswd.users 这个文件, nagiosadmin 表示要增加的用户,下一次增加用户的切勿使用参数-c,否则会把原文件覆盖,
原有的用户信息就回丢失
4)定义监控时间段,默认的定义有24x7、workhours、us-holidays、24x7_sans_holidays,
我们可以根据自己的需要增加新的时间段定义
[root@nagios objects]# vi timeperiods.cfg
# This defines a timeperiod where all times are valid for checks,
# notifications, etc. The classic "24x7" support nightmare.
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
}
# 'workhours' timeperiod definition
define timeperiod{
timeperiod_name workhours
alias Normal Work Hours
monday 09:00-17:00
tuesday 09:00-17:00
wednesday 09:00-17:00
thursday 09:00-17:00
friday 09:00-17:00
}
# 'none' timeperiod definition
define timeperiod{
timeperiod_name none
alias No Time Is A Good Time
}
# Some U.S. holidays
# Note: The timeranges for each holiday are meant to *exclude* the holidays from being
# treated as a valid time for notifications, etc. You probably don't want your pager
# going off on New Year's. Although you're employer might...
define timeperiod{
name us-holidays
timeperiod_name us-holidays
alias U.S. Holidays
january 1 00:00-00:00 ; New Years
monday -1 may 00:00-00:00 ; Memorial Day (last Monday in May)
july 4 00:00-00:00 ; Independence Day
monday 1 september 00:00-00:00 ; Labor Day (first Monday in September)
thursday -1 november 00:00-00:00 ; Thanksgiving (last Thursday in November)
december 25 00:00-00:00 ; Christmas
}
# This defines a modified "24x7" timeperiod that covers every day of the
# year, except for U.S. holidays (defined in the timeperiod above).
define timeperiod{
timeperiod_name 24x7_sans_holidays
alias 24x7 Sans Holidays
use us-holidays ; Get holiday exceptions from other timeperiod
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
}

论坛徽章:
3
CU大牛徽章
日期:2013-03-14 14:14:08CU大牛徽章
日期:2013-03-14 14:14:26CU大牛徽章
日期:2013-03-14 14:14:29
2 [报告]
发表于 2010-12-17 15:01 |只看该作者
5)修改联系人组
[root@nagios objects]# vi 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 nagios@localhost ; <<***** CHANGE THIS TO YOUR EMAIL ADDRESS ******
}
###############################################################################
###############################################################################
#
# CONTACT GROUPS
#
###############################################################################
###############################################################################
# We only have one contact in this simple configuration file, so there is
# no need to create more than one contact group.
define contactgroup{
contactgroup_name admins
alias Nagios Administrators
members nagiosadmin
}
6)新建hosts.cfg,定义被监控主机和主机组
[root@tianway objects]#vi hosts.cfg
define host{ //定义主机
host_name 192.168.0.2 //定义主机名,可起任意名字
address 192.168.0.2 //主机名对应的ip 地址
notifications_enabled 1 ; Host notifications are enabled //主机状态通知功能激活
event_handler_enabled 1 ; Host event handler is enabled //主机事件处理激活
notification_period 24x7 ; Send host notifications at any time //发送通知的时间段
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 24x7 ; 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 5 ; Resend notifications every 2 hours //发送通知的时间间隔
notification_options d,u,r ; Only send notifications for specific host states //定义那
些情况下发送通知,
; 具体含义见contacts.cfg 部分
contact_groups admins ; Notifications get sent to the admins by default //发送通知给谁
}
通过复制粘贴修改就可以增加192.168.0.3、192.168.0.4 的定义。
define hostgroup{ //定义主机组
hostgroup_name tianway.net ; The name of the hostgroup //定义主机组的名字
alias Linux Server ; Long name of the group //别名
members 192.168.0.2,192.168.0.3,192.168.0.4 ; Comma separated list of hosts that belong to this group//组成员,
使用逗号分隔
}
使用简单的复制和修改即可增加192.168.0.3、192.168.04 的服务定义。
重启nagios 服务
[root@nagios ~]# service nagios restart
Running configuration check...done.
Stopping nagios: done.
Starting nagios: done.
点击nagios 界面中的host detail 就可以看到下图
3、使用插件监控更多的服务和信息
本章的开头部分就提到,nagios 本身并不提供任何的监控功能,所有的监控任
务都是由插件来完成。nagios 安装部分已经讲完如何安装nagios 插件,所谓插件就是
用来完成特定功能的小程序,默认的安装路径是/usr/local/nagios/libexec。现在我
们查看一下有哪些可供使用的插件
[root@nagios libexec]# ls
check_apt check_flexlm check_mailq check_overcr check_swap
check_breeze check_ftp check_mrtg check_ping check_tcp
check_by_ssh check_http check_mrtgtraf check_pop check_time
check_clamd check_icmp check_nagios check_procs check_udp
check_cluster check_ide_smart check_nntp check_real check_ups
check_dhcp check_ifoperstatus check_nntps check_rpc check_users
check_dig check_ifstatus check_nt check_sensors check_wave
check_disk check_imap check_ntp check_simap negate
check_disk_smb check_ircd check_ntp_peer check_smtp urlize
check_dns check_jabber check_ntp_time check_spop utils.pm
check_dummy check_load check_nwstat check_ssh utils.sh
check_file_age check_log check_oracle check_ssmtp
所有程序都是可以执行的,具体的用法可以使用“命令名 -h”进行查看。
以上所有的命令并不能直接使用,nagios 所调用的命令来源于commands.cfg,这些插件和nagios 所调用的命令有什么关系呢?我们打开
commands.cfg 文件,看看之前用到过 的check-host-alived 和check-ssh 命令。
define command{
command_name check-host-alive //定义可被nagios 调用的命令
command_line $USER1$/check_ping -H $HOSTADDRESS$ -w 3000.0,80% -c 5000.0,100% -p 5 //定义插件路径和参数设置,
$USER1$表示插件的安装路径,在
//resources.cfg 文件中定义,
$HOSTADDRESS$表示主机地址
}
define command{
command_name check_ssh //定义可被nagios 调用的命令
command_line $USER1$/check_ssh $ARG1$ $HOSTADDRESS$ //定义插件路径和参数设置
}
新建services.cfg,定义被监控的服务器
[root@tianway ~]# vi /usr/local/nagios/etc/objects/services.cfg
define service{
host_name nagios.tianway.net //定义主机名
Service_description check-ssh //定义服务名
check_command check_ssh //指定检查服务使用的命令
notifications_enabled 1 ; Service notifications are enabled //服务通知
event_handler_enabled 1 ; Service event handler is enabled //时间处理激活
notification_period 24x7 ; Send host notifications at any time //发送通知的时间段
check_period 24x7 ; The service can be checked at any time of the day //检查的
时间段
max_check_attempts 3 ; Re-check the service up to 3 times in order to determine its
final (hard) state
normal_check_interval 5 ; Check the service every 10 minutes under normal conditions
//检查频率
retry_check_interval 1 ; Re-check the service every two minutes until a hard state
can be determined
contact_groups admins ; Notifications get sent out to everyone in the 'admins' group
//联系人组
notification_options w,u,c,r ; Send notifications about warning, unknown, critical, and
recovery events
//定义哪些情况发送通知
notification_interval 5 ; Re-notify about service problems every 5min //发送通知的
时间间隔
}
define service{
host_name nagios.tianway.net //定义主机名
Service_description check-ssh //定义服务名
check_command check_http //指定检查服务使用的命令
notifications_enabled 1 ; Service notifications are enabled //服务通知
event_handler_enabled 1 ; Service event handler is enabled //时间处理激活
notification_period 24x7 ; Send host notifications at any time //发送通知的时间段
check_period 24x7 ; The service can be checked at any time of the day //检查的
时间段
max_check_attempts 3 ; Re-check the service up to 3 times in order to determine its
final (hard) state
normal_check_interval 5 ; Check the service every 10 minutes under normal conditions
//检查频率
retry_check_interval 1 ; Re-check the service every two minutes until a hard state
can be determined
contact_groups admins ; Notifications get sent out to everyone in the 'admins' group
//联系人组
notification_options w,u,c,r ; Send notifications about warning, unknown, critical, and
recovery events
//定义哪些情况发送通知
notification_interval 5 ; Re-notify about service problems every 5min //发送通知的
时间间隔
}
按照同样的办法定义另外三个主机的服务监控
点击nagios 界面中的host detail 就可以看到下图
4、使用NRPE 监控远程服务器的本地信息
现在我们已经知道如何使用插件监控本地和远程服务器的外部信息,但是我们在运维工作中需要对服务器的本
地信息进行监测,确定是否存在安全隐患,例如:load average、内存使用情况、磁盘使用率的变化等等。如何才能获
取到这些信息呢?我们使用NRPE 来实现,使用NRPE 进行远程本地信息监控的时候监控架构图如下:
1) 现在我们对NRPE 做一个简单的介绍
NRPE 作为Nagios 的一个附加组件出现,是Nagios 解决方案中一个很重要的组成部分,它可以使你在远程执行Nagios
的插件,从而获取到远程服务器上的信息(例如:CPU load、内存使用情况等)。这些资源是不会暴露给外部机器的蓑衣
必须在远程服务器上安装一个agent 才能完成信息收集的功能。当然使用SSH 远程登陆之后再收集信息也是一种可行的方
案,但是随着监控设备数量上升,这种方式会占用监控主机和被监控机的大量资源,对正常的业务系统会带来不利影响,
甚至导致服务器过载,因此最终这种思路被放弃。
NRPE 由两个部分组成:工作在监控机一侧的check_nrpe 插件、工作在被监控机一侧的NRPE 守护进程
Nagios 服务器执行check_nrpe 插件并告诉他检查哪个服务,check_nrpe 插件通过SSL 连接方式联系远程服务器上
的NRPE 守护进程,NRPE 守护进程执行相应的插件完成指定的检查,并返回结果
注意:Nagios 插件必须安装在被监控的远程机器上,否则NRPE 将无法实现服务监控
2) NRPE 相关概念介绍
直接检测:从Nagios 可以直接检测到的外部服务或者使用NRPE 可以检测到的内部服务,称之为直接检测
间接检测:某些情况下Nagios 服务器可能不能直接访问到被监控的机器,这种情况下就可以使用NRPE 作为中间代理去访
问远程服务器,并将结果返回给Nagios,这种检测办法称为间接检测。
3) NRPE 安装
使用NRPE 你需要分别在监控机和被监控机上分别安装对应的软件。安装使用NRPE 的前提条件是你的系统必须支持TCP
wrappers 以及xinetd 超级守护进程,大多数Linux 发行版都自带这些功能。
 监控机安装NRPE plugin
监控机上不需要安装完整的NRPE,只要安装NRPE plugin 就可以了
[root@nagios ~]# tar xzvf nrpe-2.12.tar.gz
[root@nagios ~]# cd nrpe-2.12
[root@nagios nrpe-2.12]# ./configure
[root@nagios nrpe-2.12]# make all
[root@nagios nrpe-2.12]# make install-plugin
 被监控机安装
被监控机需要安装nagios 插件和NRPE
[root@nagios ~]# /usr/sbin/useradd nagios //增加nagios 用户
[root@nagios ~]# tar xzvf nagios-plugins-1.4.13.tar.gz //解压缩nagios 插件安装包
[root@nagios ~]# cd nagios-plugins-1.4.13
[root@nagios nagios-plugins-1.4.13]#./configure --prefix=/usr/local/nagios/ //编译安

[root@nagios nagios-plugins-1.4.13]#make
[root@nagios nagios-plugins-1.4.13]#make install
[root@nagios ~]# tar xzvf nrpe-2.12.tar.gz //解压缩NRPE
[root@nagios nrpe-2.12]#./configure --prefix=/usr/local/nagios/
[root@nagios nrpe-2.12]#make all
[root@nagios nrpe-2.12]#make install-plugin
[root@nagios nrpe-2.12]#make install-daemon
[root@nagios nrpe-2.12]#make install-daemon-config
[root@nagios nrpe-2.12]#make install-xinetd //NRPE 安装为xinetd 服务
编辑/etc/xinetd.d/nrpe 文件,允许监控机访问被监控机上的这个服务,增加监控机的地址
only_from = 127.0.0.1 192.168.0.1 //多个地址之间使用空格相隔
修改/etc/services 文件,增加如下一行
nrpe 5666/tcp # NRPE
重启 xinetd 服务
[root@nagios ~]# service xinetd restart
Stopping xinetd: [ OK ]
Starting xinetd: [ OK ]
修改iptables,开放NRPE 的端口
[root@nagios ~]# iptables -t filter -I INPUT -p tcp --dport 5666 -j ACCEPT //开放NRPE 的5666
端口
修改commands.cfg 文件,增加check_nrpe 的定义
define command{
command_name check_nrpe
command_line $USER1$/check_nrpe -H $HOSTADDRESS$ -c $ARG1$
}
4) NRPE 测试
安装完成了,我们进行一个测试
 在被监控机上确认服务是否启动
[root@nagios ~]# netstat -at|grep nrpe
tcp 0 0 *:nrpe *:* LISTEN
服务已经正常启动,如果输出结果与此不一致,请检查/etc/services 文件是否按照要求进行了修改,xinetd
服务是否运行正常
 在监控机上测试是被监控机的NRPE 工作是否正常
[root@nagios ~]# /usr/local/nagios/libexec/check_nrpe -H web1.tianway.net
NRPE v2.12 //NRPE 版本信息,说明NRPE 工作正常
[root@nagios ~]# /usr/local/nagios/libexec/check_nrpe -H web1.tianway.net -c check_load
OK - load average: 0.01, 0.00, 0.00|load1=0.010;15.000;30.000;0; load5=0.000;10.000;25.000;0;
load15=0.000;5.000;20.000;0;
//能够正常获取信息,NRPE 工作正常
5) NRPE 配置
NRPE 的配置文件位于/usr/local/nagios/etc/nrpe.cfg,NRPE 调用的命令必须在配置文件中定义,默认的配置文件中定
义了如下命令:
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
也就是默认的配置我们只能检查很少的几个资源使用情况,为了能够监控到更多的资源,我们可以自己对配置
文件进行修改,增加可使用的命令,如:增加对交换空间的监控命令就在配置文件中增加如下几行就可以了
command[check_total_procs]=/usr/local/nagios//libexec/check_swap -w 1024000000 -c 800000000
6) 完成服务配置文件
[root@nagios ~]# vi /usr/local/nagios/etc/objects/services.cfg
增加远程服务器磁盘和负载的监控,以web1.tianway.net 在/usr/local/nagios/et c/services.cfg 文件中增加如
下内容
define service{
host_name web1.tianway.net
service_description check-load
check_command check_nrpe!check_load
notifications_enabled 1 ; Service notifications are enabled
event_handler_enabled 1 ; Service event handler is enabled
notification_period 24x7 ; Send host notifications at any time
check_period 24x7 ; The service can be checked at any time of the
day
max_check_attempts 3 ; Re-check the service up to 3 times in order to
determine its final (hard) state
normal_check_interval 5 ; Check the service every 10 minutes under
normal conditions
retry_check_interval 1 ; Re-check the service every two minutes until
a hard state can be determined
contact_groups admins ; Notifications get sent out to everyone in the
'admins' group
notification_options w,u,c,r ; Send notifications about warning, unknown,
critical, and recovery events
notification_interval 5 ; Re-notify about service problems every 5min
}
define service{
host_name web1.tianway.net
service_description check-disk
check_command check_nrpe!check_hda1
notifications_enabled 1 ; Service notifications are enabled
event_handler_enabled 1 ; Service event handler is enabled
notification_period 24x7 ; Send host notifications at any time
check_period 24x7 ; The service can be checked at any time of
the day
max_check_attempts 3 ; Re-check the service up to 3 times in order to
determine its final (hard) state
normal_check_interval 5 ; Check the service every 10 minutes under
normal conditions
retry_check_interval 1 ; Re-check the service every two minutes until a
hard state can be determined
contact_groups admins ; Notifications get sent out to everyone in
the 'admins' group
notification_options w,u,c,r ; Send notifications about warning, unknown,
critical, and recovery events
notification_interval 5 ; Re-notify about service problems every 5min
}
check_nrpe 后边指定在被监控机上执行的命令,使用!进行分隔,该命令必须是在nrpe.cfg 文件中进行定义过的方可使
用。
依次添加其他服务器的监控项,完成后重启nagios。在浏览器页面上点击Service detail,可以看到下图
5、报警
服务器监控设置已经完成,可以从nagios 的监控页面上看到监控的结果了,但是怎样才能在服务器出现故障的时候及时通
知到维护人员呢?nagios 常见的报警方式有飞信报警、短信报警、邮件报警、QQ 报警、MSN 报警等等。我们重点讲述一
下邮件报警、飞信报警。
1) 邮件报警
邮件报警是一种广为使用的报警方式,具有成本低廉,性能稳定等优势,我们采用系统自带的sendmail 作为我们的邮件
服务器。
 sendmail 安装配置
[root@nagios RPMS]# rpm -ivh sendmail-8.13.1-3.RHEL4.5.i386.rpm //安装系统自
带的sendmail 版本
[root@nagios ~]# service sendmail start //启动
sendmail 服务
Starting sendmail: [ OK ]
Starting sm-client: [ OK ]
[root@nagios ~]# echo test |sendmail -v address@domainname.com //发送测试邮

如果能正常收到测试邮件,就进行下一步
 邮件报警配置
如果设置正常,发生故障时nagios 就会发送邮件给指定的E-mail 信箱。该邮件地址在contacts.cfg 定义联
系人的时候指定
2) 飞信报警
发生故障时nagios 发送邮件到指定的信箱是一个可行的通知办法,但是对于关键的业务服务而言,这样的报警方式显然
不能满足需要。对于关键业务的运行情况,我们需要在发生故障后的第一时间获取到相关信息,甚至希望能够在故障发生
前就进行处理。例如对磁盘空间的监控可以在磁盘被占满之前发出预警,是我们有足够的时间进行处理。综合各种情况,
最合适的关键业务报警方式无非是短信、电话两种方式。短信及时性好、花费低廉对终端要求低等各种优势,往往成为最
佳的选择。对于大企业用户而言,可以使用短信网关来发送端新,对于小型企业可以购买短信猫来实现相同的功能,对于
不原意花钱又想用短信报警的用户,飞信就成为最好的选择(139 邮箱也可以实现类似的功能但效果较差)。
 飞信客户端安装
[root@localhost ~]# wget http://www.it-adv.net/fetion/lib_lin_32.tar.gz //下载需要
的库文件
[root@localhost ~]# tar xzvf lib_lin_32.tar.gz //解压缩
[root@localhost ~]# cd library_linux32/
[root@localhost library_linux32]# cp * /lib/ //把解压缩
出来的文件cp 到/lib/目录下
[root@localhost library_linux32]# cp * /usr/lib/ //把解压缩
出来的文件cp 到/lib/目录下
[root@localhost ~]# wget http://www.it-adv.net/fetion/fetion_linux_20080402.tar.gz //下载飞信
安装文件
[root@localhost ~]# tar xzvf fetion_linux_20080402.tar.gz //解压缩
[root@localhost ~]# cp sms /usr/bin/ //复制到
/usr/bin 目录下
 相关设置
修改commands.cfg 文件,为nagios 增加两个报警命令
[root@nagios objects]# vi commands.cfg //增加以下内容
define command{
command_name notify-service-by-sms //命令名称
command_line /usr/bin/sms -f 139000000 -p xxx -t $CONTACTPAGER$ -m "$HOSTNAME$ $SERVIC
EDESC$ is $SERVICESTATE$ on $TIME$ result is $SERVICEOUTPUT$" $CONTACTPAGER$
}
define command{
command_name notify-host-by-sms
command_line /usr/bin/sms -f 139000000 -p xxx -t $CONTACTPAGER$ -m $NOTIFICATIONTYPE$
Host Alert: $HOSTNAME$ is $HOSTSTATE$ **" $CONTACTPAGER$
}
修改contacts.cfg,使用飞信作为报警方式
[root@nagios ~]# more /usr/local/nagios/etc/contacts.cfg
define contact{
contact_name sa
alias system admin
host_notification_period 24x7
service_notification_period 24x7
host_notification_options d,r,
service_notification_options c,w,r
service_notification_commands notify-service-by-sms
host_notification_commands notify-host-by-sms
pager 1390000000
}
配置文件修改完成后重启nagios 服务,使配置文件生效就可以使用飞信报警了

论坛徽章:
0
3 [报告]
发表于 2010-12-17 15:09 |只看该作者

论坛徽章:
3
CU大牛徽章
日期:2013-03-14 14:14:08CU大牛徽章
日期:2013-03-14 14:14:26CU大牛徽章
日期:2013-03-14 14:14:29
4 [报告]
发表于 2010-12-17 15:35 |只看该作者
本帖最后由 comcn2 于 2010-12-17 15:38 编辑

补充一点:文档写的比较早了。飞信报警部分所使用的客户端可能已经发生变化了。请大家根据实际情况进行调整。如果有疑问可以在QQ群里提问。另外附件内容和帖子内容完全相同。

论坛徽章:
0
5 [报告]
发表于 2010-12-17 17:04 |只看该作者
不错,整理的挺好的!

论坛徽章:
3
CU大牛徽章
日期:2013-03-14 14:14:08CU大牛徽章
日期:2013-03-14 14:14:26CU大牛徽章
日期:2013-03-14 14:14:29
6 [报告]
发表于 2010-12-31 20:51 |只看该作者
人气不旺啊。

论坛徽章:
0
7 [报告]
发表于 2010-12-31 21:18 |只看该作者
回复 6# comcn2


    因为不是谁都用!

   再是别人监控环境早就已经配置好了,你发晚了!

论坛徽章:
5
未羊
日期:2014-08-04 16:15:21天秤座
日期:2014-08-13 13:52:372015年辞旧岁徽章
日期:2015-03-03 16:54:152015年迎新春徽章
日期:2015-03-04 09:56:112015亚冠之浦和红钻
日期:2015-06-29 15:30:48
8 [报告]
发表于 2011-01-01 17:30 |只看该作者
新年来顶~

论坛徽章:
0
9 [报告]
发表于 2011-01-03 11:14 |只看该作者
不错,整理的挺好的!
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP