-
Notifications
You must be signed in to change notification settings - Fork 0
/
check-memory-usage
executable file
·48 lines (38 loc) · 1.69 KB
/
check-memory-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
#!/usr/bin/env fish
# check-memory-usage - Check current memory and swap usage and notify if it's low.
# May be useful as a cron job.
# settings:
set threshold 0.85 # notify when memory and swap usage are both above this percentage
# code:
argparse -n check-memory-usage 'h/help' 'f/force' 's/stdout' -- $argv
if set -q _flag_h
echo "check-memory-usage - Check current memory and swap usage and warn when low."
echo "Usage: check-memory-usage [arguments]"
echo
echo " -h/--help - Print help and exit."
echo " -f/--force - Force the warning and output to be printed."
echo " -s/--stdout - Print to stdout instead of OSD."
exit
end
free -m | grep '^Mem:' | string match -qr '^\S+\s+(?<mem_total>\S+)\s+(?<mem_used>\S+)\s+(?<mem_free>\S+)'
free -m | grep '^Swap:' | string match -qr '^\S+\s+(?<swap_total>\S+)\s+(?<swap_used>\S+)\s+(?<swap_free>\S+)'
set mem_pct (math $mem_used / $mem_total)
set swap_pct (math $swap_used / $swap_total)
# echo "mem%: $mem_pct"
# echo "swap%: $swap_pct"
function get_top_for -d "Get the name of the top process for the specified resource."
top -b -n 1 -o $argv[1] | head -8 | tail -n -1 | string split ' ' | tail -n -1
end
if set -q _flag_f; or test $mem_pct -gt $threshold; and test $swap_pct -gt $threshold
set -l IFS
set output (printf 'RAM: %.f%%; top: %s\nSwap: %.f%%; top: %s' (math $mem_pct '*' 100) (get_top_for '%MEM') (math $swap_pct '*' 100) (get_top_for SWAP))
if set -q _flag_s
echo $output
else
set -x DBUS_SESSION_BUS_ADDRESS "unix:path=/run/user/$(id --user)/bus"
if not set -q DISPLAY
set -x DISPLAY :0.0
end
notify-send 'Memory usage warning' $output
end
end