-
Notifications
You must be signed in to change notification settings - Fork 0
/
check-df
executable file
·56 lines (45 loc) · 1.88 KB
/
check-df
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
#!/usr/bin/env fish
# check-df - Check drive free; print a message if free space of any partitions are below a threshold.
# May be useful to run from your shell init file, or, with --notify, from crontab.
# settings:
set percent_warning 100 # warn if percent used is above this number
set gb_free_warning 10 # warn if the amount of free GB is below this number
set ignored # list of mount points to ignore
# code:
set -- script "$(path basename (status filename))"
argparse -n "$script" 'h/help' 'n/notify' 'L/non-local' -- $argv
if set -q _flag_h
echo "$script - Check drive free; print a message if free space of any partitions are below a threshold."
echo "Usage: $script [arguments]"
echo
echo " -h/--help - Print help and exit."
echo " -n/--notify - Show a notification instead of printing (useful if you want to run this script as a cron job)."
echo " -L/--non-local - Also check non-local filesystems."
exit
end
if not set -q _flag_L
set -- nonlocal -l
end
set warnings
for filesystem in (df $nonlocal -BG -x efivarfs -x tmpfs -x devtmpfs -x squashfs -x iso9660 --output=avail,pcent,target 2>/dev/null | tail -n +2)
set split (string match -r '^\s*(?<avail>\S+)\s+(?<percent>\S+)\s+(?<target>\S+)' $filesystem)
set avail_num (string replace -r '([A-Za-z]+)$' '' $avail)
set percent (string replace -a '%' '' $percent)
if string match --quiet --regex '^/(boot|efi|run/zram.*)$' $target
continue
end
if contains $target $ignored
continue
end
if test $percent -gt $percent_warning; or test $avail_num -lt $gb_free_warning
set -a warnings (printf '%s has ~%s free (%s%% used)!' $target $avail $percent)
end
end
if test (count $warnings) = '0'
exit
end
if set -q _flag_n
notification --name=check-df --urgency=critical 'check-df' "$(printf '%s\n' $warnings)"
else
printf '%s\n' $warnings
end