Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow data from external script #154

Open
toxicantidote opened this issue Mar 1, 2020 · 4 comments
Open

Allow data from external script #154

toxicantidote opened this issue Mar 1, 2020 · 4 comments

Comments

@toxicantidote
Copy link

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
Copy link
Owner

vergoh commented Mar 8, 2020

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
Copy link
Author

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
Copy link
Author

toxicantidote commented Mar 13, 2020

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
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants