Nagios安装配置[1] - Linux配置 - 坐沙发上等戈多

来源:百度文库 编辑:神马文学网 时间:2024/04/27 13:48:20
安装问题 (我找到的中文版之中最好的一个,我照着www.nagios.org手册装了一下,不成功,转而按照此文,最终成,限于篇幅原因就不在贴上来了,我自己的安装步骤基本上和这篇相同)
http://www.china-lg.com/index.php?play=search&make=view&id=282&key=nagios
-----------------------------------------------------------------------------------
我的安装步骤(操作系统 centos linxu 4.3)
1、 所需软件
nagios-2.5.tar.gz
nagios-plugins
imagepak-base
nrpe
apache
2、nagios安装
首先安装apache 具体过程参考www.apache.org手册不在赘述w
[]#adduser nagios
[]#groupadd nagios
[]#mkdir /usr/local/nagios
[]#chown nagios.nagios /usr/local/nagios
[]#tar zxvf nagios-2.5.tar.gz
[]#cd nagios-2.5
[]#./configure --prefix=/usr/local/nagios --with-gd-lib=/usr/local/lib --with-gd-inc=/usr/local/include
------------------------------------------------------------------
上面命令用到的libgd库,在centos下的安装可以用以下命令
[]#yum install gd(gdb php-gd glib-devel gd-devel libpng-devel libpng10-devel libjpeg-devel mod_perl-devel glibc mod_perl)
具体的可参考我上文给出的联接
------------------------------------------------------------------
[]#make all
[]#make install
[]#make install-init     ##This installs the init script in /usr/local/etc/rc.d
[]#make install-commandmode
[]#make install-config    ##将初始的配置文件安装到/usr/local/nagios/etc
2.2、nagios-plugins的安装
[]#tar –xvzf nagios-plugins-1.4.4.tar
[]#mkdir /usr/local/nagios-plugins
[]#cd nagios-plugins-1.4.4
[]#./configure --prefix=/usr/local/nagios-plugins
[]#make all
[]#make install
安装完成以后在/usr/local/nagios-plugins会产生一个libexec的目录,将该目录全部移动到/usr/local/nagios目录下
[]#cp /usr/local/nagios-plugins/libexec /usr/local/nagios/libexec
2.3、imagepak-base的安装
[]#tar –xvzf imagepak-base.tar.gz
解压以后是base目录
[]#cp –R base /usr/local/nagios/share/images/logos
2.4、安装过程全部结束
4、Nagios的设定:
1、配置apache
在apache的配置文件httpd.conf中追加 (注意,要有image这段,不然nagios不能显示图片,这三段顺序好象不能变,不然启动nagios的时候会提示错误)
Alias /nagios/cgi-bin/images/ "/usr/local/nagios/share/images/"

AllowOverride None
Options None
Order allow,deny
Allow from all
AuthName "Nagios Access"
AuthType Basic
AuthUserFile /usr/local/nagios/etc/.htpasswd
Require valid-user

ScriptAlias /nagios/cgi-bin/ "/usr/local/nagios/sbin/"

AllowOverride None
Options None
Order allow,deny
Allow from all
AuthName "Nagios Access"
AuthType Basic
AuthUserFile /usr/local/nagios/etc/.htpasswd
Require valid-user

Alias /nagios/ "/usr/local/nagios/share/"

AllowOverride None
Options None
Order allow,deny
Allow from all
AuthName "Nagios Access"
AuthType Basic
AuthUserFile /usr/local/nagios/etc/.htpasswd
Require valid-user

2、设置访问权限
在/usr/local/nagios/share目录下
[]#/usr/local/www/bin/htpasswd –c /usr/local/nagios/etc/.htpasswd nagios
这个apache目录根据安装目录的不同而不同,主要所以用.htpasswd这个命令生成用户名和密码
3、配置nagios
3.1、在/usr/local/nagios/etc下是nagios的配置模板文件.cfg-sample,把.cfg-sample文件全部拷贝成.cfg
例如:[]#cp nagios.cfg-sample nagios.cfg
全部拷贝完成即可.
-----------------------------------------------------------
修改minimal.cfg,把里面所有定义command的全部注释掉
[]#vi /etc/minimal.cfg
(这个我注释掉好象有问题 待改正)
-----------------------------------------------------------
修改cgi.cfg  改use_authentication=1为use_authentication=0,即不用验证.不然有一些页面不会显示。
建立所有的cfg文件
[]#touch contactgroups.cfg contacts.cfg hostgroups.cfg hosts.cfg services.cfg timeperiods.cfg
[]#chown nagios.nagios contactgroups.cfg contacts.cfg hostgroups.cfg hosts.cfg services.cfg timeperiods.cfg
然后检查配置文件是否出错
/usr/local/nagios/bin/nagios –v /usr/local/nagios/etc/nagios.cfg
出现Total Warnings: 0
Total Errors:  0
为正常
出错的话,就是.cfg文件有问题 根据错误提示 一个个修正
3.5、启动后台进程
/usr/local/nagios/bin/nagios -d /usr/local/nagios/etc/nagios.cfg
/usr/local/etc/rc.d/nagios start
3.6、http://10.4.8.81/nagios/index.html
(如果有些页面看不到的话.可以在cgi.cfg文件中
把带有authorized的选项前的#号去掉即可)
这些配置完以后,基本的nagios配置完成。
5、NRPE的安装.及监按指令的布署
nrpe 安装与使用
1、远程主机的配置
1.1、安装openssl
[]#yum install openssl (openssl-devel等等)
1.2、安装nrpe与配置
[]#./configure --enable-ssl --with-ssl-lib=/usr/local/nagios/openssl/lib--with-kerberos-inc=/usr/local/nagios/openssl/include --enable-command-args
[]#make all
[]#cp sample-config/nrpe.cfg /usr/local/nagios/etc
[]#cp src/nrpe /usr/local/nagios/bin
[]#vi /usr/local/nagios/etc/nrpe.cfg
改成你允许的IP
allowed_hosts=127.0.0.1,10.0.153.57(57是nagios服务器)
1.3、启动nrpe,端口为5666
/usr/local/nagios/bin/nrpe -c /etc/nrpe.cfg -d
1.4、安装监测的模块
nagios-plugins的安装
tar –xvzf nagios-plugins-1.4.tar
mkdir /usr/local/nagios-plugins
cd nagios-plugins-1.4
./configure --prefix=/usr/local/nagios-plugins
make all
make install
安装完成以后在/usr/local/nagios-plugins会产生一个libexec的目录,将该目录全部移动到/usr/local/nagios目录下
mv /usr/local/nagios-plugins/libexec /usr/local/nagios
2、服务器上的配置
2.1、安装openssl
[]#./config --prefix=/usr/local/nagios/openssl
[]#make
[]#make install
2.2、安装nrpe(主要是使用check_nrpe模块)
[]#./configure --enable-ssl --with-ssl-lib=/usr/local/nagios/openssl/lib --with-kerberos-inc=/usr/local/nagios/openssl/include --enable-command-args
[]#make all
[]#cp src/check_nrpe /usr/local/nagios/libexec
2.3、nagios文件的配置
vi checkcommands.cfg
定义check_nrpe命令
# 'check_nrep' command definition
define command{
command_name check_nrpe
command_line /usr/local/nagios/libexec/check_nrpe -H $HOSTADDRESS$ -c $ARG1$
}
定义检查远程主机服务
参见http://www.maxsworld.org/index.php/how-tos/nagios
Timeperiods
[me@mymachine nagios] vim timeperiods.cfg
# ‘24×7′ timeperiod definition
define timeperiod{
timeperiod_name 24×7
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” Working Hours
monday 08:00-17:00
tuesday 08:00-17:00
wednesday 08:00-17:00
thursday 08:00-17:00
friday 08:00-17:00
}
# ‘nonworkhours’ timeperiod definition
define timeperiod{
timeperiod_name nonworkhours
alias Non-Work Hours
sunday 00:00-24:00
monday 00:00-09:00,17:00-24:00
tuesday 00:00-09:00,17:00-24:00
wednesday 00:00-09:00,17:00-24:00
thursday 00:00-09:00,17:00-24:00
friday 00:00-09:00,17:00-24:00
saturday 00:00-24:00
}
# ‘none’ timeperiod definition
define timeperiod{
timeperiod_name none
alias No Time Is A Good Time
}
You can specify as many of these as you want. For instance, say you have a need to contact folks only on the weekends. You can create a template “weekends” and use only Friday, Saturday, Sunday with the appropriate times as you see fit.
Contacts/Contact Groups
Contacts are split into two different files. One holds the actual contact options, and the other holds contacts together in groups. The groups are whom you specify Nagios to contact later on.
[me@mymachine nagios] vim contacts.cfg
# service_notification_options are w,u,c,r,f,n
# w=warning u=unknown c=critical r=recovery f=flapping n=none
# host_notification_options d,u,r,f,n
# d=down u=unreachable r=recovery f=flapping n=none
define contact{
contact_name me
alias me
service_notification_period 24×7
host_notification_period 24×7
service_notification_options c,r
host_notification_options d,r
service_notification_commands notify-by-email
host_notification_commands host-notify-by-email
email me@myemailaddress.whatever
}
define contact{
contact_name you
alias you
service_notification_period workhours
host_notification_period workhours
service_notification_options c,r
host_notification_options d,r
service_notification_commands notify-by-email
host_notification_commands host-notify-by-email
email you@youremailaddress.whatever
}
You can choose to do as you wish, but for my purposes I only set contacts up to be notified on critical and recovery alerts. I really have no interest in most things I’m monitoring alerting me when there may be a temporary glitch, or when something is in a warning state, especially at 4:00 a.m. The reason I don’t is because a) I frequently check the Nagios CGI interface throughout the day, and b) all of my alerts get forwarded to a ticketing system. With that said, I don’t want unnecessary tickets being generated simply because a plugin failed to execute this time around. If I was very inspired, I could set up a separate contact and group to receive only the warning and unknowns, and then pipe these through a different e-mail address. Again, completely adaptive to your needs. Also, I’m using e-mail only. My e-mail system takes care of processing where the alerts are going. However, you could set up nagios to pipe messages straight to pagers. Again, check the object configuration options for timeperiods.cfg on the docs. If you want to see the commands being prosecuted for alerts, check out /etc/nagios/misccommands.cfg. On to the contact groups.
[me@mymachine nagios] vim contactgroups.cfg
# ‘einsteins’ contact group definitions
define contactgroup{
contactgroup_name einsteins
alias einsteins
members me,you
}
This is a simple example of contacts and contact groups. You can nest as many possibilities as you really want to. You can create as many contacts you need as well. It’s rather straightforward.
Hosts and Host Groups
Host and host group information is stored in the two files hosts.cfg and hostgroups.cfg. Just as you can mix and match contacts in various contact groups, you can do the same thing with host names in host groups. I prefer to create template configurations that I can leech off of later on in my configuration file. It saves you an incredible amount of time typing down the road.
[me@mymachine nagios] vim hosts.cfg
# Generic host definitions
define host{
name generic-host ; Generic template name
notifications_enabled 1 ; Host notifications are enabled
event_handler_enabled 1 ; Host event handler is enabled
flap_detection_enabled 1 ; Flap detection 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!
}
# This creates a generic template that any host can use.
# Notifies never, checks 15 times before showing critical on CGI interface,
define host{
name basic-host
use generic-host
check_command check-host-alive
max_check_attempts 15
notification_interval 0
notification_period none
notification_options n
register 0
}
# This creates a generic host that your routers can use
# monitors host(s) 24×7, notifies on down and recovery, checks 15 times before going critical,
# notifies the contact_group every 30 minutes
define host{
name your-routers-host
use generic-host
check_command check-host-alive
max_check_attempts 15
notification_interval 30
notification_period 24×7
contact_groups einsteins
notification_options d,r
register 0
}
define host{
use basic-host
host_name mymachine1
alias mymachine1
address 192.168.100.101
}
define host{
use your-routers-host
host_name router1
alias router1
address 192.168.100.100
}
You can begin to see how much time predefined templates can save you down the road when adding hosts. I’m monitoring around 100 hosts and over 200 services, so doing things the template way can really be productive in the long haul. It can get a little confusing, but stick to the docs and you’ll learn pretty quickly. When it comes to all of the template object options each file can contain, look for this http://localhost(or your IP)/nagios/docs/configobject.html. This will help you tremendously, because there are so many options Nagios allows you to choose from. I split things up because I want notifications on your-routers-host, but I don’t want notification on the basic-host container. Starting to understand why you use templates? Ok, now on to host groups.
[me@mymachine nagios] vim hostgroups.cfg
define hostgroup{
hostgroup_name basic-clients
alias basic clients
members mymachine1
}
define hostgroup{
hostgroup_name your-routers
alias routers
members router1
}
That’s about as simple as this can get. You specify your clients from hosts.cfg into host groups in this file. You can split them into multiple groups. For instance, mymachine1 can live within both the basic-clients and your-routers group if you so desired. Pretty simple…
Services
To start, you’re going to need at least one service to monitor. This would be a simple check-host-alive, or ping. Again, I split things into templates to make it easier down the road just as demonstrated above.
[me@mymachine nagios] vim services.cfg
# Generic service definition template
define service{
name generic-service ; Generic service name
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 (don’t disable)
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
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!
}
# Generic for all services
define service{
use generic-service
name basic-service
is_volatile 0
check_period 24×7
max_check_attempts 15
normal_check_interval 10
retry_check_interval 2
notification_interval 0
notification_period none
register 0
}
define service{
use basic-service
name ping-service
notification_options n
check_command check_ping!1000.0,20%!2000.0,60%
register 0
}
define service{
use ping-service
service_description PING
contact_groups einsteins
hostgroup_name basic-clients,your-routers
}
This is the classic example of how to nest templates. I’ve declared a general template to use called “basic-service” which leeches off of the “generic-service” definitions above that. The next defines a new service called “ping-service” which checks to see if host groups are alive specified with a simple ping. The reason I split this out is because say you want to create another host group called “your-switches,” but you want notifications to go out on this service to a different contact group. Then you just define another service and add this host group to that definition, and apply a different contact group. Ultimately, the last definitions override all other containers above it. Last man standing type deal. The last option Nagios sees, is the one it goes by. For example:
define service{
use ping-service
service_description PING
contact_groups group2
hostgroup_name your-switches
}
---------------------------------------------------------------------------------