Last active
July 6, 2025 12:09
-
-
Save h4k1m0u/b75c39029c4464d9cbae844f08e0f930 to your computer and use it in GitHub Desktop.
Plot daily data consumption calculated by vnstat using gnuplot's bar plot
This file contains hidden or 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
set style fill solid 0.5; | |
set xtics rotate | |
set xlabel "Day"; | |
set ylabel "MiB"; | |
# data file passed as a command-line argument (variable ARG1) | |
# 0: pseudo-column for indexes starting from zero = x-axis | |
# 2: y-axis=2nd column in file | |
# xticklabels(1): ticks on x-axis from 1st column in file | |
plot ARG1 using 0:2:xticlabels(1) title "Daily data usage in MiB" with boxes; | |
# wait till <Enter> key is hit | |
pause -1 |
This file contains hidden or 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
#!/usr/bin/env bash | |
set -e | |
if [ "$#" -ne 1 ]; then | |
echo "USAGE: $0 NETWORK_INTERFACE" | |
exit 1 | |
fi | |
# jq used only for indenting output | |
data_file="/tmp/data.json" | |
n_days_max="30" | |
vnstat --json d "$n_days_max" | jq '.' > "$data_file" | |
# first network interface not always the one used | |
interface="$1" | |
traffic_file="/tmp/traffic.json" | |
jq ".interfaces | map(select(.name == \"$interface\")).[0] | .traffic.day" "$data_file" > "$traffic_file" | |
# handles case when vnstat was just installed (not enough data) | |
n_days=$(jq 'length' "$traffic_file") | |
txt_file="/tmp/data.txt" | |
truncate -s 0 "$txt_file" | |
for (( i = 0; i < "$n_days"; i++ )); do | |
# get date (x-axis) | |
year=$(jq ".[$i].date.year" "$traffic_file") | |
month=$(jq ".[$i].date.month" "$traffic_file") | |
day=$(jq ".[$i].date.day" "$traffic_file") | |
# pad days & months with zero | |
day=$(printf "%02u" "$day") | |
month=$(printf "%02u" "$month") | |
date_day="$year-$month-$day" | |
# convert bytes to MiB & round-up with printf (y-axis) | |
rx=$(jq ".[$i].rx" "$traffic_file") | |
rx_mib=$(echo "scale=3; $rx / 2^20" | bc) | |
rx_mib=$(printf "%.2f" "$rx_mib") | |
echo "$date_day $rx_mib" >> "$txt_file" | |
done | |
# ticks labels taken from 1st column (i.e. dates) | |
gnuplot -c "plot.gnuplot" "$txt_file" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment