延續 rrdtool 用法與 cacti 設定對照( Part II )
http://xrcd2.blogspot.tw/2012/12/rrdtool-cacti-part-ii.html
經過前二篇的介紹我們已經知道 cacti & rrdtool 的設定方式(原理)
與 rrdtool 的關連性,接下來我要介紹的是如何利用既有 cacti 的
rrd 檔,再轉成如 MRTG 般的圖示,
如下圖示我們可以透過 Cacti Turn On Graph Debug Mode 將 圖檔
的 rrdtool 構成語法,顯示在 WEB 上
rrdtool graph 產出語法如下:
/usr/bin/rrdtool graph - \
--imgformat=PNG \
--start=-86400 \
--end=-300 \
--title='cacti - Traffic - eth0' \
--rigid \
--base=1000 \
--height=120 \
--width=500 \
--alt-autoscale-max \
--lower-limit='0' \
--vertical-label='bits per second' \
--slope-mode \
--font TITLE:10: \
--font AXIS:7: \
--font LEGEND:8: \
--font UNIT:7: \
DEF:a="/var/www/html/cacti/rra/cacti_traffic_in_21.rrd":'traffic_in':AVERAGE \
DEF:b="/var/www/html/cacti/rra/cacti_traffic_in_21.rrd":'traffic_out':AVERAGE \
CDEF:cdefa='a,8,*' \
CDEF:cdefe='b,8,*' \
AREA:cdefa#00CF00FF:"Inbound" \
GPRINT:cdefa:LAST:" Current\:%8.2lf %s" \
GPRINT:cdefa:AVERAGE:"Average\:%8.2lf %s" \
GPRINT:cdefa:MAX:"Maximum\:%8.2lf %s\n" \
LINE1:cdefe#002A97FF:"Outbound" \
GPRINT:cdefe:LAST:"Current\:%8.2lf %s" \
GPRINT:cdefe:AVERAGE:"Average\:%8.2lf %s" \
GPRINT:cdefe:MAX:"Maximum\:%8.2lf %s\n"
接下來依樣 山寨 為 perl rrdtool 的語法,產出如 cacti 中的 web 圖示
#!/usr/bin/perl -w
use RRDs;
$rrd='/var/www/html/cacti/rra/cacti_traffic_in_21.rrd';
print "Start Creating Graph \n";
RRDs::graph "/var/www/html/eth0.png",
"--imgformat","PNG",
"--start", -86400,
"--end", -300,
"--title=cacti - Traffic - eth0",
"--rigid",
"--base=1000",
"--height=120",
"--width=500",
"--alt-autoscale-max",
"--lower-limit=0",
"--vertical-label=bits per second",
"--slope-mode",
"--font","TITLE:10",
"--font","AXIS:7",
"--font","LEGEND:8:",
"--font","UNIT:7",
"DEF:a=$rrd:traffic_in:AVERAGE",
"DEF:b=$rrd:traffic_out:AVERAGE",
"CDEF:ab=a,8,\*",
"CDEF:bb=b,8,\*",
"AREA:ab#00FF00:Inbound ",
"GPRINT:ab:LAST:Current\\:%8.2lf %s ",
"GPRINT:ab:AVERAGE:avg\\:%8.2lf %s",
"GPRINT:ab:MAX:max\\:%8.2lf %s \\n",
"LINE:bb#0000FF:Outbound",
"GPRINT:bb:LAST:Current\\:%8.2lf %s ",
"GPRINT:bb:AVERAGE:avg\\:%8.2lf %s",
"GPRINT:bb:MAX:max\\:%8.2lf %s \\n",
"COMMENT:Designer XRCD2" ,
;
if ($ERROR = RRDs::error) {
die "ERROR: $ERROR\n";
};
print "END \n";
當然也可以按照自己的想法加以變化如
或者
#!/usr/bin/perl -w
use RRDs;
$rrd='/var/www/html/cacti/rra/cacti_traffic_in_21.rrd';
print "Start Creating Graph \n";
RRDs::graph "/var/www/html/before4hour.png",
"--imgformat","PNG",
"--start", -14700, # 4*60*60+300 (換算成秒數的算法)
"--end", -300,
"--title=cacti - Traffic - eth0 (before4hour)",
"--rigid",
"--base=1000",
"--height=120",
"--width=500",
"--alt-autoscale-max",
"--lower-limit=0",
"--vertical-label=bits per second",
"--slope-mode",
"--font","TITLE:10",
"--font","AXIS:7",
"--font","LEGEND:8:",
"--font","UNIT:7",
"DEF:a=$rrd:traffic_in:AVERAGE",
"DEF:b=$rrd:traffic_out:AVERAGE",
"CDEF:ab=a,8,\*",
"CDEF:bb=b,8,\*",
"AREA:ab#00FF00:Inbound ",
"GPRINT:ab:LAST:Current\\:%8.2lf %s ",
"GPRINT:ab:AVERAGE:avg\\:%8.2lf %s",
"GPRINT:ab:MAX:max\\:%8.2lf %s \\n",
"LINE:bb#0000FF:Outbound",
"GPRINT:bb:LAST:Current\\:%8.2lf %s ",
"GPRINT:bb:AVERAGE:avg\\:%8.2lf %s",
"GPRINT:bb:MAX:max\\:%8.2lf %s \\n",
"COMMENT:Designer XRCD2" ,
;
if ($ERROR = RRDs::error) {
die "ERROR: $ERROR\n";
};
print "END \n";
接下來就是自由變化的型態了,都已經變成圖檔了.要放在 web 變 mrtg 也行.
( 山寨 mrtg 示意畫面)
或者是更進階的自動產出 E-Mail 報表也行 (如日報 週報 月報 季報 年報...)也行
上面圖示中的 自動產出 E-Mail 報表 郵件程式抄改自
http://search.cpan.org/~teddy/Mail-Builder-Simple-0.16/lib/Mail/Builder/Simple.pm
#/usr/bin/perl
use Mail::Builder::Simple;
$png=' <img src="cid:logo" alt="logo"> ';
$mail = Mail::Builder::Simple->new;
$mail->image->add('/var/www/html/eth0.png', 'logo');
$mail->send(
mail_client => { mailer => 'SMTP' ,mailer_args => {host => 'smtp.server.ip.address'}},
from => ['cacti@xrcd2.com.tw', 'My Name'],
to => ['mis@xrcd2.com.tw', 'Your Name'],
subject => 'The subject with UTF-8 chars',
plaintext => "Hello,\n\nHow are you?\n\n",
htmltext => "<h1>Hello,</h1> <p>How are you?</p><p> 報表在下面看看吧 <p><p> $png </p>",
#attachment => 'file.pdf',
#image => 'demo.png',
priority => 1,
mailer => 'My Mailer 0.01',
);
當然我們也可以透過玩 CDEF 的運算去玩變形的花樣
正常版
變形版
print "Start Creating Graph \n";
RRDs::graph "/srv/www/htdocs/demo-v2.png",
"--title=Traffic Demo V2",
"--start", $begin,
"--end", $endtime,
"--interlace",
"--imgformat","PNG",
"--width=500",
"--height=120",
"--vertical-label=bps",
"DEF:a=$rrd:a:AVERAGE",
"DEF:b=$rrd:b:AVERAGE",
"CDEF:ab=a,8,\*",
"CDEF:bb=b,8,\*",
"AREA:ab#00FF00: Traffic_In",
"COMMENT:\\n",
"GPRINT:ab:MIN: min\\: %8.2lf %s",
"GPRINT:ab:AVERAGE: avg\\: %8.2lf %s",
"GPRINT:ab:MAX: max\\: %8.2lf %s",
"GPRINT:ab:LAST: last\\: %8.2lf %s",
"COMMENT:\\n",
"LINE:bb#0000FF: Traffic_Out",
"COMMENT:\\n",
"GPRINT:bb:MIN: min\\: %8.2lf %s",
"GPRINT:bb:AVERAGE: avg\\: %8.2lf %s",
"GPRINT:bb:MAX: max\\: %8.2lf %s",
"GPRINT:bb:LAST: last\\: %8.2lf %s ",
"HRULE:100000#FF0000: value > 100000 Warning",
"COMMENT:\\n",
"COMMENT:Last Updated $clock" ,
;
if ($ERROR = RRDs::error) {
die "ERROR: $ERROR\n";
};
print "END \n";
變形版部份程式範列 ( perl )
print "Start Creating Graph \n";
RRDs::graph "/srv/www/htdocs/demo-v3.png",
"--title=Traffic Demo V3",
"--start", $begin,
"--end", $endtime,
"--interlace",
"--imgformat","PNG",
"--width=500",
"--height=120",
"--vertical-label=bps",
"DEF:a=$rrd:a:AVERAGE",
"DEF:b=$rrd:b:AVERAGE",
"CDEF:ab=a,8,\*",
"CDEF:bb=b,-8,\*",
"CDEF:bc=b,8,\*",
"AREA:ab#99FF99: Traffic_In",
"LINE:ab#00DD00:Peak",
"COMMENT:\\n",
"GPRINT:ab:MIN: min\\: %8.2lf %s",
"GPRINT:ab:AVERAGE: avg\\: %8.2lf %s",
"GPRINT:ab:MAX: max\\: %8.2lf %s",
"GPRINT:ab:LAST: last\\: %8.2lf %s",
"COMMENT:\\n",
"AREA:bb#00E5EE: Traffic_Out",
"LINE:bb#0000FF:Peak",
"COMMENT:\\n",
"GPRINT:bc:MIN: min\\: %8.2lf %s",
"GPRINT:bc:AVERAGE: avg\\: %8.2lf %s",
"GPRINT:bc:MAX: max\\: %8.2lf %s",
"GPRINT:bc:LAST: last\\: %8.2lf %s ",
"HRULE:100000#FF0000: value > 100000 Warning",
"COMMENT:\\n",
"COMMENT:Last Updated $clock" ,
;
if ($ERROR = RRDs::error) {
die "ERROR: $ERROR\n";
};
print "END \n";
透過這一篇的介紹,應該可以透過 DIY 的小程式,將線上運行中的 cacti 產出如
mrtg 的網頁, 也可以 DIY 出 簡單網管 E-Mail 報表!
最後再想想透過連續這三篇介紹,將基礎打好,觀念搞清楚,再去玩 cacti 會更
了解一些,最簡單的說 cacti 只是用來簡化 rrdtool 的操作與設定.
但 cacti 的功能與強大之處,不是三言兩語可以說介紹完的. rrdtool 也是如此!
接下來如有時間我會再介紹 cacti 的 安裝.操作與使用....待續....
本 LAB 使用 cacti + rrdtool + perl + Mail 這四個要素.缺一不可.
Base on ( LAMP +P+R)
Linux Apache Mysql PHP Perl rrdtool !
沒有留言:
張貼留言