users/ryan/linux/waybar/modules/storage.sh
a2c015245921f589bbd7cc787bfd092474a07992
· 1.3 KB · 70 lines
raw
| 1 | #!/bin/sh |
| 2 | |
| 3 | set -eu |
| 4 | |
| 5 | mountpoint="/" |
| 6 | warning=20 |
| 7 | critical=10 |
| 8 | |
| 9 | error() { |
| 10 | jq -cn --arg message "$1" '{ |
| 11 | text: "N/A", |
| 12 | percentage: 0, |
| 13 | tooltip: $message, |
| 14 | class: "critical" |
| 15 | }' |
| 16 | exit 0 |
| 17 | } |
| 18 | |
| 19 | line=$(df -hP -l "$mountpoint" 2>/dev/null | tail -n 1) || |
| 20 | error "Unable to read filesystem statistics" |
| 21 | |
| 22 | # df -P guarantees a single line per filesystem. |
| 23 | set -- $line |
| 24 | |
| 25 | [ "$#" -ge 6 ] || error "Unable to parse filesystem statistics" |
| 26 | |
| 27 | filesystem=$1 |
| 28 | size=$2 |
| 29 | used=$3 |
| 30 | available=$4 |
| 31 | use=${5%\%} |
| 32 | mounted=$6 |
| 33 | |
| 34 | case "$use" in |
| 35 | ''|*[!0-9]*) error "Invalid filesystem usage percentage: $use%" ;; |
| 36 | esac |
| 37 | |
| 38 | free=$((100 - use)) |
| 39 | |
| 40 | if [ "$free" -le "$critical" ]; then |
| 41 | class="critical" |
| 42 | elif [ "$free" -le "$warning" ]; then |
| 43 | class="warning" |
| 44 | else |
| 45 | class="" |
| 46 | fi |
| 47 | |
| 48 | jq -cn \ |
| 49 | --arg text "${use}%" \ |
| 50 | --argjson percentage "$use" \ |
| 51 | --arg filesystem "$filesystem" \ |
| 52 | --arg size "$size" \ |
| 53 | --arg used "$used" \ |
| 54 | --arg available "$available" \ |
| 55 | --arg mounted "$mounted" \ |
| 56 | --arg class "$class" \ |
| 57 | '{ |
| 58 | text: $text, |
| 59 | percentage: $percentage, |
| 60 | tooltip: ( |
| 61 | "Filesystem: " + $filesystem + |
| 62 | "\rSize: " + $size + |
| 63 | "\rUsed: " + $used + |
| 64 | "\rAvail: " + $available + |
| 65 | "\rUse%: " + $text + |
| 66 | "\rMounted on: " + $mounted |
| 67 | ), |
| 68 | class: $class |
| 69 | }' |
| 70 |