-
Notifications
You must be signed in to change notification settings - Fork 11
/
disk-watcher.nix
45 lines (38 loc) · 1.07 KB
/
disk-watcher.nix
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
{ pkgs, config }:
let
cfg = config.icedos;
threshold = builtins.toString (cfg.desktop.hyprland.lock.diskUsageThreshold);
in
pkgs.writeShellScriptBin "disk-watcher" ''
DISKS=($(lsblk -d -io NAME | tail -n +2))
READ_QUERY=".sysstat.hosts[].statistics[].disk[].MB_read"
WRITE_QUERY=".sysstat.hosts[].statistics[].disk[].MB_wrtn"
DISK_THRESHOLD=${threshold}
diskstats () {
iostat -d -m -z -o JSON "$1"
}
read=()
written=()
for disk in "''${DISKS[@]}"
do
json="$(diskstats "$disk")"
read+=($(echo "$json" | jq "$READ_QUERY"))
written+=($(echo "$json" | jq "$WRITE_QUERY"))
done
sleep 1
i=0
for disk in "''${DISKS[@]}"
do
json="$(diskstats "$disk")"
current_read=($(echo "$json" | jq "$READ_QUERY"))
current_written=($(echo "$json" | jq "$WRITE_QUERY"))
READ_PER_SECOND=$(( current_read - read[i] ))
WRITE_PER_SECOND=$(( current_written - written[i] ))
if (( READ_PER_SECOND > DISK_THRESHOLD )) || (( WRITE_PER_SECOND > DISK_THRESHOLD )); then
printf true
exit
fi
((i++))
done
printf false
''