当前位置:文档之家› nagios平台搭建

nagios平台搭建

cacti与nagios的整合

Nagios官方平台:https://www.doczj.com/doc/3316075896.html,/

Cacti官方平台:https://www.doczj.com/doc/3316075896.html,/

插件、软件下载https://www.doczj.com/doc/3316075896.html,本人实验用的软件基本上都是在这里下的实验是按照本人软件安装的路径进行测试,包括文档内写的命令路径,所以大家自己安装或者正式环境需要根据自己软件的路径来测试

对于监控来说,nagios绑定cacti,可以实现所有系统和平台的监控需求,但是配置非常复杂,由于是开源产品,各个模块和系统插件的版本非常多,每个版本相互兼容不太一样,本人统一了所有整合所需的软件及其版本,若使用其他版本,可能会有异常。

需要的软件:

nagios-3.2.3.tar.gz

nagios-plugins-1.4.15.tar.gz

ndoutils-1.4b9.tar.gz

nrpe-2.12.tar.gz

NSClient0.3.9-Win32.zip

php-json-ext-1.2.1.zip

cacti-0.8.7i.tar.gz

ntop-4.0.1.tar.gz

npc-2.0.4.tar.gz

CactiEZ-10.1-i386.iso(注:这是cacti的中文版,直接捆绑了Linux系统,直接安装,无需配置)

本文中所涉及到的插件和rpm包,RHEL5.5版本都有自带。

本文部分图片来自网络,可能有部分服务器名或IP地址无法对应文章内容,但不影响配置实验环境:RHEL5.5系统是在vmware环境下搭建的

注:

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

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

网络管理(如主机负载、网络流量等的监控)是系统管理岗工作人员日常工作的一个重要组成部分,许多厂商为此提供了各自的解决方案。早期开源网管软件中比较著名的其中之一的MRTG,是一款使用perl开发的,通过SNMP协议实现管理工作站与设备代理进程间的通讯,以完成对设备的管理和运行状态的监视。MRTG安装配置简单、图形界面直观。

但MRTG也有许多固有的缺点,如:使用文本式的数据库,数据不能重复使用;只能按日、周、月、年来查看数据;每图只能画两个DS(一条线、一个块);每取一次数据即需要绘图一次,浪费系统资源;同时,它也没有提供管理功能。

有鉴于此,MRTG的作者后来另外又开发了一个工具,即rrdtool。rrdtool是一个性能优良的数据记录器,同时也是一个功能强大的绘图引擎。它使用rrd的数据存储格式,使用得数据可以重复使用;在绘图方面,它可以定义任意时间段进行绘图,能绘出多个DS。但它也有自己的缺点:虽然提供了强大的数据存储及绘图功能,但rrdtool却没有提供类似于MRTG中集成的数据采集功能;同时,它提供了过多的参数,以至于在命令行界面使用起来极为不便;此外,rrdtool也没有提供管理功能。于是cacti应运而生。

简单来说,Cacti就是rrdtool的一个forefront,它内置了快速的获数据取工具、优秀的绘图模板以及许多设计精良的数据获取脚本,从而可以通过结合rrdtool强大的数据抓取、数据存储和绘图功能,轻松实现主机负载、网络流量等信息的走势图的绘制。

cacti是用php语言实现的一个软件,它的运行需要网站服务器(如apache)及PHP 环境的支持。同时,cacti还需要Mysql配合PHP程序存储一些变量数据并对变量数据进行调用,如:主机名、主机ip、snmp团体名、端口号、模板信息等变量。

rrdtool的对主机负载、网络流量等信息的统计需要通过SNMP协议实现。经过SNMP 抓取到的数据存储于由rrdtool生成的rrd文件中,这些文件通常位于cacti的rra目录中。rrdtool对数据的更新和存储就是对rrd文件的处理,rrd文件是大小固定的归档文件(Round Robin Archive),它能够存储的数据笔数在创建时就已经定义。

这副图片简述了cacti的工作原理.

Cacti的实例应用

1)网络设置

2)主机系统

(1)网络接口流量(进与出的带宽)

(2)监控CPU的负载、内存等等

(3)监控磁盘的空间、进程数等等.........................................等等

3)cacti常见的监测对象

(1)服务器资源:CPU、内存、磁盘、进程、连接数等

(2)服务器类型:WEB、Mail、FTP、数据库、中间件

(3)网络接口:流量、转发速度、丢包率

(4)网络设备性能、配置文件(对比与备份)、路由数

(5)安全设备性能、连接数、攻击数

(6)设备运行状态:风扇、电源、温度

(7)机房运行环境:电流、电压、温湿度

Nagios简介

Nagios是一个用来监视系统和网络的开源应用软件,它通常运行于一个主服务器上,这个服务器运行Liunx 或Unix 操作系统。Nagios 利用其众多的插件实现对本机和远端服务的监控,当被监控对象出现异常,Nagios 就会及时给管理人员告警。它是一个基于TCP/IP协议的软件包,包含有nagios主程序和它的各个插件,配置非常灵活,可以监视的项目很多,也可以通过自定义shell 脚本进行监控服务,非常适合各类企业的网络应用。

Nagios功能非常强大,它可以监控服务和主机,但是他自身并不包括这部分功能的代码,所有的监控、检测功能都是有插件来完成的。再说报警功能,如果监控系统发现问题不能报警那就没有意义了,所以报警也是nagios很重要的功能之一。但是,同样的,nagios 自身也没有报警部分的代码,甚至没有插件,而是交给用户或者其他相关开源项目组去完成。nagios安装,是指基本平台,也就是nagios软件包的安装。它是监控体系的框架,也是所有监控的基础。

打开nagios官方的文档,会发现nagios基本上没有什么依赖包,只要求系统是linux 或者其他nagios支持的系统。不过如果你没有安装apache(http服务),那么你就没有那么直观的界面来查看监控信息了,所以apache姑且算是一个前提条件。

Nagios的特征包括:

1)监控网络服务(SMTP、POP3、HTTP、NNTP、PING等)

2)监控主机资源(处理器负荷、磁盘利用率等)

3)简单地插件设计使得用户可以方便地扩展自己服务的检测方法

4)并行服务检查机制

5)具备定义网络分层结构的能力,用"parent"主机定义来表达网络主机间的关系,这种关系可被用来发现和明晰主机宕机或不可达状态

6)当服务或主机问题产生与解决时将告警发送给联系人(通过EMail、短信、用户定义方式)

可以通过飞信,等方式实现时,既可传递给管理员,可高效的保证服务器的维护。

7)具备定义事件句柄功能,它可以在主机或服务的事件发生时获取更多问题定位

8)自动的日志回滚

9)可以支持并实现对主机的冗余监控

10)可选的WEB界面用于查看当前的网络状态、通知和故障历史、日志文件等Nagios定义的监控状态

再用一幅图片宏观上展示nagios在企业级应用的位置

有朋友疑问,既然cacti,和nagios都是网络监控工具,选用其一不是减少很多麻烦,在这里个人建议两者结合使用,还要结合后文所提到的ntop,以我的理解,简单阐述下cacti 和nagios的优缺点:

cacti:在监控方面有良好的绘图,cacti在流量和图型塑造上要强于nagios,但是在故障分析上有些略逊,而且报警机制也有待完善,这时nagios就派上用场了。

nagios :适合监视大量服务器上面的大批服务是否正常, 重点并不在图形化的监控, 其集成的很多功能例如报警,都是cacti 没有或者很弱的。但在绘图以及图型塑造方面精细度比cacti要弱。

nagios定义了4中监控状态,代表不同的严重级别,除了OK代表正常不用关心外,其余3种

都要引起重视.如下表

Nagios的显示效果:

通过两者比较,可以很清晰的了解各自的用途。

下面再简单介绍下ntop

Ntop简介

一. MRTG基于SNMP获取信息,对于端口的流量,MRTG能提供精确统计,但对于三层以上的信息则无从得知了。而这正是NTOP的强项。NTOP能够更加直观地将网络使用量的情况和每个节点计算机的网络带宽使用详细情况显示出来。NTOP是一种网络嗅探器,嗅探器在协助监测网络数据传输、排除网络故障等方面有着不可替代的作用。可以通过分析网络流量来确定网络上存在的各种问题,如瓶颈效应或性能下降;也可以用来判断是否有黑客正在攻击网络系统。如果怀疑网络正在遭受攻击,通过嗅探器截获的数据包可以确定正在攻击系统的是什么类型的数据包,以及它们的源头,从而可以及时地做出响应,或

者对网络进行相应的调整,以保证网络运行的效率和安全。

二. 管理人员通过NTOP还可以很方便地确定出哪些通信量属于某个特定的网络协议、占主要通信量的是哪个主机、各次通信的目标是哪个主机、数据包发送时间、各主机间数据包传递的间隔时间等。这些信息为网管员判断网络问题及优化网络性能,提供了十分宝贵的信息。

NTOP提供以下一些功能:

①自动从网络中识别有用的信息;

②将截获的数据包转换成易于识别的格式;

③对网络环境中的通信失败进行分析;

④探测网络环境下的通信瓶颈;

⑤记录网络通信时间和过程;

⑥自动识别客户端正在使用的操作系统;

⑦可以在命令行和Web两种方式下运行。

简单介绍完ntop后,本人在网上找了一张图片,很清晰的解释了ntop的工作位置,从工作位置也可以看出ntop的主要功能.

通过简单介绍,也可以看出来ntop作为监控工具与前两者cacti和nagios的区别,ntop 在监控方面,它的角度是从分析网络流量来确定网络上存在的各种问题,说白了就类似个抓包监控工具,只不过通过归纳绘图实现更多的功能。

下面是3个软件的整合过程

1.安装nagios主程序(有些内容是摘抄网上内容,可能软件版本号会不一致,无需理会)

解压缩

tar -zxvf nagios-3.2.3.tar.gz

cd nagios-3.2.3

编译,指定安装目录为/usr/local/nagios

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

输出如下信息

*** Configuration summary for nagios 2.9 04-10-2007 ***:

General Options:

-------------------------

Nagios executable: nagios

Nagios user/group: nagios,nagios

Command user/group: nagios,nagios

Embedded Perl: no

Event Broker: yes

Install ${prefix}: /usr/local/nagios

Lock file: ${prefix}/var/nagios.lock

Init directory: /etc/rc.d/init.d

Host OS: linux-gnu

Web Interface Options:

------------------------

HTML URL: [url]http://localhost/nagios/[/url]

CGI URL: [url]http://localhost/nagios/cgi-bin/[/url]

Traceroute (used by WAP): /usr/sbin/traceroute

Review the options above for accuracy. If they look okay,

type 'make all' to compile the main program and CGIs.

make all

输出如下信息

*** Compile finished ***

If the main program and CGIs compiled without any errors, you

can continue with installing Nagios as follows (type 'make'

without any arguments for a list of all possible options):

make install

-This installs the main program, CGIs, and HTML files

使用make install来安装主程序,CGI和HTML文件

make install-init

-This installs the init script in /etc/rc.d/init.d

使用make install-init在/etc/rc.d/init.d安装启动脚本

make install-commandmode

- This installs and configures permissions on the

directory for holding the external command file

使用make install-commandmode来配置目录权限

make install-config

- This installs *SAMPLE* config files in /usr/local/nagios/etc

You'll have to modify these sample files before you can

use Nagios. Read the HTML documentation for more info

on doing this. Pay particular attention to the docs on

object configuration files, as they determine what/how

things get monitored!

使用make install-commandmode来安装示例配置文件,安装的路径是/usr/local/nagios/etc.

*** Support Notes *******************************************

If you have questions about configuring or running Nagios,

please make sure that you:

- Look at the sample config files

- Read the HTML documentation

- Read the FAQs online at [url]https://www.doczj.com/doc/3316075896.html,/faqs[/url]

before you post a question to one of the mailing lists.

Also make sure to include pertinent information that could

help others help you. This might include:

- What version of Nagios you are using

- What version of the plugins you are using

- Relevant snippets from your config files

- Relevant error messages from the Nagios log file

For more information on obtaining support for Nagios, visit:

[url]https://www.doczj.com/doc/3316075896.html,/support/[/url]

*************************************************************

Enjoy.

很多人都不注意安装过程中的输出信息,直到make install出错了才到处找人求救,而实际上输出的内容包含很多有价值的信息,例如安装路径,版本,每一步做什么,接下来的步骤等.尤其是输出的最后一屏信息,我个人建议好好的读一下.例如上面就列出了很多有价值的信息,我们只需要按照他说的做就行了.

安装

make install

输出如下错误

cd ./base && make install

make[1]: Entering directory `/home/yahoon/nagios/nagios-2.9/base'

make install-basic

make[2]: Entering directory `/home/yahoon/nagios/nagios-2.9/base'

/usr/bin/install -c -m 775 -o nagios -g nagios -d /usr/local/nagios/bin

/usr/bin/install: invalid user `nagios'非法用户nagios

make[2]: *** [install-basic] Error 1

make[2]: Leaving directory `/home/yahoon/nagios/nagios-2.9/base'

make[1]: *** [install] Error 2

make[1]: Leaving directory `/home/yahoon/nagios/nagios-2.9/base'

make: *** [install] Error 2

按照文档说明增加用户,修改权限

[root@localhost nagios-2.9]#useradd nagios

[root@localhost nagios-2.9]#mkdir /usr/local/nagios

[root@localhost nagios-2.9]# chown nagios.nagios /usr/local/nagio s

查看目录权限

[root@localhost nagios-2.9]# ll /usr/local

drwxr-sr-x 2 nagios nagios 4096 Jul 10 11:14 nagios 看到nagios目录的权限已经被正确修改了

重新执行make install输出信息如下

*** Main program, CGIs and HTML files installed ***

You can continue with installing Nagios as follows (type 'make'

without any arguments for a list of all possible options):

make install-init

- This installs the init script in /etc/rc.d/init.d

make install-commandmode

- This installs and configures permissions on the

directory for holding the external command file

make install-config

- This installs *SAMPLE* config files in /usr/local/nagios/etc

You'll have to modify these sample files before you can

use Nagios. Read the HTML documentation for more info

on doing this. Pay particular attention to the docs on

object configuration files, as they determine what/how

things get monitored!

make[1]: Leaving directory `/home/yahoon/nagios/nagios-2.9'

执行如下命令来安装脚本

make install-init

执行

make install-commandmode

输出信息如下

/usr/bin/install -c -m 775 -o nagios -g nagios -d /usr/local/nagios/var/rw

chmod g+s /usr/local/nagios/var/rw

*** External command directory configured ***

You can continue with installing Nagios as follows (type 'make'

without any arguments for a list of all possible options):

make install-config

- This installs *SAMPLE* config files in /usr/local/nagios/etc

You'll have to modify these sample files before you can

use Nagios. Read the HTML documentation for more info

on doing this. Pay particular attention to the docs on

object configuration files, as they determine what/how

things get monitored!

执行

make install-config

输出信息如下

/usr/bin/install -c -m 775 -o nagios -g nagios -d /usr/local/nagios/etc

/usr/bin/install -c -m 664 -o nagios -g nagios sample-config/nagios.cfg

/usr/local/nagios/etc/nagios.cfg-sample

/usr/bin/install -c -m 664 -o nagios -g nagios sample-config/cgi.cfg /usr/local/nagios/etc/cgi.cfg-sample

/usr/bin/install -c -m 660 -o nagios -g nagios sample-config/resource.cfg

/usr/local/nagios/etc/resource.cfg-sample

/usr/bin/install -c -m 664 -o nagios -g nagios sample-config/template-object/localhost.cfg

/usr/local/nagios/etc/localhost.cfg-sample

/usr/bin/install -c -m 664 -o nagios -g nagios sample-config/template-object/commands.cfg

/usr/local/nagios/etc/commands.cfg-sample

*** Sample config file installed ***

Remember, these are *SAMPLE* config files. You'll need to read

the documentation for more information on how to actually define

services, hosts, etc. to fit your particular needs.

If you have questions about configuring Nagios properly, please:

- Look at the sample config files

- Read the HTML documentation

- Read the FAQs online at [url]https://www.doczj.com/doc/3316075896.html,/faqs[/url]

*BEFORE* you post a question to one of the mailing lists.

验证程序是否被正确安装。切换目录到安装路径(这里是/usr/local/nagio s),看是否存在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.安装插件

解压缩

tar -zxvf nagios-plugins-1.4.9.tar.gz

cd nagios-plugins-1.4.9

编译,指定路径为之前nagios的安装路径

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

make

安装

make install

ls /usr/local/nagios/libexec/

会显示安装的插件文件,即所有的插件都安装在libexec这个目录下

将apache的运行用户加到nagios组里面

(apache大家需要注意是用系统自带的还是另外安装,这会涉及apache的配置)

从httpd.conf中过滤出当前的apache运行用户

grep ^User /usr/local/apache2/conf/httpd.conf ---这是安装的路径我的是vpopmail,下面将这个用户加入nagios组

usermod -G nagios vpopmail

3.修改apache配置(依然注意自带和另装的路径)

修改apache的配置文件,增加nagios的目录,并且访问此目录需要进行身份验证

vi /usr/local/apache2/conf/httpd.conf,在最后增加如下内容

#setting for nagios 20070707

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

增加验证用户

也就是通过web访问nagios的时候,必须要用这个用户登陆.在这里我们增加用户test:密码为12345

[root@localhost conf]#/usr/local/apache2/bin/htpasswd -c /usr/loc al/nagios/etc/htpasswd test

New password: (输入12345)

Re-type new password: (再输入一次密码)

Adding password for user test

查看认证文件的内容

[root@localhost conf]# less /usr/local/nagios/etc/htpasswd

test:OmWGEsBnoGpIc 前半部分是用户名test,后面是加密后的密码

到这里nagios的安装也就基本完成了,你可以通过web来访问了. [url]http:// ip /nagios[/url] 会弹出对话框要求输入用户名密码

输入test,密码12345,就可以进入nagios的主页面了

由于是nagios与cacti整合,所以nagois配置到这里就可以了

注意

接下来关于nagios的步骤我分两部分

一、单独nagios的搭建,还必须进行如下配置:

1、Nagios基本安装和配置

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

1)预备知识

Nagios自己定义了一套规则用于配置文件,其中最重要的概念就是”对象”----object.通俗的理解:假定我们首先定义了”性别”这个对象,它的值只可能是男,女,人妖等等,然后定义某人为一个对象,例如张三,定义张三的时候有”性别”这个属性,它的值就必须来源了之前定义的性别这个对象,要么是男是女,或者是人妖。

在Nagios里面定义了一些基本的对象,一般用到的有:

另外,多个被监控主机可以定义为一个主机组,多个联系人可以被定义为一个联系人组,多个服务还能定义成一个服务组呢。

回到上面的例子,定义张三需要之前定义的性别,我们定义一个被监控的服务,当然就要指定被监控的主机,需要监控的时间段,要用哪个命令来完成这个监控操作,出了问题向哪个联系人报告。所有这些对象绝对多数都是需要我们手动定义

的,这就是nagios的安装显得复杂的地方.其实了解了原理,做一遍之后余下的工作就是复制粘贴了,下面就开始动手。

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

cd /usr/local/nagios/etc

cp cgi.cfg-sample cgi.cfg

cp commands.cfg-sample commands.cfg

cp localhost.cfg-sample localhost.cfg

cp nagios.cfg-sample nagios.cfg

cp resource.cfg-sample resource.cfg

3)修改配置文件

修改nagios的主配置文件nagios.cfg

vi nagios.cfg

注释行#cfg_file=/usr/local/nagios/etc/localhost.cfg[2],然后把下面几行的注释去掉:

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

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

vi cgi.cfg

第二个要修改的配置文件是cgi.cfg,它的作用是控制相关cgi脚本。先确保use_ authentication=1。曾看过不少的文章,都是建议把use_authentication的值设置成”0”来取消验证,这是一个十分糟糕的想法。接下来修改default_user _name=test ,再后面的修改在下表列出:

authorized_for_system_information=nagiosadmin,test

authorized_for_configuration_information=nagiosadmin,test

authorized_for_system_commands=test //多个用户之间用逗号隔开

authorized_for_all_services=nagiosadmin,test

authorized_for_all_hosts=nagiosadmin,test

authorized_for_all_service_commands=nagiosadmin,test

authorized_for_all_host_commands=nagiosadmin,test

那么上述用户名打那里来的呢?是执行命令/usr/local/apache/bin/htpassw d –c /usr/local/nagios/etc/htpasswd test 所生成的,这个要注意,不能随便加没有存在的验证用户,为了安全起见,不要添加过多的验证用户。

定义监控时间段,创建配置文件timeperiods.cfg

[root@localhost etc]#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

[root@localhost etc]#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 yahoon@https://www.doczj.com/doc/3316075896.html,

pager 1338757xxxx

address1 xxxxx.xyyy@https://www.doczj.com/doc/3316075896.html,

address2 555-555-5555

}

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

相关主题
文本预览
相关文档 最新文档