summaryrefslogtreecommitdiff
path: root/users/ryan/linux/waybar/modules/storage.sh
blob: 7cb72493c06342ce9ecbb208ed5af052363633dc (plain)
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
#!/bin/sh

set -eu

mountpoint="/"
warning=20
critical=10

error() {
    jq -cn --arg message "$1" '{
        text: "N/A",
        percentage: 0,
        tooltip: $message,
        class: "critical"
    }'
    exit 0
}

line=$(df -hP -l "$mountpoint" 2>/dev/null | tail -n 1) ||
    error "Unable to read filesystem statistics"

# df -P guarantees a single line per filesystem.
set -- $line

[ "$#" -ge 6 ] || error "Unable to parse filesystem statistics"

filesystem=$1
size=$2
used=$3
available=$4
use=${5%\%}
mounted=$6

case "$use" in
    ''|*[!0-9]*) error "Invalid filesystem usage percentage: $use%" ;;
esac

free=$((100 - use))

if [ "$free" -le "$critical" ]; then
    class="critical"
elif [ "$free" -le "$warning" ]; then
    class="warning"
else
    class=""
fi

jq -cn \
    --arg text "${use}%" \
    --argjson percentage "$use" \
    --arg filesystem "$filesystem" \
    --arg size "$size" \
    --arg used "$used" \
    --arg available "$available" \
    --arg mounted "$mounted" \
    --arg class "$class" \
    '{
        text: $text,
        percentage: $percentage,
        tooltip: (
            "Filesystem: " + $filesystem +
            "\rSize: " + $size +
            "\rUsed: " + $used +
            "\rAvail: " + $available +
            "\rUse%: " + $text +
            "\rMounted on: " + $mounted
        ),
        class: $class
    }'