Skip to content

vnstati

Console-based network traffic monitor that keeps statistics of network usage


Installation

Install vnstat

  • gd useflag gives us vnstati binary
echo "net-analyzer/vnstat gd" > /etc/portage/package.use/vnstat
emerge --ask vnstat

Server

Edit 'vnstati-generate.sh' - Set active network interfrace under interface, use below command for print active nic

ip addr | awk '/state UP/ {print $2}' | sed 's/.$//'

Add below line to cronie: (will execute script every 5 minutes)

*/5 * * * * /path/to/www/vnstati-generate.sh

Create a folder in your webroot and copy index.html into it

mkdir -p /path/to/www/vnstati

Create vnstat.html

cat << "EOF" > vnstat.html
<!DOCTYPE html>
<html lang="sv">
<head>
    <meta charset="UTF-8">
    <title>Nr1 - Bandwidth Dashboard</title>

    <style>
        body {
            background-color: #23252D;
            color: #E6E6E6;
            font-family: Arial, sans-serif;
            margin: 0;
            padding: 0;
            text-align: center;
        }

        h1 {
            background-color: #332F41;
            margin: 0;
            padding: 20px;
            font-size: 28px;
            color: #FFFFFF;
            letter-spacing: 1px;
        }

        h3 {
            background-color: #332F41;
            padding: 10px;
            margin: 40px auto 10px;
            width: 60%;
            border-radius: 6px;
            color: #FFFFFF;
        }

        .card {
            background-color: #2B2D35;
            padding: 20px;
            margin: 0 auto 40px;
            width: 60%;
            border-radius: 8px;
            box-shadow: 0 0 10px rgba(0,0,0,0.4);
        }

        img {
            width: 100%;
            border-radius: 4px;
        }
    </style>
</head>

<body>

<h1>Bandwidth Dashboard</h1>

<h3>Overview</h3>
<div class="card">
    <img src="/path/to/www/vnstat-summary.png">
</div>

<h3>Five minutes</h3>
<div class="card">
    <img src="/path/to/www/vnstat-fiveminutes.png">
</div>

<h3>Hourly</h3>
<div class="card">
    <img src="/path/to/www/vnstat-hourly.png">
</div>

<h3>Hourly  Graph</h3>
<div class="card">
    <img src="/path/to/www/vnstat-hourly-graph.png">
</div>

<h3>Daily</h3>
<div class="card">
    <img src="/path/to/www/vnstat-daily.png">
</div>

<h3>Monthly</h3>
<div class="card">
    <img src="/path/to/www/vnstat-monthly.png">
</div>

<h3>Yearly</h3>
<div class="card">
    <img src="/path/to/www/vnstat-yearly.png">
</div>

</body>
</html>
EOF

Modify configuration file

cat <<"EOF" > /etc/vnstat.conf
Interface "eno1"
DatabaseDir "/var/lib/vnstat"
Locale "-"

DayFormat    "%Y-%m-%d"
MonthFormat  "%Y-%m"
TopFormat    "%Y-%m-%d"

RXCharacter       "%"
TXCharacter       ":"
RXHourCharacter   "r"
TXHourCharacter   "t"

UnitMode 2

RateUnit 0 
RateUnitMode 1

OutputStyle 3

DefaultDecimals 2
HourlyDecimals 1

HourlySectionStyle 2

Sampletime 5
QueryMode 0

List5Mins      24
ListHours      24
ListDays       30
ListMonths     12
ListYears       0
ListTop        10

DaemonUser "vnstat"
DaemonGroup "vnstat"

BandwidthDetection 1
MaxBandwidth 1024
MaxBWethnone 8
5MinuteHours   48
HourlyDays      4
DailyDays      62
MonthlyMonths  25
YearlyYears    -1
TopDayEntries  20
UpdateInterval 20
PollInterval 5
SaveInterval 5
OfflineSaveInterval 30
MonthRotate 1
MonthRotateAffectsYears 0
CheckDiskSpace 1
BootVariation 15
TrafficlessEntries 1
TimeSyncWait 5
BandwidthDetectionInterval 5
SaveOnStatusChange 1
UseLogging 2
CreateDirs 1
UpdateFileOwner 1
LogFile "/var/log/vnstat.log"
PidFile "/run/vnstat/vnstat.pid"
64bitInterfaceCounters -2
DatabaseWriteAheadLogging 0
DatabaseSynchronous -1

HeaderFormat "%Y-%m-%d %H:%M"
HourlyRate 1
SummaryRate 1
TransparentBg 0

Background      "23252D" # Same as the page background
CEdge           "332F41" # Same as the HTML header
CHeader         "3A2A55" # Purple-toned header that matches the colors of the illustration
CHeaderTitle    "E8D9FF" # Light magenta/purple text (good visibility)
CHeaderDate     "E8D9FF" # Same as above
CText           "FFFFFF" # White text for maximum readability
CLine           "7A5BAA" # Purple lines, subtle but clear
CLineL          "-"      # Not in use
CRx             "C060FF" # Magenta-purple RX (matches the image)
CTx             "6A2CFF" # Purple-blue TX (matches the image)
CRxD            "-"      # Not in use
CTxD            "-"      # Not in use
EOF

Vnstati Generate Pictures

cat <<"EOF" > vnstati-generate.sh
#!/bin/bash

INTERFACE="eno1"
VNSTATI="/usr/bin/vnstati"
FILENAME="vnstat"
PATH="."

check_requirements() {
        for requirements in crontab vnstat vnstati vnstatd; do
                hash "$requirements" &>/dev/null ||
                        echo "$requirements is required to be installed, exiting..."
                exit 1
        done
}

check_paths() {
        DIR="/tmp/vnstat"
        if [[ ! -d "$DIR" ]]; then
                echo "Creating /tmp/vnstat"
                mkdir -vp /tmp/vnstat
        fi

}

check_permissions() {
        vnstatOwner=$(/usr/bin/stat -c '%U:%G' /tmp/vnstat)
        if [[ $vnstatOwner != "vnstat:vnstat" ]]; then
                chown -R vnstat:vnstat /tmp/vnstat/
        fi
}

check_root() {
    if [[ ${EUID} -ne 0 ]]; then
        printf "%s\n" "$0: internal error -- root privileges is required for this script"
        exit
    fi
 
}

# - NOTICE FOR CRONIE FUNCTION
# -
# Lets skip this part for public script until later, lazy cow
# -
#check_cronie() {
#    if [[ $(crontab -l | grep -iq "vnstat"; echo $?) == 1 ]]; then
#        set -f
#        echo $(crontab -l ; echo '*/5 * * * * /path/vnstati-generate.sh') | crontab -
#       set +f
#     fi
#}

#check_requirements
check_paths
check_permissions
check_root

##########################################################
# yay, now vnstati will do the real real job          ####
##########################################################

${VNSTATI} -s -i "$INTERFACE" -o "${PATH}/${FILENAME}-summary.png"      
${VNSTATI} -m -i "$INTERFACE" -o "${PATH}/${FILENAME}-top-days.png"      
${VNSTATI} -5 -i "$INTERFACE" -o "${PATH}/${FILENAME}-fiveminutes.png"   
${VNSTATI} -h -i "$INTERFACE" -o "${PATH}/${FILENAME}-hourly.png"
${VNSTATI} -hg -i "$INTERFACE" -o "${PATH}/${FILENAME}-hourly-graph.png"
${VNSTATI} -d -i "$INTERFACE" -o "${PATH}/${FILENAME}-daily.png"       
${VNSTATI} -m -i "$INTERFACE" -o "${PATH}/${FILENAME}-monthly.png"     
${VNSTATI} -y -i "$INTERFACE" -o "${PATH}/${FILENAME}-yearly.png"

Create vnstatd for /etc/vnstat.conf

cat << "EOF" > vnstatd
# /etc/conf.d/vnstatd: config file for /etc/init.d/vnstatd

VNSTATD_CONFIGFILE="/etc/vnstat.conf"
VNSTATD_PIDFILE="/run/vnstat/vnstatd.pid"
VNSTATD_OPTS=""
SSD_STARTWAIT=500
EOF

Make the file executable and run it

chmod +x ./vnstati-generate.sh
bash ./vnstati-generate.sh

Usage

Show 5 minutes

vnstat --fiveminutes [limit]                                             

Show hours

vnstat --hours [limit]                                                   

Show hours graph

vnstat --hoursgraph                                                      

Show days

vnstat --days [limit]                                                    

Show months

vnstat --months [limit]                                                  

Show years

vnstat --years [limit]                                                   

Show top days

vnstat --top [limit]                                                     

Set list begin date

vnstat --begin <date>                                                    

Set list end date

vnstat --end <date>                                                      

Show 95th percentile

vnstat --95th                                                        

Show simple parsable format

vnstat --oneline [mode]                                              

Show database in json format

vnstat --json [mode] [limit]                                        

Show database in xml format

vnstat --xml [mode] [limit]                                          

Alert if limit is exceeded

vnstat --alert <output> <exit> <type> <condition> <limit> <unit>

Calculate traffic

vnstat --traffic [time]        

Show transfer rate in real time

vnstat --live [mode]          

Select interface (default: eno1)

vnstat --iface <interface>