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}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks. The code is updated.