Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
Last revisionBoth sides next revision
docs:user-guide:services:vnstat [2018/02/18 16:56] – link to usb storage page updated after wiki merge tmomasdocs:guide-user:services:network_monitoring:vnstat [2019/08/26 11:32] – tag optimization, formatting optimization vgaetera
Line 1: Line 1:
-====== vnStat ======+====== Network Traffic Monitor with vnStat ======
 vnStat is a console-based network traffic monitor for Linux and BSD that keeps a log of network traffic for the selected interface(s). It uses the network interface statistics provided by the kernel as information source. This means that vnStat won't actually be sniffing any traffic and also ensures light use of system resources. [[http://humdi.net/vnstat/|As it says on their website]]  vnStat is a console-based network traffic monitor for Linux and BSD that keeps a log of network traffic for the selected interface(s). It uses the network interface statistics provided by the kernel as information source. This means that vnStat won't actually be sniffing any traffic and also ensures light use of system resources. [[http://humdi.net/vnstat/|As it says on their website]] 
-===== Installing ===== 
  
 +===== Installing =====
 ==== Base ==== ==== Base ====
-<code>opkg update +<code bash> 
-opkg install vnstat</code>+opkg update 
 +opkg install vnstat 
 +</code>
  
 ==== LuCI Webgui Integration (optional) ==== ==== LuCI Webgui Integration (optional) ====
-<code>opkg update +<code bash> 
-opkg install vnstati luci-app-vnstat</code> +opkg update 
 +opkg install vnstati luci-app-vnstat 
 +</code>
  
 If you want language translation for the LuCI Webgui Integration then install the following: If you want language translation for the LuCI Webgui Integration then install the following:
-<code>opkg update + 
-opkg install luci-i18n-vnstat-(YOUR_COUNTRY_CODE)</code>+<code bash> 
 +opkg update 
 +opkg install luci-i18n-vnstat-(YOUR_COUNTRY_CODE) 
 +</code>
  
 If you dont know which language codes are available you can search it with the following command: If you dont know which language codes are available you can search it with the following command:
  
-<code>opkg list | grep luci-i18n-vnstat-*</code>+<code bash> 
 +opkg list | grep luci-i18n-vnstat-* 
 +</code>
  
 An example to Install German Translation for LuCI Webgui: An example to Install German Translation for LuCI Webgui:
-<code>opkg update + 
-opkg install luci-i18n-vnstat-de</code>+<code bash> 
 +opkg update 
 +opkg install luci-i18n-vnstat-de 
 +</code> 
 ===== Configuring ===== ===== Configuring =====
 The only configuring it really needs is to tell it what interface(s) to monitor, and some method of updating the database such as a cronjob. You might want to backup your database file. The vnstati package comes with a 'restore' and init.d script that downloads the backup from a webserver upon reboot. Its up to you to chose how to backup/restore the data( via HTTP/FTP/SSH/ETC) The only configuring it really needs is to tell it what interface(s) to monitor, and some method of updating the database such as a cronjob. You might want to backup your database file. The vnstati package comes with a 'restore' and init.d script that downloads the backup from a webserver upon reboot. Its up to you to chose how to backup/restore the data( via HTTP/FTP/SSH/ETC)
Line 32: Line 44:
  
 First, you have to find out which is your WAN Interface: First, you have to find out which is your WAN Interface:
 +
 <code bash> <code bash>
 . /lib/functions/network.sh; if network_get_device if_wan wan; then . /lib/functions/network.sh; if network_get_device if_wan wan; then
Line 38: Line 51:
  
 :!: The interface must be active for the above command to work.  See ticket [[https://dev.openwrt.org/ticket/19116|19116]]. :!: The interface must be active for the above command to work.  See ticket [[https://dev.openwrt.org/ticket/19116|19116]].
-\\ +
-\\ +
-\\+
 If you have an already activated WAN connection, you will get the following output, for example: If you have an already activated WAN connection, you will get the following output, for example:
-<code>Your WAN Interface is: pppoe-wan</code> 
-\\ 
-Now edit __/etc/vnstat.conf__ with your favourite editor and change the following two Lines:\\ 
  
-<code>Interface "pppoe-wan" +Your WAN Interface is: pppoe-wan. 
-MaxBandwidth 1000</code>+Now edit __/etc/vnstat.conf__ with your favourite editor and change the following two Lines: 
 + 
 +<code bash> 
 +Interface "pppoe-wan" 
 +MaxBandwidth 1000 
 +</code> 
 "Interface" is your WAN Interface you found out above and "MaxBandwidth" is the max possible bandwidth (in mbit) speed.\\ "Interface" is your WAN Interface you found out above and "MaxBandwidth" is the max possible bandwidth (in mbit) speed.\\
  
Line 53: Line 67:
  
 :!: MaxBandwidth means: How fast is your WAN Interface at most. __MaxBandwidth 1000 = 1000 Mbit__ and __MaxBandwidth 100 = 100 Mbit__ ! It has nothing to do with your Internet speed ! :!: MaxBandwidth means: How fast is your WAN Interface at most. __MaxBandwidth 1000 = 1000 Mbit__ and __MaxBandwidth 100 = 100 Mbit__ ! It has nothing to do with your Internet speed !
-\\ +
-\\ +
-\\+
 Then edit or create __/etc/config/vnstat__ and change (or add) the following lines: Then edit or create __/etc/config/vnstat__ and change (or add) the following lines:
-<code>config vnstat + 
-        list interface 'pppoe-wan'</code>+<code bash> 
 +config vnstat 
 +        list interface 'pppoe-wan' 
 +</code>
                  
 **Please Note:** "pppoe-wan" is your WAN Interface you found out above ! **Please Note:** "pppoe-wan" is your WAN Interface you found out above !
-\\ +
-\\ +
-\\+
 Now you have to create the Database with this command: Now you have to create the Database with this command:
  
-<code>vnstat -u -i pppoe-wan</code>+<code bash> 
 +vnstat -u -i pppoe-wan 
 +</code>
  
-**Hint:** vnStat normally uses "IEC prefixes" (MiB, GiB and so on). If you want old binary prefixes (MB, GB) change the following in __/etc/vnstat.conf__ :+**Hint:** vnStat normally uses "IEC prefixes" (MiB, GiB and so on). If you want old binary prefixes (MB, GB) change the following in __/etc/vnstat.conf__ : ''UnitMode 1''.
  
-<code>UnitMode 1</code> 
 ==== Database Updating ==== ==== Database Updating ====
 | {{:meta:icons:tango:dialog-information.png?nolink}} | **This step is required for vnStat to function.** The package doesn't setup database updating at all. Its up to you to configure when vnStat will update. Use the daemon or a cron job. | | {{:meta:icons:tango:dialog-information.png?nolink}} | **This step is required for vnStat to function.** The package doesn't setup database updating at all. Its up to you to configure when vnStat will update. Use the daemon or a cron job. |
-=== Using included daemon: === 
  
-Edit __/etc/vnstat.conf__ and search for "UpdateInterval". Change "UpdateInterval" to the following: +=== Using included daemon: === 
-<code>UpdateInterval 300</code>+Edit __/etc/vnstat.conf__ and search for "UpdateInterval". Change "UpdateInterval" to the following: ''UpdateInterval 300''.
  
 UpdateInterval tells the daemon to update the Database every 300 seconds (5 minutes). If you want another Interval you can change it for your needs. UpdateInterval tells the daemon to update the Database every 300 seconds (5 minutes). If you want another Interval you can change it for your needs.
  
 Run these commands to enable the daemon. This will also auto start the daemon if you reboot your device: Run these commands to enable the daemon. This will also auto start the daemon if you reboot your device:
-<code>/etc/init.d/vnstat enable+<code bash>/etc/init.d/vnstat enable
 /etc/init.d/vnstat start</code> /etc/init.d/vnstat start</code>
  
 | {{:meta:icons:tango:dialog-error.png?nolink}} | **''/etc/config/vnstat''** is the database restore config, __not the vnstatd config.__ vnstatd config is also located at **''/etc/vnstat.conf''** | | {{:meta:icons:tango:dialog-error.png?nolink}} | **''/etc/config/vnstat''** is the database restore config, __not the vnstatd config.__ vnstatd config is also located at **''/etc/vnstat.conf''** |
 This same init.d script will automatically download a database backup if you configured ''/etc/config/vnstat'' corrrectly. Useful for recovering db after a router reboot. Down side is that there is no implemented upload method using the uci config. You will need to write a script that cron will run to do all the uploading, so why not use the same protocol for downloading too? I suggest rsync, ftp, or scp. This same init.d script will automatically download a database backup if you configured ''/etc/config/vnstat'' corrrectly. Useful for recovering db after a router reboot. Down side is that there is no implemented upload method using the uci config. You will need to write a script that cron will run to do all the uploading, so why not use the same protocol for downloading too? I suggest rsync, ftp, or scp.
 +
 === Using a cronjob: === === Using a cronjob: ===
-Add to crontab: +Update the crontab: 
-<code>echo "*/5 * * * * vnstat -u" >> /etc/crontabs/root</code> + 
-Restart cron with: +<code bash> 
-<code>/etc/init.d/cron restart</code>+echo "*/5 * * * * vnstat -u" >> /etc/crontabs/root 
 +/etc/init.d/cron restart 
 +</code>
  
 | {{:meta:icons:tango:dialog-error.png?nolink}} | **I don't recommend using crontab to update the vnStat Database.**\\ Because if you update the Database with cron you will get weird statistics like: 16777216.00 TiB in one day.\\ \\ See: https://bugzilla.redhat.com/show_bug.cgi?id=711383 | | {{:meta:icons:tango:dialog-error.png?nolink}} | **I don't recommend using crontab to update the vnStat Database.**\\ Because if you update the Database with cron you will get weird statistics like: 16777216.00 TiB in one day.\\ \\ See: https://bugzilla.redhat.com/show_bug.cgi?id=711383 |
 +
 ==== Image Generation  ==== ==== Image Generation  ====
 You can install webif and it will generate images. But thats not lightweight so RealOpty developed scripts based off webif code that will generate the images without webif. You can install webif and it will generate images. But thats not lightweight so RealOpty developed scripts based off webif code that will generate the images without webif.
Line 100: Line 117:
 I always output the images to the tmpfs so it dont always write to flash. I always output the images to the tmpfs so it dont always write to flash.
  
-<code bash>+<code bash>
 #!/bin/sh #!/bin/sh
 # vnstati image generation script. # vnstati image generation script.
Line 134: Line 151:
  
 exit 1 exit 1
-</code> |+</code>
  
 ==== Sample HTML ==== ==== Sample HTML ====
-<code html>+<code html>
 <META HTTP-EQUIV="refresh" CONTENT="300"> <META HTTP-EQUIV="refresh" CONTENT="300">
 <html> <html>
Line 170: Line 187:
   </body>   </body>
 </html> </html>
- +</code>
-</code> |+
  
 ==== Persistent stats ==== ==== Persistent stats ====
 vnStat stores stats to /var/lib/vnstat by default and information will not persist across restarts. This means that one might want to relocate the database directory to other forms of persistent storage like your device's flash or external thumb drives. vnStat stores stats to /var/lib/vnstat by default and information will not persist across restarts. This means that one might want to relocate the database directory to other forms of persistent storage like your device's flash or external thumb drives.
  
-By default, vnStat is configured to write to the directory in volatile memory every 30 minutes. Both the directory and the interval can be adjusted in vnStat's configuration. However, keep in mind that frequent writes to flash memory will deteriorate and potentially damage flash memory. Changing the directory to write to flash memory without changing the interval will result in around 17500 write operations, a number that could potentially cause problems.+By default, vnStat is configured to write to the directory in volatile memory every 30 minutes. Both the directory and the interval can be adjusted in vnStat's configuration. However, keep in mind that frequent writes to flash memory will deteriorate and potentially damage flash memory. Changing the directory to write to flash memory without changing the interval will result in around 17500 write operations each year, a number that could potentially cause problems.
  
 Additionally, the database may not persist across firmware flashes. Additionally, the database may not persist across firmware flashes.
  
 === Method 1 === === Method 1 ===
-To store the database on the thumb drive, ensure that [[docs:user-guide:storage:usb-drives]] is working. Then, edit DatabaseDir in /etc/vnstat.conf to point to your flash drive, you may also want to modify SaveInterval to a larger value (the default 30min is still a good value) to minimise writes to flash.+To store the database on the thumb drive, ensure that [[docs:guide-user:storage:usb-drives]] is working. Then, edit DatabaseDir in /etc/vnstat.conf to point to your flash drive, you may also want to modify SaveInterval to a larger value (the default 30min is still a good value) to minimise writes to flash.
    
 === Method 2 ===  === Method 2 === 
 This method automatically backs up the vnStat database to flash memory as **/etc/vnstat_backup.tar.gz** on router shutdown and restores it on startup. Note that this cannot work when the router unexpectedly loses power (unplugging, turning off a hardware power switch, power outage). You can use a cronjob to backup in regular intervals while the router is running. This method automatically backs up the vnStat database to flash memory as **/etc/vnstat_backup.tar.gz** on router shutdown and restores it on startup. Note that this cannot work when the router unexpectedly loses power (unplugging, turning off a hardware power switch, power outage). You can use a cronjob to backup in regular intervals while the router is running.
 +
 == Script == == Script ==
-The script goes into /etc/init.d/vnstat_backup +<code bash> 
-| <code bash>+cat << "EOF_cat"/etc/init.d/vnstat_backup
 #!/bin/sh /etc/rc.common #!/bin/sh /etc/rc.common
  
Line 241: Line 258:
  restore_database  restore_database
 } }
-</code>+EOF_cat 
- +chmod +x /etc/init.d/vnstat_backup 
-Then do: +/etc/init.d/vnstat_backup enable 
- +</code>
-<code>chmod +x /etc/init.d/vnstat_backup +
-/etc/init.d/vnstat_backup enable</code>+
  
 And for good measure, create an initial backup: And for good measure, create an initial backup:
  
-<code>/etc/init.d/vnstat_backup backup</code>+<code bash> 
 +/etc/init.d/vnstat_backup backup 
 +</code>
  
 == Add for cron and for init == == Add for cron and for init ==
 The below commands will add a cronjob entry that triggers a backup every 6 hours every day, if necessary adjust to an interval you feel comfortable with: The below commands will add a cronjob entry that triggers a backup every 6 hours every day, if necessary adjust to an interval you feel comfortable with:
-<code>echo "*/6 * * * /etc/init.d/vnstat_backup backup" >> /etc/crontabs/root + 
-/etc/init.d/cron restart</code>+<code bash> 
 +echo "*/6 * * * /etc/init.d/vnstat_backup backup" >> /etc/crontabs/root 
 +/etc/init.d/cron restart 
 +</code> 
  • Last modified: 2021/04/08 01:02
  • by vgaetera