Skip to content

Instantly share code, notes, and snippets.

@FanchenBao
Last active April 6, 2022 23:45
Show Gist options
  • Save FanchenBao/aa20a70ad4f30180f68832d8a6209079 to your computer and use it in GitHub Desktop.
Save FanchenBao/aa20a70ad4f30180f68832d8a6209079 to your computer and use it in GitHub Desktop.
A quick script to accumulate the "total" column of `vnstat -5hdm` output
import re
from typing import List
"""
This scripts accumulates the 'total' column of vnstat output in this format:
wlan0 / hourly
hour rx | tx | total | avg. rate
------------------------+-------------+-------------+---------------
2019-12-16
15:00 4.29 MiB | 1.88 MiB | 6.17 MiB | 14.38 kbit/s
16:00 88.34 KiB | 202.69 KiB | 291.03 KiB | 662 bit/s
17:00 152.35 KiB | 482.23 KiB | 634.58 KiB | 1.44 kbit/s
18:00 40.09 KiB | 142.19 KiB | 182.27 KiB | 414 bit/s
19:00 30.35 KiB | 82.41 KiB | 112.76 KiB | 256 bit/s
"""
file_name = 'vnstat_output.txt'
with open(file_name) as f_obj:
lines = f_obj.readlines() # read all the lines
res: List[float] = []
for line in lines:
lst: List[str] = line.split('|')
if len(lst) == 4: # use only data row
ma = re.search(r'(\d+\.\d+)\sKiB', lst[2]) # extract value from 'total' column
if ma is not None:
res.append(float(ma.group(1)))
print(res)
print(sum(res))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment