简体   繁体   English

PHP RRD图分离

[英]php RRD graph separation

I am trying to create RRD graphs with the help of PHP in order to keep track of the inoctets,outoctets and counter of a server. 我试图在PHP的帮助下创建RRD图,以便跟踪服务器的八位字节,八位字节和计数器。

So far the script is operating as expected but my problems comes when I am trying to produce 2 or more separate graphs. 到目前为止,脚本已按预期运行,但是当我尝试生成2个或更多单独的图形时,我的问题来了。 I am trying to produce (hourly, weekly , etc) graphs. 我正在尝试生成(每小时,每周等)图。 I thought by creating a loop would solve my problem, since I have split the RRA in hours and days. 我认为通过创建循环可以解决我的问题,因为我将RRA分为几个小时和几天。 Unfortunately I end up having 2 graphs that updating simultaneously as expected but plotting the same thing. 不幸的是,我最终得到了2个图,这些图可以按预期的方式同时进行更新,但是却绘制出相同的东西。 Has any one encounter similar problem? 有没有人遇到类似的问题? I have applied the same program in perl with RRD::Simple,where is extremely easy and everything is adjusted almost automatically. 我在带有RRD :: Simple的perl中应用了相同的程序,该程序非常容易,几乎所有内容都会自动调整。

图形输出

I have supplied under a working example of my code with the minimum possible data because the code is a bit long: 在我的代码的一个工作示例下,我提供了尽可能少的数据,因为代码有点长:

<?php

$file = "snmp-2";
$rrdFile = dirname(__FILE__) . "/snmp-2.rrd";
$in = "ifInOctets";
$out = "ifOutOctets";
$count = "sysUpTime";
$step = 5;
$rounds = 1;
$output = array("Hourly","Daily");

while (1) { 
  sleep (6);

  $options = array(
           "--start","now -15s", // Now -10 seconds (default)
           "--step", "".$step."",
           "DS:".$in.":GAUGE:10:U:U",
           "DS:".$out.":GAUGE:10:U:U",
           "DS:".$count.":ABSOLUTE:10:0:4294967295",
           "RRA:MIN:0.5:12:60",
           "RRA:MAX:0.5:12:60",
           "RRA:LAST:0.5:12:60",
           "RRA:AVERAGE:0.5:12:60",
           "RRA:MIN:0.5:300:60",
           "RRA:MAX:0.5:300:60",
           "RRA:LAST:0.5:300:60",
           "RRA:AVERAGE:0.5:300:60",
           );


  if ( !isset( $create ) ) {

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

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

  $t = time();

  $ifInOctets = rand(0, 4294967295);
  $ifOutOctets = rand(0, 4294967295);
  $sysUpTime = rand(0, 4294967295);

  $update = rrd_update(
               "".$rrdFile."",
               array(
                 "".$t.":".$ifInOctets.":".$ifOutOctets.":".$sysUpTime.""
                 )
               );

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

  $start = $t - ($step * $rounds);

  foreach ($output as $test) {   

    $final = array(
           "--start","".$start." -15s",
           "--end", "".$t."",
           "--step","".$step."",
           "--title=".$file." RRD::Graph",
           "--vertical-label=Byte(s)/sec",
           "--right-axis-label=latency(min.)",
           "--alt-y-grid", "--rigid",
           "--width", "800", "--height", "500", 
           "--lower-limit=0",
           "--alt-autoscale-max",
           "--no-gridfit",
           "--slope-mode",
           "DEF:".$in."_def=".$file.".rrd:".$in.":AVERAGE",
           "DEF:".$out."_def=".$file.".rrd:".$out.":AVERAGE",
           "DEF:".$count."_def=".$file.".rrd:".$count.":AVERAGE",
           "CDEF:inbytes=".$in."_def,8,/",
           "CDEF:outbytes=".$out."_def,8,/",
           "CDEF:counter=".$count."_def,8,/",
           "COMMENT:\\n",
           "LINE2:".$in."_def#FF0000:".$in."",
           "COMMENT:\\n",
           "LINE2:".$out."_def#0000FF:".$out."",
           "COMMENT:\\n",
           "LINE2:".$count."_def#FFFF00:".$count."",
           );

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

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

  } /* End of foreach function */

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

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

  var_dump ($debug);

  $rounds++;

} /* End of while loop */

?>

A couple of issues. 有几个问题。

Firstly, your definition of the RRD has a step of 5seconds and RRAs with steps of 12x5s=1min and 300x5s=25min. 首先,您对RRD的定义有5秒的步长,而RRA的步长为12x5s = 1min和300x5s = 25min。 They also have a length of only 60 rows, so 1hr and 25hr respectively. 它们的长度也只有60行,分别是1小时和25小时。 You'll never get a weekly graph this way! 这样您将永远不会获得每周图表! You need to add more rows; 您需要添加更多行; also the step seems rather short, and you might need a smaller-step RRA for hourly graphs and a larger-step one for weekly graphs. 而且这一步似乎很短,对于小时图,您可能需要较小的RRA,对于每周图,可能需要较大的RRA。

Secondly, it is not clear how you're calling the graph function. 其次,不清楚如何调用图形函数。 You seem to be specifying: 您似乎在指定:

"--start","".$start." -15s",
"--end", "".$t."",
"--step","".$step."",

... which would force it to use the 5s interval (unavailable, so the 1min one would always get used) and for the graph to be only for the time window from the start to the last update, not a 'hourly' or 'daily' as you were asking. ...这将迫使它使用5秒间隔(不可用,因此将始终使用1分钟),并且该图仅用于从开始到最后一次更新的时间范围,而不是“每小时”或“每天”。

Note that the RRA you have defined do not define the time window of the graph you are asking for. 请注意,您定义的RRA并未定义您要求的图形的时间窗口。 Also, just because you have more than one RRA defined, it doesnt mean you'll get more than one graph unless oyu call the graph function twice with different arguments. 同样,仅因为您定义了多个RRA,但这并不意味着您将获得多个图形,除非oyu使用不同的参数两次调用图形函数。

If you want a daily graph, use 如果您想要每日图表,请使用

"--start","end - 1 hour",
"--end",$t,

Do not specify a step as the most appropriate available will be used anyway. 不要指定步骤,因为无论如何都会使用最合适的步骤。 For a daily graph, use 对于每日图表,请使用

"--start","end - 1 day"
"--end",$t,

Similarly, no need to specify a step. 同样,无需指定步骤。

Hopefully this will make it a little clearer. 希望这将使它更加清晰。 Most of the RRD graph options have sensible defaults, and RRDTool is pretty good at picking the correct RRA to use based on the graph size, time window, and DEF statements. 大多数RRD图形选项都有合理的默认值,并且RRDTool非常擅长根据图形大小,时间窗口和DEF语句选择要使用的正确RRA。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM