Cacti是我最常用的监控工具,介绍Cacti安装、插件使用等文章Google一搜一大把。这里主要介绍Cacti数据合并、以及数据合并后的绘图。本文以流量图合并为例。简单介绍二种方法:
例如,我用perl编写了一个合并流量数据的脚本
#!/usr/bin/perluse warnings
;use strict
; my %IP_Document;my %IP_DataFiles;$ENV{'PATH'}="/usr/kerberos/sbin:/usr/kerberos/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin" ; sub Usage
{ STDERR < Usage: $0 /iplist.conf [-t]
iplist.conf : 指定要读取的IP文件及其数据文件的配置文件
-t : 显示将要读取的文件后退出程序
Thanks for use!
EOF (0
);} sub getIP_DocFiles
{ my $confile = ; my ($DOC,@DATAFS); CONFILE
,$confile; while(){ ; s/^\s+//; s/\s+$//; next if m/^;/ or m/^#/ or m/^$/; if(m/^--DOC=/){ $DOC = ( (/=/) )[-1
]; next; } if(m/^--DATAF=/){ @DATAFS = (/\s+/ , ( (/=/) )[-1
] ); next; } my ($ip,$doc,@datas) = (/\s+/,$_); unless($doc){ $doc = $DOC; } unless(@datas){ @datas = @DATAFS; } #print "IP:$ip\tDOC:$doc\nData:@datas\n\n"; next unless $doc and @datas; $doc =~ s/\/$//; $IP_Document{$ip} = $doc; $IP_DataFiles{$ip} = "@datas"; } CONFILE
;} sub show
{ foreach my $ip ( %IP_Document){ my $doc = $IP_Document{$ip}; my @dfs = (/\s+/,$IP_DataFiles{$ip}); "Read... IP: $ip DOC: $doc DataFiles: @dfs\n"; foreach my $df ( @dfs ) { $df = "eth$df.log"; my $path = "$doc/$ip/$df"; "\tPATH: $path "; if(-e
$path and -f _
and -r _
){ "Exist/Commonly File/Can Read!\n"; }else{ "Can't Read... Please Check...\n"; } } "\n"; } "See You...\n"; (0
);} sub init
{ use Getopt
::Std; my %opts; getopts
('ht', \%opts) or &usage(); Usage
() if $opts{'h'}; "0\n0\nNoPameter\n" unless @ARGV; "0\n0\nNoFile\n" unless ( -e
$ARGV[0
] and -f _
and -r _
); getIP_DocFiles
($ARGV[0
]); if ( $opts{'t'} ) { show
(); } } sub getdata
{ my $dataf = ; |0
0| unless ( -e
$dataf and -f _
and -r _
); my $firstl = `rrdtool fetch $dataf MAX -r 600 |tail -3|head -1`; $firstl; #my ($in,$out) = ( split(/\s+/,$firstl) )[1,2]; my ($timestamp,$in, $out) = ( /\s+/, $firstl)[0
, 1
, 2
]; $timestamp; ($timestamp,$in,$out);} sub readfiles
{ my ($timestamp,$sum_in,$sum_out,$in,$out) = (0
,0
,0
); foreach my $ip ( %IP_Document){ my $doc = $IP_Document{$ip}; my @dfs = (/\s+/,$IP_DataFiles{$ip}); foreach my $df ( @dfs ) { $df = "eth$df.log"; my $path = "$doc/$ip"; ($timestamp,$in,$out) = getdata
($path); if($in == "nan") { $in=0; } if($out == "nan") { $out=0; } $sum_in += $in; $sum_out += $out; } } "traffic_in:$sum_in traffic_out:$sum_out";} sub Main
{ init
(); readfiles
();} Main
();
该脚本读取配置文件:sum.conf。请修改响应的配置项,以适合自己的配置环境。–DOC为rra目录,.rrd文件为要合并流量图的数据文件。在那里看?在Cacti中打开debug模式。还是不会?请找Google。
--DOC=
/usr
/local
/wwwroot
/cacti2
/rra
/--DATAF=
058_83_135_205_traffic_in_20
.rrd
58_83_135_197_traffic_in_52
.rrd
58_83_135_198_traffic_in_85
.rrd
58_83_135_199_traffic_in_107
.rrd
58_83_135_200_traffic_in_118
.rrd
58_83_135_201_traffic_in_30
.rrd
58_83_135_202_traffic_in_63
.rrd
58_83_135_203_traffic_in_97
.rrd
58_83_135_204_traffic_in_75
.rrd
localhost_traffic_in_9
.rrd
58_83_142_4_traffic_in_41
.rrd
将sum.pl和sum.conf放到Cacti中scripts目录中。运行#sum.pl sum.conf应该可以看到合并后的数据。
至此,第一种合并数据,绘制流量图的方法已经完成。你也可以下载我导出的Export Templates:
这里不讲插件怎么安装,因为这样的文章Google一大把。我重点讲讲怎么讲合并后的流量图显示在同一张图中。默认aggregate讲所有被合并流量图显示在一张图中,很难看。如下图:
增加Similar和Make CDEF,如下图:
Similar CDEF
make CDEF
编辑我们刚刚合并的流量图,如下图:
这里分别给出Item 1和Item 7的定义方法,其他类似。如下图:
Item 1
Item 7
阅读(4561) | 评论(0) | 转发(0) |