RRD起步3——用指令碼包裝RRDtool(譯)
by:ysuncn(歡迎轉載,請註明譯者)
理解了RRDtool之後該開始用指令碼來使用RRDtool 了,網路管理相關的任務是:資料收集、資料儲存和資料提取,下邊的例子中,使用了“RRD起步1——通過例子理解RRD”中的target.rrd,為了方便,重新show一下:
rrdtool create target.rrd /
--start 1023654125 /
--step 300 /
DS:mem:GAUGE:600:0:671744 /
RRA:AVERAGE:0.5:12:24 /
RRA:AVERAGE:0.5:288:31
資料收集和資料儲存通過shell指令碼完成,資料的提取和報告的生成通過perl指令碼完成。指令碼如下:
shell指令碼(資料採集,更新資料庫)
#!/bin/sh
a=0
while [ "$a" == 0 ]; do
snmpwalk -c public 192.168.1.250 hrSWRunPerfMem > snmp_reply
total_mem=`awk 'BEGIN {tot_mem=0}
{ if ($NF == "KBytes")
{tot_mem=tot_mem+$(NF-1)}
}
END {print tot_mem}' snmp_reply`
# I can use N as a replacement for the current time
rrdtool update target.rrd N:$total_mem
# sleep until the next 300 seconds are full
perl -e 'sleep 300 - time % 300'
done # end of while loop
perl指令碼(從資料庫提取資料,生成圖片和統計資訊)
#!/usr/bin/perl -w
# This script fetches data from target.rrd, creates a graph of memory
# consumption on the target (Dual P3 Processor 1 GHz, 656 MB RAM)
# call the RRD perl module
use lib qw( /usr/local/rrdtool-1.0.41/lib/perl ../lib/perl );
use RRDs;
my $cur_time = time(); # set current time
my $end_time = $cur_time - 86400; # set end time to 24 hours ago
my $start_time = $end_time - 2592000; # set start 30 days in the past
# fetch average values from the RRD database between start and end time
my ($start,$step,$ds_names,$data) =
RRDs::fetch("target.rrd", "AVERAGE",
"-r", "600", "-s", "$start_time", "-e", "$end_time");
# save fetched values in a 2-dimensional array
my $rows = 0;
my $columns = 0;
my $time_variable = $start;
foreach $line (@$data) {
$vals[$rows][$columns] = $time_variable;
$time_variable = $time_variable + $step;
foreach $val (@$line) {
$vals[$rows][++$columns] = $val;}
$rows++;
$columns = 0;
}
my $tot_time = 0;
my $count = 0;
# save the values from the 2-dimensional into a 1-dimensional array
for $i ( 0 .. $#vals ) {
$tot_mem[$count] = $vals[$i][1];
$count++;
}
my $tot_mem_sum = 0;
# calculate the total of all values
for $i ( 0 .. ($count-1) ) {
$tot_mem_sum = $tot_mem_sum + $tot_mem[$i];
}
# calculate the average of the array
my $tot_mem_ave = $tot_mem_sum/($count);
# create the graph
RRDs::graph ("/images/mem_$count.png", /
"--title= Memory Usage", /
"--vertical-label=Memory Consumption (MB)", /
"--start=$start_time", /
"--end=$end_time", /
"--color=BACK#CCCCCC", /
"--color=CANVAS#CCFFFF", /
"--color=SHADEB#9999CC", /
"--height=125", /
"--upper-limit=656", /
"--lower-limit=0", /
"--rigid", /
"--base=1024", /
"DEF:tot_mem=target.rrd:mem:AVERAGE", /
"CDEF:tot_mem_cor=tot_mem,0,671744,LIMIT,UN,0,tot_mem,IF,1024,/",/
"CDEF:machine_mem=tot_mem,656,+,tot_mem,-",/
"COMMENT:Memory Consumption between $start_time",/
"COMMENT: and $end_time ",/
"HRULE:656#000000:Maximum Available Memory - 656 MB",/
"AREA:machine_mem#CCFFFF:Memory Unused", /
"AREA:tot_mem_cor#6699CC:Total memory consumed in MB");
my $err=RRDs::error;
if ($err) {print "problem generating the graph: $err/n";}
# print the output
print "Average memory consumption is ";
printf "%5.2f",$tot_mem_ave/1024;
print " MB. Graphical representation can be found at /images/mem_$count.png.";
ps:程式碼小長了一點r,不懂指令碼是無法使用RRDtool的,入門的例子相對都經典一點,希望耐心品味。
相關文章
- 原始碼包編譯安裝MySQL 5.6指令碼原始碼編譯MySql指令碼
- 用bat指令碼自動生成安裝包InnosetupBAT指令碼
- 安裝centreon+nagios時,rrd出圖出現亂碼iOS
- Linux 筆記分享十六:指令碼安裝包Linux筆記指令碼
- 如何編譯安裝原始碼包軟體編譯原始碼
- MySQL5.6一鍵編譯安裝指令碼MySql編譯指令碼
- shell指令碼企業實戰系列-nginx原始碼包安裝指令碼Nginx原始碼
- 2. 指令碼、模組、包指令碼
- Greenplum_原始碼編譯安裝和RPM包安裝原始碼編譯
- 原始碼編譯rrdtool提示找不到xxx.pc類問題處理原始碼編譯
- 徹底解除安裝rpm軟體包的指令碼指令碼
- 分析安裝包lnmp中的apache.sh指令碼薦LNMPApache指令碼
- appium 可以用自動化指令碼安裝 release 包的 ipa 檔案麼APP指令碼
- DBA指令碼 (3)指令碼
- 構建react.js應用的起步庫包倉庫:reactorReactJS
- Flutter起步之安裝Flutter
- 6502 指令譯碼器
- 《linux原始碼包的編譯安裝》RHEL6Linux原始碼編譯
- Proxifier註冊碼「Proxifier啟用安裝包」
- 在Windows下用ant編譯Android應用生成apk安裝包Windows編譯AndroidAPK
- 學習Ant編譯指令碼編譯指令碼
- pyenv 安裝指令碼指令碼
- lnmp安裝指令碼LNMP指令碼
- 譯見 | 掌舵 Kubernetes(一):如何起步?
- 指令碼新選擇——用C做指令碼指令碼
- shell指令碼:一鍵安裝LAMP、LNMP指令碼指令碼LAMPLNMP
- 安卓應用層抓包通殺指令碼釋出!《高研班》2021年3月班開始招生!安卓指令碼
- Macos端火爆遊戲:博德之門 3 中文啟用安裝包Mac遊戲
- 自動化(YUM/編譯)安裝PHP(5.3/5.5/7.0/7.1)指令碼編譯PHP指令碼
- 用儲存過程封裝awrrpt指令碼(四)儲存過程封裝指令碼
- 用儲存過程封裝awrrpt指令碼(三)儲存過程封裝指令碼
- 用儲存過程封裝awrrpt指令碼(二)儲存過程封裝指令碼
- 用儲存過程封裝awrrpt指令碼(一)儲存過程封裝指令碼
- 用儲存過程封裝awrrpt指令碼(五)儲存過程封裝指令碼
- 利用 ant 指令碼修改專案包名指令碼
- SecureCRT中文啟用安裝包+SecureCRT可用註冊碼Securecrt
- k3s單機版安裝部署 附一鍵安裝指令碼指令碼
- python3+requests:使用類封裝介面測試指令碼Python封裝指令碼