What's new

Capturing current VPN traffic speed/stats

  • SNBForums Code of Conduct

    SNBForums is a community for everyone, no matter what their level of experience.

    Please be tolerant and patient of others, especially newcomers. We are all here to share and learn!

    The rules are simple: Be patient, be nice, be helpful or be gone!

Viktor Jaep

Part of the Furniture
Hi All...

I'm wondering if anyone might know of a method to capture a snapshot of the amount of traffic being uploaded/downloaded in KB/MB for the active VPN Client tunnel via script?

I perused the available options that NVRAM might have to offer pertaining to the VPN clients, but nothing caught my eye. I suppose it would be similar to what you get from the "internet traffic" stat under the status tab, but it would need to be for traffic going over the active VPN tunnel:

1647543047735.png


Is there any way of doing this vanilla without having to download other tools/utilities?

Thank you in advance for your advice... :)
 
Last edited:
OpenVPN provides the management UI for such purposes. It runs as a local process that you can then interrogate via an API to gather such statistics. Many other firmwares (e.g., DD-WRT, FT (FreshTomato)) enable and use it just for this purpose. This is how they update the VPN traffic statistics in their respective GUIs.


For whatever reasons, ASUS just doesn't seem to bother.
 
OpenVPN provides the management UI for such purposes. It runs as a local process that you can then interrogate via an API to gather such statistics. Many other firmwares (e.g., DD-WRT, FT (FreshTomato)) enable and use it just for this purpose. This is how they update the VPN traffic statistics in their respective GUIs.


For whatever reasons, ASUS just doesn't seem to bother.

Ahhhh! Thank you @eibgrad ... this gives me much to dive deeper into! :)
 
you might be able to leverage vnstat to point at the vpn interfaces, it has command line options to monitor for a period of time and provide outputs

Thanks @Jack Yaz ! I'll look into this as well. :)
 
OpenVPN provides the management UI for such purposes. It runs as a local process that you can then interrogate via an API to gather such statistics. Many other firmwares (e.g., DD-WRT, FT (FreshTomato)) enable and use it just for this purpose. This is how they update the VPN traffic statistics in their respective GUIs.
Because it doesn't need it - it's already dumping the status file every 5 seconds or so, in /etc/openvpn/client1/status (for client 1).
 
Because it doesn't need it - it's already dumping the status file every 5 seconds or so, in /etc/openvpn/client1/status (for client 1).

Thanks Eric. I looked at the openvpn commands, and doesn't look like there's much I can pull out from there regarding real-time up/down stats. vnstat got me a little closer with this command: "vnstat -l -i tun13", but it's definitely not instantaneous, and didn't seem to have a good way to extract a snapshot of the realtime values. I think my best bet will be grabbing total bytes up/down from that openvpn client status file since it's static. I'd love to know how vnstat gets those realtime up/down traffic values for a vpn tunnel.
 
I'd love to know how vnstat gets those realtime up/down traffic values for a vpn tunnel.
Probably same way the router does for WAN traffic: read the interface stats every seconds or so, and do the calculations based on the difference over that period of time.
 
Thanks to the amazing wonders of the internet, and much sleuthing on the mysteries of the "awk" command, I was able to pull this off with vnstat afterall. W00t! ;)

Code:
vnstat -i tun12 -tr 2 | awk '$1 == "tx" {print $2,$3} $1 == "rx" {print $2,$3}'

57.64 kbit/s
747.91 kbit/s

Thanks all for your suggestions!
 
Well, I've decided to scrap the vnstat method afterall, because it would require people to have that installed to make use of the command. So I'm looking at using the openvpn status file to get some stats from instead.

But I'm seeing some strange results... I wanted to make sure my logic is right on this... because, I'm seeing varying results based on the amount of KB or MB coming off from the main page's internet traffic status, and the amount of data my internal Google Wifi router is constantly pushing and pulling based on its own stats.

So here's a snapshot of traffic 1 min apart from each other.
Code:
OpenVPN STATISTICS                                                                                                                              
Updated,2022-03-20 11:19:56                                                                                                                    
TUN/TAP read bytes,116112618                                                                                                                    
TUN/TAP write bytes,180382783                                                                                                                  
TCP/UDP read bytes,185799812                                                                                                                    
TCP/UDP write bytes,120711840                                                                                                                  
Auth read bytes,180382835                                                                                                                      
pre-compress bytes,0                                                                                                                            
post-compress bytes,0                                                                                                                          
pre-decompress bytes,0                                                                                                                          
post-decompress bytes,0

(after 60 seconds)
OpenVPN STATISTICS                                                                                                                              
Updated,2022-03-20 11:20:56                                                                                                                    
TUN/TAP read bytes,118762713                                                                                                                    
TUN/TAP write bytes,183205992                                                                                                                  
TCP/UDP read bytes,188713696                                                                                                                    
TCP/UDP write bytes,123466010                                                                                                                  
Auth read bytes,183206044                                                                                                                      
pre-compress bytes,0                                                                                                                            
post-compress bytes,0                                                                                                                          
pre-decompress bytes,0                                                                                                                          
post-decompress bytes,0

My question is, should I only be calculating based off the TUN/TAP stats, or should I be adding the TCP/UDP results into this as well? Do the Auth bytes come into play into calculating this? I haven't found any conclusive documentation on how to calculate an avg bandwidth from these results, but this is my math. Could you please comment if I'm on the right track?

Current TUN/TAP Read Bytes: 118762713 Bytes
60s old TUN/TAP Read Bytes: 116112618 Bytes
Current TUN/TAP Write Bytes: 183205992 Bytes
60s old TUN/TAP Write Bytes: 180382783 Bytes

Difference in Read Bytes over 60 seconds: 2650095 Bytes
Difference in Write Bytes over 60 seconds: 2823209 Bytes

Calculate Read Mb: 2650095 / 125000 = 21.20076 Mb
Calculate Write Mb: 2823209 / 125000 = 22.585672 Mb

Calculate Read Mbps: 21.20076 / 60 = 0.353346 Mbps
Calculate Write Mbps: 22.585672 / 60 = 0.376427 Mbps

The reason why I'm questioning these results is because 95% of my internal traffic should be going over VPN. These figures of 0.3Mbps is not realistic. As mentioned, my Google Wifi mesh network runs completely across the VPN tunnel, and it's constantly pushing 3-5Mbps (thanks to a bunch of Google Nest Cams):

Screenshot_20220320-113147.png

And the main router status constantly shows about an average of 300-600KBps being sent, which is about 3-5Mbps:
Screenshot 2022-03-20 11.43.22.png


These stats are more in range based on what I'm expecting traffic being sent from my Google Wifi Mesh, compared to what I'm calculating based off raw figure for the tunnel alone from the OpenVPN status file.

Am I going about this the right way?
 
Last edited:
Not a coder, but I would be going back to vnstat. :)
 
Not a coder, but I would be going back to vnstat. :)

Sadly, vnstat doesn't seem to be displaying expected bandwidth any better. I'd hate to say that these stats are for entertainment purposes only and not to trust them... I must be doing something wrong.
 
Well... I simulated a large file download, and the stats jumped up. It does work... it just doesn't seem to match my other counters exactly, I guess. It's difficult to determine which source is correct. One would think the openvpn byte counter would be the end-all-be-all.

The one weird discrepancy I noticed in the openvpn status file is that the read/write values seem to be reversed. When I downloaded a large file at high speed, the write values jumped. I was expecting the read values to jump.
 

Similar threads

Latest threads

Sign Up For SNBForums Daily Digest

Get an update of what's new every day delivered to your mailbox. Sign up here!
Top