diff --git a/init.sh b/init.sh index 65235f0..8e6b906 100755 --- a/init.sh +++ b/init.sh @@ -1,4 +1,4 @@ -#!/bin/bash - +#!/usr/bin/env bash CURRENT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" source "$CURRENT_DIR/scripts/helpers.sh" diff --git a/net_speed.tmux b/net_speed.tmux index a81f908..07691bf 100755 --- a/net_speed.tmux +++ b/net_speed.tmux @@ -1,4 +1,4 @@ -#!/bin/bash - +#!/usr/bin/env bash CURRENT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" source "$CURRENT_DIR/scripts/helpers.sh" @@ -13,7 +13,7 @@ upload_interpolation="\#{upload_speed}" do_interpolation() { local input=$1 - local result="" + local result="" result=${input/$download_interpolation/$download_speed} result=${result/$net_interpolation/$net_speed} diff --git a/run-tests.sh b/run-tests.sh index 70f0d41..7ed1757 100755 --- a/run-tests.sh +++ b/run-tests.sh @@ -1,4 +1,4 @@ -#!/bin/bash - +#!/usr/bin/env bash CURRENT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" for testSuite in $CURRENT_DIR/tests/suites/* ; do diff --git a/scripts/download_speed.sh b/scripts/download_speed.sh index 66f5511..493b77e 100755 --- a/scripts/download_speed.sh +++ b/scripts/download_speed.sh @@ -1,28 +1,15 @@ -#!/bin/bash - +#!/usr/bin/env bash CURRENT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" source "$CURRENT_DIR/helpers.sh" -sum_download_speed() -{ - # Output uses first column - sum_speed 1 -} - main() { - # TODO make configurable - #local file=$(get_tmux_option $DOWNLOAD_FILE) - local file=$DOWNLOAD_FILE - local old_val=$(read_file $file) - local new_val=$(sum_download_speed) - - write_file $file $new_val - local vel=$(get_velocity $new_val $old_val) + local speed=$(get_speed 1) ## Format output local format=$(get_tmux_option @download_speed_format "%s") - printf "$format" "$vel" + printf "$format" "$speed" } main diff --git a/scripts/helpers.sh b/scripts/helpers.sh index 6877eac..aa7b3ca 100644 --- a/scripts/helpers.sh +++ b/scripts/helpers.sh @@ -1,10 +1,11 @@ -#!/bin/bash - +#!/usr/bin/env bash ## # Varialbes ## -DOWNLOAD_FILE="/tmp/tmux_net_speed.download" -UPLOAD_FILE="/tmp/tmux_net_speed.upload" +LAST_FILE="/tmp/tmux_net_speed.last" +_last=() +_current=() get_tmux_option() { local option=$1 @@ -28,12 +29,12 @@ get_velocity() { local new_value=$1 local old_value=$2 + local interval=${3:-$(get_tmux_option 'status-interval' 5)} # Consts local THOUSAND=1024 local MILLION=1048576 - local interval=$(get_tmux_option 'status-interval' 5) local vel=$(( ( new_value - old_value ) / interval )) local vel_kb=$(( vel / THOUSAND )) local vel_mb=$(( vel / MILLION )) @@ -87,12 +88,22 @@ write_file() get_interfaces() { + local os=${1:-$(os_type)} local interfaces=$(get_tmux_option @net_speed_interfaces "") if [[ -z "$interfaces" ]] ; then - for interface in /sys/class/net/*; do - interfaces+=$(echo $(basename $interface) " "); - done + case $os in + freebsd|osx) + for interface in $(ifconfig -l); do + interfaces+="$interface " + done + ;; + linux) + for interface in /sys/class/net/*; do + interfaces+=$(echo $(basename $interface) " "); + done + ;; + esac fi # Do not quote the variable. This way will handle trailing whitespace @@ -101,19 +112,61 @@ get_interfaces() sum_speed() { - local column=$1 + local os=$(os_type) + local down_total=0 + local up_total=0 - declare -a interfaces=$(get_interfaces) + declare -a interfaces=$(get_interfaces $os) - local line="" - local val=0 for intf in ${interfaces[@]} ; do - line=$(cat /proc/net/dev | grep "$intf" | cut -d':' -f 2) - speed="$(echo -n $line | cut -d' ' -f $column)" - let val+=${speed:=0} + case $os in + freebsd|osx) + line=( $(netstat -I "$intf" -ibn | grep 'Link#' | awk '{ print $(NF-4), $(NF-1) }') ) + ;; + linux) + line=( $(cat /proc/net/dev | grep "$intf" | cut -d':' -f 2 | awk '{ print $1, $9 }') ) + ;; + esac + let down_total+=${line[0]} + let up_total+=${line[1]} done - echo $val + echo $down_total $up_total +} + +# 1 - download, 2 - upload +get_speed() +{ + local column=$1 + local subprocess=${2:-0} + local interval=$(get_tmux_option 'status-interval' 5) + local now=$(date +%s) + + # timestamp download_cumulative upload_cumulative + local last=( "${_last[@]}" ) + if [ ${#last[@]} -eq 0 ]; then + last=( $(read_file "$LAST_FILE" "$now 0 0") ) + _last=( "${last[@]}" ) + fi + # protect against division by 0 + if [[ $last -eq $now ]]; then sleep 1; now=$(date +%s); fi + local current=( "${_current[@]}" ) + if [ ${#current[@]} -eq 0 ]; then + current=( $now $(sum_speed) ) + _current=( "${current[@]}" ) + fi + local delta_t=$(( $current - $last )) + local speed=$(get_velocity ${current[$column]} ${last[$column]} $delta_t) + + if [[ $subprocess -eq 0 ]] && [[ $((${last[0]}+$interval)) -le $now ]]; then + function update_last() + { + write_file "$LAST_FILE" "${_current[*]}" + } + trap update_last EXIT + fi + + echo $speed } is_osx() { @@ -128,3 +181,25 @@ command_exists() { local command="$1" type "$command" >/dev/null 2>&1 } + +os_type() { + local os_name="unknown" + + case $(uname | tr '[:upper:]' '[:lower:]') in + linux*) + os_name="linux" + ;; + darwin*) + os_name="osx" + ;; + msys*) + os_name="windows" + ;; + freebsd*) + os_name="freebsd" + ;; + esac + + echo -n $os_name +} + diff --git a/scripts/net_speed.sh b/scripts/net_speed.sh index 5390925..b4ca7ea 100755 --- a/scripts/net_speed.sh +++ b/scripts/net_speed.sh @@ -1,12 +1,14 @@ -#!/bin/bash - +#!/usr/bin/env bash CURRENT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" source "$CURRENT_DIR/helpers.sh" main() { - local download=$("$CURRENT_DIR/download_speed.sh") - local upload=$("$CURRENT_DIR/upload_speed.sh") + # calls within $() are in subprocess shells, run first to cache values + get_speed 1 >/dev/null + local download=$(get_speed 1 1) + local upload=$(get_speed 2 1) ## Format output local format=$(get_tmux_option @net_speed_format "D:%10s U:%10s") diff --git a/scripts/upload_speed.sh b/scripts/upload_speed.sh index b66477b..a76341a 100755 --- a/scripts/upload_speed.sh +++ b/scripts/upload_speed.sh @@ -1,28 +1,15 @@ -#!/bin/bash - +#!/usr/bin/env bash CURRENT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" source "$CURRENT_DIR/helpers.sh" -sum_upload_speed() -{ - # Output uses ninth column - sum_speed 9 -} - main() { - # TODO make configurable - #local upload_file=$(get_tmux_option $UPLOAD_FILE) - local file=$UPLOAD_FILE - local old_val=$(read_file $file) - local new_val=$(sum_upload_speed) - - write_file $file $new_val - local vel=$(get_velocity $new_val $old_val) + local speed=$(get_speed 2) ## Format output local format=$(get_tmux_option @upload_speed_format "%s") - printf "$format" "$vel" + printf "$format" "$speed" } main diff --git a/tests/suites/helpers.test.sh b/tests/suites/helpers.test.sh index a68bbb1..8062305 100755 --- a/tests/suites/helpers.test.sh +++ b/tests/suites/helpers.test.sh @@ -1,4 +1,4 @@ -#!/bin/bash - +#!/usr/bin/env bash CURRENT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" SCRIPTS="$CURRENT_DIR/../../scripts" diff --git a/tests/test_utils.sh b/tests/test_utils.sh index 045d3e5..934767f 100644 --- a/tests/test_utils.sh +++ b/tests/test_utils.sh @@ -1,4 +1,4 @@ -#!/bin/bash - +#!/usr/bin/env bash ############################################################################### # Author: Travis Goldie # Purpose: Util funcs for unit tests