Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support freebsd/osx #18

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion init.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/bin/bash -
#!/usr/bin/env bash

CURRENT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
source "$CURRENT_DIR/scripts/helpers.sh"
Expand Down
4 changes: 2 additions & 2 deletions net_speed.tmux
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/bin/bash -
#!/usr/bin/env bash

CURRENT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
source "$CURRENT_DIR/scripts/helpers.sh"
Expand All @@ -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}
Expand Down
2 changes: 1 addition & 1 deletion run-tests.sh
Original file line number Diff line number Diff line change
@@ -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
Expand Down
19 changes: 3 additions & 16 deletions scripts/download_speed.sh
Original file line number Diff line number Diff line change
@@ -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

105 changes: 90 additions & 15 deletions scripts/helpers.sh
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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 ))
Expand Down Expand Up @@ -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
Expand All @@ -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() {
Expand All @@ -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
}

8 changes: 5 additions & 3 deletions scripts/net_speed.sh
Original file line number Diff line number Diff line change
@@ -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")
Expand Down
19 changes: 3 additions & 16 deletions scripts/upload_speed.sh
Original file line number Diff line number Diff line change
@@ -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

2 changes: 1 addition & 1 deletion tests/suites/helpers.test.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/bin/bash -
#!/usr/bin/env bash

CURRENT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
SCRIPTS="$CURRENT_DIR/../../scripts"
Expand Down
2 changes: 1 addition & 1 deletion tests/test_utils.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/bin/bash -
#!/usr/bin/env bash
###############################################################################
# Author: Travis Goldie
# Purpose: Util funcs for unit tests
Expand Down