RRD起步3——用脚本包装RRDtool(译)
来源:互联网 发布:网络销售工作职责 编辑:程序博客网 时间:2024/05/17 05:05
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的,入门的例子相对都经典一点,希望耐心品味。
- RRD起步3——用脚本包装RRDtool(译)
- RRD起步1——通过例子理解RRD(译)
- RRD起步——通过例子理解RRD(译)
- RRD起步2——神奇绘图(译)
- rrdtool info **.rrd
- shell脚本——collectd目录下将rrd转化为xml
- Nginx状态图形化监控工具,Nginx-rrd+rrdtool 配置
- JRobin中rrd——xml和xml——rrd
- RRDtool绘图——数据定义问答
- python-rrdtool创建rrd文件的TypeError: argument 0 must be string 错误
- RRDtool
- RRDTool
- RRDTool
- rrdtool
- rrdtool
- RRDtool
- rrdtool
- RRDTool
- 月加班时间达到300个小时-baryonlee
- 谈谈工作流
- CSS:IE与Firefox的CSS兼容大全
- 在程序的系统菜单中加入自定义项
- 教育行业监控解决方案
- RRD起步3——用脚本包装RRDtool(译)
- Lesson2: Breakfast or lunch?
- suckit后门程序的分析
- TeaBrowser开发手册
- 菜鸟蹒跚学ASP-抓狂篇
- 全面优化配置Windows server 2003
- 在网上看到使用PHP实现常见的数据结构和算法
- ADO技术
- ASP中处理数据库图像的方法