# -*- Perl -*-

#
# A version of llrss that uses the XML::RSS module
# which is available from CPAN (of course)
#

use strict;
use XML::RSS;
use LoadLeveler;
use Sys::Hostname;

@StepState=( "I", "P", "ST", "R",
    "CP", "XP", "RP",
    "VP", "C", "X", "RM",
    "V", "CA", "NR", "TX",
    "?", "SX", "H", "D",
    "NQ", "E", "EP",
    "MP");

my $seconds = 60;

# Try and work out our domain name

$host=hostname();
my($fqdn)=gethostbyname($host);
$fqdn=~/$host\.(.*)/;
$domain= $1;

# Hotlink for more detailed LoadLeveler Information page off webserver

my $link = "http://$host.$domain/LoadL.html";

while(TRUE) {

   $jobs_idle = 0;
   $jobs_run  = 0;
   $jobs_held = 0;
   $jobs_preempt = 0;
   $jobs_not_queued = 0;

   #
   # Query Job Status information
   #

   $query = ll_query(JOBS);
   $return=ll_set_request($query,QUERY_ALL,undef,ALL_DATA);

   if ($return != 0 )
   {
       print STDERR "ll_set_request failed Return = $return\n";
   }

   #
   # Query the scheduler for information
   #

   $job=ll_get_objs($query,LL_CM,NULL,$number,$err);
   while($job)
   {
      #
      # Get the status for the first step of that Job
      #

      $step=ll_get_data($job,LL_JobGetFirstStep);
      while($step)
      {
         $holdtype=ll_get_data($step,LL_StepHoldType);
         $state=ll_get_data($step,LL_StepState);
         $st=$StepState[$state];
         $jobs_run++     if ($st == "R");
         $jobs_idle++    if ($st == "I");
         $jobs_held++    if ($holdtype > 0 );
         $jobs_preempt++ if ($st == "E");
         $jobs_nqueued++ if ($st == "NQ");
         $step=ll_get_data($job,LL_JobGetNextStep);
      }
      $job=ll_next_obj($query);
   }

   #
   # Free up space allocated by LoadLeveler
   #

   ll_free_objs($job);
   ll_deallocate($query);

   $title_string = "Jobs: R=$jobs_run, I=$jobs_idle, H=$jobs_held, NQ=$jobs_not_queued E=$jobs_preempt";

   $nodes_busy = 0;
   $nodes_idle = 0;
   $nodes_running = 0;
   $nodes_down = 0;

   #
   # Query Job Status information
   #

   $query = ll_query(MACHINES);
   $return=ll_set_request($query,QUERY_ALL,undef,ALL_DATA);

   if ($return != 0 )
   {
       print STDERR "ll_set_request failed Return = $return\n";
   }

   #
   # Query the scheduler for information
   # $number will contain the number of objects returned
   #

   $machine=ll_get_objs($query,LL_CM,NULL,$number,$err);
   while($machine)
   {
      #
      # Get the startd status for each machine
      #

      $sts=ll_get_data($machine,LL_MachineStartdState);

      $nodes_busy++    if ($sts =~ /^Busy/);
      $nodes_idle++    if ($sts =~ /^Idle/);
      $nodes_running++ if ($sts =~ /^Running/);
      $nodes_down++    if ($sts =~ /^Down/);

      $machine=ll_next_obj($query);
   }

   #
   # Free up space allocated by LoadLeveler
   #

   ll_free_objs($machine);
   ll_deallocate($query);

   # Generate the RSS feed file

   $rss_file = "/web/htdocs/LoadL.rdf";
   $title_string .= ". Nodes: B=$nodes_busy, R=$nodes_running, I=$nodes_idle, D=$nodes_down";

   $rss = XML::RSS->new(version=>'0.99');
   $rss->channel(
                title        => "LoadLeveler Status",
                link         => "http://www.somecluster/",
                description  => "LoadLeveler Status.");

   $rss->add_item( title => $title_string, link => $link);
   $rss->save($rss_file);

   # sleep for a while

   sleep($seconds);
}
