Skip to content

Instantly share code, notes, and snippets.

@dtsmith2001
Last active April 3, 2021 15:49
Show Gist options
  • Save dtsmith2001/728fa28bbd8882e8f1c570a6a3ae90a2 to your computer and use it in GitHub Desktop.
Save dtsmith2001/728fa28bbd8882e8f1c570a6a3ae90a2 to your computer and use it in GitHub Desktop.
Monitor the Bandwidth Used by your Digital Ocean droplet
#
# monitor-bandwidth.service
#
[Unit]
Description=Ocean Network Allocation Monitor
After=network.target
Requires=network.target
[Service]
Type=simple
ExecStart=/usr/local/bin/monitor.sh
ExecStop=/usr/bin/pkill -TERM monitor.sh
User=<your_username>
Group=<your_username>
UMask=002
StandardOutput=syslog
StandardError=inherit
SyslogFacility=local0
Restart=always
RestartSec=30
[Install]
WantedBy=multi-user.target
#!/usr/bin/env bash
#
# Copyright 2021 Vallum Software, LLC. MIT license.
#
#
# Send an alert when network traffic allocation exceeds 900 GiB
#
webhook_url=''
while [ true ]
do
traffic=$(vnstat --oneline | awk -F ";" '{print $11}')
if [[ "${traffic}" == "GiB" ]]
then
if [ $(echo "$(echo "${traffic}" | sed 's/ GiB//g') > 900" | bc) -eq 1 ]
then
curl -X POST -H 'Content-type: application/json' --data "{\"text\":\"Traffic level ${traffic}\"}" ${webhook_url}
fi
fi
sleep 600
done
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment