<?php 
  $author = 'Matthias Pester';
  $author_url = 'http://www.tu-chemnitz.de/~pester/';
  
// retrieve information
  $lines = array();
  if (isset($_GET['server'])) $serv_name=$_GET['server'];
  else {
    $serv_name = exec("/usr/local/bin/qstat -B -f clic0a1 | awk '/Server: /{print $2}'", $lines, $retval);
    if ($retval != 0) {
        $serv_name = 'unknown';
    } 	
  }

  if ( isset($_GET['free']) && isset($_GET['used']) &&
       isset($_GET['offl']) && isset($_GET['down']) &&
       isset($_GET['chek']) ) {
    $arg_free=(int)$_GET['free']; 
    $arg_used=(int)$_GET['used']; 
    $arg_offl=(int)$_GET['offl']; 
    $arg_down=(int)$_GET['down']; 
    $arg_chek=(int)$_GET['chek']; 
    $arg_all =$arg_free + $arg_used + $arg_offl + $arg_down;
  } else {
    $nodestate = array();
    exec("/usr/local/bin/pbsnodes -a | awk '/state =/{print $3}'", 
	$nodestate , $retval);
    reset($nodestate);

    $arg_all=0;
    $arg_used=0;
    $arg_free=0;
    $arg_offl=0;
    $arg_down=0;
    $arg_unkn=0;
    $arg_chek=0;
    while (list($key, $val) = each($nodestate)) {
      if (strstr ($val, "job-exclusive")) {
        $arg_used++;
	if (strstr ($val, "offline")) $arg_chek++;
      }	elseif (strstr ($val, "offline")) {
        $arg_offl++;
      } elseif (strstr ($val, "down")) {
        $arg_down++;
      } elseif (strstr ($val, "free")) {
        $arg_free++;
      } else {
        $arg_unkn++;
      }
      $arg_all++;
    }
  }
// create image
$size=180;
$image = imagecreate(250, 190);

// transparent color for background 
  $bg_color = ImageColorAllocate ($image, 254, 254, 254);
  ImageColorTransparent($image, $bg_color);

// allocate some solors
ImageAlphaBlending($image,TRUE);
$col_used = imagecolorallocate($image, 0x00, 0x00, 0xFF);
$shd_used = imagecolorallocate($image, 0x00, 0x00, 0x90);
$col_free = imagecolorallocate($image, 0x00, 0xAA, 0x00);
$shd_free = imagecolorallocate($image, 0x00, 0x77, 0x00);
$col_offl = imagecolorallocate($image, 0xFF, 0x00, 0x00);
$shd_offl = imagecolorallocate($image, 0x90, 0x00, 0x00);
$col_down = imagecolorallocate($image, 0x7F, 0x00, 0x7F);
$shd_down = imagecolorallocate($image, 0x3F, 0x00, 0x3F);
$col_chek = imagecolorallocate($image, 0x44, 0x00, 0xFF);

// color for text 
  $text_color = ImageColorAllocate ($image, 0, 0, 0);

// server name and date
  $imgstring = $serv_name;
  ImageString ($image, 3, 10, 12,  $imgstring, $text_color);
  if (isset($_GET[tstamp]))
  $imgstring = date ("D M j G:i:s T Y",$_GET[tstamp]);
  else
  $imgstring = date ("D M j G:i:s T Y");
  ImageString ($image, 2, 10, 30,  $imgstring, $text_color);
// the processor numbers  
  $rpos = $size+20;
if ($arg_used > 0) ImageString ($image, 3, $rpos, 55, $arg_used, $col_used);
if ($arg_chek > 0) ImageString ($image, 2, $rpos, 75, "[".$arg_chek."]", $col_chek);
if ($arg_free > 0) ImageString ($image, 3, $rpos,135, $arg_free, $col_free);
if ($arg_offl > 0) ImageString ($image, 3, $rpos,115, $arg_offl, $col_offl);
if ($arg_down > 0) ImageString ($image, 3, $rpos, 95, $arg_down, $col_down);
  ImageString ($image, 3, $rpos,170, $arg_all, $text_color);

$beg_used = 360;
$end_used = $beg_used-(360*$arg_used / $arg_all);
$end_chek = $beg_used-(360*$arg_chek / $arg_all);
$end_free = $end_used-(360*$arg_free / $arg_all);
$end_offl = $end_free-(360*$arg_offl / $arg_all);
$end_down = 0;

$siz2 = $size/2;
$cx = $siz2;
$cy = $siz2+10;

// make the 3D effect
for ($i = $size/10; $i > 0; $i--) {
  if ($end_offl > $end_down) imagefilledarc($image, $cx, $cy+$i, $size, $siz2, $end_down, $end_offl, $shd_down, IMG_ARC_PIE);
  if ($end_free > $end_offl) imagefilledarc($image, $cx, $cy+$i, $size, $siz2, $end_offl, $end_free, $shd_offl, IMG_ARC_PIE);
  if ($end_used > $end_free) imagefilledarc($image, $cx, $cy+$i, $size, $siz2, $end_free, $end_used, $shd_free, IMG_ARC_PIE);
  if ($beg_used > $end_used) imagefilledarc($image, $cx, $cy+$i, $size, $siz2, $end_used, $beg_used, $shd_used, IMG_ARC_PIE);
}

  if ($end_offl > $end_down) imagefilledarc($image, $cx, $cy, $size, $siz2, $end_down, $end_offl, $col_down, IMG_ARC_PIE);
  if ($end_free > $end_offl) imagefilledarc($image, $cx, $cy, $size, $siz2, $end_offl, $end_free, $col_offl, IMG_ARC_PIE);
  if ($end_used > $end_free) imagefilledarc($image, $cx, $cy, $size, $siz2, $end_free, $end_used, $col_free, IMG_ARC_PIE);
  if ($beg_used > $end_used) imagefilledarc($image, $cx, $cy, $size, $siz2, $end_used, $beg_used, $col_used, IMG_ARC_PIE);
  if ($beg_used > $end_chek) imagefilledarc($image, $cx, $cy, $size, $siz2, $end_chek, $beg_used, $col_chek, IMG_ARC_PIE);


// flush image
header("Content-type: image/png");
header("Expires: 0");
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Description: CLiC nodes current state");
imagepng($image);
imagedestroy($image);
?> 
