Skip to main content

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
  • Install missing packages :
    • If last command not available, install needed package: sudo apt install wtmpdb util-linux
  • 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 a custom command with parameters (only for commands with "accept_parameters": true or a {arg1}/{args} placeholder)
python3 -m linuxmonitor --execute_command unban_ip --parameters "1.2.3.4" --config_file config-example.json --nodebug

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

Custom commands with parameters

A custom command (in the commands section of the config file) can receive parameters at execution time. This is opt-in per command: set "accept_parameters": true and/or use a placeholder in the command string.

Placeholders inside the command string:

  • {arg1}, {arg2}, ... : replaced by the Nth provided parameter
  • {args} : replaced by all provided parameters
  • no placeholder : all parameters are appended at the end of the command

Every parameter is shell-quoted before substitution, so it is always treated as a single literal argument. This prevents command injection through the parameters.

Example config entry:

"unban_ip": {
    "display_name": "Unban an IP from Fail2Ban",
    "command": "sudo fail2ban-client unban {arg1}",
    "is_private": true,
    "accept_parameters": true,
    "show_content_if_success": true,
    "show_content_if_issue": true,
    "timeout_in_sec": 60
}

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.

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.8.tar.gz (28.0 kB view details)

Uploaded Source

Built Distribution

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

linuxmonitor-1.5.8-py3-none-any.whl (28.7 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for linuxmonitor-1.5.8.tar.gz
Algorithm Hash digest
SHA256 d034d2956e778ff5f492a4a72f0c7bc3f36446c144dd6ef1a161d70b2de09b72
MD5 856329b6dc357be2ca3fdc6bf09ee1ec
BLAKE2b-256 3f4d593f67378b4ba427d0313ff9ab564269a83dc95866ae568994b26fea7dca

See more details on using hashes here.

File details

Details for the file linuxmonitor-1.5.8-py3-none-any.whl.

File metadata

  • Download URL: linuxmonitor-1.5.8-py3-none-any.whl
  • Upload date:
  • Size: 28.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/7.0.0 CPython/3.14.7

File hashes

Hashes for linuxmonitor-1.5.8-py3-none-any.whl
Algorithm Hash digest
SHA256 b38a078c60a27a86bd7fc7fccd5c9d1e57fd46b51d445574cc7a8ec2881e1d18
MD5 19332399b82d528909b4f8e27b5ac186
BLAKE2b-256 31e3d0fb84fecb79964ad8a7ae890f9ab01d5f7de0dfa0fdc320573fe748f205

See more details on using hashes here.

Release history Release notifications | RSS feed

1.5.10

2 files

1.5.9

2 files

This release

1.5.8 This release

2 files

1.5.7

2 files

1.5.6

2 files

1.5.5

2 files

1.5.4

2 files

1.5.3

2 files

1.5.1

2 files

1.5.0

2 files

1.4.9

2 files

1.4.8

2 files

1.4.7

2 files

1.4.6

2 files

1.4.5

2 files

1.4.4

2 files

1.4.3

2 files

1.4.2

2 files

1.4.1

2 files

1.4.0

2 files

1.3.11

2 files

1.3.10

2 files

1.3.9

2 files

1.3.8

2 files

1.3.7

2 files

1.3.6

2 files

1.3.4

2 files

1.3.3

2 files

1.3.2

2 files

1.3.1

2 files

1.3.0

2 files

1.2.8

2 files

1.2.7

2 files

1.2.6

2 files

1.2.5

2 files

1.2.4

2 files

1.2.2

2 files

1.2.1

2 files

1.2.0

2 files

1.1.2

2 files

1.1.1

2 files

1.1.0

2 files

1.0.6

2 files

1.0.5

2 files

1.0.4

2 files

1.0.3

2 files

1.0.2

2 files

1.0.1

2 files

1.0.0

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