File Coverage

bin/printslurm
Criterion Covered Total %
statement 30 128 23.4
branch 2 46 4.3
condition 0 5 0.0
subroutine 6 10 60.0
pod n/a
total 38 189 20.1


line stmt bran cond sub pod time code
1             #!/usr/bin/env perl
2             #PODNAME:printslurm
3             #ABSTRACT:Utility to print info from Slurm
4 1     1   5541 use v5.12;
  1         5  
5 1     1   6 use warnings;
  1         1  
  1         60  
6 1     1   809 use Getopt::Long;
  1         19479  
  1         7  
7 1     1   908 use Term::ANSIColor;
  1         14464  
  1         108  
8 1     1   521 use NBI::Slurm;
  1         5  
  1         94871  
9             # Define command line options
10 1         70160 my $help = 0;
11 1         2 my $node_info = 0;
12              
13             GetOptions(
14             "help|h" => \$help,
15             "node|n" => \$node_info,
16 0     0   0 "version" => sub { say "printslurm v", $NBI::Slurm::VERSION; exit },
  0         0  
17 1 50       11 ) or show_help();
18              
19             # Show help if requested
20 1 50       600 show_help() if $help;
21              
22             sub show_help {
23 1     1   7 print color('bold white');
24 1         38 print "Usage: $0 [options]\n\n";
25 1         3 print color('reset');
26            
27 1         15 print "Options:\n";
28 1         1 print " -h, --help Show this help message and exit\n";
29 1         2 print " -n, --node Display node-specific job information (when inside a Slurm job)\n\n";
30            
31 1         1 print "Description:\n";
32 1         1 print " This script displays information about the Slurm environment,\n";
33 1         2 print " including queues (partitions) and Slurm environment variables.\n";
34 1         2 print " When run with the --node option, it displays detailed information\n";
35 1         1 print " about the current job allocation.\n\n";
36            
37 1         70 exit(0);
38             }
39              
40             # Function to run shell commands
41             sub run_command {
42 0     0     my $cmd = shift;
43 0           my @output = `$cmd 2>/dev/null`;
44 0           chomp(@output);
45 0           return @output;
46             }
47              
48             # Print a section header
49             sub print_header {
50 0     0     my $text = shift;
51 0           print "\n", color('bold cyan'), "=== $text ===", color('reset'), "\n";
52             }
53              
54             # Print a key-value pair
55             sub print_key_value {
56 0     0     my ($key, $value, $color_name) = @_;
57 0   0       $color_name //= 'green'; # Default color
58            
59 0           print color('yellow'), "$key: ", color($color_name), "$value", color('reset'), "\n";
60             }
61              
62             # Check if sinfo command exists (are we on a Slurm system?)
63 0         0 my @check_slurm = run_command("command -v sinfo");
64 0 0       0 if (!@check_slurm) {
65 0         0 print color('bold red'), "Error: Slurm commands not found. Are you on a Slurm cluster?", color('reset'), "\n";
66 0         0 exit(1);
67             }
68              
69             # Display node-specific information if requested
70 0 0       0 if ($node_info) {
71 0         0 print_header("Current Job Information");
72            
73             # Check if we're in a Slurm job
74 0 0       0 if (!defined $ENV{SLURM_JOB_ID}) {
75 0         0 print color('bold red'), "Error: Not running inside a Slurm job allocation.", color('reset'), "\n";
76 0         0 print "Run this script with the --node option only within a Slurm job.\n";
77 0         0 exit(1);
78             }
79            
80             # Display job information
81 0         0 print_key_value("Job ID", $ENV{SLURM_JOB_ID});
82 0 0       0 print_key_value("Job Name", $ENV{SLURM_JOB_NAME}) if defined $ENV{SLURM_JOB_NAME};
83 0 0       0 print_key_value("Queue/Partition", $ENV{SLURM_JOB_PARTITION}) if defined $ENV{SLURM_JOB_PARTITION};
84            
85             # Get more detailed job info using scontrol
86 0         0 my @job_info = run_command("scontrol show job $ENV{SLURM_JOB_ID}");
87            
88             # Extract and display specific job details
89 0         0 my ($time_limit) = grep { /TimeLimit=/ } @job_info;
  0         0  
90 0         0 my ($mem_per_node) = grep { /MinMemoryNode=/ } @job_info;
  0         0  
91 0         0 my ($num_nodes) = grep { /NumNodes=/ } @job_info;
  0         0  
92 0         0 my ($num_cpus) = grep { /NumCPUs=/ } @job_info;
  0         0  
93            
94 0 0       0 if ($time_limit) {
95 0         0 $time_limit =~ s/.*TimeLimit=(\S+).*/$1/;
96 0         0 print_key_value("Time Limit", $time_limit);
97             }
98            
99 0 0       0 if ($mem_per_node) {
100 0         0 $mem_per_node =~ s/.*MinMemoryNode=(\S+).*/$1/;
101 0         0 print_key_value("Memory Per Node", $mem_per_node);
102             }
103            
104 0 0       0 if ($num_nodes) {
105 0         0 $num_nodes =~ s/.*NumNodes=(\S+).*/$1/;
106 0         0 print_key_value("Number of Nodes", $num_nodes);
107             }
108            
109 0 0       0 if ($num_cpus) {
110 0         0 $num_cpus =~ s/.*NumCPUs=(\S+).*/$1/;
111 0         0 print_key_value("Number of CPUs", $num_cpus);
112             }
113            
114             # Node list
115 0 0       0 if (defined $ENV{SLURM_JOB_NODELIST}) {
116 0         0 print_key_value("Node List", $ENV{SLURM_JOB_NODELIST});
117            
118             # Expand the node list if it's compressed
119 0 0       0 if ($ENV{SLURM_JOB_NODELIST} =~ /\[/) {
120 0         0 my @expanded_nodes = run_command("scontrol show hostnames $ENV{SLURM_JOB_NODELIST}");
121 0 0       0 print " ", color('magenta'), join(", ", @expanded_nodes), color('reset'), "\n" if @expanded_nodes;
122             }
123             }
124            
125             # Print current working directory
126 0   0     0 print_key_value("Working Directory", $ENV{PWD} || `pwd`);
127             }
128             # Default: display general Slurm information
129             else {
130             # Display Slurm version
131 0         0 print_header("Slurm Version");
132 0         0 my @version = run_command("sinfo --version");
133 0 0       0 print color('green'), $version[0], color('reset'), "\n" if @version;
134            
135             # Display available partitions (queues)
136 0         0 print_header("Available Partitions (Queues)");
137 0         0 my @partitions = run_command("sinfo --format=\"%20P %10l %5D %8m %11T %N\"");
138            
139 0 0       0 if (@partitions) {
140             # Print header with bold
141 0         0 print color('bold'), $partitions[0], color('reset'), "\n";
142            
143             # Print remaining lines
144 0         0 foreach my $i (1..$#partitions) {
145 0         0 my @parts = split(/\s+/, $partitions[$i]);
146             # Color the partition name
147 0         0 print color('yellow'), sprintf("%-20s", $parts[0]), color('reset');
148             # Print the rest
149 0         0 print join(" ", @parts[1..$#parts]), "\n";
150             }
151             }
152            
153             # Display user's current jobs
154 0         0 print_header("Your Current Jobs");
155 0         0 my @jobs = run_command("squeue --user=\$USER --format=\"%12i %20j %10P %10T %10M %10l %6D %R\"");
156            
157 0 0       0 if (@jobs > 1) { # More than just the header
158             # Print header with bold
159 0         0 print color('bold'), $jobs[0], color('reset'), "\n";
160            
161             # Print jobs with colors based on state
162 0         0 foreach my $i (1..$#jobs) {
163 0         0 my @parts = split(/\s+/, $jobs[$i], 8); # Limit to 8 parts to keep the reason as one field
164            
165             # Color job ID
166 0         0 print color('cyan'), sprintf("%-12s", $parts[0]), color('reset');
167            
168             # Color job name
169 0         0 print color('green'), sprintf("%-20s", $parts[1]), color('reset');
170            
171             # Color partition
172 0         0 print color('yellow'), sprintf("%-10s", $parts[2]), color('reset');
173            
174             # Color state based on value
175 0         0 my $state_color = 'white';
176 0 0       0 if ($parts[3] eq 'RUNNING') {
    0          
    0          
177 0         0 $state_color = 'green';
178             } elsif ($parts[3] eq 'PENDING') {
179 0         0 $state_color = 'yellow';
180             } elsif ($parts[3] =~ /^(FAILED|CANCELLED|TIMEOUT)$/) {
181 0         0 $state_color = 'red';
182             }
183 0         0 print color($state_color), sprintf("%-10s", $parts[3]), color('reset');
184            
185             # Print the rest
186 0         0 print join(" ", @parts[4..$#parts]), "\n";
187             }
188             } else {
189 0         0 print color('yellow'), "No active jobs found for your user.\n", color('reset');
190             }
191            
192             # Display Slurm environment variables
193 0         0 print_header("Slurm Environment Variables");
194 0         0 my $found_vars = 0;
195            
196 0         0 foreach my $key (sort keys %ENV) {
197 0 0       0 if ($key =~ /^SLURM_/) {
198 0         0 print_key_value($key, $ENV{$key});
199 0         0 $found_vars = 1;
200             }
201             }
202            
203 0 0       0 if (!$found_vars) {
204 0         0 print color('yellow'), "No SLURM_* environment variables found.\n", color('reset');
205 0         0 print "This is normal when not inside a Slurm job allocation.\n";
206             }
207            
208             # Display system-wide Slurm configuration
209 0         0 print_header("Slurm Configuration");
210 0         0 my @config = run_command("scontrol show config | grep -v '^ '");
211            
212 0 0       0 if (@config) {
213             # Print only key configuration items
214 0         0 my @important_configs = grep { /^(ClusterName|DefMemPerNode|MaxJobCount|SchedulerType|SelectType)/ } @config;
  0         0  
215            
216 0         0 foreach my $line (@important_configs) {
217 0         0 my ($key, $value) = split(/\s*=\s*/, $line, 2);
218 0         0 print_key_value($key, $value, 'magenta');
219             }
220            
221 0         0 print color('yellow'), "...\n", color('reset');
222             }
223             }
224              
225 0         0 exit(0);
226              
227             __END__