Pages

Monday, 8 July 2013

Running vnstat in OS X

vnstat can be used to monitor network bandwidth usage easily.
1. Install vnstat via mac ports
sudo port install vnstat
2. Assuming the default interface to be monitored is en0, open the vnstat.conf file under /opt/local/etc/vnstat.conf and update the parts
# default interface
Interface "en0"

# location of the database directory
DatabaseDir "/opt/local/var/db/vnstat"
3. Run the below command which will start monitoring en0
sudo vnstat -u -i en0
4. Now to run this as a service so that it starts up automatically at boot we can create a user daemon. In case of GNU/Linux, such scripts are present in /etc/init.d where as in OS X it's in /Library/LaunchDaemons.
sudo vim /Library/LaunchDaemons/local.vnstat.plist
Use the file content for local.vnstat.plist file as given below
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple/DTD PLIST 1.0//EN"
"http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>Label</key>
    <string>local.vnstat</string>
    <key>ProgramArguments</key>
    <array>
        <string>/opt/local/bin/vnstat</string>
        <string>-u</string>
    </array>
    <key>StartInterval</key>
    <integer>300</integer>
</dict>
</plist>
5. Change owner and group to root and wheel respectively.
sudo chown root:wheel /Library/LaunchDaemons/local.vnstat.plist
6. Use lauchctl to start the daemon
sudo launchctl load /Library/LaunchDaemons/local.vnstat.plist
To stop the daemon use unload.
sudo launchctl unload /Library/LaunchDaemons/local.vnstat.plist
Check the usage with
vnstat #lists complete usage
vnstat -d #lists daily usage
The 'estimated' data given is a prediction that vnstat calculates in realtime looking at the usage, hours etc.

To check whether the daemon is running use
sudo launchctl list | grep vnstat