Location: PHPKode > projects > Network Managment / Inventory System > clearsite/include/aloe/graph.php
<?php

function aloe_graph_cust($ds_detail, $ds_mult, $ds_label, $graph_type, $graph_start, $graph_end, $graph_title, $graph_vlabel, $graph_base, $graph_height, $graph_width, $image_name, $legend, $image_path, $rrdtool_path, $totals = "no") {
  //$ds_detail    - Array of strings
  //                ex - /rrd-data/10.10.17.220.8-41.traffic.rrd:traffic_in:AVERAGE
  //$ds_mult      - Array of integers (may be negative) (one for each ds_detail)
  //                used here: CDEF:cdefa=a,8,*
  //$ds_label     - Array of strings (one for each ds_detail)
  //                Used here: "AREA:cdefa#FFAB00:$ds_label\\n"
  //$graph_type   - String
  //                should be AREA, STACK or LINE
  //$graph_start  - String
  //                -1d for one day
  //$graph_end    - String
  //                -300 for 5 minutes ago
  //$graph_title  - String
  //                used here: "--title=$graph_title"
  //$graph_vlabel - String
  //                used here: "--vertical-label=$graph_vlabel"
  //$graph_base   - Integer
  //                1000 for traffic 1024 for memory like stuff
  //$graph_height - Integer
  //                Number of pixels (standard 100)
  //$graph_width  - Integer
  //                Number of pixels (standard 415)
  //$image_name   - String (filename)
  //                filename of image to create

  $graph_colors = array('#577AF6',
                        '#0000FF',
                        '#B8C7FB',
                        '#06207A',
                        '#065A79',
                        '#09DAAC',
                        '#009600',
                        '#003000',
                        '#0B39DD',
                        '#CCCCFE',
                        '#003200',
                        '#009900',
                        '#00FF00',
                        '#65FF65',
                        '#CCFECC',
                        '#00FC00',
                        '#62FF62');
                        
                        
  $opts = "--imgformat=PNG";
  $opts .= " --start=\"$graph_start\"";
  $opts .= " --end=\"$graph_end\"";
  $opts .= " --alt-autoscale-max";
  $opts .= " --slope-mode";
  $opts .= " --base=$graph_base";
  $opts .= " --height=$graph_height";
  $opts .= " --width=$graph_width";
  if ($legend == "nolegend") {
    $opts .= " --no-legend";
  };
  if ($graph_type != "STACK") {
    $opts .= " --rigid";
  };                
  $opts .= " --title=\"$graph_title\"";
  $opts .= " --vertical-label=\"$graph_vlabel\"";
  $placeholder = "a";
  foreach ($ds_detail as $detail) {
    $opts .= " \"DEF:$placeholder=$detail\"";
    $placeholder++;
  };
  $placeholder = "a";
  foreach ($ds_mult as $mult) {
    if (substr($mult, 0, 1) == "-") {
      $opts .= " \"CDEF:cdef$placeholder" . "n=$placeholder,$mult,*\"";
      $mult = substr($mult, 1);
    };
    $opts .= " \"CDEF:cdef$placeholder=$placeholder,$mult,*\"";
    $placeholder++;
  };
  
  if ($totals == "yes") {
    $timestmp = time();
    $totals_string = "";
    $x = "a";
    for ($y = 0; $y < count($ds_mult); $y++) {
      if ($x != "a") {
        $totals_string .= ",TIME,$timestmp,GT,$x,$x,UN,0,$x,IF,IF";
        if (isset($add)) {
          $add .= "+,";
        } else {
          $add = ",+,";
        };
      } else {
        $totals_string .= "TIME,$timestmp,GT,$x,$x,UN,0,$x,IF,IF";
      };
      $x++;
    };
    $opts .= " \"CDEF:cdef$x=" . $totals_string . $add . "8,*\"";
  };
  
  $placeholder = "a";
  $cnum = "0";
  $index = "0";
  $fnp = "N";
  foreach ($ds_label as $label) {
    if ($placeholder == "a") {
      if (substr($mult[$index], 0, 1) == "-") {
        $opts .= " \"AREA:cdef$placeholder" . "n$graph_colors[$cnum]:$label\"";
        $fnp = "Y";
      } else {
        $opts .= " \"AREA:cdef$placeholder$graph_colors[$cnum]:$label\"";
      };
    } else if ($placeholder != "a") {
      if (substr($ds_mult[$index], 0, 1) == "-") {
        if ($fnp == "N") {
          $opts .= " \"AREA:cdef$placeholder" . "n$graph_colors[$cnum]:$label\"";
          $fnp = "Y";
        } else {
          $opts .= " \"$graph_type:cdef$placeholder" . "n$graph_colors[$cnum]:$label\"";
        };
      } else {
        $opts .= " \"$graph_type:cdef$placeholder$graph_colors[$cnum]:$label\"";
      };
    };
    $opts .= " \"GPRINT:cdef$placeholder:LAST:Current\:%8.2lf %s\"";
    $opts .= " \"GPRINT:cdef$placeholder:AVERAGE:Avg\:%8.2lf %s\"";
    $opts .= " \"GPRINT:cdef$placeholder:MAX:Max\:%8.2lf %s\\n\"";
    $placeholder++;
    $index++;
    if ($cnum == "13") {
      $cnum = "0";
    } else {
      $cnum++;
    };
  };
  if ($totals == "yes") {
    $opts .= " \"GPRINT:cdef$x:LAST:Current-Total\:%8.2lf %s\"";
    $opts .= " \"GPRINT:cdef$x:MAX:MAX-Total\:%8.2lf %s\\n\"";
  };
  unset($graph_results);
  $create_graph = exec("$rrdtool_path graph $image_path/$image_name $opts", $graph_results);
  $return = "$rrdtool_path graph $image_path/$image_name $opts";
  return $return;
//  foreach ($graph_results as $gres) {
//    print "ERROR: $gres<br>";
//  };
};

function aloe_graph($ds_detail, $ds_mult, $ds_label, $graph_type, $graph_title, $graph_vlabel, $graph_base, $graph_height, $graph_width, $image_name, $image_path, $rrdtool_path) {
  //$ds_detail    - Array of strings
  //                ex - /rrd-data/10.10.17.220.8-41.traffic.rrd:traffic_in:AVERAGE
  //$ds_mult      - Array of integers (may be negative) (one for each ds_detail)
  //                used here: CDEF:cdefa=a,8,*
  //$ds_label     - Array of strings (one for each ds_detail)
  //                Used here: "AREA:cdefa#FFAB00:$ds_label\\n"
  //$graph_type   - String
  //                should be AREA, STACK or LINE
  //$graph_start  - String
  //                -1d for one day
  //$graph_end    - String
  //                -300 for 5 minutes ago
  //$graph_title  - String
  //                used here: "--title=$graph_title"
  //$graph_vlabel - String
  //                used here: "--vertical-label=$graph_vlabel"
  //$graph_base   - Integer
  //                1000 for traffic 1024 for memory like stuff
  //$graph_height - Integer
  //                Number of pixels (standard 100)
  //$graph_width  - Integer
  //                Number of pixels (standard 415)
  //$image_name   - String (filename)
  //                filename of image to create

                  
  $graph_colors = array('#577AF6',
                        '#0000FF',
                        '#B8C7FB',
                        '#06207A',
                        '#065A79',
                        '#09DAAC',
                        '#009600',
                        '#003000',
                        '#0B39DD',
                        '#CCCCFE',
                        '#003200',
                        '#009900',
                        '#00FF00',
                        '#65FF65',
                        '#CCFECC',
                        '#00FC00',
                        '#62FF62');

  $opts_start = "--imgformat=PNG";
  $opts_end = " --alt-autoscale-max";
  $opts_end .= " --slope-mode";
  $opts_end .= " --base=$graph_base";
  $opts_end .= " --height=$graph_height";
  $opts_end .= " --width=$graph_width";
  if ($graph_type != "STACK") {
    $opts_end .= " --rigid";
  };                
  $opts_end .= " --title=\"$graph_title\"";
  $opts_end .= " --vertical-label=\"$graph_vlabel\"";
  $placeholder = "a";
  foreach ($ds_detail as $detail) {
    $opts_end .= " \"DEF:$placeholder=$detail\"";
    $placeholder++;
  };
  $placeholder = "a";
  foreach ($ds_mult as $mult) {
    if (substr($mult, 0, 1) == "-") {
      $opts_end .= " \"CDEF:cdef$placeholder" . "n=$placeholder,$mult,*\"";
      $mult = substr($mult, 1);
    };
    $opts_end .= " \"CDEF:cdef$placeholder=$placeholder,$mult,*\"";
    $placeholder++;
  };
  $placeholder = "a";
  $cnum = "0";
  $index = "0";
  $fnp = "N";
  foreach ($ds_label as $label) {
    if ($placeholder == "a") {
      if (substr($mult[$index], 0, 1) == "-") {
        $opts_end .= " \"AREA:cdef$placeholder" . "n$graph_colors[$cnum]:$label\\n\"";
        $fnp = "Y";
      } else {
        $opts_end .= " \"AREA:cdef$placeholder$graph_colors[$cnum]:$label\\n\"";
      };
    } else if ($placeholder != "a") {
      if (substr($ds_mult[$index], 0, 1) == "-") {
        if ($fnp == "N") {
          $opts_end .= " \"AREA:cdef$placeholder" . "n$graph_colors[$cnum]:$label\\n\"";
          $fnp = "Y";
        } else {
          $opts_end .= " \"$graph_type:cdef$placeholder" . "n$graph_colors[$cnum]:$label\\n\"";
        };
      } else {
        $opts_end .= " \"$graph_type:cdef$placeholder$graph_colors[$cnum]:$label\\n\"";
      };
    };
    $opts_end .= " \"GPRINT:cdef$placeholder:LAST:Current\:%8.2lf %s\"";
    $opts_end .= " \"GPRINT:cdef$placeholder:AVERAGE:Avg\:%8.2lf %s\"";
    $opts_end .= " \"GPRINT:cdef$placeholder:MAX:Max\:%8.2lf %s\\n\"";
    $placeholder++;
    $index++;
    if ($cnum == "13") {
      $cnum = "0";
    } else {
      $cnum++;
    };
  };
  $opts = $opts_start;
  $opts .= " --start=-1d";
  $opts .= " --end=-300";
  $opts .= $opts_end;
  $create_graph = exec("$rrdtool_path graph $image_path/$image_name.day.png $opts", $graph_results);
//  print "$rrdtool_path graph $image_path/$image_name.day.png $opts<p>";
//  foreach ($graph_results as $gres) {
//    print "ERROR: $gres<br>";
//  };
  unset($opts);
  $opts = $opts_start;
  $opts .= " --start=-1w";
  $opts .= " --end=-300";
  $opts .= $opts_end;
  $create_graph = exec("$rrdtool_path graph $image_path/$image_name.week.png $opts", $graph_results);
//  foreach ($graph_results as $gres) {
//    print "ERROR: $gres<br>";
//  };
  unset($opts);
  $opts = $opts_start;
  $opts .= " --start=-1M";
  $opts .= " --end=-300";
  $opts .= $opts_end;
  $create_graph = exec("$rrdtool_path graph $image_path/$image_name.month.png $opts", $graph_results);
//  foreach ($graph_results as $gres) {
//    print "ERROR: $gres<br>";
//  };
};

?>
Return current item: Network Managment / Inventory System