dsebywql016137 2014-05-11 00:21
浏览 111
已采纳

php RRD图分离

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.

So far the script is operating as expected but my problems comes when I am trying to produce 2 or more separate graphs. 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. Unfortunately I end up having 2 graphs that updating simultaneously as expected but plotting the same thing. 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.

Graph Output

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()."
";
    }
  }

  $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()."
";
  }

  $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:\
",
           "LINE2:".$in."_def#FF0000:".$in."",
           "COMMENT:\
",
           "LINE2:".$out."_def#0000FF:".$out."",
           "COMMENT:\
",
           "LINE2:".$count."_def#FFFF00:".$count."",
           );

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

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

  } /* End of foreach function */

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

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

  var_dump ($debug);

  $rounds++;

} /* End of while loop */

?>
  • 写回答

1条回答 默认 最新

  • dongqi8030 2014-05-11 09:25
    关注

    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. They also have a length of only 60 rows, so 1hr and 25hr respectively. 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.

    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.

    Note that the RRA you have defined do not define the time window of the graph you are asking for. 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.

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 centos7.9脚本,怎么排除特定的访问记录
  • ¥15 关于#Django#的问题:我的静态文件呢?
  • ¥15 关于CPLEX的问题,请专家解答
  • ¥15 cocos的点击事件 怎么穿透到 原生fragment上。
  • ¥20 基于相关估计的TDOA算法中的加权最小二乘拟合法matlab仿真
  • ¥20 基于相关估计的TDOA算法中的自适应加权广义互相关法。
  • ¥15 abaqus CAE 2024软件启动问题
  • ¥20 基于相关估计的TDOA算法中的局部互相关函数滤波matlab仿真
  • ¥15 CDH6.0.1 hue报错
  • ¥15 javaFX利用scene builder的fxml文件进行开发时的label setText方法未生效问题