nagios安装与配置实例详解.docx
《nagios安装与配置实例详解.docx》由会员分享,可在线阅读,更多相关《nagios安装与配置实例详解.docx(20页珍藏版)》请在冰豆网上搜索。
nagios安装与配置实例详解
1安装环境
OS:
RedHat5.4
软件环境:
apache2.2.3,nagios-3.3.1.tar.gz,nagios-plugins-1.4.15.tar.gz,pcre-8.20.tar.gz
2安装软件
2.1添加用户
新建用户nagios,并设置密码.
#useraddnagios
#passwdnagios
Changingpasswordforusernagios.
NewUNIXpassword:
BADPASSWORD:
itisbasedonadictionaryword
RetypenewUNIXpassword:
passwd:
allauthenticationtokensupdatedsuccessfully.
2.2上传软件
上传nagios-3.3.1.tar.gz,nagios-plugins-1.4.15.tar.gz至服务器,并赋予可执行权限。
2.3安装nagios
#tarzxvfnagios-3.3.1.tar.gz
#cdnagios
#./configure--prefix=/usr/local/nagios//指定安装路径
#makeall
#makeinstall//安装主要的程序、CGI及HTML文件.
#makeinstall-commandmode//给外部命令访问nagios配置文件的权限
#makeinstall-config//把配置文件的例子复制到nagios的安装目录
2.4验证nagios是否正确安装
切换目录到/usr/local/nagios,看是否存在etc、bin、sbin、share、var这五个目录,如果存在则可以表明程序被正确的安装到系统了。
后表是五个目录功能的简要说明:
bin
Nagios执行程序所在目录,这个目录只有一个文件nagios
etc
Nagios配置文件位置,初始安装完后,只有几个*.cfg-sample文件
sbin
NagiosCgi文件所在目录,也就是执行外部命令所需文件所在的目录
Share
Nagios网页文件所在的目录
Var
Nagios日志文件、spid等文件所在的目录
2.5安装nagios的插件
#tarzxvfnagios-plugins-1.4.15.tar.gz
#cdnagios-plugins-1.4.15
#./configure--prefix=/usr/local/nagios//指定为nagios的安装目录
#make
#makeinstall
2.6检查插件是否安装正确
安装完成后,将在目录/usr/local/nagios生成目录libexec。
3软件配置
3.1配置apache
在httpd.conf末尾加入如下内容
#settingfornagios
ScriptAlias/nagios/cgi-bin/usr/local/nagios/sbin
//cgi文件所在目录
AuthTypeBasic
OptionsExecCGI
AllowOverrideNone
Orderallow,deny
Allowfromall
AuthName"NagiosAccess"
AuthUserFile/usr/local/nagios/etc/htpasswd//验证文件路径
Requirevalid-user
Alias/nagios/usr/local/nagios/share
//nagios页面文件目录
AuthTypeBasic
OptionsNone
AllowOverrideNone
Orderallow,deny
Allowfromall
AuthName"nagiosAccess"
AuthUserFile/usr/local/nagios/etc/htpasswd//验证文件路径
Requirevalid-user
3.2生成用户验证文件
#/usr/bin/htpasswd-c/usr/local/nagios/etc/htpasswdnagiosadmin//生成用户nagiosadmin
Newpassword:
Re-typenewpassword:
Addingpasswordforusernagiosadmin//设置密码
3.3验证apache是否配置正确
访问http:
//192.168.10.250/nagios,会提示输入用户名密码,按以上设置的用户及密码即可登录nagios。
如果还要添加更多的用户,执行命令htpasswd就不需要选项“-c”,否则就会覆盖所有已经生成的行。
3.4nagios配置文件简介
nagios的主配置文件为/usr/local/nagios/etc/nagios.cfg。
默认主要参数:
log_file=/usr/local/nagios/var/nagios.log//nagios的日志文件
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/localhost.cfg//监控本机的配置文件
nagios_user=nagios
nagios_group=nagios
3.5修改nagios配置文件
1.编辑nagios.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/contactgroups.cfg//联系组配置文件,可选
cfg_file=/usr/local/nagios/etc/objects/hostgroups.cfg//主机组配置文件,可选
2.改check_external_commands=0为check_external_commands=1
这行的作用是允许在web界面下执行重启nagios、停止主机/服务检查等操作。
3.修改cgi.cfg
use_authentication=1
default_user_name=nagiosadmin
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
那么上述用户名打那里来的呢?
是执行命令/usr/local/apache/bin/htpasswd–c/usr/local/nagios/etc/htpasswdnagiosadmin所生成的,这个要注意,不能随便加没有存在的验证用户,为了安全起见,不要添加过多的验证用户。
4.查找retain_state_information=0,将0改为1
设定启用状态信息保存功能。
当Nagios重新启动的时候不会是空数据,而是先显示上次离线时最后保留的状态数据。
1为保留,0为不保留。
3.6Nagios主配置文件详解
3.7增加新的配置文件
3.7.1hosts.cfg
需要监控的主机配置文件,很重要。
主机的基本样式如下:
#definemonitorhost
############################################
definehost{
host_nameapache//定义主机名称
aliaswebserver//定义描述
address192.168.10.2//IP地址
contact_groupssagroup//需要联系的组,多个联系组用逗号分隔,数据来源于contactgroups.cfg
check_commandcheck-host-alive//检测的命令,命令来源于commands.cfg
max_check_attempts5//这一项用来定义在检测返回结果不是OK时,nagios重试检测命令的次数。
设置这个值为1会导致nagios一次也不重试就报警。
notification_interval10//这一项用来定义当一个服务仍然down或unreachable时,我们间隔多久重发一次通知给联系组
notification_period24x7//标识什么时间段内给联系组送通知。
数据来源于timeperiods.cfg
notification_optionsd,u,r//这一项用来决定发送通知的时机。
选项有:
d=当有down状态时发送通知,u=当有unreachable状态时发送通知,r=当有服务recoveries时发送通知,f=当主机启动或停机时发送通知。
如果你给一个n选项,那么永远不会发送通知。
}
3.7.2services.cfg
再一个重量级的配置文件是services.cfg,没有这个文件,什么监控也没用。
基本样式如下:
#Defineaserviceto"ping"thelocalmachine
defineservice{
host_nameapache//主机名称
service_descriptionPING//服务描述
check_commandcheck_ping!
100.0,20%!
500.0,60%
//执行的检测命令
max_check_attempts10//最大失败尝试次数,值为1时只报警不重新检测
normal_check_interval5//常规检测间隔时间,默认为60分钟
(常规检测是指无论服务状态是否正常,检测次数达到“最大次数”时)
retry_check_interval3//失败尝试间隔时间,默认为60分钟
(失败尝试是指服务状态不正常,检查次数达到“最大次数”时)
check_period24x7//检测时间段
notification_interval10//当服务状态不正常时通知联系人的间隔时间,值为0时不通知联系人
notification_period24x7//通知联系人时间段
notification_optionsw,u,c,f//通知联系人选项,w警告,u未知,c危急,f启动和停止,n不发送通知
contact_groupssagroup//联系人组
}
#Defineaserviceto"ping"thewebmachine
defineservice{
host_nameapache
service_descriptionPING
check_commandcheck_ping!
100.0,20%!
500.0,60%
}
#Defineaservicetocheckthediskspaceoftherootpartition
#onthelocalmachine.Warningif<20%free,criticalif
#<10%freespaceonpartition.
defineservice{
host_nameapache
service_descriptionRootPartition
check_commandcheck_local_disk!
20%!
10%!
/
}
#Defineaservicetocheckthenumberofcurrentlyloggedin
#usersonthelocalmachine.Warningif>20users,critical
#if>50users.
#defineservice{
#Defineaserviceto"ping"thelocalmachine
defineservice{
host_nameapache
service_descriptionPING
check_commandcheck_ping!
100.0,20%!
500.0,60%
}
#Defineaservicetocheckthediskspaceoftherootpartition
#onthelocalmachine.Warningif<20%free,criticalif
#<10%freespaceonpartition.
defineservice{
host_nameapache
service_descriptionRootPartition
check_commandcheck_local_disk!
20%!
10%!
/
}
#Defineaservicetocheckthenumberofcurrentlyloggedin
#usersonthelocalmachine.Warningif>20users,critical
#if>50users.
defineservice{
host_nameapache
service_descriptionCurrentUsers
check_commandcheck_local_users!
20!
50
}
3.7.3contactgroups.cfg(可选)
这个文件是依照上一个文件contacts.cfg来的,contactgroups文件相对简单一些,其格式如下:
definecontactgroup{
contactgroup_namesagroup
aliassystemadministratorgroup
memberssa,nagiosadmin//本组成员
}
多个成员之间用逗号做分界符,如果有更多的联系组,就依相同的格式在文件中追加余下的组。
3.7.4hostgroups.cfg
主机组配置文件hostgroups.cfg,这是一个可选的项目,它建立在文件hosts之上,其格式如下:
definehostgroup{
hostgroup_namesa-servers//组名称
aliassaservers//组描述
membersapache//用逗号间隔多个主机
}
4检查nagios配置文件是否正确
运行命令/usr/local/nagios/bin/nagios–v/usr/local/nagios/etc/nagios.cfg来检查所有配置文件的正确性。
如果十分幸运的话,运行完毕将在输出尾部出现
TotalWarnings:
0
TotalErrors:
0
Thingslookokay-Noseriousproblemsweredetectedduringthepre-flightcheck
5启动和停止nagios
5.1启动nagios
执行命令/usr/local/nagios/bin/nagios–d /usr/local/nagios/etc/nagios.cfg把nagios作为守护进程。
5.2停止nagios
首先用ps–aux|grepnagios查看pid。
Kill-9pid
6nagios实例
6.1nagios监控本机
6.1.1默认监控
nagios配置正确并启动后,访问监控页面就会看到如下监控内容。
CurrentLoad:
本机负载情况
CurrentUsers:
正在登录本机的用户数
HTTP:
apache服务运行正常
RootPartition:
硬盘使用情况
SSH:
SSH服务正在运行
SwapUsage:
swap内存使用情况
TotalProcesses:
所有进程数
6.1.2监控本机内存
Nagios默认不提供监控内存的功能,需要第三方脚本支持:
check_mem。
1.上传check_mem脚本到/usr/local/nagios/libexec目录下。
2.配置commands.cfg文件
#vi/usr/local/nagios/etc/objects/commands.cfg
加入如下内容:
definecommand{
command_namecheck_memory
command_line$USER1$/check_mem-w$ARG1$-c$ARG2$
}
3.配置localhost.cfg文件
#vi/usr/local/nagios/etc/objects/localhost.cfg
加入如下内容
defineservice{
uselocal-service
host_namelocalhost
service_descriptioncheck_mem
check_commandcheck_memory!
110,50!
150,80
}
4.检测配置文件是否正确。
#/usr/local/nagios/bin/nagios–v/usr/local/nagios/etc/nagios.cfg
若没有出现error,则表示配置文件没有语法错误。
5.重新启动nagios
#ps–aux|grepnagios
#kill-9pid
#/usr/local/nagios/bin/nagios–d/usr/local/nagios/etc/nagios.cfg
成功后,访问监控页面,将会看到下图
6.说明
Ø首先要定义好command,然后通过配置service来调用command进行监控内存。
两者command名称要一致。
ØCheck_memory!
110,50!
150,80,表示内存用到110M、swap用到50M时发出警告,内存用150M、swap用到80M时发出危急报警。
6.1.3监控本机apachestatus
1.nagios默认不提供监控apachestatus的功能,需要第三方脚本支持:
check_apache
2.上传check_apache脚本至/usr/local/nagios/libexec目录下,并赋权限
#chmod755check_apache
3.check_apache脚本使用方法
./check_apache-Hlocalhost-P80-t3-b/usr/sbin-p/var/run\
-napache2.pid-sstatus_page[-S][-R][-wr]100[-cr]250
status_page默认为server-status,若apache配置文件中更改了此选项,需要用-s指定status_page
4.开启apache监控功能
(1)编辑apache配置文件,进入如下设置
#vi/conf/httpd.conf
(2)找到ExtendedStatusOn,去掉前面的#号
(3)找到server-status,做如下更改,或Allowfromall
SetHandlerserver-status
Orderdeny,allow
Allowfrom192.168.10.250
(4)重启apache使之生效。
#servicehttpdrestart
5.配置commands.cfg文件
#vi/usr/local/nagios/etc/objects/commands.cfg
加入如下内容:
definecommand{
command_namecheck_apachestatus
command_line$USER1$/check_apache-H$ARG1$-P$ARG2$-t$ARG3$
}
6.配置localhost.cfg文件
#vi/usr/local/nagios/etc/objects/localhost.cfg
加入如下内容
defineservice{
uselocal-service
host_namelocalhost
service_descriptioncheck_apache_status
check_commandcheck_apachestatus!
localhost!
80!
3
}
7.检测配置文件是否正确。
#/usr/local/nagios/bin/nagios–v/usr/local/nagios/etc/nagios.cfg
若没有出现error,则表示配置文件没有语法错误。
8.重新启动nagios
#ps–aux|grepnagios
#kill-9pid
#/usr/local/nagios/bin/nagios–d/usr/local/nagios/etc/nagios.cfg
成功后,访问监控页面,将会看到下图
9.说明
check_apachestatus!
localhost!
80!
3:
检测本机的80端口,在3秒没有响应,则置于错误状态
6.2监控远程linux主机
Nagios监控远程