vnstat setup guide/tutorial

yum -y install vnstat
chown nobody.nobody -R /var/lib/vnstat/

#replace venet0 below with your desired interface
sudo -u nobody vnstat -u -i venet0

#edit: vi /etc/sysconfig/vnstat
#VNSTAT_OPTIONS="-i venet0"
# only use the sed below if you are using venet0 instead of eth0 or replace accordingly
sed -i 's/eth0/venet0/g' /etc/sysconfig/vnstat


 

[root@monitor]# yum install vnstat
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * addons: centos.mirror.netelligent.ca
 * base: centos.mirror.netelligent.ca
 * centosplus: centos.mirror.iweb.ca
 * contrib: centos.mirror.netelligent.ca
 * extras: centos.mirror.netelligent.ca
 * rpmforge: apt.sw.be
 * updates: centos.mirror.netelligent.ca
Setting up Install Process
Resolving Dependencies
--> Running transaction check
---> Package vnstat.i386 0:1.10-1.el5.rf set to be updated
--> Finished Dependency Resolution

Dependencies Resolved

=============================================================================================================================================================
 Package                            Arch                             Version                                      Repository                            Size
=============================================================================================================================================================
Installing:
 vnstat                             i386                             1.10-1.el5.rf                                rpmforge                              65 k

Transaction Summary
=============================================================================================================================================================
Install       1 Package(s)
Upgrade       0 Package(s)

Total download size: 65 k
Is this ok [y/N]: y
Downloading Packages:
vnstat-1.10-1.el5.rf.i386.rpm                                                                                                         |  65 kB     00:00    
Running rpm_check_debug
Running Transaction Test
Finished Transaction Test
Transaction Test Succeeded
Running Transaction
  Installing     : vnstat                                                                                                                                1/1

Installed:
  vnstat.i386 0:1.10-1.el5.rf                                                                                                                               

Complete!
[root@monitor]# vnstat
vnstat       vnstat.cron 
[root@monitor]# vnstat
No database found, nothing to do. Use --help for help.

A new database can be created with the following command:
    vnstat -u -i eth0

Replace 'eth0' with the interface that should be monitored.

The following interfaces are currently available:
    lo venet0
[root@monitor]# vnstat -u -i venet0
Error: Unable to read database "/var/lib/vnstat/venet0".
Info: -> A new database has been created.
[root@monitor]#



-----------
vnstat

                      rx      /      tx      /     total    /   estimated
 ppp0: Not enough data available yet.

Let's check the cron log:
cat /var/log/cron

OK so it is executing the vnstat.cron:
crond[10880]: (nobody) CMD (/usr/sbin/vnstat.cron)

Let's execute the cron script as nobody:
sudo -u nobody /usr/sbin/vnstat.cron
Error: Unable to write database "/var/lib/vnstat/eth0".

Let's check the permissions on that path:
# ls -al /var/lib/vnstat
total 20
drwxr-xr-x  2 nobody nobody 4096 Apr  5 10:40 .
drwxr-xr-x 24 root   root   4096 Apr  5 10:38 ..
-rw-r--r--  1 root   root   2272 Apr  5 10:38 eth0
-rw-r--r--  1 nobody nobody 2272 Apr  5 10:53 .eth0
-rw-r--r--  1 root   root   2272 Apr  5 10:38 venet0

Fix:
chown nobody.nobody -R /var/lib/vnstat/.*

sudo -u nobody /usr/sbin/vnstat.cron
[root@ vnstat]# vnstat

                      rx      /      tx      /     total    /   estimated
 eth0:
       Apr '11     19.70 MiB  /  424.90 MiB  /  444.60 MiB  /    2.91 GiB
         today     19.70 MiB  /  424.90 MiB  /  444.60 MiB  /     967 MiB

The problem is that whenever you add an interface with vnstat as root is that it creates it as root in /var/lib/vnstat
The other solution (not preferable I admit) is to edit the cron script to run as root:

change:
*/5 * * * * nobody /usr/sbin/vnstat.cron

to

*/5 * * * * /usr/sbin/vnstat.cron
 


Tags:

vnstat, tutorialyum, install, chown, var, lib, venet, desired, interface, sudo, edit, vi, etc, sysconfig, vnstat_options, quot, sed, eth, accordingly, yum, plugins, fastestmirror, loading, speeds, cached, hostfile, addons, centos, netelligent, ca, centosplus, iweb, contrib, extras, rpmforge, apt, sw, updates, resolving, dependencies, transaction, rf, updated, dependency, resolution, resolved, repository, installing, summary, upgrade, download, ok, downloading, packages, rpm, kb, rpm_check_debug, succeeded, installed, cron, database, monitored, interfaces, currently, unable, info, rx, tx, estimated, ppp, executing, crond, cmd, usr, sbin, execute, permissions, ls, drwxr, xr, apr, rw, mib, gib, creates, preferable,

Latest Articles

  • Linux Debian Mint Ubuntu Bridge br0 gets random IP
  • redis requirements
  • How to kill a docker swarm
  • docker swarm silly issues
  • isc-dhcp-server dhcpd how to get longer lease
  • nvidia cannot resume from sleep Comm: nvidia-sleep.sh Tainted: Linux Ubuntu Mint Debian
  • zfs and LUKS how to recover in Linux
  • [error] (28)No space left on device: Cannot create SSLMutex Apache Solution Linux CentOS Ubuntu Debian Mint
  • Save money on bandwidth by disabling reflective rpc queries in Linux CentOS RHEL Ubuntu Debian
  • How to access a disk with bad superblock Linux Ubuntu Debian Redhat CentOS ext3 ext4
  • ImageMagick error convert solution - convert-im6.q16: cache resources exhausted
  • PTY allocation request failed on channel 0 solution
  • docker error not supported as upperdir failed to start daemon: error initializing graphdriver: driver not supported
  • Migrated Linux Ubuntu Mint not starting services due to broken /var/run and dbus - Failed to connect to bus: No such file or directory solution
  • qemu-system-x86_64: Initialization of device ide-hd failed: Failed to get "write" lock Is another process using the image [some.iso]? solution
  • File "/usr/local/lib/python3.5/dist-packages/pip/_internal/utils/entrypoints.py", line 12 f"pip{sys.version_info.major}", ^ SyntaxError: invalid syntax python pip3 error solution
  • Linux custom kernel is huge solution
  • TSC_DEADLINE disabled due to Errata; please update microcode to version: 0x3a (or later) - Linux how to upgrade CPU microcode
  • REFUSED unexpected RCODE resolving bind/named error
  • How To Stop DNSMasq from listening on all IPs/Interfaces and allow only localhost