<?php
################################################
#
# PNP v0.6 Template for Plugin stat_dev.pl
#
# 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)
#
####


$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',
    'color'  => '#00cf00',
    'unit'   => 'B',
  ),
  'writeB' => array(
    '0type'  => 'LINE1',
    'color'  => '#002a97',
    'unit'   => 'B',
  ),
  'read_time' => array(
    'redef'  => '1000,*', # time in ms
    '1type'  => 'LINE2',
    'color'  => '#880',
    'unit'   => 's',
  ),
  'write_time' => array(
    'redef'  => '1000,*', # time in ms
    '1type'  => 'LINE1',
    'color'  => '#808',
    'unit'   => 's',
  ),
  'io_wait' => array(
    '0type'  => 'LINE2',
    '2type'  => 'LINE2',
    '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',  
    'color' => '#00cf00',
    'unit'  => ' ',
  );
  $smap['write'] = array(
    '3type' => 'LINE1',
    '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',
    'color'  => '#f44',
    'unit'   => 's',
  );
  $smap['io_weighted'] = array(
  );

}


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




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

  $i = intval($k/$base)*$num_graphs +1;

  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'];
    }
    for( $g=0; $g<$num_graphs; $g++ ) {
      $opt[$i+$g]  = "--vertical-label '".$graphs[$g]['unit']."' ";
      $opt[$i+$g] .= "-l0 ";
      $opt[$i+$g] .= "--title '$hostname / $servicedesc - $dev[$i] - ".$graphs[$g]['name']."' ";
      #$this->MACRO['DISP_HOSTNAME']
      #$this->MACRO['DISP_SERVICEDESC']
      $def[$i+$g] = "";
      $ds_name[$i+$g] = $graphs[$g]['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 = "#faa";
        if ( array_key_exists("color", $smap[$v['sname']]) ) {
          $col = $smap[$v['sname']]["color"];
        }
        if ( array_key_exists("${g}color", $smap[$v['sname']]) ) {
          $col = $smap[$v['sname']]["${g}color"];
        }

        if ( $smap[$v['sname']]["${g}type"] == "AREA" ) {
          $def[$i+$g] .= rrd::area("v$k",$col,sprintf("%-20s",$dev[$i]." ".$v['sname']));
        }
        if ( $smap[$v['sname']]["${g}type"] == "LINE1" ) {
          $def[$i+$g] .= rrd::line1("v$k",$col,sprintf("%-20s",$dev[$i]." ".$v['sname']));
        }
        if ( $smap[$v['sname']]["${g}type"] == "LINE2" ) {
          $def[$i+$g] .= rrd::line2("v$k",$col,sprintf("%-20s",$dev[$i]." ".$v['sname']));
        }
        #...

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

      }
    }
  }
  
  
  if ( $k%$base == $base-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");
      }
    }
  }

}



?>
