#! /usr/bin/env perl
##**************************************************************
##
## Copyright (C) 1990-2007, Condor Team, Computer Sciences Department,
## University of Wisconsin-Madison, WI.
## 
## Licensed under the Apache License, Version 2.0 (the "License"); you
## may not use this file except in compliance with the License.  You may
## obtain a copy of the License at
## 
##    http://www.apache.org/licenses/LICENSE-2.0
## 
## Unless required by applicable law or agreed to in writing, software
## distributed under the License is distributed on an "AS IS" BASIS,
## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
## See the License for the specific language governing permissions and
## limitations under the License.
##
##**************************************************************

################################################################################
# Publish PBS queue depth to Hawkeye
#

# Perl standard library modules
use warnings;
use strict;

# Update the module include path: same directory as this script.
BEGIN
{
    my $Dir = $0;
    if ( $Dir =~ /(.*)\/.*/ )
    {
	push @INC, "$1";
    }
}
use HawkeyePublish;
use HawkeyeLib;

# Prototypes
sub Init( );
sub RunIt( );

# Setup the hawkeye stuff
my $Hawkeye;
my $Hash;
my @Params = "";
my $CmdLine = join( " ", $0, @ARGV );
my $Prog; ($Prog = $0) =~ s|.*/||;

# Parameters
my $Qstat = "/usr/pbs/bin/qstat";
my $Qname = "";

# Constants
my $Usage = <<EOF;
$Prog:  publish $Qstat output to Hawkeye

usage: $Prog -fQ <queueName>
EOF

# Do it
Init();
RunIt();
exit(0);    # Nominal exit point.


    ### subroutines ###

sub Init()
{

    HawkeyeLib::DoConfig( );        # configure

    $Hawkeye = HawkeyePublish->new; # create publish object
    $Hawkeye->Quiet( 1 );           # turn off debug babble
    $Hawkeye->AutoIndexSet( 1 );    # Turn on auto indexing

    $Qstat = HawkeyeLib::ReadConfig( "_qstat", "/usr/pbs/bin/qstat" );
    $Qname = HawkeyeLib::ReadConfig( "_qname", "" );
    die "No queue name" if ( $Qname eq "" );

}

# Do the real work here...
sub RunIt()
{
    # Start things off
    $Hash = HawkeyeHash->new( \$Hawkeye, "" );

    # Read input from qstat command
    my $qstat = "$Qstat -fQ $Qname";
    open (QSTAT, "$qstat|") or die "open $qstat for read: $!";

    # Read Queue status.
    while (<QSTAT>) {

        # Canonicalize input.
        s/^\s*//;       # Trim leading whitespace.
        s/\s*$//;       # Trim trailing whitespace.
        next if /^$/;   # Skip blank lines

        # Parse queue identity
        my ($queue) = /^Queue\s*:\s*(.*)/;
        if (defined $queue) {
            $Hash->Add( "Queue", HawkeyePublish::TypeAuto, $queue );
            next;
        }

        # Parse state count
        my ($state_count) = /^state_count\s*=\s*(.*)/;
        if (defined $state_count) {
            my @state_count = split / /, $state_count;
            foreach my $s (@state_count) {
                my ($attr,$value) = split /:/, $s;
                $Hash->Add( "state_count_" . $attr, HawkeyePublish::TypeAuto,
                    $value );
            }
            next;
        }

        # General case syntax: attribute = value
        my ($attr,$value) = /^(\S+)\s*=\s*(.*)/;
        if (defined $value) {
            $Hash->Add( $attr, HawkeyePublish::TypeAuto, $value );
            next;
        }
    }

    close( QSTAT );

    # Ok, summary is done...
    $Hash->Store( );
    $Hawkeye->Publish( );

    #print STDERR "Test module done\n";
}
