2012年11月30日 星期五

vlab 上多年Po文記念

說真的以往 Po 在 www.vlab.com.tw 上的網管文章,因為該網站消失而不見.
損失慘重,我在上面至少貢獻出不少網管專文.主要是以撰寫 網管 這個主題為核心.
目前我個人僅有保留,以往所撰寫過的Po文PDF檔.
希望有時間,可以將它再寫回目前這個 blog上.
 

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

2012年11月23日 星期五

無法匯入 Cisco Interface Reliability Status Monitor.( 無法匯入 cacti Template )

依據 URL http://docs.cacti.net/howto:determine_cacti_template_version
How to determine a Cacti template version 一文中提及匯入(使用) cacti
官方論壇所下載的 Template,基於 cacti 版本不同可能導致某 Template 在匯入
時,出現 Error: XML: Hash version does not exist 的問題.

該問題出現的原因為該 Template hash_version_codes 不適用於當前的 cacti 版本.
舉例來說我在 cacti 0.8.8a 自行撰寫出的 Cisco Interface Reliability  Status Monitor.
( http://xrcd2.blogspot.tw/2012/11/cisco-router-interface-reliability.html or
http://forums.cacti.net/viewtopic.php?f=12&t=48912 )

相關的 Template ,在匯出相關檔案後,並不能在 cacti 0.8.7.g 匯入使用.
此時解法為在 cacti 運作的主機中.開啟 /cacti_dir/include/global_arrays.php
尋找 $hash_version_codes = array 的下方處,即可看到目前所使用的 cacti  hash
版本代碼,以 0.8.8 & 0.8.8.a 為例,"0.8.8"  => "0024","0.8.8a"  => "0024",
另依據該 URL 的說明:(如下)

Take the following example: <hash_040018258d1c9487a6c58dd804f4a012007664>
The first 2 digits are the type of the template.
The next 4 digits are the Cacti version it was created on.
The next 32 digits are a random number.

簡單的說.就是將要匯入的 Template 的 hash_xx"hash_version_codes"改成現行的
版本代碼即可.可使用慣用的編輯軟體,開該該 xml檔,並使用[尋找/取代]功能,
存檔後應使正常匯入才對!

利用修改 template XML hash code 的方式. 再行匯入 
Cisco Interface Reliability  Status Monitor Template 即可.
簡單的說 透過這個方式,可以避掉不同的 cacti 版本所制做的  Template
無法匯入的問題.


2012年11月16日 星期五

Cisco Router Interface Reliability Status Monitor ( DIY cacti template )


        延續 再論 SNMP OIDs http://xrcd2.blogspot.tw/2012/10/snmp-oid.html 這個主題,上次我已經提到如何將 Cisco Router Interface Reliability  加入 zabbix 的監控,這次我將同樣的SNMP OIDs 改成
cacti template 的方式,進行監控!

sample output



This will let you know reliability status of cisco router interface. (DIY the template)

Reference: http://forums.cacti.net/viewtopic.php?f=12&t=17722

vi /cacti_dir/resource/snmp_queries/interface.xml
                <ifReliability>
                        <name>Reliability</name>
                        <method>walk</method>
                        <source>value</source>
                        <direction>output</direction>
                        <oid>.1.3.6.1.4.1.9.2.2.1.1.22</oid>
                </ifReliability>

接下來就圖解說明:

 Create Data Templates

Create Graph Templates

Create Items



Create Data Queries
Create New Graphs
下圖為 Interface Reliability + Interface Status + Interface Traffic





以下的URL為本人自行製作的 template 使用前請務必先將
cacti_data_query_snmp_-_interface_statistics先行備份.

滙出方式 (略)

cacti/console/export templates/

滙入方式(略)

cacti/console/import templates/

cacti environment: Centos 6.3 + cacti 0.8.8.a

data_query:
 http://www.mason-arts.com/xrcd2/xml/cacti_data_query_snmp_-_interface_statistics_reliability.xml
data_template:
http://www.mason-arts.com/xrcd2/xml/cacti_data_template_interface_-_reliability.xml
graph_template:
http://www.mason-arts.com/xrcd2/xml/cacti_graph_template_interface_-_reliability.xml


上述的 Template 同步發表於 cacti 官方論壇內

http://forums.cacti.net/viewtopic.php?f=12&t=48912

reliability.zip http://forums.cacti.net/download/file.php?id=26813