发表于 2019-1-17 08:35:55

Nagios监控系统配置过程1

结合网上的资料整理而成





实验环境:nagios监控服务器为192.168.1.240
          主机名为nagios
         nagios被监控服务器为192.168.1.208
          主机名为apache
用到的软件包:httpd-2.2.6.tar.gz、imagepak-base.tar.gz、mysql-5.1.22-rc-linux-i686-icc-glibc23.tar.gz、nagios-2.9.tar.gz、nagios-plugins-1.4.9.tar.gz、nrpe-2.12.tar.gz、perl-stable.tar.gz、php-5.2.4.tar.bz2、pnp-latest.tar.gz、rrdtool-1.0.50.tar.gz

在监控服务器上进行下面的操作:
1.安装nagios主程序
1)解压缩:
tar -zxvf nagios-2.9.tar.gz
cd nagios-2.9
./configure --prefix=/usr/local/nagios --with-gd-lib=/usr/local/lib --with-gd-inc=/usr/local/include

2)创建用户并且设定权限:
groupadd nagios
useradd -g nagios nagios
mkdir /usr/local/nagios
chown -R nagios.nagios /usr/local/nagios

make all
make install             //来安装主程序,CGI和HTML文件
make install-init          //在/etc/rc.d/init.d安装启动脚本
make install-commandmode   //来配置目录权限
make install-config
//来安装示例配置文件,安装的路径是/usr/local/nagios/etc.

3)验证是否安装成功:
验证程序是否被正确安装。切换目录到安装路径(这里是/usr/local/nagios),看是否存在 etc、bin、 sbin、 share、 var这五个目录,如果存在则可以表明程序被正确的安装到系统了。后表是五个目录功能的简要说明:
bin
Nagios执行程序所在目录,nagios文件即为主程序
etc
Nagios配置文件位置,初始安装完后,只有几个*.cfg-sample文件
sbin
Nagios Cgi文件所在目录,也就是执行外部命令所需文件所在的目录

share
Nagios网页文件所在的目录
var
Nagios日志文件、spid 等文件所在的目录

var/archives
Empty directory for the archived logs
/var/rw
Empty directory for the external command file


2.安装插件
1)解压缩:
tar -zxvf nagios-plugins-1.4.9.tar.gz
cd nagios-plugins-1.4.9
./configure--prefix=/usr/local/nagios/
(在redhat系统上面安装可能出现configure时,到这里checking for redhat spopen problem...就不动了,所以需要在configure时再加上--enable-redhat-pthread-workaround )
make
make install
ls /usr/local/nagios/libexec/
会显示安装的插件文件,即所有的插件都安装在libexec这个目录下

注意:要是没有这个插件目录需要用下面的命令把插件复制过来
cp /usr/local/nagios-plugins/libexec /usr/local/nagios/

2)将apache的运行用户加到nagios组里面 :
从httpd.conf中过滤出当前的apache运行用户
grep ^User /usr/local/apache2/conf/httpd.conf

我的是daemon,下面将这个用户加入nagios组
usermod -G nagios daemon

3)修改apache配置:
修改apache的配置文件,增加nagios的目录,并且访问此目录需要进行身份验证
vi /usr/local/apache2/conf/httpd.conf,在最后增加如下内容 :

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

    Options ExecCGI
    AllowOverride 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

    Options None
    AllowOverride None
    Order allow,deny
    Allow from all
    AuthName "Nagios Access"
    AuthType Basic
AuthUserFile /usr/local/nagios/etc/htpasswd//用于此目录访问身份验证的文件
Require valid-user


4)增加验证用户:
也就是通过web访问nagios的时候,必须要用这个用户登陆.在这里我们增加用户test:密码为123456
#/usr/local/apache2/bin/htpasswd -c /usr/local/nagios/etc/htpasswd test   //用户名
New password: //(输入123456)
Re-type new password: (再输入一次密码)
Adding password for user test

5)查看认证文件的内容:
# less /usr/local/nagios/etc/htpasswd
test:OmWGEsBnoGpIc//前半部分是用户名test,后面是加密后的密码

到这里nagios的安装也就基本完成了,你可以通过web来访问了.
http://192.168.1.240/nagios 会弹出对话框要求输入用户名密码
输入test,密码123456,就可以进入nagios的主页面了
但是可以发现什么也点不开,因为nagios还没启动呢!下面的工作就是修改配置文件,增加要监控的主机和服务

3.典型配置
nagios要用起来,就必须修改配置文件,增加要监控的主机和服务才行.在具体做这个动作之前,下面的概念必须要了解.

1)预备知识 :
在Nagios里面定义了一些基本的对象,一般用到的有:
联系人
contact
出了问题向谁报告?一般当然是系统管理员了
监控时间段
timeperiod
7X24小时不间断还是周一至周五,或是自定义的其他时间段
被监控主机
Host
所需要监控的服务器,当然可以是监控机自己
监控命令
command
nagios发出的哪个指令来执行某个监控,这也是自己定义的
被监控的服务
Service
例如主机是否存活,80端口是否开,磁盘使用情况或者自定义的服务等

注意:多个被监控主机可以定义为一个主机组,多个联系人可以被定义为一个联系人组

2)将示例配置文件复制为真实配置文件名:

cd /usr/local/nagios/etc
把这里.cfg-sample文件配置文件模板,全部重命名为.cfg

3)修改配置文件:
修改nagios的主配置文件nagios.cfg
vi nagios.cfg
cfg_file=/usr/local/nagios/etc/localhost.cfg      //在前面加#
cfg_file=/usr/local/nagios/etc/contacts.cfg    //联系人配置文件路径
cfg_file=/usr/local/nagios/etc/contactgroups.cfg //联系人组配置文件路径
cfg_file=/usr/local/nagios/etc/commands.cfg      //命令配置文件路径
cfg_file=/usr/local/nagios/etc/host.cfg          //主机配置文件路径
cfg_file=/usr/local/nagios/etc/hostgroups.cfg    //服务器组配置文件
cfg_file=/usr/local/nagios/etc/templates.cfg   //模板配置文件路径
cfg_file=/usr/local/nagios/etc/timeperiods.cfg   //监视时段配置文件路径
cfg_file=/usr/local/nagios/etc/services.cfg      //服务配置文件

其他配置文件以实际情况来进行配置

改check_external_commands=0为check_external_commands=1 .这行的作用是允许在web界面下执行重启nagios、停止主机/服务检查等操作。

把command_check_interval的值从默认的1改成command_check_interval=10s(根据自己的情况定这个命令检查时间间隔,不要太长也不要太短)。
主配置文件要改的基本上就是这些,通过上面的修改,发现/usr/local/nagios/etc并没有文件hosts.cfg等一干文件,怎么办?稍后手动创建它们。

4)然后检查配置文件是否出错
/usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg

出现Total Warnings: 0
      Total Errors:   0    //这样表示配置文件没有错误

5)修改CGI脚本控制文件cgi.cfg

#vi cgi.cfg
第二个要修改的配置文件是cgi.cfg,它的作用是控制相关cgi脚本。先确保use_authentication=1。接下来修改default_user_name=test(前面创建的用户名) ,再后面的修改在下表列出:
authorized_for_system_information=nagiosadmin,mandahang,test //后面跟的都是用户名
authorized_for_configuration_information=nagiosadmin,mandahang,test
authorized_for_system_commands=mandahang,test authorized_for_all_services=nagiosadmin,mandahang,test
authorized_for_all_hosts=nagiosadmin,mandahang,test
authorized_for_all_service_commands=nagiosadmin,mandahang,test
authorized_for_all_host_commands=nagiosadmin,mandahang,test

注意:在上面的配置文件里面加上新加的用户test

那么上述用户名打那里来的呢?是执行命令 /usr/local/apache2/bin/htpasswd -c /usr/local/nagios/etc/htpasswd test(用户名test) 所生成的

6)配置各种配置文件

定义监控时间段,创建配置文件timeperiods.cfg :
# vi timeperiods.cfg
define timeperiod{
      timeperiod_name         24x7   //时间段的名称,这个地方不要有空格
      alias                   24 Hours A Day,7Days 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
      }

定义了一个监控时间段,它的名称是24x7,监控的时间是每天全天24小时:

定义联系人,创建配置文件contacts.cfg
# vi contacts.cfg
define contact{
      contact_name                  test //联系人的名称,这个地方不要有空格
      alias                           sys admin
      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
      host_notification_commands      host-notify-by-email      
email                           tfhudong@sohu.com
      pager                           1391119xxxx
      }

创建了一个名为test的联系人,下面列出其中重要的几个选项做说明:

service_notification_period   24x7
服务出了状况通知的时间段,这个时间段就是上面在timeperiods.cfg中定义的.

host_notification_period      24x7
主机出了状况通知的时间段, 这个时间段就是上面在timeperiods.cfg中定义的

service_notification_options    w,u,c,r
当服务出现w—报警(warning),u—未知(unkown),c—严重(critical),或者r—从异常情况恢复正常,在这四种情况下通知联系人.

host_notification_options       d,u,r
当主机出现d­­­­—当机(down),u—返回不可达(unreachable),r—从异常情况恢复正常,在这3种情况下通知联系人

service_notification_commands   notify-by-email
服务出问题通知采用的命令notify-by-email,这个命令是在commands.cfg中定义的,作用是给联系人发邮件.

host_notification_commands      host-notify-by-email
同上,主机出问题时采用的也是发邮件的方式通知联系人

email                           yahoon@test.com
很明显,联系的人email地址
Pager                           137xxxxxxxxxxxxxx//电话
}

下面就可以将多个联系人组成一个联系人组,创建文件contactgroups.cfg :

# vi contactgroups.cfg
define contactgroup{
      contactgroup_name       sagroup    //联系人组的名称,同样不能空格
      alias                   System Administrators      //别名
      members               test
//组的成员,来自于上面定义的contacts.cfg,如果有多个联系人则以逗号相隔
      }

定义被监控主机,创建文件hosts.cfg :
# vi hosts.cfg
define host{
      host_name      nagios //被监控主机的名称,最好别带空格
      alias            nagios //别名
      address   192.168.1.240 //被监控主机的IP地址      check_command    check-host-alive
      //监控的命令check-host-alive,这个命令来自commands.cfg,用来监控主机是否存活
      max_check_attempts    5 //检查失败后重试的次数
      check_period          24x7
      //检查的时间段24x7,同样来自于我们之前在timeperiods.cfg中定义的
contact_groups      sagroup
       //联系人组,上面在contactgroups.cfg中定义的sagroup
notification_interval10
      //提醒的间隔,每隔10秒提醒一次
notification_period   24x7
      //提醒的周期, 24x7,同样来自于我们之前在timeperiods.cfg中定义的
notification_options            d,u,r
//指定什么情况下提醒
      }

通过简单的复制修改就可以定义多个主机了.我们在这加上另外一台机器

与联系人可以组成联系人组一样,多个主机也可以组成主机组:
主机名为:apache         ip:192.168.1.208

创建文件hostgrops.cfg
# vi hostgroups.cfg
define hostgroup{
      hostgroup_name          linux-servers//主机组名称
      alias                   linux-servers//别名
      members               nagios,apache
//组的成员主机,多个主机以逗号相隔,必须是上面hosts.cfg中定义的
      }

下面是最关键的了,用nagios主要是监控一台主机的各种信息,包括本机资源,对外的服务等等.这些在nagios里面都是被定义为一个个的项目(nagios称之为服务,为了与主机提供的服务相区别,我这里用项目这个词),而实现每个监控项目,则需要通过commands.cfg文件中定义的命令.
例如我们现在有一个监控项目是监控一台机器的web服务是否正常, 我们需要哪些元素呢?最重要的有下面三点:首先是监控哪台机,然后是这个监控要用什么命令实现,最后就是出了问题的时候要通知哪个联系人?

定义监控的项目,也叫服务,创建services.cfg :

# vi services.cfg
define service{
      host_name               nagios
      //被监控的主机,hosts.cfg中定义的
      service_description   check-host-alive
      //这个监控项目的描述,这个会在web页面中出现      check_command         check-host-alive
       //所用的命令,是commands.cfg中定义的
      max_check_attempts      5    //重试的次数
      normal_check_interval   3    //循环检查的间隔时间
      retry_check_interval    2   
      check_period            24x7
      //监控的时间段,是timeperiods.cfg中定义的
      notification_interval   10
      notification_period   24x7
      //通知的时间段
notification_options    w,u,c,r
      //在监控的结果是wucr时通知联系人
      contact_groups          sagroup
      //联系人组,是contactgroups.cfg中定义的
      }

这样整个的配置过程就结束了.虽然功能很简单,但是已经为以后扩展打下了良好的基础.

在运行nagios之前首先做测试

/usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg
看到下面这些信息就说明没问题了

Total Warnings: 0
Total Errors:   0

作为守护进程后台启动nagios
/usr/local/nagios/bin/nagios -d /usr/local/nagios/etc/nagios.cfg

登陆http://192.168.1.240/nagios/来查看吧.点左边的Host Detail



页: [1]
查看完整版本: Nagios监控系统配置过程1