speedwm-personal/modules/module_net

78 lines
2.2 KiB
Plaintext
Raw Permalink Normal View History

#!/bin/sh
# network module for status/stellar
# load config
module_config
[ -e "$HOME/.config/speedwm/statusrc" ] && . $HOME/.config/speedwm/statusrc
2022-12-02 12:03:26 +01:00
[ -z "$URL" ] && URL="1.1.1.1"
[ "$ENABLE_ITEM6" = "true" ] || exit
[ -e "/tmp/module_net_hidden" ] && exit
# get stats
GETNSTAT() {
2022-12-02 12:03:26 +01:00
ip link | grep -q "UP mode" || exit
2022-12-22 12:47:54 +01:00
R1="$(cat /sys/class/net/[ew]*/statistics/rx_bytes | paste -sd '+')"
T1="$(cat /sys/class/net/[ew]*/statistics/tx_bytes | paste -sd '+')"
sleep 0.1
2022-12-02 12:03:26 +01:00
R2="$(head -n 1 /sys/class/net/[ew]*/statistics/rx_bytes)"
T2="$(head -n 1 /sys/class/net/[ew]*/statistics/tx_bytes)"
2022-12-22 12:47:54 +01:00
TXPMS="$(expr "$T2" "-" "$T1")"
2022-12-02 12:03:26 +01:00
RXPMS="$(expr "$R2" "-" "$R1")"
TX="$(expr "$TXPMS" / "1024" "*" "10")"
RX="$(expr "$RXPMS" / "1024" "*" "10")"
2022-12-16 17:35:03 +01:00
[ -n "$TX" ] && printf "$TX\n" > /tmp/module_net_tx || exit
[ -n "$RX" ] && printf "$RX\n" > /tmp/module_net_rx || exit
2022-12-02 12:03:26 +01:00
[ -e "/tmp/module_net_tx" ] || exit
[ -e "/tmp/module_net_rx" ] || exit
}
[ ! -e "/tmp/module_net_tx" ] && GETNSTAT
2022-12-22 12:47:54 +01:00
NETWORK_TRANSMITTED="$(cat /tmp/module_net_tx)"
NETWORK_RECEIVED="$(cat /tmp/module_net_rx)"
# click
CLICK() {
# values
ICON="$ITEM6_ICON"
[ -e "/tmp/speedwm-button" ] && BUTTON="$(cat /tmp/speedwm-button)" || BUTTON="0"
[ -z "$TERMINAL" ] && TERMINAL=st
2022-12-16 17:35:03 +01:00
[ -n "$ICON" ] && ICONSPACING=" " # one character spacing
2022-12-22 12:47:54 +01:00
# check value of button
case "$BUTTON" in
"3") $TERMINAL -e bmon ; return ;;
esac
2022-12-22 12:47:54 +01:00
# send the notification
SEND_NOTIF() {
2022-11-01 20:38:29 +01:00
FULL_NETWORK="${ICON}${ICONSPACING}${NETWORK_TRANSMITTED} transmitted, ${NETWORK_RECEIVED} received."
notify-send "$FULL_NETWORK"
2022-12-22 12:47:54 +01:00
# some information
echo "Notification sent successfully!"
echo "INFO: $FULL_NETWORK"
2022-12-22 12:47:54 +01:00
return
}
2022-12-22 12:47:54 +01:00
command -v notify-send > /dev/null && SEND_NOTIF || echo "FATAL: libnotify not installed, can't send notification."
return
}
# information itself
PRINT() {
2022-12-16 17:35:03 +01:00
[ -n "$NETWORK_TRANSMITTED" ] && [ -n "$NETWORK_RECEIVED" ] && echo "$ITEM6_SEPARATOR $ITEM6_ICON $ITEM6_FORMAT" | sed "s|@t|$NETWORK_TRANSMITTED|g; s|@r|$NETWORK_RECEIVED|g"
}
# argument 1
case "$1" in
"") CLICK ;;
"--print-file") PRINT > /tmp/module_net ;;
"--print") PRINT ;;
"--click") CLICK ;;
esac
2022-11-01 20:38:29 +01:00
# gather some stats before exiting
GETNSTAT