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 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵