#!/usr/bin/perl
#
# This script is a component of Warewulf,
# http://www.runlevelzero.net/greg/warewulf
#
#########################################################################
#
# Copyright (C) 2001  Greg M. Kurtzer
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# The GNU GPL Document can be found at:
# http://www.gnu.org/copyleft/gpl.html
#
#########################################################################
#
# Written and maintained by:
#       Greg Kurtzer, <gmkurtzer@lbl.gov>

use lib "/usr/lib/warewulf/", "/usr/lib64/warewulf/";
use Warewulf::Config;
use Warewulf::Status;
use Warewulf::Util;
use Getopt::Long;
use IO::Socket;
use strict;

Getopt::Long::Configure ("bundling");

my $info = "USAGE: $0 [OPTIONS]
  About:
   wwps is similiar to a distributed ps command to get a list of the current
   jobs running on the nodes. Be default wwjobs will only show processes that
   have a CPU utilization above 5%.
  Options:
   -u, --user       Match string to username
   -m, --me         Show me only my processes
   -a, --all        Don't filter low CPU utilizing processes
   -h, --help       Display this very short help screen

  This tool is part of the Warewulf cluster distribution
       http://warewulf-cluster.org/
";

my (
   $username,
   $showall,
   $help,
   $showme,
   %passwd,
   @f,
   $out,
   $nodename,
   $group,
   %nodestatus,
   $ignore,
   %filter,
   $nodename,
);

GetOptions(
   'user|u=s'        => \$username,
   'all|a'        => \$showall,
   'me|m'        => \$showme,
   'group|g' => \$group,
   'ignore|i'   => \$ignore,
   'help|h'        => \$help,
);

if ( $help ) {
        print $info;
        exit;
}

my %nodestatus = &node_status();
my %nodes = &node_config();
my %master = &master_config();
%filter = &users_filter();



chomp ( my $localhost = `hostname -s`);
chomp ( my $whoami = `whoami`);

my $command = 'ps -ax --format "uid %cpu %mem cputime command" 2>/dev/null';

open(PASSWD, "/etc/passwd");
foreach (<PASSWD>) {
        my @f = split(/:/, $_);
        $passwd{$f[2]} = $f[0];
}

format DISPLAY = 
 @<<<<<<< @<<<<<<<< @>>>> @>>>> @>>>>>>>> @<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
$nodename, $passwd{$f[0]}, $f[1], $f[2], $f[3], $f[4]
.
$~ = 'DISPLAY';

print " NODE     USER       \%CPU  \%MEM    TIME   COMMAND\n";

#open(OUTPUT,"$command |");
#foreach $out ( <OUTPUT> ) {
#        $_ = 'MASTER';
#        chomp $out;
#        $out =~ s/^\s+//g;
#        @f = split(/\s+/, $out);
#        next if ( $f[0] =~ /\D/ );
#        next unless ( $f[0] => '500' );
#        if ( defined $username ) {
#                next unless ( $passwd{$f[0]} =~ /$username/ );
#        }
#        next if ( $passwd{$f[0]} ne $whoami && $showme );
#        next if ( $f[1] < '5' && ! $showall);
#        next if ( $f[4] eq 'ps' );
#        write;
#}
#close OUTPUT;

foreach ( sort keys %nodestatus ) {
   if ( ! $filter{$_} and defined %filter ) {
      next;
   }
   if ( $nodestatus{$_}{NODESTATUS} ne 'READY' ) {
      next;
   }
   if ( $_ eq 'master' or $_ eq $localhost ) {
      next;
   }
   $nodename = $nodestatus{$_}{NODENAME};
   open(OUTPUT,"$master{'network'}{'rsh command'} $nodes{$nodename}{'admin ipaddr'} '$command' |");
    foreach $out ( <OUTPUT> ) {
      chomp $out;
      $out =~ s/^\s+//g;
      @f = split(/\s+/, $out);
      next if ( $f[0] =~ /\D/ );
      next unless ( $f[0] => '500' );
      if ( defined $username ) {
         next unless ( $passwd{$f[0]} =~ /$username/ );
      }
      next if ( $passwd{$f[0]} ne $whoami && $showme );
      next if ( $f[1] < '5' && ! $showall);
      next if ( $f[4] eq 'ps' );
      write;
   }
   close OUTPUT;
}



