0

我是 Perl 和 RRD 的新手。

我试图实现一个简单的例子,虽然看起来运行正常,但没有显示输出。图片正常生成,但图中没有数据。

我一直在遵循 CPAN 文档进行实施RRD::Simple,理论上我做错了。我尝试调试代码,看起来不错,但是在打印图表时没有数据。

#!/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_in\n";
  print FH "This is the bytes_out: $random_number_out\n";
  print FH "This is the bytes_sec: $random_number_sec\n";

  # 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 %s\n", 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: %s\n", $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
}

我不明白为什么该值未定义?

4

2 回答 2

0

经过 3-4 天的测试和在 Internet 上搜索更多信息后,我刚刚找到了问题的答案。RRD 是一个非常简单易用但功能非常强大的工具。我建议任何人通过 Perl 使用它,尤其是使用 RRD::Simple 模块非常容易。

回答:

我将 RRD 的心跳调整为 10 秒,而我的步长(数据收集时间)默认为 300。如果用户默认不指定“采样频率”步长,系统将使用 300。结果图形取 0 值,因此没有输出。可以在这里找到更多信息和非常好的分析HeartBeat

根据我的实验,我发现由于我在 create 函数中使用了 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 模块可以很容易地找到 CPAN RRD::Simple,它提供了可以添加到代码中的分析和额外的“功能”。

总之, RRD::Simple非常简单,可以通过复制粘贴到您的程序中来执行。任何进一步的修改(例如采样率、平均值/最大值/最小值等)都需要阅读一些内容,但绝对值得付出努力。不幸的是,在线示例并不多,因此需要进行一些测试才能了解我需要在代码中修改哪些内容才能使其正常工作。通过编写这个简短的分析并提供一些阅读链接,我希望可以避免其他人花费几天时间来解决他的问题。

我再次鼓励任何人尝试实施 RRD,这是一个非常强大的工具,可以以图形方式查看您的结果并将数据存储长达 3 年。

于 2013-12-01T15:51:19.613 回答
0

我认为可能对某些人有用的另一个更新。而不是通过添加和删除代码来执行所有这些过程以使 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

于 2013-12-01T23:19:55.130 回答