This Python bot monitors system services on an Ubuntu machine using the /etc/systemd/system/*.service
files and provides real-time status updates via a Discord bot. The bot is capable of starting, stopping, and restarting services directly through Discord interactions.
- Monitors services under
/etc/systemd/system/*.service
. - Displays service status and dynamically updates service status in Discord messages.
- Provides a dropdown for selecting services and controlling them (start, stop, restart).
- Periodically updates the status of services and the overall system status every 10 seconds.
- Dynamically changes the Discord channel name based on overall service health.
- Clears the channel on bot startup.
- Python 3.8 or higher
- A Discord bot with appropriate permissions (manage channel, send messages, etc.).
- The following Python libraries:
discord.py
python-dotenv
You can install the required libraries by running:
pip install discord.py python-dotenv
sudo locale-gen de_DE.UTF-8
sudo update-locale
Create a .env
file in the same directory as the bot script. The file should contain the following variables:
DISCORD_BOT_TOKEN=
DISCORD_CHANNEL_ID=
SERVICE_CHECK_RATE=10
CHANNEL_PRECEDING_CHARACTER=┏
SERVICE_USER=
Replace <your_discord_bot_token>
with the actual token of your Discord bot and <your_discord_channel_id>
with the ID of the channel where the bot will send status updates.
If your bot requires sudo to start, stop, or restart services, you need to allow the bot user to run systemctl without a password. To do this, follow these steps:
- Open the sudoers file:
sudo visudo
- Add the following line to allow the bot user (replace
botuser
with your actual username) to run systemctl without a password:
botuser ALL=(ALL) NOPASSWD: /bin/systemctl
To run the bot, use the following command:
python3 missioncontrol.py
- The bot monitors services located under /etc/systemd/system/ and only considers .service files that include a User= directive.
- The bot provides the following real-time status information: -- 🟢 for active services -- 🔴 for failed or inactive services -- 🟡 for services with unknown or transitional states
- Service Dropdown: Users can select a service from the dropdown to view details and control the service (start, stop, restart).
- Automatic Updates: The bot updates the service status in the channel every 10 seconds.
- Channel Name Update: The channel name reflects the overall health of the services: -- 🟢 if all services are running. -- 🟡 if some services are in an unknown state. -- 🔴 if any service has failed or is inactive.
The bot provides buttons for:
- Start - Starts a selected service.
- Stop - Stops a running service.
- Restart - Restarts a running service.
- When the bot starts, it clears the Discord channel.
- The bot checks for services under /etc/systemd/system/*.service and monitors their status.
- It displays the current status of all relevant services in the channel and provides a dropdown for interaction.
- Every 10 seconds, the status is updated, and if necessary, the bot modifies the channel name to reflect the overall status of the services.
If you encounter issues where the bot cannot start, stop, or restart services because sudo requires a password, ensure that you have configured the sudoers file to allow the bot user to run systemctl without a password.
Ensure that your Discord bot has the following permissions:
- Manage Channel
- Read Messages
- Send Messages
- Embed Links
- Add Reactions
If the bot updates the status too frequently, you may encounter Discord's rate limits. You can adjust the update frequency by modifying the @tasks.loop(seconds=10)
decorator in the script.