| 1 | #!/usr/bin/env perl | ||||||||||||||||||||||||||||
| 2 | #ABSTRACT: List the users with active jobs, and the number of jobs in the cluster | ||||||||||||||||||||||||||||
| 3 | #PODNAME: whojobs | ||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | 1 | ▶ | use v5.12; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 1 | ▶ | use Getopt::Long; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 1 | ▶ | use FindBin qw($RealBin); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 1 | ▶ | use Data::Dumper; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 1 | ▶ | use Term::ANSIColor qw(:constants); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 1 | ▶ | use Cwd; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 1 | $Data::Dumper::Sortkeys = 1; | |||||||||||||||||||||||||||
| 13 | 1 | ▶ | if (-e "$RealBin/../dist.ini") { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 14 | 1 | ▶ | say STDERR "[dev mode] Using local lib" if ($ENV{"DEBUG"}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 15 | 1 | ▶ | use lib "$RealBin/../lib"; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | } | ||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | 1 | my ($opt_color, $opt_no_color, $opt_min_jobs, $opt_verbose, $opt_scramble); | |||||||||||||||||||||||||||
| 20 | GetOptions( | ||||||||||||||||||||||||||||
| 21 | 'n|no-color' => \$opt_no_color, | ||||||||||||||||||||||||||||
| 22 | 'm|min-jobs=i' => \$opt_min_jobs, | ||||||||||||||||||||||||||||
| 23 | 'v|verbose' => \$opt_verbose, | ||||||||||||||||||||||||||||
| 24 | 's|scramble' => \$opt_scramble, | ||||||||||||||||||||||||||||
| 25 | 1 | ▶ | 'version' => sub { say "whojobs v", $NBI::Slurm::VERSION; exit }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 26 | 0 | ▶ | 'h|help' => sub { usage() }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 27 | 1 | ); | |||||||||||||||||||||||||||
| 28 | 0 | my $opt_pattern = shift; | |||||||||||||||||||||||||||
| 29 | 1 | ▶ | use NBI::Slurm; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | 0 | our $user_max = 14; | |||||||||||||||||||||||||||
| 32 | 0 | my $unix_users = unix_users(); | |||||||||||||||||||||||||||
| 33 | 0 | my $slurm_users = slurm_users(); | |||||||||||||||||||||||||||
| 34 | 0 | my $all_users = {}; | |||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | 0 | for my $user (@{$unix_users}) { | |||||||||||||||||||||||||||
| 37 | 0 | $slurm_users->{$user} = 0; | |||||||||||||||||||||||||||
| 38 | } | ||||||||||||||||||||||||||||
| 39 | 0 | my $opt_user = $ENV{USER}; | |||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | 0 | my $c = 0; | |||||||||||||||||||||||||||
| 42 | 0 | my $p = 0; | |||||||||||||||||||||||||||
| 43 | 0 | for my $user (sort {$$slurm_users{$a} <=> $$slurm_users{$b}}keys %{$slurm_users}) { | |||||||||||||||||||||||||||
| 44 | 0 | $c++; | |||||||||||||||||||||||||||
| 45 | 0 | ▶ | my $star = ($user eq $opt_user) ? '*' : ''; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 46 | 0 | ▶ | if ($opt_pattern && $user !~ /$opt_pattern/i) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$opt_pattern and not $user =~ /$opt_pattern/iu
MC/DC0%0/2
$opt_pattern and not $user =~ /$opt_pattern/iu
$opt_pattern
not $user =~ /$opt_pattern/iu
Truth table0%0/3
A and not(B)
A= $opt_patternB= not $user =~ /$opt_pattern/iu
| |||||||||||||||||||||||||||||
| 47 | 0 | next; | |||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||
| 49 | 0 | ▶ | if ($opt_min_jobs && $slurm_users->{$user} < $opt_min_jobs) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$opt_min_jobs and $slurm_users->{$user} < $opt_min_jobs
MC/DC0%0/2
$opt_min_jobs and $slurm_users->{$user} < $opt_min_jobs
$opt_min_jobs
$slurm_users->{$user} < $opt_min_jobs
Truth table0%0/3
A and B
A= $opt_min_jobsB= $slurm_users->{$user} < $opt_min_jobs
| |||||||||||||||||||||||||||||
| 50 | 0 | next; | |||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||
| 52 | # Dedicate 40 chars to user, 10 to jobs, 10 to star | ||||||||||||||||||||||||||||
| 53 | 0 | ▶ | if ($c % 2 == 0) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 54 | 0 | ▶ | if ( !$opt_no_color) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 55 | 0 | print GREEN BOLD; | |||||||||||||||||||||||||||
| 56 | } else { | ||||||||||||||||||||||||||||
| 57 | 0 | print RESET; | |||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||
| 59 | } else { | ||||||||||||||||||||||||||||
| 60 | 0 | print RESET; | |||||||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||||||||
| 62 | 0 | $p++; | |||||||||||||||||||||||||||
| 63 | 0 | ▶ | my $user_string = $opt_scramble ? scramble_string($user, [$opt_user]) : "$user"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | 0 | printf "%4s %-${user_max}s %5s %2s\n", $c, $user_string, $slurm_users->{$user}, $star; | |||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | # END | ||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | 0 | print STDERR RESET "\n"; | |||||||||||||||||||||||||||
| 71 | 0 | ▶ | if ($opt_verbose) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 72 | 0 | ▶ | unless ($opt_no_color) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 73 | 0 | print STDERR CYAN; | |||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||
| 75 | 0 | print STDERR "Total users with jobs: ", scalar(keys %{$slurm_users}), "\n"; | |||||||||||||||||||||||||||
| 76 | 0 | print STDERR "Total users logged: ", scalar(@{$unix_users}), "\n"; | |||||||||||||||||||||||||||
| 77 | 0 | print STDERR "Printed users: $p\n", RESET; | |||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | 1 | ▶ | END { | ||||||||||||||||||||||||||
|
Subroutine
END: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 81 | 1 | print RESET ""; | |||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||
| 83 | 0 | ▶ | sub unix_users { | ||||||||||||||||||||||||||
|
Subroutine
unix_users: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 84 | 0 | my $cmd = "who | cut -d' ' -f1"; | |||||||||||||||||||||||||||
| 85 | 0 | my @users = `$cmd`; | |||||||||||||||||||||||||||
| 86 | 0 | chomp @users; | |||||||||||||||||||||||||||
| 87 | # Remove 'USER' | ||||||||||||||||||||||||||||
| 88 | 0 | shift @users; | |||||||||||||||||||||||||||
| 89 | # Sort uniq | ||||||||||||||||||||||||||||
| 90 | 0 | @users = sort { $a cmp $b } @users; | |||||||||||||||||||||||||||
| 91 | 0 | my %seen = (); | |||||||||||||||||||||||||||
| 92 | 0 | @users = grep { ! $seen{ $_ }++ } @users; | |||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | 0 | return \@users; | |||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||
| 96 | 0 | ▶ | sub slurm_users { | ||||||||||||||||||||||||||
|
Subroutine
slurm_users: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 97 | 0 | ▶ | if (not NBI::Slurm::has_squeue()) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 98 | 0 | say STDERR RED, "[WARNING]", RESET, " `squeue` not found, are you in the cluster?"; | |||||||||||||||||||||||||||
| 99 | 0 | say STDERR "Will just print some logged users..."; | |||||||||||||||||||||||||||
| 100 | 0 | return {}; | |||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||
| 102 | 0 | my $cmd = "squeue --format='%u'"; | |||||||||||||||||||||||||||
| 103 | 0 | my @users = `$cmd`; | |||||||||||||||||||||||||||
| 104 | 0 | chomp @users; | |||||||||||||||||||||||||||
| 105 | # Remove 'USER' | ||||||||||||||||||||||||||||
| 106 | 0 | shift @users; | |||||||||||||||||||||||||||
| 107 | # Sort uniq | ||||||||||||||||||||||||||||
| 108 | 0 | @users = sort { $a cmp $b } @users; | |||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | # Return a hash user -> times seen | ||||||||||||||||||||||||||||
| 112 | 0 | my %seen = (); | |||||||||||||||||||||||||||
| 113 | 0 | @users = grep { ! $seen{ $_ }++ } @users; | |||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | 0 | return \%seen; | |||||||||||||||||||||||||||
| 116 | } | ||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | 0 | ▶ | sub scramble_string { | ||||||||||||||||||||||||||
|
Subroutine
scramble_string: not called CC=5 SCAR=34.0
| |||||||||||||||||||||||||||||
| 119 | # change odd chars to random chars | ||||||||||||||||||||||||||||
| 120 | 0 | my $string = shift; | |||||||||||||||||||||||||||
| 121 | 0 | my $whitelist = shift; | |||||||||||||||||||||||||||
| 122 | 0 | ▶ | return "" unless ($string); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 123 | 0 | my @chars = split(//, $string); | |||||||||||||||||||||||||||
| 124 | 0 | my $scrambled = ''; | |||||||||||||||||||||||||||
| 125 | 0 | ▶ | return $string if (grep { $string eq $_ } @{$whitelist}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 126 | 0 | for my $char (@chars) { | |||||||||||||||||||||||||||
| 127 | 0 | ▶ | if (rand() > 0.5) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 128 | 0 | $scrambled .= $char; | |||||||||||||||||||||||||||
| 129 | } else { | ||||||||||||||||||||||||||||
| 130 | 0 | $scrambled .= chr(int(rand(26)) + 97); | |||||||||||||||||||||||||||
| 131 | } | ||||||||||||||||||||||||||||
| 132 | } | ||||||||||||||||||||||||||||
| 133 | 0 | return $scrambled; | |||||||||||||||||||||||||||
| 134 | } | ||||||||||||||||||||||||||||
| 135 | 0 | ▶ | sub get_terminal_width { | ||||||||||||||||||||||||||
|
Subroutine
get_terminal_width: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 136 | 0 | my $terminal_width = `tput cols`; | |||||||||||||||||||||||||||
| 137 | 0 | chomp($terminal_width); | |||||||||||||||||||||||||||
| 138 | 0 | ▶ | return $terminal_width > 20 ? $terminal_width : 80; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 139 | } | ||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | 0 | ▶ | sub usage { | ||||||||||||||||||||||||||
|
Subroutine
usage: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 142 | 0 | print STDERR <<END; | |||||||||||||||||||||||||||
| 143 | ------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 144 | whojobs - List the users with jobs, and the number of jobs in the cluster | ||||||||||||||||||||||||||||
| 145 | ------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 146 | Usage: whojobs [options] [pattern] | ||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | Options: | ||||||||||||||||||||||||||||
| 149 | -n, --no-color Do not use colors | ||||||||||||||||||||||||||||
| 150 | -m, --min-jobs INT Only show users with at least one job | ||||||||||||||||||||||||||||
| 151 | -v, --verbose Verbose output | ||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | END | ||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | 0 | exit 0; | |||||||||||||||||||||||||||
| 156 | } | ||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | __END__ | ||||||||||||||||||||||||||||