#!/bin/sh

# echo Location: "URL zum File"
# echo Content-type: text/plain
echo Content-type: text/html
echo
echo '<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">'
echo '<html>'
echo '<HEAD>'
echo '  <title>CLIC current state</title>'
echo '  <META HTTP-EQUIV="expires" CONTENT="0">'
echo '  <META NAME="author" CONTENT="Matthias Pester">'
echo '  <LINK rel="SHORTCUT ICON" href="CLiC1.ico">' 
echo '</head>'
echo '<body bgcolor="#ffffff">'
echo '<p>'
echo '<a href="http://www.tu-chemnitz.de/urz/anwendungen/CLIC/"><img SRC="../CLIC-Einweihung/click25anim.gif" ALT="CLIC" BORDER=0  align=RIGHT></a>'
echo '<h1>Current use of CLIC</h1>'
date
if ( test `date "+%Y%j"` -lt 2001272 )
then 
echo "<BR><BLINK><font color=#ff0000>NOW BENCHMARKING</font></BLINK>"
echo "<BR>best result up to now: 221.6 Gflop/s on 529 nodes"
fi

pbsn="/usr/local/bin/pbsnodes -s clic0a1";
grep="/bin/grep"

echo '</p><br clear=all><hr noshade>'
case $REMOTE_ADDR in 
  134.109.*) chek=`$pbsn -a | $grep job-excl | $grep offl | /usr/bin/wc -l`;;
  default) chek=0
esac  
used=`$pbsn -a | $grep job-excl | wc -l`
free=`$pbsn -a | $grep free | $grep -v offl | wc -l`
offl=`$pbsn -a | $grep offl | $grep -v job-excl | wc -l`
down=`/usr/local/bin/pbsnodes -a | $grep down | $grep -v offl |wc -l`
# remove leading blanks
chek=`expr $chek + 0`
used=`expr $used + 0`
free=`expr $free + 0`
offl=`expr $offl + 0`
down=`expr $down + 0`
# serv=`/usr/local/bin/qstat -B -f clic0a1 | /bin/awk '/Server: /{print $2}'`
#
if ( `expr $used + $free + $offl + $down` == 0 ) 
then
echo "Sorry, server is momentary not reachable."
#
else
#
#stamp=`date +%y%m%d%H%M%S`
stamp=`date +%s`
# set terminal gif transparent interlace small size 200,220 x000000 x000000 x000000 x000000 x0000ff x0000ff x0000ff x0000ff x00aa00 x00aa00 x00aa00 x00aa00 xff0000 xff0000 xff0000 xff00ff xff00ff xff00ff
# if which gnuplot 
if false    
then
gnuplot <<! 2>&1  >cnt/gnuplot.err
a=$used
b=$free
c=$offl
d=$down
g=a+b+c+d
u=0.95
eps=pi/720
wa=2*pi*a/g
wb=2*pi*b/g
wc=2*pi*c/g
wd=2*pi*d/g
set parametric
set noxtics
set noytics
set noborder
set terminal jpeg small size 200,220 xffffff x000000 x000000 x000000 x0000ff x0000ff x0000ff x0000ff x00aa00 x00aa00 x00aa00 x00aa00 xff0000 xff0000 xff0000 xff00ff xff00ff xff00ff
set output "plot.jpg" 
set title "`date`"
plot [t=0:1] [-1.1:1.1] [-1.1:1.1] \
 t*cos(eps),t*sin(eps) t "", \
 cos(t*wa),sin(t*wa) t "used" with l lw 2, \
 u*cos(t*wa),u*sin(t*wa) t "" with l lw 5, \
 t*cos(wa),t*sin(wa) t "", \
 t*cos(wa+eps),t*sin(wa+eps) t "", \
 cos(wa+t*wb),sin(wa+t*wb) t "free" with l lw 2, \
 u*cos(wa+t*wb),u*sin(wa+t*wb) t "" with l lw 5, \
 t*cos(wa+wb-eps),t*sin(wa+wb-eps) t "", \
 t*cos(wa+wb),t*sin(wa+wb) t "", \
 cos(wa+wb+t*wc),sin(wa+wb+t*wc) t "offl" with l lw 2, \
 u*cos(wa+wb+t*wc),u*sin(wa+wb+t*wc) t "" with l lw 5, \
 t*cos(wa+wb+wc-eps),t*sin(wa+wb+wc-eps) t "", \
 cos(wa+wb+wc+t*wd),sin(wa+wb+wc+t*wd) t "down" with l lw 2, \
 u*cos(wa+wb+wc+t*wd),u*sin(wa+wb+wc+t*wd) t "" with l lw 5, \
 t*cos(-eps),t*sin(-eps) t ""
!

echo "<a href=http://www.tu-chemnitz.de/urz/clic/stat/piecake.php3><img SRC=plot.jpg?stamp=$stamp ALT='clicstat' BORDER=0 width=200 height=220 align=RIGHT></a>"
else
echo "<img SRC=\"makepie.php3?used=$used&free=$free&offl=$offl&down=$down&chek=$chek&server=$serv&tstamp=$stamp\" ALT='clicstat' BORDER=0 width=250 height=190 align=RIGHT>"
fi


echo '<pre>'
# /usr/local/bin/qstat -B clic0a1 2>&1 
echo 
echo '<font color="#0000ff">'
  if [ $used -gt 1 ]; then s='s'; else s=' '; fi
  if [ $used -eq 0 ]; then used="     no"; fi
echo -n "$used  node$s  in use"
if [ $chek -gt 0 ] 
then echo ',<font color="#4400ff" size="-1">' "$chek of them intended for automatic system check" '</font>'
else echo " "
fi
echo -n '</font><font color="#00aa00">'
  if [ $free -gt 1 ]; then s='s'; else s=' '; fi
  if [ $free -eq 0 ]; then free="     no"; fi
echo "$free  node$s  free"
echo -n '</font><font color="#ff0000">'
if [ $offl -gt 0 ]
 then 
 if [ $offl -gt 1 ]; then s='s'; else s=' '; fi
 echo "$offl  node$s  offline (maintenance)"
fi
echo -n '</font><font color="#7f007f">'
if [ $down -gt 0 ]
then
  if [ $down -gt 1 ]; then s='s'; else s=' '; fi
  echo "$down  node$s  down (unreachable)"
fi
echo '</font>'
echo '</pre><br clear=all>'
echo '<hr noshade>'
case $REMOTE_ADDR in 
#  134.109.40.212) echo "<a href=http://www.tu-chemnitz.de/urz/anwendungen/CLIC/curr_usage.php3>Link to urz/anwendungen</a><br>" ;;
  134.109.*) echo "<B> Hello, $REMOTE_HOST, CLiC was switched off in 2007.</B>"
#  echo "<B>Hello,  $REMOTE_HOST, here is a list of running or queued jobs: </b>"
#             echo '<pre><font color="#0000ff">'
#             /usr/local/bin/qstat @clic0a1 -R
#             echo '</font></pre>'
             echo '<hr> <a href="http://www.tu-chemnitz.de/urz/clic/curr_usage_intern.html"> more technical details </a>';;
  default) ;;
esac
#
fi
echo '</body></html>'
ed cnt/clicstat.cnt <<! >/dev/null
1s/^.*/$REMOTE_HOST `date` `echo -n Server\:\ ; hostname`/
1m\$
w
q
!

