<?php
################################################
#
# PNP v0.6 Template for Plugin stat_dev.pl (Multipath-Device)
#
# Thomas Sesselmann <t.sesselmann@dkfz.de> 2010
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
#
# Changelog:
# v0.9  2010.10.22 (ts)
#
####

#stat_mdev_NSD_SN02::stat_mdev::
#dm-3_readB=1844625920c; dm-3_writeB=101541888c; 
#dm-3_read_time=10715830c; dm-3_write_time=474560c; dm-3_io_wait=0c; 
#sdg_readB=517632c; sdg_writeB=0c; 
#sdg_read_time=60c; sdg_write_time=0c; sdg_io_wait=0c; 
#sdc_readB=1845143552c; sdc_writeB=101541888c; 
#sdc_read_time=6678300c; sdc_write_time=463990c; sdc_io_wait=0c; 



$base   = 5; # Datasources per Device

### short: (5 sources)
#hda_readB=0c; hda_writeB=0c; hda_read_time=0c; hda_write_time=0c; hda_io_wait=0c;
$graphs = array(
  0 => array(
    'name' => 'Read/Write Bytes',
    'unit' => 'Bytes / Second',
  ),
  1 => array(
    'name' => 'Read/Write Times',
    'unit' => 'Seconds',
  ),
  2 => array(
    'name' => 'IO Waits',
    'unit' => '#',
  ),
);

$smap = array ( 
  'readB' => array(
    #'0type'  => 'AREA',
    #'0type'  => array( 'LINE2','AREA','LINE1','LINE1' ),
    #'0color' => array( '#8f8','#8f8','#0f0','#080' ),
    '0type'  => array( 'LINE2','AREA','AREA','SAREA','C','C','C','C' ),
    '0color' => array( '#0c0','#0c0','#bf4','#4fb' ),
    'color'  => '#00cf00',
    'unit'   => 'B',
  ),
  'writeB' => array(
    #'0type'  => 'LINE1',
    '0type'  => array( 'LINE2','LINE1','LINE1','SLINE1','C','C','C','C' ),
    '0color' => array( '#00f', '#00f', '#a6f', '#6af' ),
    'color'  => '#00f',
    'unit'   => 'B',
  ),
  'read_time' => array(
    'redef'  => '1000,*', # time in ms
    #'1type'  => 'LINE2',
    '1type'  => array( 'LINE2','AREA','AREA','SAREA','C','C','C','C' ),
    '1color' => array( '#880', '#880', '#fd9', '#df9' ),
    'color'  => '#880',
    'unit'   => 's',
  ),
  'write_time' => array(
    'redef'  => '1000,*', # time in ms
    #'1type'  => 'LINE1',
    '1type'  => array( 'LINE2','LINE1','LINE1','SLINE1','C','C','C','C' ),
    '1color' => array( '#808', '#808', '#f9d', '#d9f' ),
    'color'  => '#808',
    'unit'   => 's',
  ),
  'io_wait' => array(
    #'0type'  => 'LINE2',
    '0type'  => array( 'LINE2','LINE1' ),
    #'0color' => array( '#f00', '#f00', '#fbb', '#f88' ),
    #'2type'  => 'LINE2',
    '2type'  => array( 'LINE2','LINE1','AREA','SAREA','C','C','C','C' ),
    '2color' => array( '#f00', '#f00', '#fec', '#fce' ),
    'color'  => '#f00',
    'unit'   => ' ',
  ),
);



### full: (11 sources)
#dm-0_read=125923c; dm-0_write=9173388; 
#dm-0_read_merged=0c; dm-0_write_merged=0c; 
#dm-0_readB=1803682816c; dm-0_writeB=37574197248c; 
#dm-0_read_time=629735c; dm-0_write_time=611855205c; 
#dm-0_io_wait=0c; dm-0_io_time=5899188; dm-0_io_weighted=612485267c;
if ( count($this->DS)%11 == 0 ) {
  $base = 11; # Long version

  $graphs[3] = array(
    'name' => 'Read/Write IOs',
    'unit' => 'IOs / Second',
  );

  $smap['read'] = array(
    #'3type' => 'AREA',
    '3type' => $smap['readB']['0type'],
    '3color' => $smap['readB']['0color'],
    'color' => '#00cf00',
    'unit'  => ' ',
  );
  $smap['write'] = array(
    #'3type' => 'LINE1',
    '3type' => $smap['writeB']['0type'],
    '3color' => $smap['writeB']['0color'],
    'color' => '#002a97',
    'unit'  => ' ',
  );
  $smap['read_merged'] = array(
    '3type'  => 'LINE1',
    'color'  => '#7a0',
    'unit'   => ' ',
  );
  $smap['write_merged'] = array(
    '3type'  => 'LINE1',
    'color'  => '#70a',
    'unit'   => ' ',
  );
  $smap['io_time'] = array(
    'redef'  => '1000,*', # time in ms
    #'1type'  => 'LINE1',
    '1type'  => array( '','LINE1','C','C','C','C','C','C' ),
    '1color' => array( '#f00', '#f00', '#fec', '#fce' ),
    'color'  => '#f44',
    'unit'   => 's',
  );
  $smap['io_weighted'] = array(
  );

}


$num_graphs = count($graphs); # Graphs per base


## Get color from array

if (!function_exists('get_color')) {
function get_color(array $a, $g=0, $d=0)
{

  $col = "#faa";
  if ( array_key_exists("color", $a) ) {
    $col = $a["color"];
  }
  if ( array_key_exists("${g}color", $a) ) {
    if ( is_array($a["${g}color"])) {
      if ( array_key_exists($d,$a["${g}color"]) ) {
        $col = $a["${g}color"][$d];
      }
    } else {
      $col = $a["${g}color"];
    }
  }
  return $col;
}
}


## Get type from array
if ( !function_exists('get_type')) {
function get_type(array $a, $g=0, $d=0)
{
  $type = "";
  if ( array_key_exists("${g}type",$a) ) {
    if ( is_array($a["${g}type"]) ) {
      if ( array_key_exists($d,$a["${g}type"]) ) {
        $type = $a["${g}type"][$d];
      }
    } else {
      $type = $a["${g}type"];
    }
  }
  return $type;
}
}


## Sum definitions for the graphs at beginning
for( $g=0; $g<$num_graphs; $g++ ) {
  $opt[1+$g]  = "--vertical-label '".$graphs[$g]['unit']."' ";
  $opt[1+$g] .= "-l0 ";
  $opt[1+$g] .= "--title '$hostname / $servicedesc ".$graphs[$g]['name']."' ";
  #$this->MACRO['DISP_HOSTNAME']
  #$this->MACRO['DISP_SERVICEDESC']
  $def[1+$g] = "";
  $ds_name[1+$g] = $graphs[$g]['name'];
  $graphs[$g]['def'] = array ();
  $graphs[$g]['defa'] = "";
}


$dev = array();
foreach ( $this->DS as $k=>$v ) {

  #$i = intval($k/$base)*$num_graphs +1;
  $i = 1; # map all to one graph
  $d = intval($k/$base) +1; # device (1 master, >1 slaves)

  if ( $k%$base == 0 ) { # Some definitions at beginning
    if ( preg_match( "/^(.*)_readB$/",$v['NAME'], $treffer ) ) {
      $dev[$i] = $treffer[1];
      $ds_name[$i] = $dev[$i];
    }else{
      $dev[$i] = "xx ".$v['NAME'];
    }
  }

  if ( preg_match( "/^$dev[$i]_(.*)$/",$v['NAME'], $treffer ) ) {
    $v['sname'] = $treffer[1];
  } else {
    $v['sname'] = $v['NAME'];
  }


  for( $g=0; $g < $num_graphs ; $g++ ) {
    if ( array_key_exists($v['sname'],$smap) ) {
      if ( array_key_exists("${g}type", $smap[$v['sname']]) ) {

        if ( array_key_exists("${g}redef", $smap[$v['sname']]) ) {
          $def[$i+$g] .= rrd::def("rv$k", $v['RRDFILE'], $v['DS'], "AVERAGE" );
          $def[$i+$g] .= rrd::cdef("v$k", "rv$k,".$smap[$v['redef']] );
        } else {
          $def[$i+$g] .= rrd::def("v$k", $v['RRDFILE'], $v['DS'], "AVERAGE" );
        }

        $col = get_color($smap[$v['sname']],$g,$d);

        $type = get_type($smap[$v['sname']],$g,$d);
       
        if ( $d == 1 ) {
          $col0  = get_color($smap[$v['sname']],$g,0);
          $type0 = get_type($smap[$v['sname']],$g,0);
          
          if ( $type0 == "LINE1" ) { 
            $graphs[$g]['defa'] .= rrd::line1("v$k",$col0,"");
          }
          if ( $type0 == "LINE2" ) { 
            $graphs[$g]['defa'] .= rrd::line2("v$k",$col0,"");
          }
          #...
        }

        if ( ! array_key_exists($v['sname'],$graphs[$g]['def']) ) {
          $graphs[$g]['def'][$v['sname']] = "";
        }

        if ( $type == "AREA" ) {
          $graphs[$g]['def'][$v['sname']] .= rrd::area("v$k",$col,sprintf("%-6s %-14s",$dev[$i],$v['sname']));
        }
        if ( $type == "SAREA" ) {
          $graphs[$g]['def'][$v['sname']] .= rrd::area("v$k",$col,sprintf("%-6s %-14s",$dev[$i],$v['sname']),1);
        }
        if ( $type == "LINE1" ) {
          $graphs[$g]['def'][$v['sname']] .= rrd::line1("v$k",$col,sprintf("%-6s %-14s",$dev[$i],$v['sname']));
        }
        if ( $type == "SLINE1" ) {
          $graphs[$g]['def'][$v['sname']] .= rrd::line1("v$k",$col,sprintf("%-6s %-14s",$dev[$i],$v['sname']),1);
        }
        if ( $type == "LINE2" ) {
          $graphs[$g]['def'][$v['sname']] .= rrd::line2("v$k",$col,sprintf("%-6s %-14s",$dev[$i],$v['sname']));
        }
        if ( $type == "C" ) {
          $graphs[$g]['def'][$v['sname']] .= rrd::comment(sprintf("  %-6s %-14s",$dev[$i],$v['sname']));
        }
        #...

        if ( $type != "" ) {
          $graphs[$g]['def'][$v['sname']] .= rrd::gprint("v$k","MAX","MAX\: %4.3lg%s".$smap[$v['sname']]['unit']);
          $graphs[$g]['def'][$v['sname']] .= rrd::gprint("v$k","MIN","MIN\: %4.3lg%s".$smap[$v['sname']]['unit']);
          $graphs[$g]['def'][$v['sname']] .= rrd::gprint("v$k","AVERAGE","AVG\: %4.3lg%s".$smap[$v['sname']]['unit']);
          $graphs[$g]['def'][$v['sname']] .= rrd::gprint("v$k","LAST","LAST\: %4.3lg%s".$smap[$v['sname']]['unit']);
          $graphs[$g]['def'][$v['sname']] .= rrd::comment("\\n");
        }

      }
    }
  }
  
  
}



for ( $g=0; $g<$num_graphs; $g++ ) {
  $def[1+$g] .= join(" ",$graphs[$g]['def']);
  $def[1+$g] .= $graphs[$g]['defa'];
}


if ( 1 ) { # Some definitions at the end
  for( $g=0; $g<$num_graphs; $g++ ) {
    if ( preg_match("/Bytes/i",$graphs[$g]['unit']) ) {
      $def[$i+$g] .= rrd::hrule(   10*131072, "#333", "10MBit/s");
      $def[$i+$g] .= rrd::hrule(  100*131072, "#773", "100MBit/s");
      $def[$i+$g] .= rrd::hrule( 1024*131072, "#484", "1GBit/s");
      $def[$i+$g] .= rrd::hrule(10240*131072, "#844", "10GBit/s");
      $def[$i+$g] .= rrd::comment("\\n");
    }
  }
}



?>
