Skip to content

Instantly share code, notes, and snippets.

@nyck33
Created November 17, 2023 01:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nyck33/2c20c7b3f5fb4b80727626993b1d28e7 to your computer and use it in GitHub Desktop.
Save nyck33/2c20c7b3f5fb4b80727626993b1d28e7 to your computer and use it in GitHub Desktop.
Monitor Ubuntu logs for errors
import re
import subprocess
from time import sleep
log_file_path = '/var/log/syslog'
check_interval = 60 # in seconds
def notify(message):
subprocess.Popen(['notify-send', message])
def check_logs():
with open(log_file_path, 'r') as file:
lines = file.readlines()
for line in lines[-100:]: # Check the last 100 lines
if re.search(r'.*CRITICAL.*|.*ERROR.*|.*ALERT.*', line):
notify("Critical error detected.")
elif re.search(r'kernel:.*panic.*|systemd:.*Failed.*|.*Segmentation fault.*', line):
notify("System error detected.")
elif re.search(r'.*Out of memory.*|.*OOM killer activated.*', line):
notify("Resource exhaustion detected.")
elif re.search(r'.*authentication failure.*|.*invalid user.*', line):
notify("Authentication failure detected.")
elif re.search(r'.*EXT4-fs error.*|.*filesystem error.*', line):
notify("Filesystem error detected.")
elif re.search(r'systemd:.*unit.*failed.*', line):
notify("Systemd service failure detected.")
elif re.search(r'.*D-Bus error.*', line):
notify("D-Bus error detected.")
elif re.search(r'.*disk error.*|.*thermal overheating.*', line):
notify("Hardware issue detected.")
while True:
check_logs()
sleep(check_interval)

Change permissions

chmod +x log_monitor.py

run the script

nohup /path/to/log_monitor.py &

Alternative to use Pure Python:

To create a Python script that sends a desktop notification, you can use the notify2 package, which is a pure Python implementation for sending desktop notifications. It's more flexible and integrated within Python compared to using subprocess to call notify-send. First, you'll need to install the notify2 package, which you can do via pip:

pip install notify2

Here's a simple example of how to use notify2 in Python to send a notification:

import notify2

def send_notification(title, message):
    # Initialize the d-bus connection
    notify2.init("Log Notifier")

    # Create Notification object
    n = notify2.Notification(title, message)

    # Set the urgency level
    n.set_urgency(notify2.URGENCY_NORMAL)

    # Set the timeout (milliseconds)
    n.set_timeout(10000)

    # Show the notification
    n.show()

send_notification("Test Notification", "This is a test message.")

This script initializes a connection to the D-Bus (Desktop Bus) interface for notifications, creates a notification object with a title and message, sets the urgency and timeout for the notification, and then shows it.

Integration with Log Monitoring:

To integrate this into your log monitoring script, replace the notify function with the send_notification function. For example:

def check_logs():
    with open(log_file_path, 'r') as file:
        lines = file.readlines()

    for line in lines[-100:]:  # Check the last 100 lines
        if re.search(r'.*CRITICAL.*|.*ERROR.*|.*ALERT.*', line):
            send_notification("System Alert", "Critical error detected.")
        # ... other conditions ...

# At the beginning of your script
notify2.init("Log Monitor")

This modification uses notify2 for sending notifications instead of calling notify-send via the subprocess module. It integrates more seamlessly within the Python script and provides more control over the notification properties.

check status

ps -ef | grep log_monitor.py

stop script by finding PID using top or other command then

kill [PID]

or

pkill -f log_monitor.py

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment