vergoh / vnstat

vnStat - a network traffic monitor for Linux and BSD
GNU General Public License v2.0
1.42k stars 120 forks source link

Allow data from external script #154

Open toxicantidote opened 4 years ago

toxicantidote commented 4 years ago

It would be nice to be able to have vnstat execute an external program to get transfer information and data usage, similar to how MRTG allows you to execute custom commands.

In my example, I have a system watching how much data comes in and out of a certain folder, and would like to see this with vnstat (and then vnstati).

vergoh commented 4 years ago

I'll have to check how MRTG currently does that, at which interval does it poll the script and what sort of output its expecting. Supporting the same format could be the best option, if possible. However, I'm a little bit hesitant on adding the possiblity of executing external programs from vnstad itself since those may not have predictable execution times or outputs which could then result in performance issues.

Another option could be to allow more direct input to the database, like something in the style of the old vnstat -u but with and external script or file as input. That would in either case require some changes in the database schema for marking some "interfaces" as coming from external source and making the daemon ignore those. From the user point of view, that would still result in the vnstat and vnstati commands having regular view of the data as neither currently care from where the information was collected from.

toxicantidote commented 4 years ago

Personally, I'm using MRTG in single-run mode, and have it called from crontab every five minutes. Apparently it does support a daemon mode, but I've never used this.

When pulling data from an external script, MRTG expects four values, separated by newlines:

Receive bytes
Transmit bytes
Uptime
System name

Now, realistically, receive and transmit bytes don't have to be bytes. They can be any quantity you want, and can be configured as cumulative (always increasing value), or as a gauge (increasing or decreasing). The uptime and system names are plugged straight in to the output pages too - they can be any bogus value you want, as long as they are present. Having vnstat support this feature would be great (for me, but probably others too).

toxicantidote commented 4 years ago

Here's a quick reference config for MRTG that you could test if desired (make sure you use backticks to enclose the command, single and double quotes don't work):

Target[internet]: `/root/mrtg/mrtg-internet.py`
MaxBytes[internet]: 125000
LegendI[internet]: Internet in
LegendO[internet]: Internet out
Options[internet]: growright, nopercent, integer
PageTop[internet]: <h1>Internet bandwidth</h1>

And then in /root/mrtg/mrtg-internet.py you have:

#!/usr/bin/python3

## The interface to get data for
interface = 'internet'

###
import subprocess
import re

net = subprocess.check_output('cat /proc/net/dev | grep ' + interface + ':', shell = True).decode('utf-8')
r_net = re.search(r':\s+(\d+)(\s+\d+){7}\s+(\d+)', net)

print(str(r_net.group(1)))
print(str(r_net.group(3)))
print('forever')
print(interface)
paulo-erichsen commented 3 years ago

I'm interested in this feature as well. Namely for getting cellular packet data from qmicli or mmcli

example of what qmicli and mmcli reports

# qmicli -d /dev/cdc-wdm0 -p --wds-get-packet-statistics
[/dev/cdc-wdm0] Connection statistics:
    TX packets OK: 368
    RX packets OK: 366
    TX packets dropped: 0
    RX packets dropped: 0
    TX bytes OK: 29352
    RX bytes OK: 75582

# mmcli -m 0 -b 0
...
...
  ------------------------------------
  Statistics         |       duration: 2279
                     |       bytes rx: 75582
                     |       bytes tx: 29352
                     |       attempts: 1
                     | total-duration: 2279
                     | total-bytes rx: 75582
                     | total-bytes tx: 29352