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 "127.0.0.1" --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.

Metadata

Release files for LinuxMonitor 1.7.4

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for LinuxMonitor 1.7.4
File Size Uploaded
linuxmonitor-1.7.4.tar.gz 32.4 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for LinuxMonitor 1.7.4
File Interpreter ABI Platform
linuxmonitor-1.7.4-py3-none-any.whl Python 3 none any Details

Total release size: 65.3 kB

Release files / linuxmonitor-1.7.4.tar.gz

Download URL linuxmonitor-1.7.4.tar.gz
Size 32.4 kB
Tags Source
SHA-256 checksum
How to use checksums
756c73c22f8db2acb455eda907b6449b9949d7f9b79d518e1124f553285d8c63
BLAKE2b-256 checksum
How to use checksums
45030d547d76c123608e015e5bb3bf54bb357807aecd19ab7a2662694e73704b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.14.7

Release files / linuxmonitor-1.7.4-py3-none-any.whl

Download URL linuxmonitor-1.7.4-py3-none-any.whl
Size 32.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
23c5be56aae3f3b06d37fb0c90d97995f017295d6366d77993b671308b5052c0
BLAKE2b-256 checksum
How to use checksums
bbbdcdfe50f8c4377d4c87aa8d66bb02caf533bc58ab9abc05301e3b0b93d1e9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.14.7

Release history Release notifications | RSS feed

This release

1.7.4 This release

2 release files

1.7.3

2 release files

1.7.2

2 release files

1.7.0

2 release files

1.5.12

2 release files

1.5.11

2 release files

1.5.10

2 release files

1.5.9

2 release files

1.5.8

2 release files

1.5.7

2 release files

1.5.6

2 release files

1.5.5

2 release files

1.5.4

2 release files

1.5.3

2 release files

1.5.1

2 release files

1.5.0

2 release files

1.4.9

2 release files

1.4.8

2 release files

1.4.7

2 release files

1.4.6

2 release files

1.4.5

2 release files

1.4.4

2 release files

1.4.3

2 release files

1.4.2

2 release files

1.4.1

2 release files

1.4.0

2 release files

1.3.11

2 release files

1.3.10

2 release files

1.3.9

2 release files

1.3.8

2 release files

1.3.7

2 release files

1.3.6

2 release files

1.3.4

2 release files

1.3.3

2 release files

1.3.2

2 release files

1.3.1

2 release files

1.3.0

2 release files

1.2.8

2 release files

1.2.7

2 release files

1.2.6

2 release files

1.2.5

2 release files

1.2.4

2 release files

1.2.2

2 release files

1.2.1

2 release files

1.2.0

2 release files

1.1.2

2 release files

1.1.1

2 release files

1.1.0

2 release files

1.0.6

2 release files

1.0.5

2 release files

1.0.4

2 release files

1.0.3

2 release files

1.0.2

2 release files

1.0.1

2 release files

1.0.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page