我是Perl和RRD的新手。
我试着实现了一个简单的例子,尽管看起来操作正确,但输出并没有显示出来。图片制作正常,但图表中没有数据。
我一直在遵循实现RRD::Simple
的CPAN文档,理论上我做错了什么。我试着调试代码,看起来很好,但当打印图形时,没有数据。
#!/usr/bin/perl
use strict;
use warnings;
use RRD::Simple ();
use Data::Dumper;
$| = 1; # Flush the output
my ($rrd, $unixtime, $file);
$file = "perl.txt";
my $path = '/home/os/Desktop/Test_Perl/';
my $period = '3years';
my $rrdfile = 'myfile.rrd';
while (sleep 15) {
open(FH, ">>", $file) || die "Unable to open $file: $!n";
my $range = 50;
my $minimum = 100;
my $random_number_in = int(rand($range)) + $minimum;
my $random_number_out = int(rand($range)) + $minimum;
my $random_number_sec = int(rand($range)) + $minimum;
# Create an interface object
$rrd = RRD::Simple->new(
file => $rrdfile,
cf => [qw( AVERAGE MIN MAX LAST )],
#default_dstype => "DERIVE",
);
unless (-e $rrdfile) {
# Create a new RRD file with 3 data sources called
# bytesIn, bytesOut and faultsPerSec.
$rrd->create(
$period,
step => 5, # 5 sec interval
bytesIn => "GAUGE",
bytesOut => "GAUGE",
faultsPerSec => "GAUGE"
);
}
# Put some arbitary data values in the RRD file for the same
# 3 data sources called bytesIn, bytesOut and faultsPerSec.
$rrd->update(
bytesIn => $random_number_in,
bytesOut => $random_number_out,
faultsPerSec => $random_number_sec
);
print FH "This is the bytes_in: $random_number_inn";
print FH "This is the bytes_out: $random_number_outn";
print FH "This is the bytes_sec: $random_number_secn";
# Generate graphs:
# /home/os/Desktop/Test_Perl/myfile-hourly.png, /home/os/Desktop/Test_Perl/myfile-daily.png
# /home/os/Desktop/Test_Perl/myfile-weekly.png, /home/os/Desktop/Test_Perl/myfile-monthly.png
my %rtn = $rrd->graph(
$rrdfile,
destination => $path,
basename => "my_graph",
timestamp => "both", # graph, rrd, both or none
periods => [qw(hour day week month)], # omit to generate all graphs
sources => [qw(bytesIn bytesOut faultsPerSec)],
source_colors => [qw(ff0000 aa3333 000000)],
source_labels => [("Bytes In", "Bytes Out", "Faults Per Second")],
source_drawtypes => [qw(LINE1 AREA LINE)],
line_thickness => 2,
extended_legend => 1,
title => "Network Interface eth0",
vertical_label => "Bytes/Faults",
width => 800,
height => 500,
interlaced => "", # If images are interlaced they become visible to browsers more quickly
);
printf("Created %sn", join(", ", map { $rtn{$_}->[0] } keys %rtn));
# Return information about an RRD file
my $info = $rrd->info($rrdfile); # This method will return a complex data structure containing details about the RRD file, including RRA and data source information.
print Data::Dumper::Dumper($info);
my @sources = $rrd->sources($rrdfile);
my $seconds = $rrd->retention_period($rrdfile); # This method will return the maximum period of time (in seconds) that the RRD file will store data for.
# Get unixtime of when RRD file was last updated
$unixtime = $rrd->last($rrdfile);
print FH "myfile.rrd was last updated at " . scalar(localtime($unixtime)) . "n";
# Get list of data source names from an RRD file
my @dsnames = $rrd->sources;
print "Available data sources: " . join(", ", @dsnames) . "n";
my $heartbeat_In = $rrd->heartbeat($rrdfile, "bytesIn");
my $heartbeat_Out = $rrd->heartbeat($rrdfile, "bytesOut");
my $heartbeat_sec = $rrd->heartbeat($rrdfile, "faultsPerSec"); # This method will return the current heartbeat of a data source.
printf "This is the heartbeat_in: %sn", $heartbeat_In;
my @rtn_In = $rrd->heartbeat($rrdfile, "bytesIn", 10);
my @rtn_Out = $rrd->heartbeat($rrdfile, "bytesOut", 10);
my @rtn_sec = $rrd->heartbeat($rrdfile, "faultsPerSec", 10); # This method will set a new heartbeat of a data source.
close(FH);
}
部分输出:
'myfilerrd' => {
'last_ds' => 'U',
'value' => undef,
'min' => '0',
'max' => undef,
'minimal_heartbeat' => 120,
'index' => 3,
'type' => 'DERIVE',
'unknown_sec' => 15
}
我不明白为什么这个值没有定义?
经过3-4天的测试和在互联网上搜索更多信息,我终于找到了问题的答案。RRD是一个非常简单易用的工具,但功能非常强大。我建议任何人通过Perl使用它,尤其是与RRD::Simple模块一起使用非常容易。
答案:
我将RRD的心跳调整为10秒,而我的步数(数据收集时间)默认为300。如果用户默认情况下未指定步长"采样频率",系统将使用300。结果,图形取0值,因此没有输出。更多信息和非常好的分析可以在这里找到心跳
根据我的实验,我发现由于我在创建函数中使用while循环,我必须首先给出命令:
my $rrd = RRD::Simple->new( file => "myfile.rrd" );
作为第二步,我不得不终止这个过程,并通过输入命令来设置步骤:
my $rrd = RRD::Simple->new(
file => "myfile.rrd",
step => 50 );
根据我的实验,我发现我必须删除下面的代码块,作为第二步,必须添加到文件中。首先必须进行创建,然后将其添加到我的循环中。这是因为在用户开始修改之前,最初必须使用所有设置创建"myfile.rrd"。
unless (-f "myfile.rrd") {
$rrd->create(
step => 50,
bytesIn => "GAUGE",
bytesOut => "GAUGE",
faultsPerSec => "COUNTER"
);
}
这里值得一提的另一点是,默认情况下,RRD数据源(DS)设置为GAUGE。更多信息可以在这里找到RRDtool
Perl模块可以很容易地找到CPANRRD::Simple,它提供了分析和可以添加到代码中的额外"功能"。
总之RRD::Simple非常简单,它可以通过复制粘贴到程序中来执行。任何进一步的修改(例如采样率、平均值/最大值/最小值等)都需要一些阅读,但绝对值得付出努力。不幸的是,网上没有太多的例子,所以需要进行一些测试,以了解我需要在代码中修改什么才能使其工作。通过写下这篇简短的分析并提供一些链接来阅读,我希望能避免其他人花几天时间来找出他的问题的答案。
我再次鼓励任何人尝试实现RRD。它是一个非常强大的工具,可以图形化地查看结果并将数据存储长达3年。
我认为可能对某些人有用的另一个更新。而不是通过添加和删除代码来完成所有这些过程,以使rrd文件正常工作。
经过修改和实验,我找到了另一个解决方案。
use strict;
use RRD::Simple;
use RRDs;
my $rrd = RRD::Simple->new(
file => "myfile.rrd",
rrdtool => "/usr/local/rrdtool-1.2.11/bin/rrdtool", #optional
tmpdir => "/var/tmp", #optional
cf => [ qw(AVERAGE MAX) ], #optional
default_dstype => "COUNTER", #optional
on_missing_ds => "add", #optional
RRDs::tune("myfile.rrd", "-i", "Source_Name:0") #optional -i or --minimum
RRDs::tune("myfile.rrd", "-a", "Source_Name:200") #optional -a or --maximum
);
有些人可以使用几个可选值,但我建议您使用所有这些值,这样您就可以完全控制程序。
我正在使用:
default_dstype => "COUNTER", #optional
因为默认情况下,RRD会将GAUGE设置为数据源(DS)。通过将DS设置为COUNTER,用户可以设置最小值和最大值。简短的例子也可以在这里找到RRD::Simple::examples。