环境:CentOS-5.5



需要的安装包:

apache(httpd-2.2.19.tar.gz),

nagios2.6.tar.gz,

nagios-plugins-1.4.15.tar.gz,

nagios-cn-3.2.3.tar.bz2

nagios下载地址:http://sourceforge.jp/projects/sfnet_nagios/releases/

其他可以去官网下载:www.nagios.org



一. 安装nagios

1.先创建nagios用户和用户组

将nagios进程的运行用户和组设置为nagios,并且将nagios的主程序目录设置为nagios保证安全

useradd -s /sbin/nologin nagios

mkdir /usr/local/nagios

chown -R nagios.nagios /usr/local/nagios

2.编译nagios

tar -zxvf nagios2.6.tar.gz

cd nagios2.6

./configure --prefix=/usr/local/nagios

make all

make install 安装nagios主程序的CGI和html文件

make install-init 在/etc/rc.d/init.d 目录下创建nagios启动脚本

make install-commandmode 配置目录权限

make install-config 安装nagios示例配置文件,安装路径是/usr/local/nagios/etc

验证是否正确安装,可以查看/usr/local/nagios下是否有以下目录:

bin Nagios执行程序所在目录,这个目录只有一个文件nagios
etc Nagios配置文件位置,初始安装完后,只有几个*.cfg-sample文件
sbin Nagios Cgi文件所在目录,也就是执行外部命令所需文件所在的目录
Share Nagios网页文件所在的目录
Var Nagios日志文件、spid 等文件所在的目录


看到这5个目录,nagios应该就算安装完成了。

3.安装插件

nagios提供的各种监控功能基本都是插件来完成的,而nagios核心只提供了很少的监控功能,因此安装插件是必须的,这里下载的是 nagios-plugins-1.4.15.tar.gz,插件与nagios之间的版本关联不大,不一定非得用nagios-plugins-1.4.5这个版本

tar -zxvf nagios-plugins-1.4.15.tar.gz

cd nagios-plugins-1.4.15

./configure --prefix=/usr/local/nagios

make

make install

说明一下,插件安装的路径最好和nagios安装路径一样,安装完成之后会在/usr/local/nagios下生libexec目录,里面的可执行文件就是nagios所需的插件。

4.安装汉化插件

安装nagios-cn-3.2.3.tar.bz2

tar -jxvf nagios-cn-3.2.3.tar.bz2

cd nagios-cn-3.2.3

./configure

make all

make install

5.安装和配置apache

Web服务不是nagios所必须的,但是nagios提供了web监控页面,如果没有web查看监控对象的状态将是非常费事和没有趣味的事情,可以选择apache,nginx,这里我选择apache,版本是httpd-2.2.19

这里需要注意一下,nagios在nagios 3.1x版本以后,配置web监控界面时需要php的支持,这里我用的是nagios2.6版本,因此不需要安装php。

tar zxvf httpd-2.2.19.tar.gz

cd httpd-2.2.19

./configure --prefix=/usr/local/apache

make

make install



如果需要安装,可以参照以下步骤:

tar zxvf php-5.3.2.tar.gz

cd php-5.3.2

./configure --prefix=/usr/local/php --with-apxs2=/usr/local/apache/bin/apxs --with-zlib --with-gd

make

make install

接着需要配置apache,进入/usr/local/apache/conf/httpd.conf

修改:

User nagios

Group nagios

然后找到:

DirectoryIndex index.html index.html.var

修改为:

DirectoryIndex index.html index.php

接着增加:

AddType application/x-httpd-php .php



安全起见,nagios的目录需要进行用户验证,只有合法的授权用户才可以访问nagios的页面文件,因此需要增加验证配置,在httpd.conf文件最后添加: #setting for nagios
ScriptAlias /nagios/cgi-bin "/usr/local/nagios/sbin"
<Directory "/usr/local/nagios/sbin"> //Cgi文件所在目录
AuthType Basic
Options ExecCGI
AllowOverride None
Order allow,deny
Allow from all
AuthName "Nagios Access"
AuthUserFile /usr/local/nagios/etc/htpasswd //验证文件路径
Require valid-user
</Directory>

Alias /nagios "/usr/local/nagios/share"
<Directory "/usr/local/nagios/share"> //nagios页面文件目录
AuthType Basic
Options None
AllowOverride None
Order allow,deny
Allow from all
AuthName "nagios Access"
AuthUserFile /usr/local/nagios/etc/htpasswd //验证文件路径
Require valid-user
</Directory>

创建apache目录验证文件,上面的配置已经指定了目录验证文件的路径已经文件名
/usr/local/apache/bin/htpasswd –c /usr/local/nagios/etc/htpasswd sery
New password:
Re-type new password:
Adding password for user sery
这样就在/usr/local/nagios/etc/目录下创建了一个htpasswd验证文件,用户名是sery

到这里,就可以启动apache
/usr/local/apache/bin/apachectl -k start
然后输入网址:http://IP/nagios 然后需要输入账号和密码,就能看见nagios页面了.
二.配置nagios
nagios默认的配置文件在/usr/local/nagios/etc目录下,有5个文件,说明如下:
cgi.cfg-sample 控制CGI访问的配置文件
commands.cfg-sample 命令定义配置文件,其中定义的命令可以被其他配置文件引用
localhost.cfg-sample 监控本地主机的配置文件
nagios.cfg-sample nagios主配置文件
resource.cfg-sample 变量定义文件,在此文件中定义变量,以便由其他配置文件引用,如$USER1$
我们先把这些文件重命名:cp cgi.cfg-sample cgi.cfg 其他文件一样操作

nagios配置过程设计的定义有:主机、主机组、服务、服务组、联系人、联系人组、监控时间和监控命令,各个配置文件之间是互为关联彼此引用的,成功配置一台nagios监控系统,需要弄清楚每个配置文件之间的关系,最重要的是4点:第一要定义监控哪些主机、主机组、服务和服务组,第二要定义这个监控要通过什么命令监控,第三要定义监控时间段,最后定义主机或服务出现问题时需要通知的联系人和联系人组。
1.先修改主配置文件nagios.cfg
进入nagios.cfg,注释掉:# cfg_file=/usr/local/nagios/etc/localhost.cfg
然后去掉以下注释:cfg_file=/usr/local/nagios/etc/contactgroups.cfg //联系组配置文件路径
cfg_file=/usr/local/nagios/etc/contacts.cfg //联系人配置文件路径
cfg_file=/usr/local/nagios/etc/hostgroups.cfg //主机组配置文件路径
cfg_file=/usr/local/nagios/etc/hosts.cfg //主机配置文件路径
cfg_file=/usr/local/nagios/etc/services.cfg //服务配置文件路径
cfg_file=/usr/local/nagios/etc/timeperiods.cfg //监视时段配置文件路径

修改check_external_commands=0为check_external_commands=1 .这行的作用是允许在web界面下执行重启nagios、停止主机/服务检查等操作
把command_check_interval的值从默认的1改成command_check_interval=10s(根据自己的情况定这个命令检查时间间隔,不要太长也不要太短)
2.修改cgi.cfg文件
cgi.cfg它的作用是控制相关cgi脚本,如果想在nagios的web监控界面执行CGI脚本,例如重启nagios进程、关闭nagios通知、停止nagios主机检测,这是就需要配置cgi.cfg文件,由于nagios的web监控界面验证用户为sery,因此只需在文件中添加此用户,先确保use_authentication=1 这是个需要验证的意思.
接着修改 default_user_name=sery 之后还要修改如下: authorized_for_system_information=nagiosadmin,sery
authorized_for_configuration_information=nagiosadmin,sery
authorized_for_system_commands=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

上面的用户名sery是之前用命令:/usr/local/apache/bin/htpasswd –c /usr/local/nagios/etc/htpasswd sery 得来的
3.增加新的文件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
}


以上是建立7x24小时监控

4.增加contacts.cfg文件

define contact {
contact_name sery //联系人名称
alias system administrator
service_notification_period 24x7 //当服务出现异常时,发送通知的时间段,是在timeperiods.cfg中定义
host_notification_period 24x7 //当主机出现异常时,发送通知的时间段,是在timeperiods.cfg中定义
service_notification_options w,u,c,r
host_notification_options d,u,r
service_notification_commands notify-by-email //服务故障发送通知方式,可以是短信和邮件,这里是邮件发送,notify-by-email 是在commands.cfg文件中定义
host_notification_commands host-notify-by-email //主机故障发送通知方式,可以是短信和邮件,这里是邮件发送,host-notify-by-email是在commands.cfg文件中定义
email sery@sohu.com
}
如果有更多联系人的话,照这个格式在后面追加即可.服务通知选项(service_notification_options),主机通知选项(host_notification_options)几个选项在这里说明一下:
w-warning(警告), u-unknown(不明状态),c-critical(紧急状态),r-recover(恢复状态);
d-down(宕机状态),u-unreachable(不可到达状态),r-recovery(重新恢复),注意一下,主机报警和服务报警有些差异。
5.手动创建contactgroups.cfg define contactgroup {
contactgroup_name sagroup //不要用空格
alias system administrator group
members sery //本例有2个成员
}

6.配置hosts.cfg

define host {
host_name nagios-server
alias nagios server
address 10.x.x.113
contact_groups sagroup //多个联系组用逗号分隔,数据来源于contactgroups.cfg
check_command check-host-alive //指定检查主机状态的命令,check-host-alive在commands.cfg中定义
max_check_attempts 5 //nagios对主机的最大检查次数,也就是nagios在检查过程中发现主机异常时,并不马上判断为异常状况,而是多试几次,因为可能只是一时网络太拥挤或是其他一些原因,使主机受到一些影响,这里的5就是至少尝试5次的意思
notification_interval 10 //在主机出现异常后,一直没有解决和恢复,nagios再次对使用者发出通知的时间,单位是分钟,如果只需要通知一次,可以设为0
notification_period 24x7 //主机故障时,发送通知的时间范围,在timeperiods.cfg中进行定义
notification_options d,u,r //定义主机在什么状态下发送通知给使用者d-down(宕机状态),u-unreachable(不可到达状态),r-recovery(重新恢复)
}
define host {
host_name test-200
alias server 200
address 10.x.x.200
contact_groups sagroup
check_command check-host-alive //down机就发报警通知
max_check_attempts 5
notification_interval 10
notification_period 24x7
notification_options d,u,r
}

7.配置services.cfg
define service {
host_name nagios-server //来源:hosts.cfg
service_description check_tcp 80
check_period 24x7
max_check_attempts 4 //nagios对服务的最大检查次数
normal_check_interval 3 //服务检查时间间隔,也就是说,nagios这一次检查与下一次检查所隔的时间,这里是3分钟
retry_check_interval 2 //重试检查时间间隔,单位分钟
contact_groups sagroup //来源:contactgroups.cfg 指定联系人组
notification_interval 10
notification_period 24x7
notification_options w,u,c,r
check_command check_tcp!80 //检查tcp 80端口是否存活
}
define service {
host_name test-200
service_description check_tcp 80
check_period 24x7
max_check_attempts 4
normal_check_interval 3
retry_check_interval 2
contact_groups sagroup
notification_interval 10
notification_period 24x7
notification_options w,u,c,r
check_command check_tcp!80 //检查tcp 80端口服务是否正常
}

8.配置hostgroups.cfg
define hostgroup {
hostgroup_name sa-servers
alias sa servers
members nagios-server,test-200//用逗号间隔多个主机
}

9.检查配置
/usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg
10.启动和关闭
/etc/rc.d/init.d/nagios reload
/etc/rc.d/init.d/nagios restart

/etc/init.d/nagios start
/etc/init.d/nagios stop

service nagios start
service nagios stop
service nagios restart