Skip to main content

Get information and warning status of Linux server like service, port, ping, ssl certificate, disk/folder/cpu/ram/swap usage, ip connection, ... (Python and shell library, Linux ONLY)

Project description

Linux Monitor (Python library)

PyPI version License: MIT Donate Downloads Downloads

What is it

This python library is designed to be integrated in python or shell projects to monitor Linux servers. It is compatible with python 3+ and usable only on Linux.

It is also designed to be easily integrated in discussion channel bots python scripts (example: Discord Bot Linux Monitor)

Functionalities

Most important functionalities:

  • Do all checks bellow in a scheduled tasks and display the results only if there is an issue (only in console if using only the library)
  • Do all checks bellow in a scheduled tasks and display the results every time (only in console if using only the library)

List of 'check' functionalities:

  • Check CPU, RAM, SWAP, Temperature
  • Check disk usage
  • Check folder usage
  • Check websites basic availability (ping)
  • Check websites access with optional authentication (GET request)
  • Check services status and restart them if needed
  • Check certificates expiration and validity
  • Check last user connections IPs
  • Check uptime (to inform if the server has been rebooted)
  • Check custom commands

Additionnal functionalities:

  • Get hostname, OS details, kernel version, server datetime, uptime
  • Get connected users
  • Get processes list (PID and name)
  • Kill a process by PID
  • Reboot server
  • Restart/stop a service
  • Execute custom commands

How to install (python script and shell)

  • Install package calling pip install linux-monitor (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)
  • 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 "USERNAME_HERE ALL=(ALL) NOPASSWD: /sbin/reboot" >> /etc/sudoers.d/USERNAME_HERE
# Only if this library should be able to kill a process on demand:
echo "USERNAME_HERE ALL=(ALL) NOPASSWD: /bin/kill" >> /etc/sudoers.d/USERNAME_HERE

# To check last user connection
echo "USERNAME_HERE ALL=(ALL) NOPASSWD: /usr/bin/last" >> /etc/sudoers.d/USERNAME_HERE

# 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 "USERNAME_HERE ALL=(ALL) NOPASSWD: /bin/systemctl" >> /etc/sudoers.d/USERNAME_HERE
echo "USERNAME_HERE ALL=(ALL) NOPASSWD: /etc/init.d/apache2" >> /etc/sudoers.d/USERNAME_HERE
echo "USERNAME_HERE ALL=(ALL) NOPASSWD: /etc/init.d/mariadb" >> /etc/sudoers.d/USERNAME_HERE
  • Check monitor possibilities (you can find it out by calling python -m linuxmonitor --help from the root of this repository)
  • Load your shell or python script

How to use in shell

# Get help
python3 -m linuxmonitor --help
# Use "--debug" to show more information during command
# Use "--nodebug" to not show any warning information during command



# Start periodic task to show potential issues periodically (will run indefinitely)
# Best is to call this as a service and put the result in a log file or do something of the stdout
python3 -m linuxmonitor --start_scheduled_task_check_for_issues --config_file config-example.json --nodebug

# Start periodic task to show system information periodically (will run indefinitely)
# Best is to call this as a service and put the result in a log file or do something of the stdout
python3 -m linuxmonitor --start_scheduled_task_show_info --config_file config-example.json --nodebug



# View disk space, CPU, RAM, ...
python3 -m linuxmonitor --usage --config_file config-example.json --nodebug

# View basic system information
python3 -m linuxmonitor --os_infos --config_file config-example.json --nodebug

# View connected users
python3 -m linuxmonitor --users --config_file config-example.json --nodebug

# View last user connections
python3 -m linuxmonitor --user_logins --config_file config-example.json --nodebug

# Check websites (ping)
python3 -m linuxmonitor --ping --config_file config-example.json --nodebug

# Check websites access with optional authentication (GET request)
python3 -m linuxmonitor --websites --config_file config-example.json --nodebug

# Check SSL certificates
python3 -m linuxmonitor --certificates --config_file config-example.json --nodebug

# Check if services are running and restart if down
python3 -m linuxmonitor --services_status --config_file config-example.json --nodebug

# List all available services
python3 -m linuxmonitor --list_services --config_file config-example.json --nodebug

# Restart all services
python3 -m linuxmonitor --restart_all --config_file config-example.json --nodebug

# Restart a service
python3 -m linuxmonitor --restart_service SERVICE_NAME_HERE --config_file config-example.json --nodebug

# Stop a service
python3 -m linuxmonitor --stop_service SERVICE_NAME_HERE --config_file config-example.json --nodebug

# Check ports
python3 -m linuxmonitor --ports --config_file config-example.json --nodebug

# List active processes
python3 -m linuxmonitor --list_processes --config_file config-example.json --nodebug

# Stop a process by PID
python3 -m linuxmonitor --kill_process PID_HERE --config_file config-example.json --nodebug

# Restart the entire server
python3 -m linuxmonitor --reboot_server --config_file config-example.json --nodebug

# List all custom commands
python3 -m linuxmonitor --list_commands --config_file config-example.json --nodebug

# Execute a custom commands
python3 -m linuxmonitor --execute_command CUSTOM_COMMAND_HERE --config_file config-example.json --nodebug

# Execute all custom commands
python3 -m linuxmonitor --execute_all_commands --config_file config-example.json --nodebug

How to use in python script

Example of python script using this library:

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.

Project details


Download files

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

Source Distribution

linuxmonitor-1.5.6.tar.gz (25.5 kB view details)

Uploaded Source

Built Distribution

LinuxMonitor-1.5.6-py3-none-any.whl (26.3 kB view details)

Uploaded Python 3

File details

Details for the file linuxmonitor-1.5.6.tar.gz.

File metadata

  • Download URL: linuxmonitor-1.5.6.tar.gz
  • Upload date:
  • Size: 25.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.7

File hashes

Hashes for linuxmonitor-1.5.6.tar.gz
Algorithm Hash digest
SHA256 c7c7f00ee344e271ccb1e23618ec0d5851fe35df64fe9f6d3b145ffedc93e92a
MD5 52c27ed10e81ed5ba0430bf5cf60f783
BLAKE2b-256 4ef005ccae42d9b72d33b9ec79649663e0aefd4464c95a3d988db1ff4bb050c0

See more details on using hashes here.

File details

Details for the file LinuxMonitor-1.5.6-py3-none-any.whl.

File metadata

File hashes

Hashes for LinuxMonitor-1.5.6-py3-none-any.whl
Algorithm Hash digest
SHA256 03849bfa520b95c2e2b4b444f3e3dd229c34cb139bf43ca77ccb098e746dc247
MD5 d0b6071a57cac798759b6d8d3da41ca0
BLAKE2b-256 15ad79df19d6990df856d5a47992efea3bdd19b0f93a66b64df951109b8792c4

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page