-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsbill.sh
More file actions
183 lines (176 loc) · 8.37 KB
/
sbill.sh
File metadata and controls
183 lines (176 loc) · 8.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# Time-stamp: <Thu 2022-04-28 09:25 jmorey>
################################################################################
# [/etc/]profile.d/slurm.sh - Various Slurm helper functions and aliases to
# . use on the UL HPC Platform (https://hpc.uni.lu)
#
# Copyright (c) 2020 UL HPC Team <hpc-team@uni.lu>
#
# Usage: source path/to/slurm.sh
#
# 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 3 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.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
################################################################################
################################################################################
# AZURE INSTRUCTIONS
# 1. copy script (ie. sbill.sh) to login and/or scheduler node(s)
# 2. Modify the "partition" array with your specific Partition names & costs
# 3. Copy script to /etc/profile.d/sbill.sh
# 4. source /etc/profile.d/sbill.sh
# 5. Run the function as sbill [...]
#
# NOTE:
# - This script assumes 1 job per compute node
################################################################################
# Default formats
#export SQUEUE_FORMAT="%.8i %.6P %.9q %.20j %.10u %.4D %.5C %.2t %.12M %.12L %.8Q %R"
### Most useful format field for squeue (see man squeue)
# --format --Format
# | Short (-o) | Long (-O) | Description |
# |------------|--------------|----------------------------------------------------|
# | %i | JobID | Job ID |
# | %j | Name | Job or job step name. |
# | %R | ReasonList | For pending jobs: the reason a job is waiting |
# | %t | StateCompact | Job state in compact form. |
# | %P | Partition | Partition |
# | %q | QOS | Quality of service associated with the job. |
# | %u | UserID | User name for a job or job step. |
# | %D | NumNodes | Number of nodes allocated to the job |
# | %C | NumCPUs | Number of CPUs (cores) |
# | %A | NumTasks | Number of tasks |
# | | NTPerNode | The number of task per node |
# | %l | TimeLimit | Time limit of the job: days-hours:minutes:seconds |
# | %M | TimeUsed | Time used by the job: days-hours:minutes:seconds |
# | %L | TimeLeft | Time left for the job: days-hours:minutes:seconds. |
###
# Job billing utility
##
sbill() {
local start=$(date +%F)
local end=$(date +"%Y-%m-%dT%H:%M")
# in the array add the [partion_name]=cost
declare -A partition=( [execute]=2.30 [highmem]=6.91 )
local jobid=""
local options=""
local username=""
local account=""
while [ -n "$1" ]; do
case $1 in
-S | --start) shift; start=$1;;
-E | --end) shift; end=$1;;
-m | -M | --month) start="$(date +%Y-%m)-01";;
-y | -Y | --year) start="$(date +%Y)-01-01";;
-u | --user) shift; username=$1;;
-j | --jobid) shift; jobid=$1;;
-a | --account) shift; account=$1;;
-h | --help)
echo "Display job charging / billing summary";
echo " ";
echo "Usage: ";
echo " sbill -j <jobid> # Show a specific job cost";
echo " sbill -a <account> # Show a specific account cost";
echo " ";
echo " sbill [-m] [-Y] [-S YYYY-MM-DD] [-E YYYT-MM-DDTHH:MM] # Show costs for all jobs for all user";
echo " -m = From start of the current Month";
echo " -Y = From start of the current Year";
echo " -S = Specific START Date; DEFAULT=Today";
echo " -E = Specific END Date; DEFAULT=Now";
echo " ";
echo " sbill -u <username> [...] # Show costs for all jobs for a specific user";
echo " ";
return;;
# *) options=$*; break;;
*) jobid=$*; break;;
esac
shift
done
if [ -n "${jobid}" ]; then
cmd="sacct -X --format=JobName,AllocTRES%60,ElapsedRaw -j ${jobid}"
echo "# ${cmd}"
$cmd
echo " "
test=$($cmd -n -P | cut -d "|" -f1 )
if [ -n "${test}" ]; then
local nodes=$($cmd -n -P | cut -d '|' -f 2 | tr ',' '\n' | grep node | cut -d '=' -f 2)
local part=$(sacct -X -j ${jobid} -P | grep ${jobid} | cut -d "|" -f 3)
local sec=$(sacct -X --format=ElapsedRaw -j ${jobid} -n -P)
local usage=$(printf "%0.2f\n" $(echo "$nodes*$sec/3600" | bc -l))
local cost=$(printf "$%0.2f\n" $(echo "$usage*${partition[$part]}" | bc -l))
echo " Total usage: $usage VMU (Estimated price: $cost)"
else
echo "JobID ${jobid} does not exist"
fi
elif [ -n "${account}" ]; then
cmd="sacct -X --format=jobid,user,account,partition,AllocNodes%10,ElapsedRaw -A ${account} --starttime ${start} --endtime ${end}"
echo "# ${cmd}"
$cmd
echo " "
test=$($cmd -n -P | cut -d "|" -f1 )
if [ -n "${test}" ]; then
for i in ${!partition[@]}; do
local walltime=$($cmd -n -P | grep $i | awk -F '|' '{sec += $6*$5} END {print sec}')
if [ -n "${walltime}" ]; then
local usage=$(printf "%0.2f\n" $(echo "$walltime/3600" | bc -l))
local cost=$(printf "$%0.2f\n" $(echo "$usage*${partition[$i]}" | bc -l))
echo " $i usage: $usage VMU ($i estimated price: $cost)"
else
echo " $i usage: 0.00 VMU ($i estimated price: \$0.00)"
continue
fi
done
else
echo "No jobs during this timeframe: ${start} - ${end}"
fi
elif [ -n "${username}" ]; then
cmd="sacct -X --format=jobid,user,partition,AllocNodes%10,ElapsedRaw -u ${username} --starttime ${start} --endtime ${end}"
echo "# ${cmd}"
$cmd
echo " "
test=$($cmd -n -P | cut -d "|" -f1 )
if [ -n "${test}" ]; then
for i in ${!partition[@]}; do
local walltime=$($cmd -n -P | grep $i | awk -F '|' '{sec += $5*$4} END {print sec}')
if [ -n "${walltime}" ]; then
local usage=$(printf "%0.2f\n" $(echo "$walltime/3600" | bc -l))
local cost=$(printf "$%0.2f\n" $(echo "$usage*${partition[$i]}" | bc -l))
echo " $i usage: $usage VMU ($i estimated price: $cost)"
else
echo " $i usage: 0.00 VMU ($i estimated price: \$0.00)"
continue
fi
done
else
echo "No jobs during this timeframe: ${start} - ${end}"
fi
else
cmd="sacct -X --format=jobid,user,account,partition,AllocNodes%10,ElapsedRaw -a --starttime ${start} --endtime ${end}"
echo "# ${cmd}"
$cmd
echo " "
test=$($cmd -n -P | cut -d "|" -f1 )
if [ -n "${test}" ]; then
for i in ${!partition[@]}; do
local walltime=$($cmd -n -P | grep $i | awk -F '|' '{sec += $6*$5} END {print sec}')
if [ -n "${walltime}" ]; then
local usage=$(printf "%0.2f\n" $(echo "$walltime/3600" | bc -l))
local cost=$(printf "$%0.2f\n" $(echo "$usage*${partition[$i]}" | bc -l))
echo " $i usage: $usage VMU ($i estimated price: $cost)"
else
echo " $i usage: 0.00 VMU ($i estimated price: \$0.00)"
continue
fi
done
else
echo "No jobs during this timeframe: ${start} - ${end}"
fi
fi
}