<?php
/*****************************************************************************
 *
 * linux-sysperf.php - gadget for NagVis for showing system perf stats
 *
 *****************************************************************************
 *
 * This is a gadget for NagVis to show system perf stats from a linux system
 * using the sgichk_snmp_system.pl plugin
 *
 * The gadget gets its data from the NagVis frontend by parameters. This
 * gadget only needs the "perfdata" parameter. NagVis also passes the
 * following parameters to the gadgets:
 *  - name1:     Hostname
 *  - name2:     Service description
 *  - state:     Current state
 *  - stateType: Current state type (soft/hard)
 *
 *****************************************************************************/

$font = '/db/oracle/product/11.1.0/client_1/jdk/jre/lib/fonts/LucidaSansRegular.ttf';

/** 
 * Dummy perfdata for WUI
 *
 * This string needs to be set in every gadget to have some sample data in the 
 * WUI to be able to place the gadget easily on the map
 ******************************************************************************/
$sDummyPerfdata = 'UserCPU=10.0 NiceCPU=10.0 SystemCPU=10.0 IdleCPU=10.0 WaitCPU=10.0 KernelCPU=10.0 IntCPU=10.0 SoftIRQCPU=10.0 mem_used=50.0 load1=1.21 load5=1.08 load15=1.01';

// Load gadget core functions
require('./gadgets_core.php');

/*******************************************************************************
 * Start gadget main code
 ******************************************************************************/

header("Content-type: image/png");

//==========================================
// Set Minimum, Default, and Maximum values.
//==========================================

$min = 0;
$max = -1;
$default = 0; 
 
/* Now read the parameters */

// Pull out values by their labels
for ($i = 0; $i < count($aPerfdata); $i++) {
   $value = $aPerfdata[$i]['value'];
   $label = $aPerfdata[$i]['label'];
   // Divide percentages by 100.0
   if (strstr($label, "CPU") || (strcmp($label, "mem_used") == 0)) {
      $vals[$label] = $value / 100.0;
   } else {
      $vals[$label] = $value;
   }
}


//==================
// Set image sizing.
//==================

if ($aOpts['scale'] < 20) {
   $aOpts['scale'] = 20;
}

$imgwidth = $aOpts['scale'] * 2;
$imgheight = $aOpts['scale'];
$innerdia = 0;
$outerdia = 150;
$linedia = 160;
$linewidth = 3;
$centerx = $imgwidth / 2;
$centery = $imgheight - 20;
$innerrad = $innerdia / 2;
$outerrad = $outerdia / 2-1;
$linerad = $linedia / 2;
$lineext = $linewidth/2;

// Create image itself
$img=imagecreatetruecolor($imgwidth, $imgheight);
$oBackground = imagecolorallocate($img, 122, 23, 211);
$oBlack = imagecolorallocate($img, 0, 0, 0);
$oWhite = imagecolorallocate($img, 255, 255, 255);
$oGreen = imagecolorallocate($img, 0, 255, 0);
$oYellow = imagecolorallocate($img, 255, 255, 0);
$oRed = imagecolorallocate($img, 255, 0, 0);
$oBlue = imagecolorallocate($img, 0, 0, 255);
$oGrey = imagecolorallocate($img, 170, 171, 161);
$oOrange = imagecolorallocate($img, 255, 125, 0);
//$oPurple = imagecolorallocate($img, 255, 0, 255);
$oPurple = imagecolorallocate($img, 141, 0, 186);
$oCyan = imagecolorallocate($img, 175, 236, 237);

imagefill($img, 0, 0, $oBackground);
imagecolortransparent($img, $oBackground);

//====================
// Create CPU Bar
//====================

$barx = $imgwidth * 0.05;
$bary = $imgheight * 0.05;
$barw = $imgwidth * 0.9;
$barh = ($imgheight / 3.0 * 0.9);

$vx = $barx;

// Kernel time should be a portion of System time so graph it as a split
// bar showing the kernel time overlapping the system time
$vw = $barw * $vals['SystemCPU'];
imagefilledrectangle ($img, $vx, $bary, ($vx+$vw), ($bary+$barh), $oRed);
$vw2 = $barw * $vals['KernelCPU'];
imagefilledrectangle ($img, $vx, $bary, ($vx+$vw2), ($bary+($barh/2)-1), $oPurple);
$vx = $vx + $vw;

$vw = $barw * $vals['WaitCPU'];
imagefilledrectangle ($img, $vx, $bary, ($vx+$vw), ($bary+$barh), $oYellow);
$vx = $vx + $vw;

$vw = $barw * $vals['SoftIRQCPU'];
imagefilledrectangle ($img, $vx, $bary, ($vx+$vw), ($bary+$barh), $oGrey);
$vx = $vx + $vw;

$vw = $barw * $vals['IntCPU'];
imagefilledrectangle ($img, $vx, $bary, ($vx+$vw), ($bary+$barh), $oOrange);
$vx = $vx + $vw;

$vw = $barw * $vals['UserCPU'];
imagefilledrectangle ($img, $vx, $bary, ($vx+$vw), ($bary+$barh), $oBlue);
$vx = $vx + $vw;

$vw = $barw * $vals['NiceCPU'];
imagefilledrectangle ($img, $vx, $bary, ($vx+$vw), ($bary+$barh), $oGreen);
$vx = $vx + $vw;

//$vw = $barw * $vals['KernelCPU'];
//imagefilledrectangle ($img, $vx, $bary, ($vx+$vw), ($bary+$barh), $oPurple);
//$vx = $vx + $vw;

imagerectangle ($img, $barx, $bary, ($barx+$barw), ($bary+$barh), $oBlack);

//====================
// Create Memory Bar
//====================

$bary = $bary + ($imgheight * 0.9 / 3);
$vw = $barw * $vals['mem_used'];
imagefilledrectangle ($img, $barx, $bary, ($barx+$vw), ($bary+$barh), $oCyan);

imagerectangle ($img, $barx, $bary, ($barx+$barw), ($bary+$barh), $oBlack);

//====================
// Load Average Text
//====================
if (strlen($vals['load1']) > 0) {
   $ty = $imgheight * 0.9;
   //$txt = "LA: " . $vals['load1'] . "," . $vals['load5'] . "," . $vals['load15'];
   $txt = $vals['load1'] . "," . $vals['load5'] . "," . $vals['load15'];
   imagefttext ($img, ($imgheight * 0.9 / 4.5), 0, $barx, $ty, $oBlack, $font, $txt);
}


//==============
// Output image.
//==============

if(function_exists('imageantialias')) {
	imageantialias($img, true);
}

imagepng($img);
imagedestroy($img);
?>
