Skip to content

Instantly share code, notes, and snippets.

@bdwilson
Last active May 31, 2020 21:38
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bdwilson/861628d2e5dd0587f668ace15f92819d to your computer and use it in GitHub Desktop.
Save bdwilson/861628d2e5dd0587f668ace15f92819d to your computer and use it in GitHub Desktop.
vnstat configuration for Ubiquiti EdgeOS Edgerouter; these files are stored in /config/vnstat. Based on this (https://snippets.khromov.se/vnstat-on-edgerouter-historical-bandwidth-monitoring-and-graphical-dashboard-tutorial/) however the scripts below store the data in memory and only back them up nightly (to save on flash writes)
cp -Rp /var/log/vnstat /config
23 11 * * * /config/vnstat/backup.sh
*/5 * * * * /config/vnstat/vnstat-update.sh
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>EdgeRouter Bandwidth Dashboard</title>
<style>
body {
text-align: center;
}
</style>
</head>
<body>
<h3>Overview</h3>
<img src="vnstat-summary.png">
<h3>Monthly</h3>
<img src="vnstat-monthly.png">
<h3>Daily</h4>
<img src="vnstat-daily.png">
<h3>Top 10</h3>
<img src="vnstat-top.png">
</body>
</html>
mkdir -p /var/log/vnstat
cp /config/vnstat/vnstat.init /etc/init.d/vnstat
cp /config/vnstat/vnstat.conf /etc
cp -Rp /config/vnstat/eth0 /var/log/vnstat
service vnstat restart
if [ ! -L /var/www/htdocs/media/stat ]; then
ln -s /var/log/stat /var/www/htdocs/media
fi
/config/vnstat/vnstat-update.sh
#!/bin/sh
mkdir -p /var/log/stat
if [ ! -f /var/log/stat/index.html ]; then
cp /config/vnstat/index.html /var/log/stat
fi
vnstati -vs -i eth0 -o /var/log/stat/vnstat-summary.png
vnstati -m -i eth0 -o /var/log/stat/vnstat-monthly.png
vnstati -d -i eth0 -o /var/log/stat/vnstat-daily.png
vnstati -t -i eth0 -o /var/log/stat/vnstat-top.png
# vnStat 1.11 config file
##
# default interface
Interface "eth0"
# location of the database directory
DatabaseDir "/var/log/vnstat"
# locale (LC_ALL) ("-" = use system locale)
Locale "-"
# on which day should months change
MonthRotate 19
# date output formats for -d, -m, -t and -w
# see 'man date' for control codes
DayFormat "%x"
MonthFormat "%b '%y"
TopFormat "%x"
# characters used for visuals
RXCharacter "%"
TXCharacter ":"
RXHourCharacter "r"
TXHourCharacter "t"
# how units are prefixed when traffic is shown
# 0 = IEC standard prefixes (KiB/MiB/GiB/TiB)
# 1 = old style binary prefixes (KB/MB/GB/TB)
UnitMode 0
# output style
# 0 = minimal & narrow, 1 = bar column visible
# 2 = same as 1 except rate in summary and weekly
# 3 = rate column visible
OutputStyle 3
# used rate unit (0 = bytes, 1 = bits)
RateUnit 1
# maximum bandwidth (Mbit) for all interfaces, 0 = disable feature
# (unless interface specific limit is given)
MaxBandwidth 1000
# interface specific limits
# example 8Mbit limit for eth0 (remove # to activate):
#MaxBWeth0 8
# how many seconds should sampling for -tr take by default
Sampletime 5
# default query mode
# 0 = normal, 1 = days, 2 = months, 3 = top10
# 4 = dumpdb, 5 = short, 6 = weeks, 7 = hours
QueryMode 0
# filesystem disk space check (1 = enabled, 0 = disabled)
CheckDiskSpace 1
# database file locking (1 = enabled, 0 = disabled)
UseFileLocking 1
# how much the boot time can variate between updates (seconds)
BootVariation 15
# log days without traffic to daily list (1 = enabled, 0 = disabled)
TrafficlessDays 1
# vnstatd
##
# how often (in seconds) interface data is updated
UpdateInterval 30
# how often (in seconds) interface status changes are checked
PollInterval 5
# how often (in minutes) data is saved to file
SaveInterval 60
# how often (in minutes) data is saved when all interface are offline
OfflineSaveInterval 60
# force data save when interface status changes (1 = enabled, 0 = disabled)
SaveOnStatusChange 0
# enable / disable logging (0 = disabled, 1 = logfile, 2 = syslog)
UseLogging 2
# file used for logging if UseLogging is set to 1
LogFile "/var/log/vnstat.log"
# file used as daemon pid / lock file
PidFile "/var/run/vnstat.pid"
# vnstati
##
# title timestamp format
HeaderFormat "%x %H:%M"
# show hours with rate (1 = enabled, 0 = disabled)
HourlyRate 1
# show rate in summary (1 = enabled, 0 = disabled)
SummaryRate 1
# layout of summary (1 = with monthly, 0 = without monthly)
SummaryLayout 1
# transparent background (1 = enabled, 0 = disabled)
TransparentBg 0
# image colors
CBackground "FFFFFF"
CEdge "AEAEAE"
CHeader "606060"
CHeaderTitle "FFFFFF"
CHeaderDate "FFFFFF"
CText "000000"
CLine "B0B0B0"
CLineL "-"
CRx "92CF00"
CTx "606060"
CRxD "-"
CTxD "-"
#! /bin/sh
### BEGIN INIT INFO
# Provides: vnstat
# Required-Start: $local_fs $remote_fs $network
# Required-Stop: $local_fs $remote_fs $network
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: lightweight network traffic monitor
### END INIT INFO
PATH=/usr/sbin:/usr/bin:/sbin:/bin
DESC="vnStat daemon"
NAME=vnstatd
DAEMON=/usr/sbin/$NAME
DAEMON_ARGS="-d"
PIDFILE=/var/run/vnstat.pid
SCRIPTNAME=/etc/init.d/vnstat
. /lib/lsb/init-functions
# Exit if the package is not installed
[ -x "$DAEMON" ] || exit 0
case "$1" in
start)
if [ ! -d "/var/log/vnstat" ]; then
mkdir /var/log/vnstat
/config/vnstat/restore.sh
#vnstat -u -i eth0
fi
log_daemon_msg "Starting $DESC" "$NAME"
start-stop-daemon --start --quiet --oknodo --pidfile $PIDFILE --exec $DAEMON -- \
$DAEMON_ARGS
log_end_msg $?
;;
stop)
/config/vnstat/backup.sh
log_daemon_msg "Stopping $DESC" "$NAME"
start-stop-daemon --stop --quiet --oknodo --retry=TERM/15/KILL/5 --pidfile $PIDFILE \
--name $NAME
log_end_msg $?
rm -f $PIDFILE
;;
status)
pidofproc -p $PIDFILE $DAEMON >/dev/null
status=$?
if [ $status -eq 0 ]; then
log_success_msg "$DESC is running"
else
log_failure_msg "$DESC is not running"
fi
exit $status
;;
reload|force-reload)
log_daemon_msg "Reloading $DESC configuration..."
start-stop-daemon --stop --signal 1 --quiet --pidfile $PIDFILE --name $NAME
log_end_msg 0
;;
restart)
$0 stop
sleep 1
$0 start
;;
*)
echo "Usage: $SCRIPTNAME {start|stop|restart|reload|force-reload|status}"
exit 1
;;
esac
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment