免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 4381 | 回复: 4
打印 上一主题 下一主题

求助:用perl脚本调用rrdtool绘图...(在线等.......) [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2007-09-20 15:45 |只看该作者 |倒序浏览
我是想用perl脚本来调rrdtool绘图,网上找来的代码如下,但是我运行时有错,说下面红色标记的那一行"Use of uninitialized value in addition (+) at graph.pl line 42.","problem generating the graph: 'SCALAR' is not a valid function name",初学者不知道哪里有错,请高手看一下这个代码指点一下,感激不尽!
#!/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.";

论坛徽章:
1
荣誉会员
日期:2011-11-23 16:44:17
2 [报告]
发表于 2007-09-20 16:06 |只看该作者
$tot_mem[$i]
这个可能包含了没有被initialize的数据...
请检查他的长度跟$count一样..每个element都有数据...

论坛徽章:
0
3 [报告]
发表于 2007-09-20 16:18 |只看该作者

回复 #2 apile 的帖子

在那之前的那个循环里已经赋值了啊,for $i ( 0 .. $#vals ) {
    $tot_mem[$count] = $vals[$i][1];
    $count++;
}

而且这种错误是每个循环都一句"Use of uninitialized for value in addition (+) at graph.pl line 42."
我怀疑过for(0..$#vals)中多了一个,改成for(0..$#vals-1)还是不对,继续求助,谢谢楼上的大哥啊

论坛徽章:
0
4 [报告]
发表于 2007-09-20 16:39 |只看该作者

回复 #3 hrp313 的帖子

to avoid warning "Use of uninitialized ..." , simply switch -w off in the top line:
#!/usr/bin/perl

or initial the array @tot_mem:
my @tot_mem = ();

-- ulmer

论坛徽章:
0
5 [报告]
发表于 2007-09-20 16:55 |只看该作者

回复 #4 ulmer 的帖子

switch -w off in the top linek
however,the second way cannot work.
there is an error : "problem generating the graph: 'SCALAR' is not a valid function name"

i want to know the reason,who can tell me?

thanks!
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP