Last active
January 1, 2017 20:10
-
-
Save ampersandcastles/21ed6f98f8cc5c9bf3dfb4d4c4e66fc8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
gistup |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Global Variables | |
LAST_MONTH=$(/bin/date -d '-2 days' "+%m-%Y") | |
LAST_MONTH_M=$(/bin/date -d '-2 days' "+%b") | |
LAST_MONTH_Y=${LAST_MONTH#*-} | |
# Get the Data from WRT (Assumes ssh keys are in place) | |
DATA=$(/usr/bin/ssh -q router -l root "nvram get traff-$LAST_MONTH") | |
DATA_FILE="/tmp/bw-"$LAST_MONTH".data" | |
# Sample DATA | |
#DATA="1139:972 5872:548 1050:216 439:116 227:213 1815:479 5151:1447 | |
#976:294 6004:531 492:86 334:101 1574:184 879:118 297:85 1014:190 | |
#7706:489 592:122 411:100 107:64 1409:270 1428:205 2887:146 3192:314 | |
#783:85 707:124 1838:243 1780:314 3147:369 2768:399 8230:1304 | |
#[64248:10128]" | |
# Convert data string to Array | |
ARR_DATA=($DATA) | |
# Get the last element of the array | |
ARR_DATA_SUM=${ARR_DATA[${#ARR_DATA[@]}-1]} | |
# remove the last element of the array, since that's the sum data | |
unset ARR_DATA[${#ARR_DATA[@]}-1] | |
# Copy Array into another array | |
ARR_DATA_POINTS=(${ARR_DATA[@]}) | |
# Print the data to be plotted, increment index since we start at zero | |
for i in "${!ARR_DATA_POINTS[@]}"; do | |
# get substring before colon | |
IN_MB=${ARR_DATA_POINTS[$i]%:*} | |
# get substring after colon | |
OUT_MB=${ARR_DATA_POINTS[$i]#*:} | |
# convert megabytes to bytes | |
IN_B=$(($IN_MB * 1024 * 1024)) | |
OUT_B=$(($OUT_MB * 1024 * 1024)) | |
printf "%02d %s %s\n" "$(($i+1))" "$IN_B" "$OUT_B" >> $DATA_FILE | |
done | |
# Remove square brakets from the Sum Array | |
ARR_DATA_SUM=( "${ARR_DATA_SUM[@]/#[/}" ) | |
ARR_DATA_SUM=( "${ARR_DATA_SUM[@]/%]/}" ) | |
# Grab substings for incoming and outgoing values | |
TOT_IN_MB=${ARR_DATA_SUM%:*} | |
TOT_OUT_MB=${ARR_DATA_SUM#*:} | |
# Convert to GB without decimals | |
# didn't use this | |
#TOT_IN_GB=$(($TOT_IN_MB/1024)) | |
#TOT_OUT_GB=$(($TOT_OUT_MB/1024)) | |
# convert to GB with decimals | |
TOT_IN_GB=$(echo "scale=2; $TOT_IN_MB / 1024" | /usr/bin/bc) | |
TOT_OUT_GB=$(echo "scale=2; $TOT_OUT_MB / 1024" | /usr/bin/bc) | |
GNUPLOT_OUTPUT="/tmp/bw-"$LAST_MONTH".png" | |
GNUPLOT_SCRIPT='set title "'$LAST_MONTH_M' '$LAST_MONTH_Y' (Incoming: | |
'$TOT_IN_GB'GB Outgoing: '$TOT_OUT_GB'GB)"\n | |
set terminal png size 800,600 enhanced font "Ariel,10"\n | |
set output "'$GNUPLOT_OUTPUT'"\n | |
set grid\n | |
set style data histogram\n | |
set style histogram cluster gap 1\n | |
set style fill solid border -1 \n | |
set xtic scale 0\n | |
set format y "%.2b%B"\n | |
plot "'$DATA_FILE'" using 2:xtic(1) title "Incoming",""using 3:xtic(1) | |
title "Outgoing"' | |
GNUPLOT_SCRIPT_FILE="/tmp/bw.gnu" | |
# Create gnuplot script | |
echo -e $GNUPLOT_SCRIPT >> $GNUPLOT_SCRIPT_FILE | |
# Generate the graph | |
/usr/bin/gnuplot $GNUPLOT_SCRIPT_FILE | |
if [ $? -ne 0 ]; then | |
echo "GnuPlot Failed, exiting" | |
exit 1 | |
fi | |
# Send email if you want | |
#echo "BW Summary" | /usr/bin/mailx -s "BW Usage for $LAST_MONTH_M | |
$LAST_MONTH_Y" -a $GNUPLOT_OUTPUT | |
if [ $? -ne 0 ]; then | |
echo "Email Failed, exiting" | |
exit 1 | |
fi | |
# clean up | |
/bin/rm $GNUPLOT_SCRIPT_FILE $DATA_FILE | |
/bin/mv $GNUPLOT_OUTPUT ~/Dropbox/bandwidth/ | |
if [ $? -ne 0 ]; then | |
echo "clean-up Failed, exiting" | |
exit 1 | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment