command -v notify-send >/dev/null 2>&1 || exit 0
STAMP=${RUNTIME_DIRECTORY:-/run}/iowatchd-notify.stamp
if [ "$IOWATCHD_EVENT" = alert ]; then
now=$(date +%s)
last=$(cat "$STAMP" 2>/dev/null || echo 0)
[ $((now - last)) -ge 60 ] || exit 0
{ echo "$now" > "$STAMP"; } 2>/dev/null
fi
if [ "$IOWATCHD_EVENT" = alert ]; then
urgency=critical
summary="High disk IO on $IOWATCHD_DEVICE ($IOWATCHD_UTIL%)"
body=${IOWATCHD_MESSAGE#* | }
[ "$body" = "$IOWATCHD_MESSAGE" ] && body=""
body=$(printf '%s' "$body" | sed 's/ cmd="[^"]*"//g; s/ | /\n/g' | head -n 3)
else
urgency=low
summary="Disk IO back to normal on $IOWATCHD_DEVICE"
body="$IOWATCHD_MESSAGE"
fi
body=$(printf '%s' "$body" | sed 's/&/\&/g; s/</\</g; s/>/\>/g')
for bus in /run/user/*/bus; do
[ -S "$bus" ] || continue
uid=${bus#/run/user/}
uid=${uid%/bus}
[ "$uid" -gt 0 ] 2>/dev/null || continue
user=$(id -nu "$uid" 2>/dev/null) || continue
runuser -u "$user" -- env "DBUS_SESSION_BUS_ADDRESS=unix:path=$bus" \
notify-send --app-name=iowatchd --urgency="$urgency" \
--icon=drive-harddisk "$summary" "$body" >/dev/null 2>&1 &
done
wait