-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstorage_quota
More file actions
executable file
·234 lines (193 loc) · 8.58 KB
/
storage_quota
File metadata and controls
executable file
·234 lines (193 loc) · 8.58 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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
#!/bin/bash
#----------------------------------------------------------------------------
# bmrc_storage_quota — BMRC storage usage reporter
#
# home : du on $HOME vs 10 GiB policy limit
# /well/<group> : df, auto-detected; works on GPFS and Lustre
# /well/kir/scratch : df, shared scratch
#
# Usage: bmrc_storage_quota [-h] [-u <username>]
#----------------------------------------------------------------------------
CWD="$(basename "$0")"
# ── policy limit for home directories (adjust if this changes) ───────────────
HOME_QUOTA_GiB=10
HOME_QUOTA_KB=$(( HOME_QUOTA_GiB * 1024 * 1024 ))
# ── ANSI colours ──────────────────────────────────────────────────────────────
BOLD="\033[1m"; RESET="\033[0m"; DIM="\033[2m"
RED="\033[38;5;196m"; YELLOW="\033[38;5;220m"
GREEN="\033[38;5;82m"; CYAN="\033[38;5;81m"
WHITE="\033[97m"; BG_HDR="\033[48;5;237m"
BAR_FILL="█"; BAR_EMPTY="░"; BAR_WIDTH=26
# ── Helpers ───────────────────────────────────────────────────────────────────
usage() {
cat <<EOF
${BOLD}${CWD}${RESET}
${BOLD}SYNOPSIS${RESET}
$CWD [-h] [-u <username>]
${BOLD}DESCRIPTION${RESET}
Reports storage usage for BMRC filesystems:
• Home directory — du vs ${HOME_QUOTA_GiB} GiB policy limit (may take a few seconds)
• Group space(s) — /well/<group> (auto-detected, GPFS or Lustre)
• Shared scratch — /well/kir/scratch
• KIR projects (collab,datasets,mirror) — /well/kir/scratch
${BOLD}OPTIONS${RESET}
-h, --help Show this help
-u, --user <name> Inspect another user's home (must have read access)
EOF
exit 0
}
colour_for() {
local pct=$1
(( pct >= 90 )) && { echo -n "$RED"; return; }
(( pct >= 70 )) && { echo -n "$YELLOW"; return; }
echo -n "$GREEN"
}
draw_bar() {
local used=$1 total=$2 pct=0
(( total > 0 )) && pct=$(( used * 100 / total ))
(( pct > 100 )) && pct=100
local filled=$(( pct * BAR_WIDTH / 100 ))
local empty=$(( BAR_WIDTH - filled ))
local col; col=$(colour_for "$pct")
printf "${col}"
for (( i=0; i<filled; i++ )); do printf "%s" "$BAR_FILL"; done
printf "${DIM}"
for (( i=0; i<empty; i++ )); do printf "%s" "$BAR_EMPTY"; done
printf "${RESET}"
}
human_kb() {
local kb=$1
if (( kb >= 1073741824 )); then printf "%.1f TiB" "$(echo "scale=1; $kb/1073741824" | bc)"
elif (( kb >= 1048576 )); then printf "%.1f GiB" "$(echo "scale=1; $kb/1048576" | bc)"
elif (( kb >= 1024 )); then printf "%.1f MiB" "$(echo "scale=1; $kb/1024" | bc)"
else printf "%d KiB" "$kb"
fi
}
# Detect filesystem type label from df output (GPFS vs Lustre vs other)
fs_type_label() {
local path="$1"
local fsname
fsname=$(df "$path" 2>/dev/null | tail -1 | awk '{print $1}')
if [[ "$fsname" == *"@o2ib"* || "$fsname" == *"exafs"* ]]; then echo "Lustre"
elif [[ "$fsname" == WTGPFS* || "$fsname" == gpfs* ]]; then echo "GPFS"
else echo "$(basename "$fsname")"
fi
}
print_header() {
printf "\n"
printf "${BG_HDR}${BOLD}${WHITE}"
printf " %-24s %-11s %-11s %-${BAR_WIDTH}s %5s %s${RESET}\n" \
"Filesystem" "Used" "Limit/Size" "Usage" "%" "Notes"
printf "${DIM}%s${RESET}\n" "$(printf '%0.s─' {1..92})"
}
print_separator() {
printf "${DIM}%s${RESET}\n" "$(printf '%0.s─' {1..92})"
}
print_row() {
local label="$1" used_kb=$2 total_kb=$3 note="${4:-}"
local pct=0
(( total_kb > 0 )) && pct=$(( used_kb * 100 / total_kb ))
(( pct > 100 )) && pct=100
local col; col=$(colour_for "$pct")
printf " ${BOLD}%-24s${RESET} ${col}%-11s${RESET} %-11s " \
"$label" "$(human_kb "$used_kb")" "$(human_kb "$total_kb")"
draw_bar "$used_kb" "$total_kb"
printf " ${col}%4d%%${RESET}" "$pct"
[[ -n "$note" ]] && printf " ${DIM}%s${RESET}" "$note"
printf "\n"
}
# ── Home: du against policy quota ─────────────────────────────────────────────
report_home() {
local target_user="$1"
# Resolve home path — use $HOME if checking ourselves, else derive it
local home_path
if [[ "$target_user" == "$USER" ]]; then
home_path="$HOME"
else
home_path=$(getent passwd "$target_user" 2>/dev/null | cut -d: -f6)
if [[ -z "$home_path" || ! -d "$home_path" ]]; then
printf " ${YELLOW}⚠ Cannot resolve home directory for '%s'${RESET}\n" "$target_user"
return 1
fi
fi
# Check read access before running du
if [[ ! -r "$home_path" ]]; then
printf " ${YELLOW}⚠ No read access to %s${RESET}\n" "$home_path"
return 1
fi
# du can be slow — print an inline notice, then overwrite with the result
printf " ${DIM}Calculating home usage (du)...${RESET}" >&2
local used_kb
used_kb=$(du -sk "$home_path" 2>/dev/null | awk '{print $1}')
# Clear the "calculating" message
printf "\r%-40s\r" "" >&2
if [[ -z "$used_kb" ]]; then
printf " ${YELLOW}⚠ du failed on %s${RESET}\n" "$home_path"
return 1
fi
print_row "home (${target_user})" "$used_kb" "$HOME_QUOTA_KB" \
"${home_path} [${HOME_QUOTA_GiB} GiB policy]"
}
# ── df-based reporter ─────────────────────────────────────────────────────────
report_df() {
local label="$1" path="$2" note="${3:-}"
if [[ ! -d "$path" ]]; then
printf " ${DIM}%-24s (path not accessible)${RESET}\n" "$label"
return
fi
local df_out; df_out=$(df -BK "$path" 2>/dev/null | tail -1) || {
printf " ${DIM}%-24s (df failed)${RESET}\n" "$label"
return
}
local _fs size_k used_k _rest
read -r _fs size_k used_k _rest <<< "$df_out"
size_k=${size_k%K}; used_k=${used_k%K}
# Auto-build note with filesystem type if none supplied
if [[ -z "$note" ]]; then
note="$(fs_type_label "$path")"
fi
print_row "$label" "$used_k" "$size_k" "$note"
}
# ── Auto-detect /well/<group> mounts ─────────────────────────────────────────
report_groups() {
local target_user="$1"
local found=0
while IFS= read -r grp; do
[[ "$grp" == "kir" ]] && continue # ← skip the shared admin group
[[ -d "/well/${grp}" ]] || continue
local fslabel; fslabel=$(fs_type_label "/well/${grp}")
report_df "group: ${grp}" "/well/${grp}" "$fslabel"
(( found++ ))
done < <(id -Gn "$target_user" 2>/dev/null | tr ' ' '\n' | sort -u)
(( found == 0 )) && \
printf " ${DIM}%-24s (no /well/<group> mount found for %s's groups)${RESET}\n" \
"group" "$target_user"
}
# ── Main ──────────────────────────────────────────────────────────────────────
main() {
local target_user="$USER"
while (( $# > 0 )); do
case $1 in
-u|--user) shift; target_user="$1" ;;
-h|--help) usage ;;
-*) printf "${YELLOW}Unknown option: %s${RESET}\n" "$1"; usage ;;
esac
shift
done
if [[ "$target_user" != "$USER" ]] && (( EUID != 0 )); then
printf "${YELLOW}Note: reading another user's home requires read access to their directory.${RESET}\n"
fi
printf "\n${BOLD}${CYAN} BMRC Storage Report${RESET} "
printf "${DIM}%s | user: %s${RESET}\n" "$(date '+%Y-%m-%d %H:%M')" "$target_user"
print_header
report_home "$target_user"
print_separator
report_groups "$target_user"
print_separator
report_df "kir/scratch" "/well/kir/scratch" "GPFS (shared — not per-user)"
printf " ${BOLD}kir/projects/${RESET}\n"
report_df " {collab,mirror...}" "/well/kir/projects" "GPFS (shared — not per-user)"
printf "\n${DIM} Colour guide: ${GREEN}█${RESET}${DIM} <70%% ${YELLOW}█${RESET}${DIM} 70–89%% ${RED}█${RESET}${DIM} ≥90%%"
printf " | group & scratch figures are filesystem-wide${RESET}\n\n"
}
main "$@"