A Docker-based bot that monitors your Linux system's CPU usage and sends you Discord DM alerts when it exceeds a specified threshold.
- 🔍 Monitors host CPU usage (not container CPU)
- 📨 Sends Discord alerts via DM or server channel
- 💬 Responds to commands in DMs and servers
- ⏱️ Configurable check interval and alert cooldown
- 🐳 Fully containerized with Docker
- 🔄 Auto-restart on failure
- 📊 Includes memory usage and load average in alerts
- 🌐 HTTP API for manual triggers
- Docker and Docker Compose installed
- A Discord bot token
- Your Discord user ID
- Go to Discord Developer Portal
- Click "New Application" and give it a name
- Go to the "Bot" section
- Click "Add Bot"
- Under "TOKEN", click "Reset Token" and copy it (you'll need this)
- Important: Under "Privileged Gateway Intents", enable "Message Content Intent" (required for commands)
- Go to "OAuth2" > "URL Generator"
- Select scopes:
bot - Select bot permissions:
Send Messages - Copy the generated URL and open it in your browser to invite the bot to a server (or just use it for DMs)
Option A: DM Alerts (sends to you privately)
- Open Discord
- Go to User Settings > Advanced
- Enable "Developer Mode"
- Right-click on your username anywhere and select "Copy ID"
- Use this as
DISCORD_USER_ID
Option B: Server Channel Alerts (posts in a channel)
- Enable "Developer Mode" in Discord (User Settings > Advanced)
- Right-click on the channel where you want alerts
- Select "Copy ID"
- Use this as
DISCORD_CHANNEL_ID - Make sure the bot has permission to send messages in that channel!
-
Clone or download this repository
-
Copy the example environment file:
cp .env.example .env
-
Edit
.envand fill in your details:DISCORD_BOT_TOKEN=your_actual_bot_token # Choose ONE or BOTH alert destinations: DISCORD_USER_ID=your_actual_user_id # For DM alerts # DISCORD_CHANNEL_ID=your_channel_id # For server channel alerts CPU_THRESHOLD=80 CHECK_INTERVAL=60 COOLDOWN_PERIOD=300
Alert Destination:
- Set
DISCORD_USER_IDto receive alerts via DM - Set
DISCORD_CHANNEL_IDto post alerts in a server channel - Set both if you want channel alerts but DM commands
- The bot will use channel alerts over DMs if both are set
- Set
Using Docker Compose (recommended):
docker-compose up -dOr using Docker directly:
docker build -t cpu-monitor .
docker run -d \
--name cpu-monitor-bot \
--restart unless-stopped \
--pid host \
-v /proc:/host/proc:ro \
--env-file .env \
cpu-monitorCheck the logs:
docker-compose logs -fYou should see output like:
Logged in as YourBot#1234
Monitoring CPU usage. Threshold: 80%
Check interval: 60 seconds
[2024-02-12 10:30:00] CPU Usage: 45.2%
| Variable | Default | Description |
|---|---|---|
DISCORD_BOT_TOKEN |
required | Your Discord bot token |
DISCORD_USER_ID |
optional | Your Discord user ID (for DM alerts) |
DISCORD_CHANNEL_ID |
optional | Channel ID (for server alerts) |
CPU_THRESHOLD |
80 | CPU percentage threshold for alerts |
CHECK_INTERVAL |
60 | How often to check CPU (seconds) |
COOLDOWN_PERIOD |
300 | Minimum time between alerts (seconds) |
HTTP_PORT |
8080 | Port for HTTP trigger endpoints |
COMMAND_PREFIX |
! | Prefix for Discord commands |
Note: Set either DISCORD_USER_ID (for DM alerts) or DISCORD_CHANNEL_ID (for server channel alerts), or both!
The bot responds to commands in DMs (from configured user) and server channels (if CHANNEL_ID is set, only in that channel; otherwise in any channel).
Command formats:
!status (using prefix)
@BotName status (mentioning the bot)
| Command | Description |
|---|---|
!status (or !stats, !cpu) |
Get current CPU, memory, and load average |
!test |
Send a test alert to see what alerts look like |
!ping |
Check if bot is responsive |
!help (or !commands) |
Show available commands |
Note: Discord commands require "Message Content Intent" enabled in your bot settings.
The bot also runs an HTTP server for more reliable manual triggers:
# Get current status
curl http://localhost:8080/status
# Send test alert
curl http://localhost:8080/test
# Get bot info
curl http://localhost:8080/infoYou can also trigger from anywhere on your network:
curl http://your-server-ip:8080/statusThese HTTP endpoints are much more reliable than Discord commands!
- The bot monitors your host system's CPU usage (not the container's)
- Every
CHECK_INTERVALseconds, it checks the current CPU percentage - If CPU >
CPU_THRESHOLD, it sends an alert:- To your DMs if
DISCORD_USER_IDis set - To a server channel if
DISCORD_CHANNEL_IDis set - If both are set, it uses the channel (you can still use commands in DMs)
- To your DMs if
- To prevent spam, it won't send another alert until
COOLDOWN_PERIODhas passed - The alert includes CPU usage, memory usage, load average, and timestamp
- You can trigger manual status checks via Discord commands or HTTP endpoints
If !status and other commands don't work:
- Use HTTP endpoints instead (more reliable):
curl http://localhost:8080/status - Make sure "Message Content Intent" is enabled in Discord Developer Portal
- Check the logs for "Received message from..." to see if bot is receiving messages
- Try restarting the bot after enabling the intent
- Make sure you share at least one server with the bot
- Check your Discord privacy settings (User Settings > Privacy & Safety > Allow direct messages from server members)
- Try sending the bot a message first
- Make sure you're using
--pid hostand mounting/proc - The docker-compose.yml already includes these settings
Check logs with:
docker-compose logs cpu-monitorCommon issues:
- Invalid bot token
- Invalid user ID
- Bot doesn't have permission to send messages
Start the bot:
docker-compose up -dStop the bot:
docker-compose downView logs:
docker-compose logs -fRestart the bot:
docker-compose restartRebuild after changes:
docker-compose up -d --build- Never commit your
.envfile to version control - Keep your bot token secret
- The bot only needs "Send Messages" permission
- The container runs with host PID namespace to read CPU stats - this is necessary but means the container can see host processes
MIT License - feel free to modify and use as needed!