设为首页 收藏本站
查看: 848|回复: 0

基于nagios网络监控平台的实现

[复制链接]

尚未签到

发表于 2019-1-14 14:32:05 | 显示全部楼层 |阅读模式
Nagios的安装
  本论文以如下三台主机为例进行事例搭建监控,三台主机详细信息如下
  表 5-1 三台事例主机详细信息
主机名

操作系统

IP

作用

nagios-server

redhat 9

192.168.77.128

监控机

linux-client

redhat 9

192.168.77.130

被监控机

window-client

xp

192.168.77.1

被监控机

注:

监控机:安装了nagios软件的机器,对监控的数据做处理,并且提供web界面查看和管理.当然也可以对本机自身的信息进行监控.

被监控机:安装了NRPE等客户端,根据监控机的请求执行监控,然后将结果回传给监控机.


准备工作
  本监控平台不需要特别的运行环境支持,只要求操作系统是主流Linux即可,本监控平台能很好的支持各类Linux的分支系统如RedHat、Fedora、opeensuse、Ubuntu等。在监控环境下,由于主监控机承担着揭示整个监控系统运行情况的工作,所以需要安装有ApaChe(或者其他http服务,大多数发行版Linux都已自带)用来提供一个直观的界面来查看所有的汇总监控信息。
1.前提条件

基于nagios是在linux主机上的一种开源软件,所以需要在linux系统的主机上才能搭建。

2.需要的软件

nagios-2.9.tar.gz(nagios主程序)

nagios-plugins-1.4.9.tar.gz(监控linux主机需要)

nrpe-2.8.1.tar.gz(监控linux主机需要))

  3.配置防火墙selinux
  如果系统开启了selinux服务,则默认为拒绝nagios web cgi程序的运行。从而无法执行监控页面,通过下面的命令来确认系统关闭了selinux:
  #setenforce 0
  想在以后完全关闭selinux,可以通过编辑/etc/sysconfig/selinux文件,将其中的selinux后面的值“force”修改为“disable”即可。


5.1.2安装依赖包,启动相关服务
1、 解决安装Nagios的依赖关系:

Nagios基本组件的运行依赖于httpd、gcc和gd。可以通过以下命令来检查nagios所依赖的rpm包是否已经完全安装:

# yum -y install httpd gcc glibc glibc-common gd gd-devel php php-mysql mysql mysql-server

此外,要想完整的安装关运行本文中所描述的环境,您得准备一个功能完备的LAMP运行环境。(linux+apache+mysql+php)

2.启动相关服务

因为nagios监控系统是基于web页面形式的,所以需要开始httpd服务,又因为之后要用到邮件报警所以,也需要邮件服务sendmail开始

5.1.3 Nagios监控系统的编译安装
  一、添加nagios运行所需要的用户和组:
  # groupadd nagcmd
  # useradd -m nagios
  # usermod -a -G nagcmd nagios
  把apache加入到nagcmd组,以便于在通过web Interface操作nagios时能够具有足够的权限:
  # usermod -a -G nagcmd apache
  二、编译安装nagios:
  # tar zxf nagios-3.1.2.tar.gz
  # cd nagios-3.1.2
  # ./configure --with-command-group=nagcmd --enable-event-broker
  # make all&& make install&& make install-init&& make install-config&& make install-commandmode
  说明:第一步执行make install安装主要的程序、CGI及HTML文件,第二步执行make install-init的步骤,它的作用是把nagios做成一个运行脚本,使nagios随系统开机启动,这是一个很方便的措施。第三步执行 make install-config 把配置文件的例子复制到nagios的安装目录。执行 make install-commandmode 给外部命令访问nagios配置文件的权限。
  

  为email指定您想用来接收nagios警告信息的邮件地址,默认是本机的nagios用户:
  # vi /usr/local/nagios/etc/objects/contacts.cfg
  email        nagios@localhost       #这个是默认设置
  在httpd的配置文件目录(conf.d)中创建Nagios的Web程序配置文件:
  # make install-webconf
  创建一个登录nagios web程序的用户,这个用户帐号在以后通过web登录nagios认证时所用:
  # htpasswd -c /usr/local/nagios/etc/htpasswd.users nagiosadmin
  以上过程配置结束以后需要重新启动httpd:
  # service httpd restart
  三、编译、安装nagios-plugins
  nagios的所有监控工作都是通过插件完成的,因此,在启动nagios之前还需要为其安装官方提供的插件。
  # tar zxf nagios-plugins-1.4.13.tar.gz
  # cd nagios-plugins-1.4.13
  # ./configure --with-nagios-user=nagios --with-nagios-group=nagios
  # make&& make install
  四、配置并启动Nagios
  (1)把nagios添加为系统服务并将之加入到自动启动服务队列:
  # chkconfig --add nagios
  # chkconfig nagios on
  (2)检查其主配置文件的语法是否正确:
  # /usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg
  (3)如果上面的语法检查没有问题,接下来就可以正式启动nagios服务了:
  # service nagios start
  (4)通过web界面查看nagios:
  http://192.168.77.128/nagios

5.1.4 Nagios的初始化前配置
1)预备知识
Nagios自己定义了一套规则用于配置文件,其中最重要的概念就是”对象”----object.通俗的理解:假定我们首先定义了”性别”这个对象,它的值只可能是男,女 ,然后定义某人为一个对象,例如张三,定义张三的时候有”性别”这个属性,它的值就必须来源了之前定义的性别这个对象,要么是男是女,在Nagios里面定义了一些基本的对象,一般用到的有:
  另外,多个被监控主机可以定义为一个主机组,多个联系人可以被定义为一个联系人组,多个服务还能定义成一个服务组呢.回到上面的例子,定义张三需要之前定义的性别,我们定义一个被监控的服务,当然就要指定被监控的主机,需要监控的时间段,要用哪个命令来完成这个监控操作,出了问题向哪个联系人报告.
2)修改配置文件-开启相应的配置
  vi /usr/local/nagios/etc/nagios.cfg
  /usr/loca check_external_commands=1//允许在web界面下执行重启nagios停止主机/服务检查等操作
  command_check_interval=10s(根据自己的情况定这个命令检查时间间隔,不要太长也不要太短)。
3)修改CGI脚本控制文件cgi.cfg
(确保use_authentication=1//验证)
//多个用户之间用逗号隔开(用户名来自htpasswd,不要添加过多)
authorized_for_system_information=nagiosadmin,用户名
4)定义监控时间段,创建配置文件timeperiods.cfg
5)定义联系人,创建配置文件contacts.cfg
6)下面就可以将多个联系人组成一个联系人组contactgroups.cfg
7)定义被监控主机,创建文件hosts.cfg
注意:按照对应的方式简单的复制修改主机名、IP和别名就可以创建多个被监控主机.
8)与联系人可以组成联系人组一样,多个主机也可以组成主机组. hostgrops.cfg
  注意:按照对应的方式简单的复制修改主机名、IP和别名就可以创建多个被监控主机组。
9)定义监控的项目,也叫服务,services.cfg
  注意:按照对应的方式简单的复制修改主机名、命令和组名就可以创建多个服务。并且主机名要与hosts.cfg文件中定义的一致
10)在运行nagios之前首先做测试
  /usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg
  

  看到下面这些信息就说明没问题了
  Total Warnings: 0
  Total Errors:0
  Things look okay - No serious problems were detected during the pre-flight check
11)作为守护进程后台启动nagios,并设置为开机自动启动
12)一切配置完成,即可登录页面查看监控信息
5.2使用Nrpe插件监控Linux主机上的本地信息
  对于像磁盘容量,cpu负载这样的”本地信息”,nagios只能监测自己所在的主机,而对其他的机器则显得有点无能为力.毕竟没得到被控主机的适当权限是不可能得到这些信息的.为了解决这个问题,nagios有这样一个附加组件----NRPE.用它就可以完成对linux类型主机”本地信息”的监控.
其中的具体原理已经在2.5节详细说明,此节重点说明具体的实现过程。
  5.2.1被监控linux主机上的 配置
1)增加用户并设置密码
  [root@dbpi root]# useradd nagios
  [root@dbpi root]# passwd nagios
2)编译安装nagios插件nagios-plugins
3)编译安装监控插件nrpe
4)编辑这个nrpe脚本
vi /etc/xinetd.d/nrpe
  only_from= 127.0.0.1 192.168.77.128(监控主机ip地址)//注意中间用空格而不是逗号间隔
5)编辑/etc/services文件,增加NRPE服务
Vi /etc/services
Nrpe 5666/tcp
6)重启xinetd服务
[root@dbpi nrpe-2.8.1]# service xinetd restart
7)查看NRPE是否已经启动
[root@dbpi nrpe-2.8.1]# netstat -at|grep nrpe
[root@dbpi nrpe-2.8.1]# netstat -an|grep 5666
8)测试NRPE是否则正常工作
/usr/local/nagios/libexec/check_nrpe -H localhost//正确结果会返回当前NRPE的版本
9)可查看或添加 NRPE的监控命令//在/usr/local/nagios/etc/目录下
ps:我的linux服务器端的配置文件代码如下
/usr/local/nagios/etc/nagios.cfg
/usr/local/nagios/etc/objects/localhost.cfg
# Define a host for the local machine
define host{
use         linux-server,hosts-pnp      ; 给网络设备用的
host_name       HomeSwitch
alias           HomeSwitch
address                192.168.77.128
icon_image              switch.gif
statusmap_image         switch.gd2
2d_coords       100,200
3d_coords       100,200,100
}
define host{
use                     linux-server,hosts-pnp
host_name               monitor
alias                   理工毕业设计
parents         HomeSwitch
address                 192.168.77.128
icon_image              web.gif
statusmap_image         web.gd2
2d_coords       100,300
3d_coords       100,300,100
}
#define host{
#        use                     linux-server
#        host_name               MyGamePC
#        alias                   MyGamePC
#   parents         HomeSwitch
#        address                 127.0.0.1
#        icon_image              server.gif
#        statusmap_image         server.gd2
#   2d_coords       200,300
#   3d_coords       200,300,100
#        }
#define host{
#        use                     linux-server
#        host_name                  HomeRouter
#        alias                   HomeRouter
#   parents         HomeSwitch
#        address                 127.0.0.1
#        icon_image              router.gif
#        statusmap_image         router.gd2
#   2d_coords       200,200
#   3d_coords       200,200,100
#        }
#define host{
#        use                     linux-server
#        host_name                  xDSL
#        alias                   xDSL
#   parents         HomeSwitch
#        address                 127.0.0.1
#        icon_image              router.gif
#        statusmap_image         router.gd2
#   2d_coords       200,100
#   3d_coords       200,100,100
#        }
#define host{
#        use                     linux-server
#        host_name                  Firewall
#        alias                   Firewall
#   parents         HomeRouter
#        address                 127.0.0.1
#        icon_image              router.gif
#        statusmap_image         router.gd2
#   2d_coords       300,200
#   3d_coords       300,200,100
#        }
#define host{
#        use                     linux-server
#        host_name                  RADIUS
#        alias                   RADIUS
#   parents         xDSL,MainSwitch
#        address                 192.168.2.1
#        icon_image              router.gif
#        statusmap_image         router.gd2
#   2d_coords       300,100
#   3d_coords       300,100,100
#        }
#
#define host{
#   use         linux-server        ; 给网络设备用的
#   host_name       MainSwitch
#   alias           MainSwitch
#   parents         Firewall
#        address                 127.0.0.1
#        icon_image              switch.gif
#        statusmap_image         switch.gd2
#   2d_coords       400,200
#   3d_coords       400,200,100
#   }
#define host{
#        use                     linux-server
#        host_name               IBME450
#        alias                   IBME450
#   parents         MainSwitch
#        address                 127.0.0.1
#        icon_image              server.gif
#        statusmap_image         server.gd2
#   2d_coords       300,300
#   3d_coords       300,300,100
#        }
#define host{
#        use                     linux-server
#        host_name                  SunUltra10
#        alias                   SunUltra10
#   parents         MainSwitch
#        address                 127.0.0.1
#        icon_image              server.gif
#        statusmap_image         server.gd2
#   2d_coords       400,300
#   3d_coords       400,300,100
#        }
#define host{
#        use                     linux-server
#        host_name                  Manager
#        alias                   Manager
#   parents         MainSwitch
#        address                 127.0.0.1
#        icon_image              server.gif
#        statusmap_image         server.gd2
#   2d_coords       500,200
#   3d_coords       500,200,100
#        }
###############################################################################
###############################################################################
#
# HOST GROUP DEFINITION
#
###############################################################################
###############################################################################
# Define an optional hostgroup for Linux machines
define hostgroup{
hostgroup_name  linux-servers ; The name of the hostgroup
alias           Linux Servers ; Long name of the group
members         *     ; Comma separated list of hosts that belong to this group
}
define hostgroup{
hostgroup_name 本地监控机器组
alias   毕业设计服务器
members monitor
}
define servicegroup{
servicegroup_name 系统负荷检查
alias 负荷检查
members monitor,进程总数,monitor,登录用户数,monitor,根分区,monitor,交换空间利用率
}
define servicegroup{
servicegroup_name 全部联通性检查
alias 联通性检查
members HomeSwitch,PING,monitor,PING
}
###############################################################################
###############################################################################
#
# SERVICE DEFINITIONS
#
###############################################################################
###############################################################################
# Define a service to "ping" the local machine
define service{
use                             local-service,services-pnp         ; Name of service template to use
host_name                       *
service_description             PING
check_command           check_ping!100.0,20%!500.0,60%
}
# Define a service to check the disk space of the root partition
# on the local machine.  Warning if < 20% free, critical if
# < 10% free space on partition.
define service{
use                             local-service         ; Name of service template to use
host_name                       monitor
service_description             根分区
check_command           check_local_disk!20%!10%!/
}
# Define a esrvice to check the number of currently logged in
# users on the local machine.  Warning if > 20 users, critical
# if > 50 users.
define service{
use                             local-service         ; Name of service template to use
host_name                       monitor
service_description             登录用户数
check_command           check_local_users!20!50
}
# Define a service to check the number of currently running procs
# on the local machine.  Warning if > 250 processes, critical if
# > 400 users.
define service{
use                             local-service,services-pnp         ; Name of service template to use
host_name                       monitor
service_description             进程总数
check_command           check_local_procs!250!400!RSZDT
}
# Define a service to check the load on the local machine.
define service{
use                             local-service,services-pnp         ; Name of service template to use
host_name                      monitor
service_description             系统负荷
check_command           check_local_load!5.0,4.0,3.0!10.0,6.0,4.0
}
# Define a service to check the swap usage the local machine.
# Critical if less than 10% of swap is free, warning if less than 20% is free
define service{
use                             local-service,services-pnp         ; Name of service template to use
host_name                      monitor
service_description             交换空间利用率
check_command           check_local_swap!20!10
}
# Define a service to check SSH on the local machine.
# Disable notifications for this service by default, as not all users may have SSH enabled.
define service{
use                             local-service,services-pnp         ; Name of service template to use
host_name                      monitor
service_description             SSH
check_command           check_tcp!22!1.0!10.0
notifications_enabled       0
}
# Define a service to check HTTP on the local machine.
# Disable notifications for this service by default, as not all users may have HTTP enabled.
define service{
use                             local-service,services-pnp        ; Name of service template to use
host_name                       monitor
service_description             HTTP
check_command           check_http
notifications_enabled       0
}
#网卡流量
define service{
use                             local-service,services-pnp
host_name monitor
service_description check_traffic
check_command check_traffic
max_check_attempts 5
normal_check_interval 3
retry_check_interval 2
check_period 24x7
notification_interval 10
notification_period 24x7
notification_options w,u,c,r
}
/usr/local/nagios/etc/objects/mylinux-client.cfg
define host{
use         linux-server
host_name   mylinux-client
alias       mylinux
address     192.168.77.130
#小太阳图标画图
process_perf_data  1
action_url  /nagios/pnp/index.php?host=$HOSTNAME$
}
define service{
use                 generic-service
#使用的是/usr/local/nagios /etc/objects/templates.cfg中所定义的
host_name           mylinux-client
service_description Swap Usage
check_command       check_nrpe!check_swap
}
define service{
use                 generic-service
host_name           mylinux-client
service_description Current Load
check_command       check_nrpe!check_load
}
define service{
use                 generic-service
host_name           mylinux-client
service_description Partition Usage
check_command       check_nrpe!check_disk
}
define service{
use                 generic-service
host_name           mylinux-client
service_description Current Users
check_command       check_nrpe!check_users
}
define service{
use                 generic-service
host_name           mylinux-client
service_description Total Processes
check_command       check_nrpe!check_total_procs
}
define service{
use                 generic-service
host_name           mylinux-client
service_description PING
check_command       check_nrpe!check_ping
}
define service{
use                    generic-service
host_name            mylinux-client
service_description  httpd
check_command     check_http! -H 192.168.77.130
#此时ip为监控对方客户端ip!
}
5.2.2 Nagios的监控主机上的配置
因为之前监控主机上,已经将nagios运行起来了,现在要做的事情是:
1) 安装check_nrpe插件并测试与被监控linux主机的联通性
监控机使用check_nrpe与被监控机(192.168.77.130)运行的nrpedaemon之间的通信.
/usr/local/nagios/libexec/check_nrpe -H 192.168.77.130
注意:监控主机与被监控主机使用的nrpe版本要一致!
2) 在commands.cfg中创建check_nrpe的命令定义
因为只有在commands.cfg中定义过的命令才能在services.cfg中使用
vi /usr/local/nagios/etc/commands.cfg(确定是在nagios.cfg定义的)
define command{
command_name check_nrpe
command_line $USER1$/check_nrpe -H $HOSTADDRESS$ -c $ARG1$
}


3)在services.cfg文件中创建对被监控主机的监控项目
4)在被监控机上增加check_swap命令的定义
vi /usr/local/nagios/etc/nrpe.cfg
增加下面这一行
command[check_swap]=/usr/local/nagios/libexec/check_swap -w 20% -c 10%
注意:如果你是以独立的daemon运行的nrpe,那么需要手动重启.如果你是在xinetd或者inetd下面运行的,则不需要.由于我们是xinetd下运行的,所以不需要重启服务
5.3使用NSClient插件监控windows主机系统
NSClient的原理如下图

根据前几个章节的具体描述可以看到,NSClient与nrpe最大的区别就是:
被监控机上安装有nrpe,并且还有插件,最终的监控是由这些插件来进行的.当监控主机将监控请求发给nrpe后,nrpe调用插件来完成监控.NSClient则不同,被监控机上只安装NSClient,没有任何的插件.当监控主机将监控请求发给NSClient后,NSClient直接完成监控,所有的监控是由NSClient完成的.这也说明了NSClient的一个很大的问题,不灵活,没有可扩展性.它只能完成自己本身包含的监控操作,不能由一些插件来扩展.好在NSClient已经做的不错了,基本上可以完全满足我们的监控需要.
监控 Windows 服务必须在被监控端装有插件程序,基于我校监控系统对所有 windows 服务器安全保护措施,在实现过程中仅以个人主机为被监控的windows客户端。:
5.3.1 Windows 服务器被监控主机的配置
1.下载安装NSClient
http://sourceforge.net/projects/nscplus下载并解压安装,按一般windows软件进行安装即可。
2.在运行里面输入services.msc打开”服务” 看到下图就说明NSClient服务已经安装上了
  
3. 修改NSCliet++ 的配置文件NES.ini,默认安装在C盘下
在[modules]选项里,启用所有注释的配置命令,但保留 CheckWMI.dll 和 RemoteConfiguration.dll 的注释。
4.更改[Settings]选项
修改 allowd_host=192.168.77.128 //nagios 服务器的 ip 地址
5.修改[NSClient]选项
启用 port=12489 的配置,系统通过端口 12489 侦听,系统中取消对该端口的限制后再执行 nsclient 命令。
6.重启服务,确认服务开启,并处于监听状态
我的电脑—右键管理—服务和应用程序—启动NSClient++服务,打开cmd执行netstat –an查看监听状态
5.3.2 Nagios 服务器监控主机端的配置
对监控主机的配置
与之前的nrpe的过程类似,在监控主机上做的就3件事情
1.安装监控windows的插件(已经默认安装了,check_nt)
2.定义命令
3.定义要监控的项目
第一种方法:如果只是定义一台windows主机,那么可以使用templates.cfg里面的windows配置板,此处我只定义一台windows主机,所以我使用模板
1.首先更改nagios.cfg配置文件,让其支持windows.cfg定义对象,找到41行,去掉前面#
2.查看nagios的命令配置定义对象,看是否有check_nt的定义,如果没有,自行加入(此处commands.cfg文件中已有定义,不用自己编写)
3.定义主机,编辑objects/windows.cfg文件,此文件中,只需更改29行的ip地址即可,改成被监控端windows的(由于此文件中已经写好要监控的东西,所以不用再另行编写,如果想定义此文件中没有的内容,可自动编写)
Vim  /usr/local/nagios/etc/objects/windows.cfg
define host{
use     windows-server,hosts-pnp    ; Inherit default values from a template
host_name   zangwinserver   ; The name we're giving to this host
alias       My Windows Server   ; A longer name associated with the host
address     192.168.77.1; IP address of the host
process_perf_data 1
action_url /nagios/pnp/index.php?host=$HOSTNAME$
}
define hostgroup{
hostgroup_name  windows-servers ; The name of the hostgroup
alias       Windows Servers ; Long name of the group
}
define service{
use         generic-service
host_name       zangwinserver
service_description NSClient++ Version
check_command       check_nt!CLIENTVERSION
}
define service{
use         generic-service,services-pnp
host_name       zangwinserver
service_description Uptime
check_command       check_nt!UPTIME
}
define service{
use         generic-service,services-pnp
host_name       zangwinserver
service_description CPU Load
check_command       check_nt!CPULOAD!-l 5,80,90
}
define service{
use         generic-service,services-pnp
host_name       zangwinserver
service_description Memory Usage
check_command       check_nt!MEMUSE!-w 80 -c 90
}
define service{
use         generic-service,services-pnp
host_name       zangwinserver
service_description C:\ Drive Space
check_command       check_nt!USEDDISKSPACE!-l c -w 80 -c 90
}
define service{
use         generic-service,services-pnp
host_name       zangwinserver
service_description W3SVC
check_command       check_nt!SERVICESTATE!-d SHOWALL -l W3SVC
}
define service{
use         generic-service,services-pnp
host_name       zangwinserver
service_description Explorer
check_command       check_nt!PROCSTATE!-d SHOWALL -l Explorer.exe
}



  4.最后检查配置是否正确,并重启nagios服务。
  

5.4 监控网络核心交换机配置
1)编辑 Nagios 的主配置文件:
root@sz-yunfan:~# vi /usr/local/nagios/etc/nagios.cfg
启用下面这行配置
cfg_file=/usr/local/nagios/etc/objects/switch.cfg
2)配置 Nagios
需要做些对象定义用于监控新的交换机与路由器等网络设备。
打开 switch.cfg 配置文件进行配置
# vi /usr/local/nagios/etc/objects/switch.cfg
对被监控的交换机加一个新的主机对象定义。 编辑主机对象中的 host_name、alias 和 address 值。
define host{
use generic-switch
host_name 交换机名称
alias 交换机别名
address 核心交换机ip地址
hostgroups 主机组成员
}
3)监控丢包率和 RTA
增加服务定义配置,用于监控被 Nagios 监控主机到交换机的丢包率和平均回包周期 RTA,检测周期一般是每 5 分钟。
define service{
use generic-service
host_name RG6806E
service_description PING
check_command check_ping! 200.0,20%! 600.0,60%
normal_check_interval 5
retry_check_interval 1
}
Nagios 监控系统实现了网络服务系统重要信息的监控,提供了可靠的、稳定的应用服务,保证了网络服务的正常运行。解决了日常网络服务器管理的繁杂工作,释放了大量的人工管理工作时间。 Nagios 监控系统能准确记录、定位并及时报警,为解决网络操作系统故障提供了宝贵的时间,同时也将故障导致服务中断的时间降低至最低。有效、可靠的报警为网络服务的正常运行提供了良好的保障。
/usr/local/nagios/etc/nagios.cfg
define host{
use     generic-switch,hosts-pnp        ; Inherit default values from a template
host_name   switch      ; The name we're giving to this switch
alias       Linksys SRW224P Switch  ; A longer name associated with the switch
address     192.168.32.25       ; IP address of the switch
hostgroups  switches        ; Host groups this switch is associated with
}
define hostgroup{
hostgroup_name  switches        ; The name of the hostgroup
alias       Network Switches    ; Long name of the group
}
define service{
use         generic-service,services-pnp    ; Inherit values from a template
host_name       switch  ; The name of the host the service is associated with
service_description PING        ; The service description
check_command       check_ping!200.0,20%!600.0,60%  ; The command used to monitor the service
normal_check_interval   5       ; Check the service every 5 minutes under normal conditions
retry_check_interval    1       ; Re-check the service every minute until its final/hard state is determined
}
#监控SNMP状态信息
# Monitor uptime via SNMP
define service{
use         generic-service ; Inherit values from a template
host_name       switch
service_description Uptime
check_command       check_snmp! -C public -o sysUpTime.0
}
监控端口带宽和流量
define service{
use         generic-service ; Inherit values from a template
host_name       linksys-srw224p
service_description Port 1 Link Status
check_command       check_snmp!-C public -o ifOperStatus.1 -r 1 -m RFC1213-MIB
}
# Monitor bandwidth via MRTG logs
define service{
use         generic-service ; Inherit values from a template
host_name       linksys-srw224p
service_description Port 1 Bandwidth Usage
#check_command      check_local_mrtgtraf!/var/lib/mrtg/192.168.20.25_1.log!AVG!1000000,1000000!5000000,5000000!10
}
5.5 监控网络核心路由器配置
5.5 nagios报警功能
5.5.1邮箱报警方式
Nagios 的报警功能一直是它最强大的地方,Nagios 默认使用mail 命令发送报警邮件,RedHat默认安装并启动了sendmail,使用邮件报警,可以使用139 邮箱的短信通知功能,如此可以省得很大一部分资金的开销,测试你的Nagios 是否可以正常发送邮件:
[root@Nagios-Server ~]# echo "hehe" | mail -s "test" 1003028158@qq.com
如果测试没问题,你的Nagios 配置也没有问题,就可以正常使用了。
注意:发送的邮件一般会被当做垃圾邮件,存放在垃圾邮件箱。
5.5.2飞信报警方式
1.安装飞信机器人程序。
# tar -zxvf fetion_linux_2010...tar.gz(现在飞信用的是新版本)
# cp sms /usr/local/nagios/libexec
然后拷贝Library32下的所有库文件到 /lib 以及/usr/lib。 现在可以测试飞信是否能发信息。
# /usr/local/nagios/libexec/sms -f 号码 -p 飞信的密码 -t 接受信息的手机号码 -m ″信息内容″
注意:只能给好友发信息,发送成功后系统会有相应的提示。 可以使用 /usr/local/nagios/libexec/sms --help 获得飞信的使用帮助。
2.增加Nagios监控服务器的飞信报警命令。
(1)修 改/usr/local/nagios/etc/objects/command.cfg 文件在邮件报警相关定义 “# ′notify-service-by-email′”后,加入飞信报警的定义。
#notify-host-by-sms
define command{
command_name notify-host-by-sms
command_line /usr/local/nagios/libexec/sms -f号码 -p 飞信密码 -t $CONTACTPAGER$ -m ″$HOSTNAME$ $SERVICEDESC$ is $SERVICESTATE$ on $TIME$ result is $SERVICEOUTPUT$″ $CONTACTPAGER$
}
#notify-service-by-sms
define command{
command_name notify-service-by-sms
command_line /usr/local/nagios/libexec/sms -f号码 -p 飞信密码 -t $CONTACTPAGER$ -m ″$HOSTNAME$ $SERVICEDESC$ is $SERVICESTATE$ on $TIME$ result is $SERVICEOUTPUT$″ $CONTACTPAGER$
}
(2) 修 改/usr/local/nagios/etc/objects/contacts.cfg 文 件 , 定义报警短信的接受人。@163.com,报警短信接收手机号为18238811
define contact{
contact_name zang
use generic-contact
alias System-Admin
email o@163.com
pager
}
然后把zang添加到组中。
define contactgroup{
contactgroup_name admins
alias Nagios Administrators
members zang
}
(3)修 改/usr/local/nagios/etc/objects/templates.cfg模 版 文 件 。
增加飞信报警。
service_notification_commands notify-service-by-sms, noti-fy-service-by-email
host_notification_commands notify-host-by-sms, notify-service-by-email
保存以上修改后,重启Nagios服务。 至此基于飞信报警的Nagios服务器配置成功。 现在可以把Nagios监控的一个服务关闭,测试飞信报警。
在复杂的大型网络的管理中,利用飞信和Nagios网络监控系统进行管理,网络管理人员可大大减轻工作量,提高工作效率,最重要的是能够及时发现网络的异常,快速处理故障,保证正常教学工作的有效进行。
5.5.3关闭报警

报警是使用率最高的功能了,如果某些服务器需要送修,如果预期进行某些主机和服务的维护工作,我们可以提前关闭该主机或者服务的报警功能,不然你的邮箱会不停的接受报警邮件,默认5 分钟发送一封报警邮件,直到恢复为止(Nagios 誓不罢休的精神值得我们应用到学习中)。在Services 标签下,点击相应的服务名称,在右侧点击Disable notifications for this service,直接提交即可关闭,完成后,返回Services 标签,你会发现,在该服务前面有个小喇叭的符号,上面有个红色的X。
  四、Nagios性能分析图表的实现
1)安装PNP支持包rrdtool工具
[root@localhost aaa]# tar zxf rrdtool-1.4.5.tar.gz
[root@localhost aaa]# cd rrdtool-1.4.5
[root@localhost rrdtool-1.4.5]# ./configure --prefix=/usr/local/rrdtool ; make ; make install
2)安装PNP
[root@localhost rrdtool-1.4.5]# cd /aaa/
[root@localhost aaa]# tar zxf pnp-0.4.13.tar.gz
[root@localhost aaa]# cd pnp-0.4.13
[root@localhost pnp-0.4.13]# ./configure --with-nagios-user=nagios --with-nagios-group=nagios --with-rrdtool=/usr/local/rrdtool/bin/rrdtool --with-perfdata-dir=/usr/local/nagios/share/perfdata ; make all ; make install ; make install-config ; make install-init
3)配置PNP
A,创建默认配置文件
[root@localhost pnp-0.4.13]# cd /usr/local/nagios/etc/pnp/
[root@localhost pnp]# cp process_perfdata.cfg-sample process_perfdata.cfg
[root@localhost pnp]# cp npcd.cfg-sample npcd.cfg
[root@localhost pnp]# cp rra.cfg-sample rra.cfg
[root@localhost pnp]# chown -R nagios:nagios /usr/local/nagios/etc/pnp/
B,修改process_perfdata.cfg
[root@localhost pnp]# vi /usr/local/nagios/etc/pnp/process_perfdata.cfg
44 LOG_LEVEL = 2
4)修改Nagios主配置文件
A,增加小太阳图标
[root@localhost pnp]# cd /usr/local/nagios/etc/
[root@localhost etc]# vi templates.cfg
define host{
name hosts-pnp
register 0
action_url /nagios/pnp/index.php?host=$HOSTNAME$
process_perf_data 1
}
define service{
name services-pnp
register 0
action_url /nagios/pnp/index.php?host=$HOSTNAME$&srv=$SERVICEDESC$
process_perf_data 1
}
B、修改nagios.cfg
找到如下几行去掉注释
[root@localhost etc]# vi nagios.cfg
833 process_performance_data=1
845 host_perfdata_command=process-host-perfdata
846 service_perfdata_command=process-service-perfdata
C、修改commands.cfg
# 'process-host-perfdata' command definition
define command{
command_name process-host-perfdata
command_line /usr/bin/perl  /usr/local/nagios/libexec/process_perfdata.pl  
}
# 'process-service-perfdata' command definition
define command{
command_name process-service-perfdata
command_line /usr/bin/perl  /usr/local/nagios/libexec/process_perfdata.pl
}
D、修改hosts.cfg文件和services.cfg文件
[root@localhost ~]# vi /usr/local/nagios/etc/objects/hosts.cfg
define host{
use linux-server,hosts-pnp
host_name web
alias benet-web
address 192.168.1.1
}
define host{
use linux-server,hosts-pnp
host_name mysql
alias benet-mysql
address 192.168.1.2
}
define hostgroup{
hostgroup_name sa-servers
alias sa servers
members web,mysql
}
[root@localhost ~]# vi /usr/local/nagios/etc/objects/services.cfg
################- benet web -##########################
define service{
use local-service,services-pnp
host_name web
service_description PING
check_command check_ping!100.0,20%!500.0,60%
}
define service{
use local-service,services-pnp
host_name web
service_description SSH
check_command check_ssh
}
点击小太阳,出现对主机或服务的监控页面
  





运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-663269-1-1.html 上篇帖子: 运维监控系统介绍之Nagios 下篇帖子: CentOS安装Nagios网络监视工具
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表