cacti流量监控--verygood

来源:互联网 发布:光纤收发器知指示灯 编辑:程序博客网 时间:2024/04/27 21:50

http://hi.baidu.com/52hack/blog/item/e4428f947096b216d21b70be.html

2009年05月25日 星期一 上午 06:24

基于RRDTOOL CACTI性能监视系统的架构
一、概述
Linux下用的最多的是MRTG的性能监视,MRTG配置比较简单,MRTG的确是非常好的东东,但我认为它毕竟已经是一套很旧的软件了,其作者在多年前就已经开发了RRDTool代替该软件,现在已经发展得很成熟。既然有更好的选择,为什么我们还要用MRTG呢?
简单的说,rrdtool就是一个强大的绘图的引擎,很多语言都可以调用rrdtool绘图。
整个系统的架构是这样的:
基于SNMP协议,被监控端是服务器,或一些网络设备,
网络管理工作站,采用Linux(或Freebsd)操作系统,并且安装Net-SNMP工具,使用RRDTOOL采集数据,存储数据,并用Cacti调用rrdtool显示出来。
CACTI采用PHP编写,基于B/S结构。
二、介绍
MRTG的优点:简单、易上手,基本安装完了之后只要更改一下配置文件即可。
缺点
1、使用文本式的数据库,数据不能重复使用;
2、只能按日、周、月、年来查看数据;
3、只能画两个DS(一条线、一个块);
4、每取一次数据即需要绘图一次,浪费系统资源;
5、无管理功能;
rrdtool的优点:
1、使用rrd存储格式,数据能重复使用,比如我可以将一个rrd文件中的数据与另一个rrd文件中的数据相加。
2、可以定义任意时间段画图,即你可以画出一张半年以来的数据的图,也可以画出一张半小时以来的图。
3、能画任意个DS。
4、CDEF让你能任意摆弄数据。
缺点:
1、rrdtool的作用只是存储数据和画图,它没有mrtg中集成的数据采集功能;
2、在命令行的使用非常复杂,参数极多。
3、无管理功能。
简单的说,rrdtool就是一个强大的绘图的引擎。
由于其非常复杂的命令,对用户非常不友好。幸运的是,PHP可管理化的cacti(
www.cacti.net
)出现了。对该工具只有一个字形容:“great!”。cacti其实是一套php程序,它运用snmpget采集数据,使用rrdtool绘图。它的界面非常漂亮,能让你根本无需明白rrdtool的参数能轻易的绘出漂亮的图形。更难能可贵的是,它提供了强大的数据管理和用户管理功能,一张图是属于一个host的,每一个host又可以挂载到一个树状的结构上。用户的管理上,作为一个开源软件,它居然做到为指定一个用户能查看的“树”、host、甚至每一张图,还可以与LDAP结合进行用户的验证!真是不由得佩服作者考虑的周到!Cacti还提供自己增加模板的功能,让你添加自己的snmp_query和script!可以说,cacti将rrdtool的所有“缺点”都补足了!


screen.width*0.7){this.resized=true; this.width=screen.width*0.7; this.alt='Click hereto open new window/nCTRL Mouse wheel to zoom in/out';}"onmouseover="if(this.width>screen.width*0.7) {this.resized=true;this.width=screen.width*0.7; this.style.cursor='hand'; this.alt='Clickhere to open new window/nCTRL Mouse wheel to zoom in/out';}"onclick="if(!this.resized) {return true;} else{window.open('http://p.blog.csdn.net/images/p_blog_csdn_net/superpan/05bf69bccccc4035aa0291b7ffb1abf6.gif');}"onmousewheel="return imgzoom(this);" alt="" />

Cacti的架构 ( 图一 )
screen.width*0.7){this.resized=true; this.width=screen.width*0.7; this.alt='Click hereto open new window/nCTRL Mouse wheel to zoom in/out';}"onmouseover="if(this.width>screen.width*0.7) {this.resized=true;this.width=screen.width*0.7; this.style.cursor='hand'; this.alt='Clickhere to open new window/nCTRL Mouse wheel to zoom in/out';}"onclick="if(!this.resized) {return true;} else{window.open('http://p.blog.csdn.net/images/p_blog_csdn_net/superpan/56830ac83311448a8065c609acb998cd.png');}"onmousewheel="return imgzoom(this);" alt="" />

Cacti的工作流程( 图二 )

三、监控管理工作站配置
采用Linux系统,CACTI要用到Net-SNMP,RRDTool,Aapche,PHP,Mysql,以下是安装过程:
1.安装Apache Mysql PHP
安装过程比较简单,参考其它的资料,需要注意的是,PHP最好加上SNMP支持,编译的时候,加上—enable-snmp
注意。PHP不能运行在安全模式,否则,Cacti工作不正常。无法调用/usr/local/bin/下面的程序。
Mysql数据库不能使用5以上的版本,测试mysql 5 cacti不能正常运行!
2.安装rrdtool
1.0.x 版本:
官方网站:http://people.ee.ethz.ch/~oetiker/webtools/rrdtool/
下载:
http://people.ee.ethz.ch/~oetiker/webtools/rrdtool/pub/rrdtool-1.0.x/rrdtool-1.0.50.tar.gz
然后(可去查看下载最新版)
#./configure –prefix=/usr/local/rrdtool
#make
#make install

与mrtg相比,rrdtool1.0.x自带了gd库,所以不用先安装gd库.(不过由于rrdtool自带的gd库不支持中文,所以rrdtool画出来的图也不能有中文,否则会出现乱码).
1.2.x 版本
1.2版的rrdtool画出的图,比1.0的要漂亮些. 1.0用的是GD画图,1.2用的是libart和freetype画图,1.2的画图会慢一点.
注意:rrdtool1.2的版本由于已经不再自带外部的lib库(如cgilib,zlib等),所以需要从http://people.ee.ethz.ch/~oetiker/webtools/rrdtool/pub/libs/下载这些库来安装。

库的编译安装:
安装完后,执行rrdtool看是否正确,
如果执行不成功,提示命令不存在,则需要将所有RRDTool的可执行文件,拷到/usr/local/bin #cp /usr/local/rrdtool/bin/* /usr/local/binBuilding cgilib
#tar zxvf cgilib-0.5.tar.gz#cd cgilib-0.5#make CC=gcc CFLAGS="-O3 -fPIC -I."#cp *.h /usr/include/#cp libcgi* /usr/libBuilding zlib
#tar zxvf zlib-1.2.2.tar.gz
#cd zlib-1.2.2
#./configure --prefix=/usr
#make
#make install
Building libpng
#tar zxvf libpng-1.2.8-config.tar.gz
#cd libpng-1.2.8-config
#cp scripts/makefile.gcmmx makefile
#./configure --disable-shared --prefix=/usr
#make
#make install
Building freetype
#tar zxvf freetype-2.1.10.tar.gz
#./configure --disable-shared --prefix=/usr
#make
#make install
Building libart_lgpl
#tar zxvf libart_lgpl-2.3.17.tar.gz
#./configure --disable-shared --prefix=/usr
#make
#make install
Building rrdtool-1.2.6
#tar zxvf rrdtool-1.2.6.tar.gz
#./configure
#make
#make install
#cp /usr/local/rrdtool/bin/* /usr/local/bin/
#ln -s /usr/local/rrdtool-1.2.6 /usr/local/rrdtool
3.安装Net-SNMP
RH下面,默认安装了SNMP服务,但好象没有snmpwalk,snmpget这两个命令,所以需要安装NET-SNMP。NET-SNMP官方网站:http://www.net-snmp.org/
下载tar zxvf net-snmp-5.2.1.2.tar.gz
#tar zxvf tar zxvf net-snmp-5.2.1.2.tar.gz
#cd net-snmp-5.2.1.2
#./configure
#make
#make install
运行snmpget,snmpwalk测试是否安装成功
RPM包的snmpwalk的名称是:net-snmp-utils.*.rpm,也可以从安装光盘上RPM安装。
4.安装CACTI
官方网站:http://www.cacti.net/
a) 安装cacti源文件
下载cacti-0.8.6g.tar.gz
将这个压缩包解压到网站根目录
#cp cacti-0.8.6g.tar.gz /home/wwwroot //httpd站点目录所在
#cd /home/wwwroot/
#tar xzvf cacti-0.8.6g.tar.gz
#mv cacti-0.8.6g cacti
#chown –R apache.apache cacti
#cd cacti
b) 配置Mysql数据库
为cacti配置用户和数据库:
#mysql –u root –p
mysql>create database cactidb;
mysql>grant all privileges on cactidb.* to cactiuser@localhost identified by ‘password’;
mysql>quit







c) 配置Cacti连接数据库
#vi /home/wwwroot/cacti/include/config.php
$database_type = “mysql”;
$database_default = “cactidb”;
$database_hostname = “localhost”;
$database_username = “cacti”;
$database_password = “password”;
d) 导入数据表
/usr/local/mysql/bin/mysql -u root –p cactidb cacti.sql
chown -R cactiuser rra/ log/
e) 定时crontab运行cacti的收集数据程序
crontab -u cactiuser –e
*/5 * * * * /usr/local/bin/php /home/wwwroot/cacti/poller.php > /dev/null 2>&1
注意: 不要使用root用户运行上面的命令,否则要再运行一次chown –R cactiuser rra/ log/
f) 配置Cacti
在浏览器上输入:
http://IP/cacti
进入cacti的初始设置页面:
在这里我们要输入一些原始的信息:
NEXT -》
输入一些信息,如rrdtool、php、snmpwalk、snmpget的位置,使用ucd-snmp还是net-snmp等-》
输入原始的用户和密码:admin/admin -》
更改admin用户的密码-》
点击 Save
安装完成!!!
现在可以在浏览器中进入Cacti的世界了!
5.测试
1) 添加监控站点

Devices
中的Add进入添加服务器如图3、4、5
screen.width*0.7){this.resized=true; this.width=screen.width*0.7; this.alt='Click hereto open new window/nCTRL Mouse wheel to zoom in/out';}"onmouseover="if(this.width>screen.width*0.7) {this.resized=true;this.width=screen.width*0.7; this.style.cursor='hand'; this.alt='Clickhere to open new window/nCTRL Mouse wheel to zoom in/out';}"onclick="if(!this.resized) {return true;} else{window.open('http://p.blog.csdn.net/images/p_blog_csdn_net/superpan/1ee0aff263a346679f78d82ab1351b2d.jpg');}"onmousewheel="return imgzoom(this);" alt="" />
添加监控服务器(图3)
screen.width*0.7){this.resized=true; this.width=screen.width*0.7; this.alt='Click hereto open new window/nCTRL Mouse wheel to zoom in/out';}"onmouseover="if(this.width>screen.width*0.7) {this.resized=true;this.width=screen.width*0.7; this.style.cursor='hand'; this.alt='Clickhere to open new window/nCTRL Mouse wheel to zoom in/out';}"onclick="if(!this.resized) {return true;} else{window.open('http://p.blog.csdn.net/images/p_blog_csdn_net/superpan/f3276b2b01ae493f9def004340c075d9.jpg');}"onmousewheel="return imgzoom(this);" alt="" />
添加监控服务器(图4)
screen.width*0.7){this.resized=true; this.width=screen.width*0.7; this.alt='Click hereto open new window/nCTRL Mouse wheel to zoom in/out';}"onmouseover="if(this.width>screen.width*0.7) {this.resized=true;this.width=screen.width*0.7; this.style.cursor='hand'; this.alt='Clickhere to open new window/nCTRL Mouse wheel to zoom in/out';}"onclick="if(!this.resized) {return true;} else{window.open('http://p.blog.csdn.net/images/p_blog_csdn_net/superpan/c82c34d146ca49edbe762d8cf4658cde.jpg');}"onmousewheel="return imgzoom(this);" alt="" />
添加监控服务器(图5)
如果能连接得上,则添加后会在头部显示
61.129.102.51 (61.129.102.51)
SNMP Information
System: 00:LINUX:MAIL.BIZCN.COM:2.4.20-30.9SMP:#1:SMP:WED:FEB:04:20:36:46:EST:2004:I686
Uptime: 156994868
Hostname: mail.bizcn.com


screen.width*0.7){this.resized=true; this.width=screen.width*0.7; this.alt='Click hereto open new window/nCTRL Mouse wheel to zoom in/out';}"onmouseover="if(this.width>screen.width*0.7) {this.resized=true;this.width=screen.width*0.7; this.style.cursor='hand'; this.alt='Clickhere to open new window/nCTRL Mouse wheel to zoom in/out';}"onclick="if(!this.resized) {return true;} else{window.open('http://p.blog.csdn.net/images/p_blog_csdn_net/superpan/3fe5fa229e2248b2b23624fa47253558.jpg');}"onmousewheel="return imgzoom(this);" alt="" />
添加监控服务器(图6)

2) 配置添加的服务器的监控服务(这里一般使用UCD/NET 模板)

New Graphs
选择监控点名,勾选相应的creat
screen.width*0.7){this.resized=true; this.width=screen.width*0.7; this.alt='Click hereto open new window/nCTRL Mouse wheel to zoom in/out';}"onmouseover="if(this.width>screen.width*0.7) {this.resized=true;this.width=screen.width*0.7; this.style.cursor='hand'; this.alt='Clickhere to open new window/nCTRL Mouse wheel to zoom in/out';}"onclick="if(!this.resized) {return true;} else{window.open('http://p.blog.csdn.net/images/p_blog_csdn_net/superpan/f96d1812d525496dbbe75f2acd5a2df4.jpg');}"onmousewheel="return imgzoom(this);" alt="" />
添加监控服务器(图6)

3)、添加目录树(增加后将在监控的位置出现)

Graph Trees
中的Add(如图)
screen.width*0.7){this.resized=true; this.width=screen.width*0.7; this.alt='Click hereto open new window/nCTRL Mouse wheel to zoom in/out';}"onmouseover="if(this.width>screen.width*0.7) {this.resized=true;this.width=screen.width*0.7; this.style.cursor='hand'; this.alt='Clickhere to open new window/nCTRL Mouse wheel to zoom in/out';}"onclick="if(!this.resized) {return true;} else{window.open('http://p.blog.csdn.net/images/p_blog_csdn_net/superpan/8f30d8b5ce04417d92b97fcb08eab5c0.jpg');}"onmousewheel="return imgzoom(this);" alt="" />
添加监控服务器(图7)
选择host,并在下拉列表中选择你刚添加的
screen.width*0.7){this.resized=true; this.width=screen.width*0.7; this.alt='Click hereto open new window/nCTRL Mouse wheel to zoom in/out';}"onmouseover="if(this.width>screen.width*0.7) {this.resized=true;this.width=screen.width*0.7; this.style.cursor='hand'; this.alt='Clickhere to open new window/nCTRL Mouse wheel to zoom in/out';}"onclick="if(!this.resized) {return true;} else{window.open('http://p.blog.csdn.net/images/p_blog_csdn_net/superpan/714b6a51ccb44b9e84e692dba0d7255d.jpg');}"onmousewheel="return imgzoom(this);" alt="" />

添加监控服务器(图8)
4)、监控
点击总菜单中的Graphs 显示监控图,可在左边选择相应的监控端查看(一般添加5分钟后就有图出来)
screen.width*0.7){this.resized=true; this.width=screen.width*0.7; this.alt='Click hereto open new window/nCTRL Mouse wheel to zoom in/out';}"onmouseover="if(this.width>screen.width*0.7) {this.resized=true;this.width=screen.width*0.7; this.style.cursor='hand'; this.alt='Clickhere to open new window/nCTRL Mouse wheel to zoom in/out';}"onclick="if(!this.resized) {return true;} else{window.open('http://p.blog.csdn.net/images/p_blog_csdn_net/superpan/3764bc5428c84ef891920318b92be05b.jpg');}"onmousewheel="return imgzoom(this);" alt="" />
监控服务器(图9)

以下是关于流量负载内存等的截图
screen.width*0.7){this.resized=true; this.width=screen.width*0.7; this.alt='Click hereto open new window/nCTRL Mouse wheel to zoom in/out';}"onmouseover="if(this.width>screen.width*0.7) {this.resized=true;this.width=screen.width*0.7; this.style.cursor='hand'; this.alt='Clickhere to open new window/nCTRL Mouse wheel to zoom in/out';}"onclick="if(!this.resized) {return true;} else{window.open('http://p.blog.csdn.net/images/p_blog_csdn_net/superpan/d3fdab4604a140f1a20665546480940c.jpg');}"onmousewheel="return imgzoom(this);" alt="" />

screen.width*0.7){this.resized=true; this.width=screen.width*0.7; this.alt='Click hereto open new window/nCTRL Mouse wheel to zoom in/out';}"onmouseover="if(this.width>screen.width*0.7) {this.resized=true;this.width=screen.width*0.7; this.style.cursor='hand'; this.alt='Clickhere to open new window/nCTRL Mouse wheel to zoom in/out';}"onclick="if(!this.resized) {return true;} else{window.open('http://p.blog.csdn.net/images/p_blog_csdn_net/superpan/7aa4c81c75ea4e7c97facc8bab74c2e8.jpg');}"onmousewheel="return imgzoom(this);" alt="" />

四、被监控端配置
大部分情况下,我们监控的是服务器,以RedHat Linux为例,看看如何打开SNMP服务。
只有开启了SNMP服务,监控端才可以收集数据。
打开默认的/etc/snmp/snmp.conf文件,更改如下配置:
1、查找以下字段:
代码:
# sec.name source community
com2sec notConfigUser default public
将"comunity"字段改为你要设置的密码.比如"bizcnpublic".
将“default”改为你想哪台机器可以看到你的snmp信息,如10.10.10.10。授权服务器IP
2、查找以下字段:代码:
####
# Finally, grant the group read-only access to the systemview view.
# group context sec.model sec.level prefix read write notif
access notConfigGroup "" any noauth exact systemview none none
将"read"字段改为all.
代码:
#access notConfigGroup "" any noauth exact all none none
3、查找以下字段:代码:
## incl/excl subtree mask
#view all included .1 80
将该行前面的"#"去掉.
4、查找以下字段:代码#
#name incl/excl subtree mask(optional)
添加一行view all included .1
变成
#name incl/excl subtree mask(optional)
view all included .1

4、查找一下字段:代码
#view mib2 included .iso.org.dod.internet.mgmt.mib-2 fc
将该行前面的"#"去掉.
保存关闭.
4、运行/etc/init.d/snmpd start命令运行snmpd.
5、如果有防火墙,打开UDP 161端口。
最后运行netstat -ln查看161端口是否打开了.
使用ntsysv,让snmp服务,每次开机自动运行。
如果没有安装snmp服务,请在RH的安装光盘上找到net-snmp.rpm,再安装。
五、测试
打开本机的SNMP服务,
打开http://IP/cacti
默认Cacti有LocalHost的四项参数,直接可以查看了。
六、排错
1. 首先检查一下rra/下面,有没有数据,如果没有检查rra/ log/的拥有者是不是cactiuser
2. snmpwalk -v 2c -c public ServerIP if 用来测试被控对象(serverIP)是否开启了SNMP服务
3. snmpwalk -v 2c ServerIP -c public .1.3.6.1.4.1.2021.10.1.3 查看被控端是否有CPU负载的数据返回
4. /usr/local/bin/php /home/wwwroot/cacti/poller.php 用来测试PHP是否可以采集到数据。如果上面的都正确,但这步出错,很有可能是PHP配置的问题,或开启了SuLinux。
5. 如果按第2步snmpwalk能采集到数据,但第3步无法采集,可能是PHP设置的问题,修改PHP.ini,很有可能是PHP权限问题。如果第4步正常而无法显示图,则可能是rra/ log/ 的Owner不对



Cacti rrdtool 非常灵活,可以使用到非常广的范围,但是很多需要自己写插件和模板,现在对cacti和rrdtool的研究还处中初级阶段!要想好好利用这个工具,也得费一番心思!