-
Notifications
You must be signed in to change notification settings - Fork 0
/
disk_usage
executable file
·97 lines (82 loc) · 2.44 KB
/
disk_usage
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
#!/bin/bash
# parse the arguments
OPTIONS=$(getopt -o hc --long help,check -n 'disk_usage' -- "$@")
if [ $? != 0 ] ; then echo "Terminating..." >&2 ; exit 1 ; fi
eval set -- "$OPTIONS"
DEPENDENCIES=(
notify-send
df
)
[ -z "$THRESHOLD" ] && THRESHOLD=90
check_dependencies () {
for package in ${DEPENDENCIES[@]}; do
if [ ! $(which $package) ]; then
echo "All of '$(echo ${DEPENDENCIES[@]})' are needed to run $0"
exit 1
fi
done
}
check_root_partition_usage () {
local disk_usage=$1
if [ "$disk_usage" -gt "$THRESHOLD" ]; then
notify-send "Root partition almost full" "root '/' partition is used at ${DISK_USAGE}! \n You should clear caches 'sudo pacman -Scc' for example" --urgency critical
fi
}
usage () {
#
# the usage function.
#
echo "Usage: disk_usage [-hc]"
echo "Type -h or --help for the full help."
exit 0
}
help () {
#
# the help function.
#
echo "disk_usage:"
echo " a script to check the root partition usage and throw appropriate notifications."
echo " dependencies are automatically checked"
echo ""
echo ' *TESTED on MANJARO*'
echo " run \`sudo systemctl enable cronie\` to activate the \`cron\` daemon if not already done, then"
echo " add \`* * * * * DISPLAY=:0 DBUS_SESSION_BUS_ADDRESS=<address> /path/to/disk_usage --check\`"
echo " to the \`cron\` table with \`crontab -e\` and restart the \`cron\` daemon with"
echo " \`sudo systemctl restart cronie\`. The address can be found with \`echo \$DBUS_SESSION_BUS_ADDRESS\`."
echo ""
echo "Usage:"
echo " disk_usage [-hc]"
echo ""
echo "Switches:"
echo " -h/--help shows this help."
echo " -c/--check check the root partition usage and throw a notification if critical."
echo ""
echo "Environment variables:"
echo " THRESHOLD the partition usage threshold to trigger the notification"
echo ""
echo "Dependencies:"
echo " notify-send"
exit 0
}
main () {
#
# TODO.
#
check_dependencies
while [[ $# -gt 0 ]]; do
case "$1" in
-h | --help ) help ;;
-c | --check ) ACTION="check"; shift 1 ;;
-- ) shift; break ;;
* ) break ;;
esac
done
DISK_USAGE=$(df / --output=pcent | grep --only-matching --extended-regexp '[0-9]{2,3}')
# an action is required
[ -z "$ACTION" ] && usage
case "$ACTION" in
check ) check_root_partition_usage "$DISK_USAGE" ;;
* ) echo "an error occured (got unexpected '$ACTION')"; exit 1 ;;
esac
}
main "$@"