Skip to content

Instantly share code, notes, and snippets.

@smj10j
Last active May 20, 2017 08:40
Show Gist options
  • Save smj10j/210f79b3ba58f1139c77da9133916d42 to your computer and use it in GitHub Desktop.
Save smj10j/210f79b3ba58f1139c77da9133916d42 to your computer and use it in GitHub Desktop.
Script to run with cron to update vnstati output
#!/usr/bin/env bash
# Fail on any error
set -e
# Debug
# set -x
###########################################################################
####### Set below as appropropriate for your environment ##################
####### IFACE is the interface (use `ifconfig` to get the name) ###########
####### WEB_DIR is where vnstat files will be output ######################
####### USER is the user vnstat will be run as ############################
###########################################################################
IFACE='en0'
WEB_DIR='/path/to/my/webdir'
USER='_www'
PATH=/usr/local/bin:$PATH
CWD=$(pwd)
#########################################################
function cleanup {
cd "$CWD"
exit 1
}
trap 'cleanup' SIGHUP SIGINT SIGTERM ERR
#########################################################
sudo -u ${USER} bash <<EOF
# Fail on any error
set -e
# Debug
# set -x
if [[ ! "\$(whoami)" == "${USER}" ]]; then
echo "Must be run as ${USER}, you are \$(whoami)"
exit 1
fi
cd "${WEB_DIR}"
vnstati -i ${IFACE} -vs -o vnstat.png
vnstati -i ${IFACE} -d -o vnstat_d.png
vnstati -i ${IFACE} -m -o vnstat_m.png
vnstat --xml > vnstat.xml
echo "vnstati files successfully generated in ${WEB_DIR}"
EOF
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment