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.9.tar.gz (28.3 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.9-py3-none-any.whl (29.0 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: linuxmonitor-1.5.9.tar.gz
  • Upload date:
  • Size: 28.3 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.9.tar.gz
Algorithm Hash digest
SHA256 55dfad2b67ddd54d76320c071003b0f14c30cb2fef98bf538152cc6cfe0f4479
MD5 ca8ddb1ba8fb719bf9192a24ed952d3e
BLAKE2b-256 e1a45a16546a5669ee7a9b3aaa8bece13dc265b34223090e6f13e85d0a0a88b1

See more details on using hashes here.

File details

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

File metadata

  • Download URL: linuxmonitor-1.5.9-py3-none-any.whl
  • Upload date:
  • Size: 29.0 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.9-py3-none-any.whl
Algorithm Hash digest
SHA256 4338e2f25dd83b2e956164c058815845b7ef3fa6a37bcff78f0f20c8619cafb3
MD5 9916001b76ed2f8ea3779f25ed169ced
BLAKE2b-256 923cb554a0741fe00253851105a55741e02e638f91af622a761c951938e98674

See more details on using hashes here.

Release history Release notifications | RSS feed

1.5.10

2 files

This release

1.5.9 This release

2 files

1.5.8

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