Skip to main content

Discord Bot Linux Monitor (Python library)

PyPI version License: MIT Donate Downloads Downloads

What is it

This python library is designed to be used as a Linux service to monitor the Linux server manually with discord commands and to send warning or information of the Linux server status to a discord channel periodically (if relevant).

It is possible to have separate 'private discord channel' and 'public discord channel' for:

  • Sendind discord commands to the bot (visual example in next section)
  • Getting periodic status of the Linux server (doing periodically some commands from next section)
  • Getting warnings if there is an issue in the Linux server (periodic status check)

Example of warning:

It is compatible with python 3+ and usable only on Linux.

List of Discord commands:

Public and private discord channel commands

Displayed infos are not the same if you do the command in private or public channel (all info are displayed in private channel, some are hidden from public channels)

  • /usage: ๐Ÿ“Š View disk space, Load average, CPU, RAM, ... ๐Ÿ“Š
  • /os_infos: ๐Ÿ–ฅ View basic system information ๐Ÿ–ฅ
  • /ping: ๐ŸŒ Ping websites ๐ŸŒ
  • /websites: ๐ŸŒ Check websites access (GET requests) ๐ŸŒ
  • /certificates: ๐Ÿ”’ Check SSL certificates ๐Ÿ”’
  • /services_status: ๐Ÿฉบ Check services are running ๐Ÿฉบ
  • /restart_all: ๐Ÿš€ Restart all services ๐Ÿš€
  • /restart_service {service_name}: ๐Ÿš€ Restart a service ๐Ÿš€
  • /stop_service {service_name}: ๐Ÿšซ Stop a service ๐Ÿšซ

  • /list_services: ๐Ÿ“‹ List all available services ๐Ÿ“‹

  • /ports: ๐Ÿ”’ Check ports ๐Ÿ”’
  • /list_commands: ๐Ÿ“‹ List all available commands ๐Ÿ“‹

  • /execute_command {command_name} [parameters]: ๐Ÿš€ Execute a command (optional parameters, only for commands with accept_parameters or a {arg1}/{args} placeholder) ๐Ÿš€

  • /execute_all_commands: ๐Ÿš€ Execute all commands ๐Ÿš€

Private discord channel commands:

  • /force_sync: ๐Ÿ”„ Force discord command synchronization ๐Ÿ”„
  • /users: ๐Ÿ‘ฅ View connected users ๐Ÿ‘ฅ
  • /user_logins: ๐Ÿ‘ฅ View last user connections ๐Ÿ‘ฅ
  • /reboot_server: ๐Ÿ”„ Restart the entire server ๐Ÿ”„
  • /list_processes: ๐Ÿ“‹ List active processes ๐Ÿ“‹
  • /kill_process: ๐Ÿšซ Stop a process by PID ๐Ÿšซ

How to install (for first launch)

  • Install package calling python -m pip install discordbotlinuxmonitor (or python setup.py install from the root of this repository)
  • Copy and edit config-example.json file depending on your need (on first launch, remove all restart_command from config file to prevent potential looping service restart issues on your server in case your config file is not well configured)
  • Launch the lib for testing it works:
# Get help
python3 -m discordbotlinuxmonitor --help
# Use "--debug" to show more information during command
# Use "--nodebug" to not show any warning information during command

# Start the discord bot Linux monitor (First time)
python3 -m discordbotlinuxmonitor --config_file config-example.json --force_sync_on_startup --debug

# Start the discord bot Linux monitor (after first time)
python3 -m discordbotlinuxmonitor --config_file config-example.json
  • Go to discord (restart discord if you were already in it)
  • You should see welcome messages on channels you configured in the config file and be able to communicate with the bot using command defined in previous section

How to install this lib as a service (to keep it running in the Linux server as a monitor)

  • Stop the discord bot and create a service to have it running even after computer reboot:
### Define all base info
DISCORD_BOT_SERVICE_USER="discordbotlinuxmonitor"
DISCORD_BOT_SERVICE_GROUP="discordbotlinuxmonitor"
DISCORD_BOT_SERVICE_NAME="discord-bot"
DISCORD_BOT_SERVICE_FILE="/etc/systemd/system/${DISCORD_BOT_SERVICE_NAME}.service"
DISCORD_BOT_FOLDER="/opt/DiscordBot/"

### Add rights to user launching the library depending on what you want it to do ###
# Only if this library should be able to reboot the server on demand:
echo "$DISCORD_BOT_SERVICE_USER ALL=(ALL) NOPASSWD: /sbin/reboot" >> /etc/sudoers.d/$DISCORD_BOT_SERVICE_USER
# Only if this library should be able to kill a process on demand:
echo "$DISCORD_BOT_SERVICE_USER ALL=(ALL) NOPASSWD: /bin/kill" >> /etc/sudoers.d/$DISCORD_BOT_SERVICE_USER
# Add also all processes added in your config JSON file you want the library to be able to execute
# Example for the existing config-example.json file:
echo "$DISCORD_BOT_SERVICE_USER ALL=(ALL) NOPASSWD: /bin/systemctl" >> /etc/sudoers.d/$DISCORD_BOT_SERVICE_USER
echo "$DISCORD_BOT_SERVICE_USER ALL=(ALL) NOPASSWD: /etc/init.d/apache2" >> /etc/sudoers.d/$DISCORD_BOT_SERVICE_USER
echo "$DISCORD_BOT_SERVICE_USER ALL=(ALL) NOPASSWD: /etc/init.d/mariadb" >> /etc/sudoers.d/$DISCORD_BOT_SERVICE_USER

### Create a specific user and group to launch the discord bot service ###
echo "Creating user $DISCORD_BOT_SERVICE_USER..."
sudo useradd -r -s /usr/sbin/nologin $DISCORD_BOT_SERVICE_USER
sudo mkdir -p /home/$DISCORD_BOT_SERVICE_USER
sudo chown $DISCORD_BOT_SERVICE_USER:$DISCORD_BOT_SERVICE_GROUP /home/$DISCORD_BOT_SERVICE_USER
sudo usermod -d /home/$DISCORD_BOT_SERVICE_USER $DISCORD_BOT_SERVICE_USER

### Install DiscordBotLinuxMonitor lib for the user ###
echo "Installing DiscordBotLinuxMonitor for user $DISCORD_BOT_SERVICE_USER..."
sudo -u $DISCORD_BOT_SERVICE_USER -s
python3 -m venv /home/$DISCORD_BOT_SERVICE_USER/venv
source /home/$DISCORD_BOT_SERVICE_USER/venv/bin/activate
python3 -m pip install discordbotlinuxmonitor
deactivate
exit

# Create the Discord bot folder where you should put the json file
sudo mkdir $DISCORD_BOT_FOLDER

### Create a systemd service file for the Discord bot ###
cat <<EOF > $DISCORD_BOT_SERVICE_FILE
[Unit]
Description=Discord Bot Linux Monitor Service
After=network.target

[Service]
ExecStart=/home/$DISCORD_BOT_SERVICE_USER/venv/bin/python3 -m discordbotlinuxmonitor --config_file ${DISCORD_BOT_FOLDER}config.json
WorkingDirectory=$DISCORD_BOT_FOLDER
User=$DISCORD_BOT_SERVICE_USER
Group=$DISCORD_BOT_SERVICE_GROUP
Restart=always
RestartSec=3

[Install]
WantedBy=multi-user.target
EOF

echo "Service file created at $DISCORD_BOT_SERVICE_FILE"

### Reload systemd to recognize the new service ###
echo "Reloading systemd..."
sudo systemctl daemon-reload

### Enable and start the service ###
echo "Enabling and starting the Discord bot service..."
sudo systemctl enable $DISCORD_BOT_SERVICE_NAME
  • Copy your config file into $DISCORD_BOT_FOLDER/config.json
  • Launch the service: sudo systemctl start $DISCORD_BOT_SERVICE_NAME

License

This project is under MIT license. This means you can use it as you want (just don't delete the library header).

Contribute

If you want to add more examples or improve the library, just create a pull request with proper commit message and right wrapping.

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

discordbotlinuxmonitor-1.6.0.tar.gz (12.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

discordbotlinuxmonitor-1.6.0-py3-none-any.whl (13.4 kB view details)

Uploaded Python 3

File details

Details for the file discordbotlinuxmonitor-1.6.0.tar.gz.

File metadata

  • Download URL: discordbotlinuxmonitor-1.6.0.tar.gz
  • Upload date:
  • Size: 12.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/7.0.0 CPython/3.14.7

File hashes

Hashes for discordbotlinuxmonitor-1.6.0.tar.gz
Algorithm Hash digest
SHA256 30f7494e627a3a7c4004d1f4309fc60d2b22a1d096d3823cc299915f3d749e59
MD5 490705ffd3852b8319a0d346f6b8a6f5
BLAKE2b-256 c7cbf836deb9ab18f4c6e8755fdba9e05216f47fb2aa52ea81d8c14794d58521

See more details on using hashes here.

File details

Details for the file discordbotlinuxmonitor-1.6.0-py3-none-any.whl.

File metadata

File hashes

Hashes for discordbotlinuxmonitor-1.6.0-py3-none-any.whl
Algorithm Hash digest
SHA256 6b2d5ac62c7ffcf0217c826103e38a36e4d8e0c94e30cf7e69011080e79ba946
MD5 9168da7a925996f416e402ecd2c7ee2a
BLAKE2b-256 eb8f772761b0150758d34baf14c94e1a4b8c837131ec7fdfd82bc2de93e527f2

See more details on using hashes here.

Release history Release notifications | RSS feed

1.6.7

2 files

1.6.6

2 files

1.6.5

2 files

1.6.4

2 files

1.6.3

2 files

1.6.2

2 files

1.6.1

2 files

This release

1.6.0 This release

2 files

1.5.1

2 files

1.5.0

2 files

1.4.3

2 files

1.4.2

2 files

1.4.1

2 files

1.4.0

2 files

1.3.1

2 files

1.3.0

2 files

1.2.2

2 files

1.2.1

2 files

1.1.1

2 files

1.0.3

2 files

1.0.2

2 files

1.0.1

2 files

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page