Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/267.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/symfony/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用PHP获取RRD图形数据_Php_Rrdtool_Rrd - Fatal编程技术网

如何使用PHP获取RRD图形数据

如何使用PHP获取RRD图形数据,php,rrdtool,rrd,Php,Rrdtool,Rrd,提前感谢您的时间和努力。这是我第一个使用PHP和RRD的脚本。当我为SNMP编写一个简短的程序时,我遇到了一个强大的图形输出工具RRD。我试图编写一个简短的操作脚本来模拟图形的输出。我从官方页面上尽可能多地阅读了关于RRD的文档,并试图将它们添加到我的PHP代码中。我在调试代码时发现了一些函数,这些函数显示了我的数据是正常输入的,并且按照预期在引用中进行了大小写。提供的样本如下: ["last_update"]=> int(1396917542) ["ds_cnt"]=> int(3

提前感谢您的时间和努力。这是我第一个使用PHP和RRD的脚本。当我为SNMP编写一个简短的程序时,我遇到了一个强大的图形输出工具RRD。我试图编写一个简短的操作脚本来模拟图形的输出。我从官方页面上尽可能多地阅读了关于RRD的文档,并试图将它们添加到我的PHP代码中。我在调试代码时发现了一些函数,这些函数显示了我的数据是正常输入的,并且按照预期在引用中进行了大小写。提供的样本如下:

["last_update"]=>
int(1396917542)
["ds_cnt"]=>
int(3)
["ds_navm"]=>
array(3) {
  [0]=>
  string(10) "ifInOctets"
  [1]=>
  string(11) "ifOutOctets"
  [2]=>
  string(9) "sysUpTime"
}
["data"]=>
array(3) {
  [0]=>
  string(4) "1405"
  [1]=>
  string(4) "1219"
  [2]=>
  string(4) "1893"
 }
}
基于功能:

"$debug = rrd_lastupdate (
                          "".$rrdFile.""
             );"
我很难理解,因为输入是正确的,并且在编译代码时没有显示打印错误。为什么我没有得到任何输出

我已经把我的工作代码作为可能的复制和更好地理解我的错误的例子

<?php

// rrdtool info /var/www/snmp.rrd Debugging command

while (1) {
  sleep (1);
  $file = "snmp";
  $rrdFile = dirname(__FILE__) . "/".$file.".rrd";
  $in = "ifInOctets";
  $out = "ifOutOctets";
  $count = "sysUpTime";

  $options = array(
       "--start","now -10s", // Now -10 seconds (default)
       "--step", "10", // Step size of 300 seconds 5 minutes
       "DS:".$in.":COUNTER:20:U:U",
       "DS:".$out.":COUNTER:20:U:U",
       "DS:".$count.":COUNTER:20:U:U",
       /* DS:ds-name:DST:dst arguments
          (DST: GAUGE, COUNTER, DERIVE, and ABSOLUTE):heartbeat:min:max
          heartbeat: in case that there is not input up to 600 seconds
          then the input will characterised as undefined (blank)
          Based on Nyquist rate (Fs >= 2 * Fmax) 300 (step) 600 (heartbeat)
          32-bit = 2^32-1 = 4294967295 (counter ticks)
          64-bit = 2^64-1 = 18446744073709551615 (counter ticks)
          64-bit counter (caution!!!) different oid's
       */
       "RRA:MIN:0.5:10:300",
       "RRA:MIN:0.5:20:600",
       "RRA:MAX:0.5:10:300",
       "RRA:MAX:0.5:20:600",
       "RRA:AVERAGE:0.5:10:300",
       "RRA:AVERAGE:0.5:20:600",
       /* RRA:AVERAGE | MIN | MAX | LAST:xff:steps:rows
          xff range: 0-1 (exclusive) defines the allowed number of unknown
          *UNKNOWN* PDPs to the number of PDPs in the interval. Step defines
          how many of those data points are used to build consolidated data.
          rows defines how many data values are kept in an RRA.
       */
       );

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

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

  $ifInOctets = rand(0, 1500); // ifInOctets (OID: 1.3.6.1.2.1.2.2.1.10)
  $ifOutOctets = rand(0, 2500); // ifOutOctets (OID: 1.3.6.1.2.1.2.2.1.16)
  $sysUpTime = rand(0, 2000); // sysUpTime (OID: 1.3.6.1.2.1.1.3)

  $t = time();

  //update rrd file
  $update = rrd_update(
           "".$rrdFile."",
           array(
             /* Update database with 3 values 
            based on time now (N:timestamp) */
             "".$t.":".$ifInOctets.":".$ifOutOctets.":".$sysUpTime.""
             )
           );

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

  $start = "now";
  $title = "Hourly Server Data";

  $final = array(
     "--start","".$start." -10s",
     "--step","10",
     "--title=".$title."",
     "--vertical-label=Bytes/sec",
     "--lower-limit=0",
     //"--no-gridfit",
     "--slope-mode",
     //"--imgformat","EPS",
     "DEF:".$in."_def=".$file.".rrd:".$in.":AVERAGE",
     "DEF:".$out."_def=".$file.".rrd:".$out.":AVERAGE",
     "DEF:".$count."_def=".$file.".rrd:".$count.":AVERAGE",
     "CDEF:inbits=".$in."_def,8,*",
     "CDEF:outbits=".$out."_def,8,*",
     "CDEF:counter=".$count."_def,8,*",
     /* "VDEF:".$in_min."=inbits,MINIMUM",
        "VDEF:".$out_min."=outbits,MINIMUM",
        "VDEF:".$in_max."=inbits,MAXIMUM",
        "VDEF:".$out_max."=outbits,MAXIMUM",
        "VDEF:".$in_av."=inbits,AVERAGE",
        "VDEF:".$out_av."=outbits,AVERAGE", */
     "COMMENT:\\n",
     "LINE:".$in."_def#FF00FF:".$in."",
     "GPRINT:inbits:LAST:last \: %6.2lf %SBps",
     "COMMENT:\\n",
     "LINE:".$out."_def#0000FF:".$out."",
     "GPRINT:outbits:LAST:last \: %6.2lf %SBps",
     "COMMENT:\\n",
     "LINE:".$count."_def#FFFF00:".$count."",
     "GPRINT:counter:LAST:last\: %6.2lf %SBps",
     "COMMENT:\\n",
     );

  // graph output 
  $outputPngFile = rrd_graph(
             "".$file.".png",
             $final
             );

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

  /*  Returns the first data sample from the 
      specified RRA of the RRD file. */
   $result = rrd_first (
           $rrdFile,
           $raaindex = 0
           );

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

   /* Returns the UNIX timestamp of the most 
      recent update of the RRD database. */
   $last = rrd_last (
        $rrdFile
        );

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

    $info = rrd_info (
        "".$rrdFile.""
        );

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

    /*  Gets array of the UNIX timestamp and the values stored 
        for each date in the most recent update of the RRD database file. */
    $debug = rrd_lastupdate (
           "".$rrdFile.""
           );

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

     var_dump ($debug);

     /* $version = rrd_version ( );
        echo "This is the version ".$version."\n"; */

} // End of while condition 
?>

您的代码中有很多问题

首先,您的RRD文件显然是在While循环的每次迭代中重新创建的!这将覆盖上一个循环的更新

第二,尽管创建的RRD步长为10,但在每个更新循环结束时都没有进行睡眠(10)。更新RRD的频率不能超过步长

第三,您为DS使用了一种计数器类型,它假定计数不断增加。但是,您的测试数据是随机数,因此不会增加。减少值可能被视为计数器环绕,因此会存储一个巨大的数字,它超出DS的有效范围,因此会存储一个未知值

第四,您需要连续两次更新计数器才能获得有效速率;您每次迭代都会覆盖您的RRD文件,因此永远无法获得该文件

第五,被定义的最小RRA计数为10;这意味着您需要10个数据点才能在RRA中创建一个合并点。步骤为10秒,这意味着您需要运行110秒(11次更新),然后才能绘制单个数据点。您可能应该尝试添加计数1 RRA

最后,您的图形将被请求一个10秒的时间窗口。这不到一个RRA样本。记住,你的r步是10秒,你的最小RRA是count=10,所以整合步是100秒


我建议你修复这个循环,这样创建就在外面了;使睡眠等同于RRD步骤;添加计数1平均RRA;并提出更长时间窗口的图形请求。

感谢您提供的信息和对我的系统的分析。我尝试了一些修改,但仍然以相同的空图结束。我应用的修改:
sleep(10)
“DS:.$in.”:仪表:2:U:U“
”--step”,“1”
“RRA:MIN:0.5:2:1”
和图形步骤:
--step”,“15”
。我又走错方向了吗?我感谢您的时间和努力。我注意到我没有正确指定睡眠时间和图形时间,相反:
sleep(15)
“--step”,“15”
,但这仍然不能解决我的问题。我又漏掉了一些东西,我还没弄明白。你应该让你的slepp()调用与你的--step定义大小相同。另外,除非您将rrd_create调用移到while()循环之外,并使其无法获得任何结果!最后,如果您想使用这些随机更新值,您应该使用DS的仪表类型,而不是计数器。如果你想让它发挥作用,请处理我在原始帖子中提到的所有6个问题。谢谢你的时间和努力。我将使用这些值来尝试理解rrd数据库是如何工作和更新数据的。