diff options
Diffstat (limited to 'users/ryan/linux/waybar/modules')
| -rwxr-xr-x | users/ryan/linux/waybar/modules/storage.sh | 77 |
1 files changed, 61 insertions, 16 deletions
diff --git a/users/ryan/linux/waybar/modules/storage.sh b/users/ryan/linux/waybar/modules/storage.sh index 90cc3dc..7cb7249 100755 --- a/users/ryan/linux/waybar/modules/storage.sh +++ b/users/ryan/linux/waybar/modules/storage.sh | |||
| @@ -1,25 +1,70 @@ | |||
| 1 | #!/bin/sh | 1 | #!/bin/sh |
| 2 | 2 | ||
| 3 | mount="/" | 3 | set -eu |
| 4 | |||
| 5 | mountpoint="/" | ||
| 4 | warning=20 | 6 | warning=20 |
| 5 | critical=10 | 7 | critical=10 |
| 6 | 8 | ||
| 7 | df -h -P -l "$mount" | awk -v warning=$warning -v critical=$critical ' | 9 | error() { |
| 8 | /\/.*/ { | 10 | jq -cn --arg message "$1" '{ |
| 9 | text=$4 | 11 | text: "N/A", |
| 10 | tooltip="Filesystem: "$1"\rSize: "$2"\rUsed: "$3"\rAvail: "$4"\rUse%: "$5"\rMounted on: "$6 | 12 | percentage: 0, |
| 11 | use=$5 | 13 | tooltip: $message, |
| 12 | exit 0 | 14 | class: "critical" |
| 15 | }' | ||
| 16 | exit 0 | ||
| 13 | } | 17 | } |
| 14 | END { | 18 | |
| 15 | class="" | 19 | line=$(df -hP -l "$mountpoint" 2>/dev/null | tail -n 1) || |
| 16 | gsub(/%$/,"",use) | 20 | error "Unable to read filesystem statistics" |
| 17 | if ((100 - use) < critical) { | 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 | ||
| 18 | class="critical" | 41 | class="critical" |
| 19 | } else if ((100 - use) < warning) { | 42 | elif [ "$free" -le "$warning" ]; then |
| 20 | class="warning" | 43 | class="warning" |
| 21 | } | 44 | else |
| 22 | print "{\"text\":\""text"\", \"percentage\":"use",\"tooltip\":\""tooltip"\", \"class\":\""class"\"}" | 45 | class="" |
| 23 | } | 46 | fi |
| 24 | ' | 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 | }' | ||
| 25 | 70 | ||
