Skip to content

Instantly share code, notes, and snippets.

@pradishb
Created September 24, 2023 11:13
Show Gist options
  • Save pradishb/9de44a41e024a7acb9b66c077e787e78 to your computer and use it in GitHub Desktop.
Save pradishb/9de44a41e024a7acb9b66c077e787e78 to your computer and use it in GitHub Desktop.
A script that sends discord notification when the disk size is below a threshold, can be used in servers to monitor disk usage periodically by pairing with cron
import json
import subprocess
threshold = 10 # GB
webhook_url = "https://discord.com/api/webhooks/xxx/xxx"
environment = "my-server-name"
free_space = (
int(
(
subprocess.check_output(
"df --output=avail / | tail -n 1", shell=True
)
.decode()
.strip()
)
)
/ 1024
/ 1024
)
data = {
"username": "Disk Usage Bot",
"embeds": [
{
"title": "Warning",
"color": 16711680,
"description": (
f"{environment} has only {free_space:.2f} GB disk left"
),
}
],
}
cmd = (
f"curl -H \"Content-Type: application/json\" -d '{json.dumps(data)}'"
f' "{webhook_url}"'
)
if free_space < threshold:
subprocess.run(cmd, shell=True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment