Skip to content

Instantly share code, notes, and snippets.

@Lance1o7
Last active November 21, 2024 04:48
Show Gist options
  • Save Lance1o7/78b679e26f6273ee020ac0469cab7dc0 to your computer and use it in GitHub Desktop.
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.
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})
@vergoh
Copy link

vergoh commented Jul 19, 2023

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.

@Lance1o7
Copy link
Author

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