-
Notifications
You must be signed in to change notification settings - Fork 0
/
disk.sh
executable file
·75 lines (61 loc) · 1.85 KB
/
disk.sh
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
#!/bin/bash
# https://www.baeldung.com/linux/source-include-files#setting-a-correct-path
source "$(dirname "$0")"/lib/log.sh
source "$(dirname "$0")"/lib/env.sh
source "$(dirname "$0")"/lib/notification.sh
VERBOSE=
GLOBAL=
NOTIFY=
# https://sookocheff.com/post/bash/parsing-bash-script-arguments-with-shopts/
while getopts ":hgvn" opt; do
case ${opt} in
h)
echo "Disk script options:"
echo " -h Display help"
echo " -g Output global disk usage"
echo " -n Notify admin via slack and email"
echo " -v Output verbose volume usage (expects volume name parameter)"
exit 0
;;
g) GLOBAL=1 ;;
n) NOTIFY=1 ;;
v) VERBOSE=1 ;;
\?)
error "Invalid Option: -$OPTARG" 1>&2
exit 1
;;
esac
done
shift $((OPTIND - 1))
DIR="$1"
shift # Remove option from the argument list
# https://www.howtogeek.com/409611/how-to-view-free-disk-space-and-disk-usage-from-the-linux-terminal/
if [[ -n "$GLOBAL" ]]; then
if [[ -n "$DIR" ]]; then
warn "Global output, volume ignored."
fi
df -h --output=target,source,fstype,size,used,avail,pcent
exit 0
fi
if [[ -z "$DIR" ]]; then
error "Missing directory parameter." 1>&2
exit 1
fi
if [[ -n "$VERBOSE" ]]; then
df -h --output=target,source,fstype,size,used,avail,pcent "$DIR"
exit 0
fi
USAGE=$(df -h --output=pcent,target | grep -E "${DIR}$" | cut -d "%" -f 1 | xargs echo -n)
if [[ -n "$NOTIFY" ]]; then
if [[ "$USAGE" -gt "$STORAGE_PCNT_THRESHOLD" ]]; then
SLACK_RESULT=$(slack "<@$SLACK_RECIPIENT_ID> <!here>: Disk space \`$DIR\` at $USAGE%!")
log "Disk space slack notification sent $SLACK_RESULT"
BODY=$(<"$(dirname "$0")"/tmpl/dsk.txt)
BODY="${BODY/:STORAGE_NAME:/${DIR}}"
BODY="${BODY/:USAGE:/${USAGE}}"
EMAIL_RESULT=$(email "$BODY" "Storage alert (${USAGE}%)")
log "Disk space alert email sent $EMAIL_RESULT"
fi
exit 0
fi
echo -e "$USAGE"