System Monitoring to Discord Private Messages is a Python 3 application that monitors specific system events and sends direct messages to a specified Discord user when these events occur. The application is designed to run on both Ubuntu Server 24.04 and Windows 10/11, utilizing a virtual environment (venv
) for Python dependencies.
- Service Status Monitoring: Monitors the status of specified system services and sends a notification if their status changes.
- Disk Usage Monitoring: Monitors specified disk paths and sends a notification when disk usage exceeds a defined limit.
- CPU Temperature Monitoring: Monitors the CPU temperature and sends a notification if it exceeds a defined threshold (Linux only).
- Shutdown and Restart Monitoring: Detects system shutdown or restart events and sends a notification (Linux only).
- Logging: Logs events and errors to both the console and a file (
application.log
) for better traceability. - Cross-Platform Support: Automatically detects the operating system and loads the appropriate configurations and libraries.
- Discord Integration: Sends direct messages to a specified Discord user using a Discord bot.
.
├── main.py
├── discord_handler.py
├── event_monitor.py
├── services_monitor.py
├── disk_monitor.py
├── cpu_monitor.py
├── shutdown_monitor.py
├── config.txt
├── servicesLinux.txt
├── servicesWindows.txt
├── drivesLinux.txt
├── drivesWindows.txt
├── requirements.txt
├── application.log
└── README.md
- main.py: The entry point of the application. Initializes the logging system and the Discord handler, and starts the bot.
- discord_handler.py: Handles interactions with Discord, including sending messages and managing the bot client.
- event_monitor.py: Manages the initialization and coordination of various monitoring tasks.
- services_monitor.py: Monitors the status of specified system services.
- disk_monitor.py: Monitors disk usage of specified paths.
- cpu_monitor.py: Monitors CPU temperature and sends notifications when thresholds are exceeded (Linux only).
- shutdown_monitor.py: Monitors for system shutdown or restart events (Linux only).
- config.txt: Configuration file containing Discord bot settings and monitoring thresholds.
- servicesLinux.txt: List of services to monitor on Linux.
- servicesWindows.txt: List of services to monitor on Windows.
- drivesLinux.txt: List of disk paths and usage limits to monitor on Linux.
- drivesWindows.txt: List of disk paths and usage limits to monitor on Windows.
- requirements.txt: Lists all Python dependencies required to run the application.
- application.log: The log file where all events and errors are recorded.
- README.md: Documentation and instructions for the application.
This file contains configuration settings for the Discord bot and CPU temperature monitoring.
[Discord]
bot_token = YOUR_DISCORD_BOT_TOKEN
user_id = YOUR_DISCORD_USER_ID
cpu_temp_limit = 80
cpu_temp_notification_interval = 10
- bot_token: The token for your Discord bot. Replace
YOUR_DISCORD_BOT_TOKEN
with your actual bot token. - user_id: The Discord ID of the user to whom the bot will send direct messages.
- cpu_temp_limit: The CPU temperature threshold for sending notifications (Linux only).
- cpu_temp_notification_interval: The minimum interval between CPU temperature notifications, in minutes.
List of system services to monitor on Linux. Each line contains the name of a service.
Example:
w3p_bsh.service
w3p_geth.service
List of system services to monitor on Windows. Each line contains the name of a service.
Example:
TeamViewer
List of disk paths and usage limits to monitor on Linux. Each line contains a path and a usage limit percentage.
Example:
/dev/sda1 90
/mnt/storage 80
/home/ethereum 95
List of disk paths and usage limits to monitor on Windows. Each line contains a path and a usage limit percentage.
Example:
C:/ 90
D:/ 80
- Python 3.x installed on your system.
- Administrator or root permissions may be required for certain monitoring features.
- Linux:
lm-sensors
package for CPU temperature monitoring.
-
Clone the Repository
git clone https://github.com/cmd0s/sys-mon-to-discord-prv-msg.git cd sys-mon-to-discord-prv-msg
-
Create a Virtual Environment
python3 -m venv venv
-
Activate the Virtual Environment
-
Linux:
source venv/bin/activate
-
Windows:
venv\Scripts\activate
-
-
Install Dependencies
pip install -r requirements.txt
-
Configure the Application
-
Edit
config.txt
Replace placeholders with your actual Discord bot token and user ID.
[Discord] bot_token = YOUR_DISCORD_BOT_TOKEN user_id = YOUR_DISCORD_USER_ID cpu_temp_limit = 80 cpu_temp_notification_interval = 10
-
Edit
servicesLinux.txt
orservicesWindows.txt
List the services you want to monitor.
-
Edit
drivesLinux.txt
ordrivesWindows.txt
List the disk paths and usage limits you want to monitor.
-
-
Additional Setup for Linux
-
Install
lm-sensors
for CPU temperature monitoring.sudo apt-get install lm-sensors sudo sensors-detect
-
-
Run the Application
python main.py
- The application will start monitoring and send notifications as configured.
- Log messages will be displayed in the console and saved to
application.log
.
After running the program, you should see the following output on the console.
Sending direct messages to a user on Discord requires setting up a Discord bot, obtaining a bot token, and knowing the user's Discord ID. The bot must share a server with the user or the user must have sent a direct message to the bot at least once.
-
Go to the Discord Developer Portal
Visit the Discord Developer Portal.
-
Create a New Application
- Click on "New Application".
- Enter a name for your application and click "Create".
-
Add a Bot to Your Application
- Navigate to the "Bot" tab on the left sidebar.
- Click on "Add Bot" and confirm by clicking "Yes, do it!".
-
Enable "Message Content Intent"
- Under the "Privileged Gateway Intents" section, enable the "Message Content Intent".
- This is necessary for the bot to read message content and send direct messages properly.
-
Retrieve the Bot Token
- Under the "Bot" tab, click on "Copy" under "Token".
- Keep this token secure and do not share it publicly.
-
Navigate to OAuth2 URL Generator
- Go to the "OAuth2" tab.
- Click on "URL Generator".
-
Generate an Invite Link
- Under "Scopes", select "bot".
- Under "Bot Permissions", select "Send Messages".
- Copy the generated URL.
-
Invite the Bot
- Paste the URL into your browser.
- Select the server you want to add the bot to and authorize it.
You need your Discord User ID to configure the application.
-
Enable Developer Mode
- Open Discord.
- Go to User Settings > Advanced.
- Enable Developer Mode.
-
Copy Your User ID
- Right-click on your username in Discord.
- Click "Copy ID".
For more detailed instructions, refer to this guide.
- Requirement: The bot must share a server with you or you must have sent a direct message to the bot at least once.
- Establishing a DM Channel: Send a direct message to your bot to establish the DM channel.
-
Startup Notification
- When the application starts, it sends a direct message to the specified Discord user indicating that monitoring has begun.
-
Service Monitoring
- The application monitors the status of services listed in
servicesLinux.txt
orservicesWindows.txt
. - If a service changes status (e.g., from "running" to "stopped"), a notification is sent.
- The application monitors the status of services listed in
-
Disk Usage Monitoring
- Monitors disk usage for paths specified in
drivesLinux.txt
ordrivesWindows.txt
. - Sends a notification when disk usage exceeds the specified limit.
- Notifications are sent only once when crossing the threshold to avoid spamming.
- Monitors disk usage for paths specified in
-
CPU Temperature Monitoring
- Monitors CPU temperature on Linux systems.
- Sends a notification when the temperature exceeds the specified limit.
- Notifications are throttled based on
cpu_temp_notification_interval
.
-
Shutdown and Restart Monitoring
- On Linux systems, the application listens for shutdown and restart signals (
SIGTERM
andSIGINT
). - Sends a notification when such an event is detected.
- On Linux systems, the application listens for shutdown and restart signals (
-
Logging
- The application logs events and errors to both the console and a log file named
application.log
. - Logs include timestamps, severity levels, and informative messages.
- This aids in debugging and provides a record of the application's activities.
- The application logs events and errors to both the console and a log file named
-
Log File:
application.log
-
Log Levels:
INFO
,WARNING
,ERROR
-
Console Output: Log messages are also displayed in the console.
-
Customization:
- The logging configuration is set up in
main.py
. - You can adjust logging levels and formats as needed.
- By default, both console and file handlers are set to
INFO
level.
- The logging configuration is set up in
You can adjust the logging levels for the console and file outputs independently in main.py
:
# Set levels for handlers
c_handler.setLevel(logging.INFO) # Console handler level
f_handler.setLevel(logging.DEBUG) # File handler level
To prevent the log file from growing indefinitely, you can implement log rotation using RotatingFileHandler
:
from logging.handlers import RotatingFileHandler
f_handler = RotatingFileHandler('application.log', maxBytes=1048576, backupCount=5)
- maxBytes: Maximum size of the log file before rotation (e.g., 1 MB).
- backupCount: Number of backup files to keep.
-
Running on Startup
- Linux: Use
systemd
to create a service that runs the application at startup. - Windows: Use Task Scheduler to run the application at startup.
- Linux: Use
-
Logging
- Review the
application.log
file regularly to monitor the application's behavior. - Adjust the logging level in
main.py
if you want more or less verbosity. - Both console and file outputs help in real-time monitoring and historical analysis.
- Review the
-
Security
- Keep your Discord bot token and user ID secure. Do not share them publicly or commit them to a public repository.
-
Testing
- Test each monitoring feature individually to ensure it works as expected.
- Use low thresholds temporarily to trigger notifications during testing.
-
Extensibility
- The modular design allows for easy addition of new monitoring features.
- You can create additional monitoring modules and integrate them into
event_monitor.py
.
-
Permissions
- Ensure the application has sufficient permissions to monitor services and system metrics.
- You may need to run the application with elevated privileges.
-
Dependencies
-
Make sure all dependencies are installed in your virtual environment.
-
If you encounter issues, try reinstalling dependencies with:
pip install --upgrade --force-reinstall -r requirements.txt
-
-
CPU Temperature Monitoring on Windows
- Currently, CPU temperature monitoring is disabled on Windows.
- The code includes placeholders for future implementation.
-
Error Messages
- Monitor the console output and
application.log
for error messages. - Common issues include incorrect file paths or missing configuration settings.
- Monitor the console output and
-
Duplicate Logs
- If you see duplicate log entries, ensure that logging is configured correctly in
main.py
, andlogging.basicConfig
is not called elsewhere.
- If you see duplicate log entries, ensure that logging is configured correctly in
-
No Console Output
- If log messages are not appearing in the console, verify that the
StreamHandler
is correctly configured and added to the logger inmain.py
.
- If log messages are not appearing in the console, verify that the
This application provides a flexible and extensible solution for system monitoring with notifications via Discord direct messages. The addition of logging enhances traceability and aids in debugging.
This project is licensed under the GPL-3.0 license.