2012年11月30日 星期五

rrdtool 用法與 cacti 設定對照 ( Part I )

http://oss.oetiker.ch/rrdtool/doc/rrdcreate.en.html
rrdtool create filename [--start|-b start time]
                        [--step|-s step]
                        [--no-overwrite]
                        [DS:ds-name:DST:dst arguments]
                        [RRA:CF:cf arguments]

http://cuddletech.com/articles/rrd/ar01s02.html

建立RRD檔 
RRDtool 建檔語法再詳項分解
rrdtool create filename
                        [--start|-b start time]             
                        [--step|-s step]                     
                        [DS:ds-name:DST:heartbeat:min:max]
                        [RRA:CF:xff:steps:rows]


重要字彙說明:

DS Data Sources (Field)
DST Data Source Type (GAUGE, COUNTER, DERIVE, ABSOLUTE )
RRA Round Robin Archives (Format: RRA:AVERAGE | MIN | MAX | LAST:xff:steps:rows)
CF Consolidation Function (AVERAGE, MIN, MAX, LAST)
XFF Defines XFiles Factor
DEF Definitions

簡單來說透過 rrdtool 的工具,可以建立特定區間(TimeStamp)為X軸,
Data Source為圖示的Y軸的圖示.
而 Data Source 可經由預設或自訂的計算方式[CDEFs function],
提供Y軸數值的單位大小.最大值.最小值.平均值等的運算.

example (rrdtool 1.4.7 版提供的範例 )
http://oss.oetiker.ch/rrdtool/pub/rrdtool-1.4.7.tar.gz

[root@centos63-test examples]# pwd
/usr/local/rrdtool/share/rrdtool/examples

[root@centos63-test examples]# cat minmax.pl
#!/usr/bin/perl
use lib qw( /usr/local/rrdtool/lib/perl );
use RRDs;
my $start=time;
my $rrd="randome.rrd";
my $name = $0;
$name =~ s/.*\///g;
$name =~ s/\.pl.*//g;
RRDs::create ($rrd, "--start",$start-1, "--step",300,
              "DS:a:GAUGE:600:U:U",
              "RRA:AVERAGE:0.5:1:300",
              "RRA:MIN:0.5:12:300",  
              "RRA:MAX:0.5:12:300",
);


my $ERROR = RRDs::error;
die "$0: unable to create `$rrd': $ERROR\n" if $ERROR;
# dropt some data into the rrd
my $t;
for ($t=$start; $t<$start+300*300; $t+=300){
  RRDs::update $rrd, "$t:".(sin($t/3000)*50+50);
  if ($ERROR = RRDs::error) {
    die "$0: unable to update `$rrd': $ERROR\n";
  }
}
RRDs::graph "$name.png",
  "--title", uc($name)." Demo",
  "--start", "now",
  "--end", "start+1d",
  "--lower-limit=0",
  "--interlace",
  "--imgformat","PNG",
  "--width=450",
  "DEF:a=$rrd:a:AVERAGE",
  "DEF:b=$rrd:a:MIN",
  "DEF:c=$rrd:a:MAX",
  "AREA:a#00b6e4:real",
  "LINE1:b#0022e9:min",
  "LINE1:c#00ee22:max",
;
if ($ERROR = RRDs::error) {
  die "ERROR: $ERROR\n";
};

print "This script has created $name.png in the current directory\n";
print "This demonstrates the use of MIN and MAX archives\n";

程式補充說明如下

#"--step",300, (300秒/5分鐘間隔區間)
#"RRA:AVERAGE:0.5:1:300",
#(XFF使用0.5,每隔五分鐘(1*5)存一次資料的平均值,資料保留300筆後自動覆蓋 [300 Rows])


=========================================================

抄改上述範例 1 用來簡單說明程式...

root@centos63-test perl]# cat ver1.pl
#!/usr/bin/perl
use RRDs;
$start=time;$rrd="random.rrd";
RRDs::create ($rrd, "--start",$start-1, "--step",1,
              "DS:a:GAUGE:12:U:U",
              "RRA:AVERAGE:0.5:1:12"
);
$ERROR = RRDs::error;
die "$0: unable to create `$rrd': $ERROR\n" if $ERROR;

for ($i=1;$i<=12;$i++){
 sleep 1;
 @time[$i] =time; $random_number = int(rand(9))+1;
 print "$i @time[$i] $random_number \n";
 RRDs::update $rrd,"@time[$i]:".$random_number.":".$i;
 if ($ERROR = RRDs::error) {
    die "$0: unable to update `$rrd': $ERROR\n";
 }
}

RRDs::graph "/var/www/html/1.png",
  "--title=Random_Number Demo",
  "--start", $start,
  "--end", $time[12],
  "--interlace",
  "--imgformat","PNG",
  "--width=500",
  "--height=120",
  "--vertical-label=value",
  "DEF:a=$rrd:a:AVERAGE",
  "AREA:a#FF0000:random_number",
  "GPRINT:a:MIN: min\\: %2.2lf",
  "GPRINT:a:AVERAGE: avg\\: %2.2lf",
  "GPRINT:a:MAX: max\\:  %2.2lf",
  "GPRINT:a:LAST: last\\: %2.2lf",
  "HRULE:8#0000FF: value > 8  Warning",
;
if ($ERROR = RRDs::error) {
  die "ERROR: $ERROR\n";
};

print "This script has created 1.png in the current directory\n";
print "This demonstrates the use of MIN and MAX archives\n";
[root@centos63-test perl]#

程式執行過程

[root@centos63-test perl]# perl ver1.pl
1 1354005047 6
2 1354005048 4
3 1354005049 1
4 1354005050 4
5 1354005051 9
6 1354005052 6
7 1354005053 3
8 1354005054 1
9 1354005055 6
10 1354005056 2
11 1354005057 5
12 1354005058 1
This script has created 1.png in the current directory
This demonstrates the use of MIN and MAX archives
[root@centos63-test perl]#

產出的圖型如下:
















範例 2 仿造 cacti 網路流量圖型 

[root@centos63-test perl]# cat traffic.pl
#!/usr/bin/perl
use RRDs;
$start=time;
$rrd="random.rrd";
RRDs::create ($rrd, "--start",$start-1, "--step",1,
              "DS:a:GAUGE:17:U:U",
              "DS:b:GAUGE:17:U:U",
              "RRA:AVERAGE:0.5:1:17"
);
$ERROR = RRDs::error;
die "$0: unable to create `$rrd': $ERROR\n" if $ERROR;

for ($i=1;$i<=17;$i++){
 sleep 1;
 @time[$i] =time;
 $random_number_a = int(rand(9))+1;
 $random_number_b = int(rand(9))+1;
 print "$i @time[$i] $random_number_a  $random_number_b \n";
 RRDs::update $rrd,"@time[$i]:".$random_number_a.":"."$random_number_b".":".$i;
 if ($ERROR = RRDs::error) {
    die "$0: unable to update `$rrd': $ERROR\n";
 }
}

RRDs::graph "/var/www/html/traffic.png",
  "--title=Random_Number Demo",
  "--start", $start,
  "--end", $time[17],
  "--interlace",
  "--imgformat","PNG",
  "--width=500",
  "--height=120",
  "--vertical-label=value",
  "DEF:a=$rrd:a:AVERAGE",
  "DEF:b=$rrd:b:AVERAGE",
  "AREA:a#00FF00:random_number_a",
  "GPRINT:a:MIN: min\\: %2.2lf",
  "GPRINT:a:AVERAGE: avg\\: %2.2lf",
  "GPRINT:a:MAX: max\\:  %2.2lf",
  "GPRINT:a:LAST: last\\: %2.2lf  ",
  "LINE:b#0000FF:random_number_b ",
  "GPRINT:b:MIN: min\\: %2.2lf",
  "GPRINT:b:AVERAGE: avg\\: %2.2lf",
  "GPRINT:b:MAX: max\\:  %2.2lf",
  "GPRINT:b:LAST: last\\: %2.2lf",
  "HRULE:8#FF0000: value > 8  Warning",
;
if ($ERROR = RRDs::error) {
  die "ERROR: $ERROR\n";
};

print "This script has created traffic.png in the current directory\n";
print "This demonstrates the use of MIN and MAX archives\n";

產出的圖型如下與 Traffic 對照:


最後仿造 cacti 以 每五分鐘 poller 一次 產出的圖型如下: ( 不滿 10 個小時 )



rrdtool 用法 與 cacti 設定對照


RRDtool Command ......

CF ....

Data Source ....

RRA ....




透過這一篇的介紹,我想說明的是 cacti 與 rrdtool 的重要繪圖技法及其關連性.
理論上要 DIY 出個人版的簡易網管程式應該是不難的.再配合snmpget的技法.
及其它 Alert 機制,如 MSN + SMS + E-Mail...配合那就是一套特製版的 
簡易型網管程式了.至少它應該擁有 MRTG 的水平還加上 Alert 機制! (哈)
但話又說回來,我還是會推薦大家使用 OpenSource 的網管程式.如 cacti or  zabbix or opennms
Zenoss Core or Nagios

如果公司有錢的話.那就看看
HP OpenView or IBM Tivoli or Solarwinds or  WhatsUp or CiscoWorks .....
OpenSource 還是有一定的極限在...廢話不過也是實話....


關於 snmpget 的技法可參考下列文章
再論 SNMP OIDs
http://xrcd2.blogspot.tw/2012/10/snmp-oid.html
利用SNMP OIDs 加入 Zabbix 監控
http://xrcd2.blogspot.tw/2012/10/snmp-oids-zabbix.html

關於網管系統的建置可參考
Cacti + XSMS_API (SOAP) (自行客製化 Cacti + SMS )
http://xrcd2.blogspot.tw/2012/05/cacti-xsmsapi-soap-cacti-sms.html

沒有留言:

張貼留言