feat: Add disk usage monitoring script

This commit is contained in:
Christopher
2024-05-19 20:45:29 -05:00
parent 490f93cdd7
commit 6f54010a49
2 changed files with 82 additions and 0 deletions

View File

@@ -0,0 +1,17 @@
## Why?
This is a handy script to monitor disk usage and send an email or Discord message if the usage exceeds a certain threshold (80% by default).
## Parmeters
The script accepts the following parameters:
- `--email=<email address>`: The email address to send the message to. If not provided, the script will prompt the user to enter the email address.
- `--discord=<webhook URL>`: The Discord webhook URL to send the message to. If not provided, the script will prompt the user to enter the webhook URL.
- `--threshold=<percentage>`: The percentage threshold for disk usage. The default value is 80.
## How to use?
```bash
bash -c "$(wget -qLO - https://raw.githubusercontent.com/bigbeartechworld/big-bear-scripts/master/disk-usage-monitor/run.sh)"
```

65
disk-usage-monitor/run.sh Normal file
View File

@@ -0,0 +1,65 @@
#!/bin/bash
# Default values
EMAIL=""
DISCORD_WEBHOOK=""
THRESHOLD=80
# Parse arguments
for ARG in "$@"
do
case $ARG in
--email=*)
EMAIL="${ARG#*=}"
;;
--discord=*)
DISCORD_WEBHOOK="${ARG#*=}"
;;
--threshold=*)
THRESHOLD="${ARG#*=}"
;;
*)
# Unknown option
;;
esac
done
# Prompt for email if not set
if [ -z "$EMAIL" ]; then
read -p "Please enter your email address (or press Enter to skip): " EMAIL
fi
# Prompt for Discord webhook if not set
if [ -z "$DISCORD_WEBHOOK" ]; then
read -p "Please enter your Discord webhook URL (or press Enter to skip): " DISCORD_WEBHOOK
fi
# Get disk usage
DISK_USAGE=$(df -h / | grep -vE '^Filesystem|tmpfs|cdrom' | awk '{ print $5 }')
USAGE=$(echo $DISK_USAGE | sed 's/%//g')
# Display current disk usage
echo "Current disk usage is $DISK_USAGE."
# Check if usage exceeds threshold
if [ $USAGE -ge $THRESHOLD ]; then
MESSAGE="Disk usage is at $DISK_USAGE - Time to clean up!"
# Send email if an email address is provided
if [ -n "$EMAIL" ]; then
echo "$MESSAGE" | mail -s "Disk Usage Alert" "$EMAIL"
fi
# Send message to Discord webhook if a webhook URL is provided
if [ -n "$DISCORD_WEBHOOK" ]; then
curl -H "Content-Type: application/json" \
-X POST \
-d "{\"content\": \"$MESSAGE\"}" \
"$DISCORD_WEBHOOK"
fi
else
MESSAGE="Disk usage is within acceptable limits."
fi
# Display the message
echo "$MESSAGE"