-
Notifications
You must be signed in to change notification settings - Fork 0
/
acctusage
executable file
·122 lines (103 loc) · 3.46 KB
/
acctusage
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
#!/usr/bin/env bash
# acctusage
set -euo pipefail
help() {
# Display help information
cat << EOF
acctusage / Report Slurm account usage by users or accounts
Usage: acctusage [-h] [-u <users>] [-a <accounts>] [-s <startdate>] [-e <enddate>]
Options:
-h Display help
-u Specify users
* When only using users option, default value is current user
* When only using accounts option, default value is all users in accounts
* Multiple users can be specified with comma separation
-a Specify project accounts
* When only using users option, default value is all accounts
* When only using accounts option, default value is all users in accounts
* Multiple accounts can be specified with comma separation
-s Specify start date/time
* Default value is 7 days ago
-e Specify end date/time
* Default value is current date
Examples:
acctusage
acctusage -h
acctusage -s 2023-01-01
acctusage -u ttrojan
acctusage -u ttrojan -s 2023-01-01
acctusage -u ttrojan -a ttrojan_123
acctusage -a ttrojan_123
acctusage -a ttrojan_123 -s 2023-01-01 -e 2023-01-31
EOF
}
# Set default values for option arguments
users=""
accounts=""
startdate=$(date --date="-7 day" +%Y-%m-%d)
enddate=$(date +%Y-%m-%d)
# Define options
while getopts ":hu:a:s:e:" option; do
case $option in
h) # display help
help
exit 0
;;
u) # select users
users="$OPTARG"
if [[ $users =~ ^- ]]; then
echo "Error: Invalid argument to option -u" >&2
exit 1
fi
;;
a) # select accounts
accounts="$OPTARG"
if [[ $accounts =~ ^- ]]; then
echo "Error: Invalid argument to option -a" >&2
exit 1
fi
;;
s) # select startdate
startdate="$OPTARG"
if [[ $startdate =~ ^- ]]; then
echo "Error: Invalid argument to option -s" >&2
exit 1
fi
;;
e) # select enddate
enddate="$OPTARG"
if [[ $enddate =~ ^- ]]; then
echo "Error: Invalid argument to option -e" >&2
exit 1
fi
;;
\?) # invalid argument
echo "Error: Invalid option -$OPTARG" >&2
exit 1
;;
esac
done
# Set output format
format="login,accounts,cluster,tresname,used"
# Set TRES to display
tres="cpu,gres/gpu,mem,billing"
cat << EOF
--------------------------------------------------------------------
Slurm account usage from $startdate to $enddate
--------------------------------------------------------------------
User Account Cluster Resource Usage
--------- --------------- --------- -------------- -----------------
EOF
if [[ -z $users && -z $accounts ]]; then
sreport cluster accountutilizationbyuser -n -a "users=$USER" "start=$startdate" "end=$enddate" "format=$format" "--tres=$tres"
elif [[ -z $accounts ]]; then
sreport cluster accountutilizationbyuser -n -a "users=$users" "start=$startdate" "end=$enddate" "format=$format" "--tres=$tres"
elif [[ -z $users ]]; then
sreport cluster userutilizationbyaccount -n -a "accounts=$accounts" "start=$startdate" "end=$enddate" "format=$format" "--tres=$tres"
else
sreport cluster userutilizationbyaccount -n -a "users=$users" "accounts=$accounts" "start=$startdate" "end=$enddate" "format=$format" "--tres=$tres"
fi
cat << EOF
--------------------------------------------------------------------
*CPU/GPU usage in minutes
EOF