Auto shutdown if there's no 'real' network traffic for a 'homeserver'

0

Hello I just installed my homeserver I'm using the following:

  • TvHeadend
  • PyLoad
  • NzbGet
  • Samba

I plan to shutdown if the up/downstream by those services is less than 1 Mb/s(Bandwith) or Traffic in the last 10 minutes was under 100mb.

So my questions is if there are any tools to easily monitor the traffic and pass and then let it me simply read out with a script i setup to be regulary called(every 10 minutes or so with cronjob) and then simply check if the usage is too low and shutdown in this case.

I'm using Archlinux if it matters.

BubbaLebba

Posted 2015-08-01T19:11:02.540

Reputation: 1

Answers

1

I needed a script to do this, so I wrote this:

#!/bin/bash -e
#
# Wait until there's less than -t of traffic in an interval of -i seconds.
#

fmt() {
    numfmt --to=si --suffix=B $1
}

while getopts "i:m:" opt; do
    case $opt in
        i)
            interval=$OPTARG
            ;;
        t)
            minimum2=$(numfmt --from=si $OPTARG)
            ;;
        \?)
            echo "Invalid option: -$OPTARG" >&2
            exit 1
            ;;
    esac
done
shift $((OPTIND-1))

bytes=$(< /sys/class/net/eth1/statistics/rx_bytes )

minimum=-1

sleep $interval

while [[ $(( $(< /sys/class/net/eth1/statistics/rx_bytes ) - $bytes )) -gt $minimum ]]; do
    minimum=$minimum2
    rate=$(( $(< /sys/class/net/eth1/statistics/rx_bytes ) - $bytes ))
    echo $(date +%Y%m%d-%H%M%S)': received' $(fmt $rate) '('$(fmt $(( $rate / $interval )))'/s)'
    bytes=$(< /sys/class/net/eth1/statistics/rx_bytes )
    sleep $interval
done

echo "End: received" $(fmt $(( $(< /sys/class/net/eth1/statistics/rx_bytes ) - $bytes ))) '('$(fmt $(( $rate / $interval )))'/s)'

Then use it like until-low-traffic -i 60 -m 1M && shutdown -h 1 or whatever.

(It could be made a lot simpler, if you fix the parameters and don't format the output.)

MattBlissett

Posted 2015-08-01T19:11:02.540

Reputation: 11