一、前言1.什么是Nagios?
Nagios是一个监控系统和网络的应用程序,他可以监控你指定的主机和服务,当他们出现问题发出警告
2.Nagios有哪些特性?
监控网络服务(SMTP,POP3,HTTP,NNTP,PING......)
监控主机资源(processor load,disk usage…….)
简单的插件设计,让用户方便地开发自己的服务检查
并行化服务检查
能够定义网络主机的层次采用“parent”的主机,使检测和区分主机之间的宕机,而哪些无法访问
当主机或者服务出现问题能发送电子邮件或者短信或者用户自定义的方法进行通知
自动日志回滚
可以支持并实现对主机的冗余监控
可以通过WEB接口查看主机和服务的状态和警告信息、历史警告信息、日志文件等
二.安装Nagios1.获取Nagios
你可以在 下载到最新版本的Nagios
2.解压缩安装包
tar xzvf nagios-version.tar.gz
3.创建Nagios用户和用户组
useradd nagios
4.创建安装目录
mkdir /usr/local/nagios
chown nagios.nagios /usr/local/nagios
5.运行配置脚本
./configure –prefix=/usr/local/nagios --with-nagios-user=nagios —with-nagios-group=nagios
6.编译
make all
7.安装
make install#执行完这个步骤后,程序会提示依次运行 make install-init , make
install-commandmode ,make install-config 这几个命令。我们选取其中的两个来执行
make install-commandmod
make install-config
三、设置web接口 1.注意
在这个结构中我们将采用Aapche作为web服务器,你也可以用其他web服务器只需稍作修改即可
2. 安装Apache
tar -xzvf httpd-vsersion.tar.gz
cd httpd-version
./configure —prirex=/usr/local/apache
make
make install
3. 修改Apache配置文件httpd.conf
a)修改 apache运行用户和组。默认是 daemon,需要把它改成 nagios。这样它才能有
权限访问我们安装的 nagios目录,执行相关的 cgi命令,如通过浏览器界面关闭 nagios、停
止某个故障对象发送报警信息等。
b)添加 nagios 访问目录(nagios 的安装路径/usr/local/nagios),同时使用 http 用户验证。把下面的内容追加到httpd.conf文件的末尾:
ScriptAlias /nagios/cgi-bin /usr/local/nagios/sbin
AuthType Basic
Options ExecCGI
AllowOverride None
Order allow,deny
Allow from all
AuthName "Nagios Access"
AuthUserFile /usr/local/nagios/etc/htpasswd
Require user ty #有效的用户名,多个用户名用空格分隔
Alias /nagios /usr/local/nagios/share
AuthType Basic
Options None
AllowOverride None
Order allow,deny
Allow from all
AuthName "Nagios Access"
AuthUserFile /usr/local/nagios/etc/htpasswd
Require user ty #有效的用户,多个用户名用空格分隔
到这步配置文件虽然修改好了,但还不能通过语法检查,因为有 2 个与之相关联的东西还不存在,他们是用户和组 nagios 及 apache 的验证用户文件/usr/local/nagios/etc/htpasswd .在后续操作中完成这任务后,再来验证
4. 启动Apache服务
/usr/local/apache/bin/apachectl start
四.安装Nagios插件1.Nagios插件作用
插件是nagios扩展功能的强大武器,除了下载常用的插件外,没有插件,nagios将什么作用也没有,我们还可以根据实际要求编写自己的插件
2.获取Nagios插件
你可以在 下载到最新版本的Nagio插件,此例中我们采用的是nagios-plugins-1.4.14,目前的最新版本
3.安装Nagios插件
tar xzvf nagios-plugins-1.4.14.tar.gz
cd nagios-plugins-1.4.14
./configure —prefix=/usr/local/nagios#安装完成后,将在目录/usr/local/nagios生成目录 libexec
make
make install
五.配置Nagios1.概述
其配置文件的目录是/usr/local/nagios/etc
2. 文件改名
先把这些文件改名,如 cgi.cfg-sample改成 cgi.cfg ,用命令 cp cgi.cfg-sample cgi.cfg …依样把余下的几个*.cfg-sample 都复制成*.cfg 文件。从 nagios2.6 版开始,不用修改配置文件localhost.cfg就可以直接运行../bin/nagios –v nagios.cfg验证程序是否能正常运行
3.修改主配置文件Nagios.cfg
基于方便维护的原则,把各个配置目标单独放在文件中,如联系人信息在 contacts.cfg中定义。Nagios.cfg 文件比较长,我只把修改过的内容贴出来:
#注释或删掉这行
#cfg_file=/usr/local/nagios/etc/localhost.cfg
#主机配置文件路径
cfg_file=/usr/local/nagios/etc/hosts.cfg
#主机组配置文件路径
cfg_file=/usr/local/nagios/etc/hostgroups.cfg
#联系人配置文件路径
cfg_file=/usr/local/nagios/etc/contacts.cfg
#服务配置文件路径
cfg_file=/usr/local/nagios/etc/services.cfg
#监视时段配置文件路径
cfg_file=/usr/local/nagios/etc/timeperiods.cfg
#在 web 界面下重启 nagios、停止主机/服务检查等操作,.默认值是 0.
check_external_commands=1
#根据自己的情况定这个命令检查时间间隔.默认值是 1 秒.
command_check_interval=10s
3. 修改cgi配置文件cgi.cfg(同nagios.cfg一样,只列出修改之处)
#如有多个用户,中间用逗号隔开
authorized_for_system_information=ty
authorized_for_configuration_information=ty
authorized_for_system_commands=ty
authorized_for_all_services=ty
authorized_for_all_hosts=ty
authorized_for_all_service_commands=ty
authorized_for_all_host_commands=ty
这里的ty用户可以通过浏览器操作Nagios服务的关闭、重启等操作
4. 新增其他配置文件
在主配置文件 nagios.cfg中,我们注释cfg_file=/usr/local/nagios/etc/localhost.cfg 这一行,不使用默认配置
(1 ) 定义主机配置文件hosts.cfg
define host {
host_name nagios-server
alias nagios server
address 172.2.9.226
contact_groups tygroup
check_command check-host-alive
max_check_attempts 5
notification_interval 10
notification_period 24x7
notification_options d,u,r
}
说明:
● 联系组 contact_group没有建立,需在后面的步骤完成。
● 主机检查命令行一般选择检查主机存活 check-host-alive。
● 通知时间间隔 notification_interval 根据自己实际情况设定,它的单位是分钟。
● 通知选项 notification_options 几个值的意思是 d-down,u-unreacheable,r-recovery
(2)定义主机组配置文件hostgroups.cfg
define hostgroup {
hostgroup_name ty-servers
alias ty servers
members nagios-server
}
说明:
● 主机组的成员必须是在 hosts.cfg里已经定义了的,多个主机成员间用逗号分隔
(3)定义联系人配置文件 contacts.cfg
define contact {
contact_name ty
alias system admin
contactgroup_name tygroup
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,notify-service-by-sms
host_notification_commands host-notify-by-email,notify-host-by-sms
email tanyangxf@163.com
pager 135174926xx
}
说明:
● 服务通知选项 w-warning,u-unknown,c-critical,r-recovery.
● 主机通知选项 d-down,u-unreacheable,r-recovery。
● 服务通知命令行及服务通知命令行在配置文件 commands.cfg 中得到定义,如果有
报警发生,则邮件和手机短信一起发送给相关人,即下两行定义的 email,pager.
● 收报警信息的邮件和手机,一个人如有 2个手机,手机号之间有逗号分隔,邮件也
如此。
● 如果这里定义的用户需要通过浏览器察看他所负责的服务器监控状态的话,还需要
用 apache的工具htpasswd增加同名帐号
(4) 定义联系组配置文件 contactgroups.cfg
define contactgroup {
contactgroup_name tygroup
alias system administrator group
members ty
}
说明:
● 当有多个人行使同样的职责时,定义成组是非常有用的。
● 多个成员之间用逗号分隔。
● 成员必须在联系人配置文件(contacts.cfg)已经定义。
(5)定义服务配置文件 services.cfg
define service {
host_name nagios-server
service_description check-host-alive
check_period 24x7
max_check_attempts 4
normal_check_interval 1
retry_check_interval 2
contact_groups tygroup
notification_interval 1
notification_period 24x7
notification_options w,u,c,r
check_command check-host-alive
}
define service {
host_name nagios-server
service_description check_tcp 80
check_period 24x7
max_check_attempts 4
normal_check_interval 1
retry_check_interval 2
contact_groups tygroup
notification_interval 1
notification_period 24x7
notification_options w,u,c,r
check_command check_tcp!80
}
define service{
host_name nagios-server
service_description check-disk
check_command check_nrpe!check_df
max_check_attempts 4
normal_check_interval 1
retry_check_interval 2
check_period 24x7
notification_interval 1
notification_period 24x7
notification_options w,u,c,r
contact_groups tygroup
}
define service{
host_name nagios-server
service_description check-load
check_command check_nrpe!check_load
max_check_attempts 4
normal_check_interval 1
retry_check_interval 2
check_period 24x7
notification_interval 1
notification_period 24x7
notification_options w,u,c,r
contact_groups tygroup
}
define service{
host_name nagios-server
service_description total_procs
check_command check_nrpe!check_total_procs
max_check_attempts 4
normal_check_interval 1
retry_check_interval 2
check_period 24x7
notification_interval 1
notification_period 24x7
notification_options w,u,c,r
contact_groups tygroup
}
说明:
● 主机名 host_name,必须是主机配置文件 hosts.cfg中定义的主机。
● 检查用的命令 check_command,在命令配置文件中定义或在 nrpe配置文件中有定
义。
● 最大重试次数 max_check_attempts 一般设置为 3-4次比较好,这样不会因为网络
闪断片刻而发生误报。
● 检查间隔和重试检查间隔的单位是 单位是 单位是 单位是分钟 分钟 分钟 分钟。 。。 。
● 通知间隔指探测到故障以后,每隔多少时间发送一次报警信息。 它的单位是分钟。
● 通知选项跟服务定义配置文件相同。
● 联系组 contact_groups由配置文件 contactgroup.cfg 定义。
● 检查主机资源需要安装和配置 nrpe,这个过程在后面完成。
六.配置Nrpe1.安装Nrpe
tar -xzvf nrpe-2.8.1.tar.gz
cd nrpe-2.8.1
./configure —prefix=/usr/local/nrpe
make
make install
注意:如果在其他监控机上安装Nrpe,需要添加系统用户nagios
2.复制文件
安装完 nrpe后,在安装目录/usr/local/nrpe/libexec只有一个文件 check_nrpe,而在 nagios插件目录,却缺少这个文件,因此需要把这个文件复制到 nagios 插件目录;同样,因为 nrpe 需要调用的诸如 check_disk 等插件在自己的目录没有,可是这些文件确是 nagios 插件所存在的,所以也需要从 nagios目录复制一份过来。我们把复制过程列举出来:
cp /usr/local/nrpe/libexec/check_nrpe /usr/local/nagios/libexec
cp /usr/local/nagios/libexec/check_disk /usr/local/nrpe/libexec
cp /usr/local/nagios/libexec/check_load /usr/local/nrpe/libexec
cp /usr/local/nagios/libexec/check_ping /usr/local/nrpe/libexec
cp /usr/local/nagios/libexec/check_procs /usr/local/nrpe/libexec
3.配置Nrpe
安装完nrpe以后,在安装目录并没有可用的配置文件,但我们只需把解压目录的样例文件复制到安装目录,然后修改这个文件.
a) mkdir /usr/local/nrpe/etc
b) sample-config/nrpe.cfg /usr/local/nrpe/etc
c) 修改配置文件/usr/local/nrpe/etc/nrpe.cfg,下面只列出修改过的部分
server_address=172.2.9.226
allowed_hosts=127.0.0.1,172.2.9.226
#command[check_hda1]=/usr/local/nrpe/libexec/check_disk -w 20 -c 10 -p /dev/hda1
command[check_df]=/usr/local/nrpe/libexec/check_disk -w 20% -c 10%
说明:
● allowed_hosts=127.0.0.1,172.2.9.226 必须允许nagios监控服务器可以访问
● command[check_df]=/usr/local/nrpe/libexec/check_disk -w 20% -c 10% 检查整个服务的磁盘利用率;如果是 freebsd 系统,因为其/dev分区为 100%,需要排除这个分区因此其命令行应该为 “command[check_df]=/usr/local/nrpe/libexec/check_disk /dev -w 20% -c 10%”
4.启动Nrpe并检查其配置
a)以独立守护进程启动 nrpe服务 /usr/local/nrpe/bin/nrpe –c /usr/local/nrpe/etc/nrpe.cfg -d
b ) 通过察看系统日志,正常启动可以看到如下输出:
Mar 2 21:07:18 MONITOR nrpe[23823]: Starting up daemon
Mar 2 21:07:18 MONITOR nrpe[23823]: Listening for connections on port 5666
Mar 2 21:07:18 MONITOR nrpe[23823]: Allowing connections from: 127.0.0.1,172.2.9.226
察看端口,也能看见 5666 端口处于监听状态:
[root@MONITOR nrpe]# netstat -an
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 172.2.9.226:5666 0.0.0.0:* LISTEN
c)检查插件功能
检查Nrpe服务
[root@MONITOR nrpe]# libexec/check_nrpe -H 172.2.9.226
NRPE v2.8.1
通过 nrpe检查主机资源
[root@sa-server nrpe]# libexec/check_nrpe -H 172.2.9.226 -c check_df
DISK OK - free space: / 8241 MB (77% inode=98%); /var 6758 MB (90% inode=91%); /usr
8323 MB (81% inode=90%); /dev/shm 1013 MB (100% inode=99%);|
/=2048MB;11218;11228;0;11238 /var=273MB;5792;5802;0;5812
/usr=2048MB;14508;14518;0;14528 /dev/shm=0MB;993;1003;0;1013
[root@MONITOR nrpe]#libexec/check_nrpe -H 172.2.9.226 -c check_ips
OK -connect counts is 139
七、启动Nagios 1。检查配置
/usr/local/nagios/bin/nagios –v /usr/local/nagios/etc/nagios.cfg
Reading configuration data...
Running pre-flight check on configuration data...
……………
Total Warnings: 0
Total Errors: 0
Things look okay - No serious problems were detected during the pre-flight check
2.启动Nagios
/usr/local/nagios/bin/nagios -d /usr/local/nagios/etc/nagios.cfg
3.创建 web验证用户
我们根据前面 apache配置文件的验证方法来创建用户
/usr/local/apache/bin/htpasswd –c /usr/local/nagios/etc/htpasswd ty
4.启动 apache服务
然后在别的机器的浏览器地址栏输入 ,再输入上面产生的用户名及设定的密码,即可通过 web方式察看监控系统当前的状态(浏览器自动刷新屏幕),如下图所示: