php RRD Graph浮动值而不是整数

时间:2014-05-16 21:32:04

标签: php graph floating-point rrdtool rrd

enter image description here我正在制作rrd图,我遇到了2个问题。

问题1:我打印的数字是不带小数的整数,尽管它们打印时会出现小数。这真的令人困惑。所以我在rrdgraph_graph上看了网上,虽然我使用的是正确的语法而且我没有应用任何计算,但我仍然得到浮动值而不是整数。

根据官方网站:%s place this after %le, %lf or %lg. This will be replaced by the appropriate SI magnitude unit and the value will be scaled accordingly (123456 -> 123.456 k).

我附上照片作为输出样本。我还提供了一个工作示例代码,如果有人理解RRD可以查看可能的错误。

问题2:我试图在我的图表上添加VRULE:time#color[:legend][:dashes[=on_s[,off_s[,on_s,off_s]...]][:dash-offset=offset]]功能,并根据在线说明我可以提供时间。由于我的图表正在转移,我计划做时间(值) - 1800秒。我想在图表的中间放置一条垂直线,这样我就可以查看大约30分钟的平均值。当我应用这种格式时,我收到此错误:

<b>Graph error: </b>parameter '1400274668-1800' does not represent time in line VRULE:1400274668-1800#0000CD:Half way values

当我删除减法时,一切正常。有没有办法在我的图形中间生成这一行?

<?php

$file = "snmp";
$rrdFile = dirname(__FILE__) . "/".$file.".rrd";
$in = "losses";
$png = "/home/linux/Desktop/";
$in_min = "vdef_in_min";
$in_max = "vdef_in_max";
$in_lst = "vdef_in_lst";
$in_av = "vdef_in_av";
$title = "Losses RRD::Graph";
$output = array("1h","1d");
$step = 5;
$heartbeat = 2 * $step;

while (1) { 
  sleep (1);

  $options = array(
           "--start","now -15s",
           "--step", "".$step."",
           "DS:".$in.":GAUGE:".$heartbeat.":0:U",
           "RRA:LAST:0.5:1:3600",
           "RRA:MIN:0.5:1:3600",
           "RRA:MAX:0.5:1:3600",
           "RRA:AVERAGE:0.5:6:600",
           "RRA:LAST:0.5:300:288",
           "RRA:MIN:0.5:300:288",
           "RRA:MAX:0.5:300:288`",
           "RRA:AVERAGE:0.5:600:144"
           );

   if ( !isset( $create ) ) {

     $create = rrd_create(
              "".$rrdFile."",
              $options
              );

   if ( $create === FALSE ) {
      echo "Creation error: ".rrd_error()."\n";
   }
  }

  $t = time();
  $losses = rand(0, 150);

  $update = rrd_update(
               "".$rrdFile."",
               array(
                 "".$t.":".$losses.""
                 )
               );

  if ($update === FALSE) {
     echo "Update error: ".rrd_error()."\n";
  }

  date_default_timezone_set('Europe/Stockholm');
  $timezone = new DateTime(NULL, new DateTimeZone('Europe/Stockholm'));
  $date = date('l jS \of F Y h\\:i\\:s A' , $timezone->format('U'));
  $comment = "RRD last updated:".$date;
  $comment = str_replace( ":", "\\:", $comment );
  $graph = "Graph last updated:".$date;
  $graph = str_replace( ":", "\\:", $graph );

  foreach ($output as $test) {

     $final = array(
            "--start","end - ".$test."",
            "--end", "".$t."",
            "--title=".$file." RRD::Graph with - ".$test." Periods",
            "--vertical-label=Bytes(s)/sec",
            "--right-axis-label=latency(ms)",
            "--alt-y-grid", "--rigid",
            "--width", "800", "--height", "500",
            "--lower-limit=0",
            "--no-gridfit",
            "--slope-mode",
            "DEF:".$in."_def=".$file.".rrd:".$in.":LAST",
            "CDEF:inbytes=".$in."_def",
            "VDEF:".$in_lst."=inbytes,LAST",
            "VDEF:".$in_min."=inbytes,MINIMUM",
            "VDEF:".$in_max."=inbytes,MAXIMUM",
            "VDEF:".$in_av."=inbytes,AVERAGE",
            "COMMENT:\\n",
            "LINE2:".$in."_def#FF0000:".$in."",
            "GPRINT:".$in_min.": Minimum\:%6.2lf %S",
            "GPRINT:".$in_max.":Maximum\:%6.2lf %S",
            "GPRINT:".$in_lst.":Last\:%6.2lf %s",
            "GPRINT:".$in_av.":Average\:%6.2lf %s",
            "COMMENT:\\n",
            "VRULE:".$t."#0000CD:Half way values",
            "COMMENT:\\n",
            "HRULE:50#FFFF00:Maximum value",
            "COMMENT:\\n",
            "COMMENT: ",
            "COMMENT:\\n",
            "COMMENT:".$comment."\\r",
            "COMMENT:".$graph."\\r"
            );

    $outputPngFile = rrd_graph(
                   "".$png."".$test.".png",
                   $final
                   );

    if ($outputPngFile === FALSE) {
      echo "<b>Graph error: </b>".rrd_error()."\n";
    }

   }

   $debug = rrd_lastupdate (
                "".$rrdFile.""
                );

   if ($debug === FALSE) {
     echo "<b>Graph result error: </b>".rrd_error()."\n";
   } 

   var_dump ($debug);
 }

?>

1 个答案:

答案 0 :(得分:1)

您的第一个问题的答案几乎肯定是数据规范化。由于您不是每次都在Step边界上精确更新RRD,因此将提交的数据值标准化为步长边界,从而得到十进制值。要理解这一点,请阅读有关该主题的Alex van den Bogeardt's excellent article

你的第二个问题是你不能以这种方式使用VRULE声明。 VRULE的第一个参数可以是数字或VDEF变量,但它不能是公式。因此,VRULE:12345678#0000CD:FooVRULE:vdefname#FF00FF:Bar一样好。但是,您可能不会使用VRULE:123456-123#0000CD:No。之前进行计算,如下所示:

"VRULE:".($t-1800)."#0000CD:Half way values",

...这应该会产生有效的语法。