Last active
November 21, 2024 04:48
-
-
Save Lance1o7/78b679e26f6273ee020ac0469cab7dc0 to your computer and use it in GitHub Desktop.
Using vnstat to monitor the monthly traffic of a Linux server, if it exceeds the limit, send a notification to Discord.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from subprocess import run | |
import requests | |
INTERFACE = "enp0s6" | |
LIMIT = "500" | |
WEBHOOK_URL = '' | |
MESSAGE = 'Traffic Warning' | |
p = run(["vnstat", INTERFACE, "--alert", "0", "3", "m", "total", LIMIT, "GB"]) | |
# Reference: | |
# | |
# Valid parameters for | |
# --alert <output> <exit> <type> <condition> <limit> <unit> | |
# <output> | |
# *0 - no output | |
# 1 - always show output | |
# 2 - show output only if usage estimate exceeds limit | |
# 3 - show output only if limit is exceeded | |
# <exit> | |
# 0 - always use exit status 0 | |
# 1 - always use exit status 1 | |
# 2 - use exit status 1 if usage estimate exceeds limit | |
# *3 - use exit status 1 if limit is exceeded | |
# <type> | |
# h, hour, hourly d, day, daily | |
# *m, month, monthly y, year, yearly | |
# <condition> | |
# rx, tx, *total, rx_estimate, tx_estimate, total_estimate | |
# <limit> | |
# greater than zero integer without decimals | |
# <unit> for <limit> | |
# B, KiB, MiB, GiB, TiB, PiB, EiB | |
# B, KB, MB, *GB, TB, PB, EB | |
if p.returncode == 1: | |
response = requests.post(WEBHOOK_URL, json={'content': MESSAGE}) |
Few comments:
- The use of
sudo
is likely to be unnecessary.--oneline
doesn't output by default in bytes and with the unit getting split out, the comparison isn't likely to work as expected. Depending on the used vnStat version, it's possible to use--oneline b
to force all values to bytes.- Instead of cutting and splitting the command output, it's often more convenient to use
--json
instead of--oneline
when a json parser is available (import json
in case of Python).--json m 1
would give the monthly values for the current month out of which the value in bytes can easily be referenced.- Assuming a recent version of vnStat is used, there's also the option of using
--alert
and let vnStat do all the comparisons for you. Either read just the exit code or use also the output depending on what you'd want to get included in the notification.
Thanks. The code is updated.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Few comments:
sudo
is likely to be unnecessary.--oneline
doesn't output by default in bytes and with the unit getting split out, the comparison isn't likely to work as expected. Depending on the used vnStat version, it's possible to use--oneline b
to force all values to bytes.--json
instead of--oneline
when a json parser is available (import json
in case of Python).--json m 1
would give the monthly values for the current month out of which the value in bytes can easily be referenced.--alert
and let vnStat do all the comparisons for you. Either read just the exit code or use also the output depending on what you'd want to get included in the notification.