dsebywql016137 2014-05-10 16: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 01: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.

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

报告相同问题?

悬赏问题

  • ¥100 二维码被拦截如何处理
  • ¥15 怎么解决LogIn.vue中多出来的div
  • ¥15 优博讯dt50巴枪怎么提取镜像
  • ¥30 在CodBlock上用c++语言运行
  • ¥15 求C6748 IIC EEPROM程序固化烧写算法
  • ¥50 关于#php#的问题,请各位专家解答!
  • ¥15 python 3.8.0版本,安装官方库ibm_db遇到问题,提示找不到ibm_db模块。如何解决?
  • ¥15 TMUXHS4412如何防止静电,
  • ¥30 Metashape软件中如何将建模后的图像中的植被与庄稼点云删除
  • ¥20 机械振动学课后习题求解答
手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部