Last active
September 7, 2023 11:14
-
-
Save PaulGoldschmidt/ea66988c44873d71719c59cde1bafbfd to your computer and use it in GitHub Desktop.
A small script to notify via telegram if the storage on a linux server runs low. Tested on Ubuntu.
This file contains 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
#!/bin/bash | |
# Telegram Bot API Token and Chat ID | |
TELEGRAM_API_TOKEN="YOUR_TELEGRAM_BOT_API_TOKEN" | |
CHAT_ID="YOUR_CHAT_ID" | |
# Set the threshold for used storage in percentage | |
THRESHOLD=85 | |
# Function to check used storage | |
check_storage() { | |
# Get the used storage in percentage | |
used_percent=$(df -h / | awk 'NR==2 {print $5}' | sed 's/%//') | |
# Check if used storage is above the threshold | |
if [ "$used_percent" -gt "$THRESHOLD" ]; then | |
message="⚠️ High storage usage alert! Used storage is at ${used_percent}%" | |
send_telegram_message "$message" | |
fi | |
} | |
# Function to send a message via the Telegram API | |
send_telegram_message() { | |
local message="$1" | |
curl -s -X POST "https://api.telegram.org/bot${TELEGRAM_API_TOKEN}/sendMessage" \ | |
-d "chat_id=${CHAT_ID}" \ | |
-d "text=${message}" \ | |
> /dev/null | |
} | |
# Main script | |
check_storage | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment