Python Event Monitoring Project

|

Python Login Event Tracking & Report Generation

Python Automation, Event Processing & Debugging

Python | Event Tracking | Dictionary Processing | Debugging | Report Generation

This project focused on processing login and logout events using Python to identify currently active users on different machines. The project involved sorting event data, tracking users through dictionaries and sets, resolving a logout-handling error, and generating a machine-wise user activity report.

The project demonstrates practical Python programming skills including function design, class construction, list processing, dictionary management, set operations, debugging, and formatted report generation.

Project Objective

The objective was to create a Python-based event tracking workflow that processes login and logout events, updates machine-user relationships, prevents runtime errors from invalid logout events, and generates a clean report of active users by machine.

Step 1: Define Event Date Sorting Function

This function returns the event date and is used as the sorting key before processing login and logout events.

def get_event_date(event):
    return event.date

Step 2: Track Current Users by Machine

This function sorts events by date, creates a dictionary of machines, and updates active users based on login and logout activity.

def current_users(events):
    events.sort(key=get_event_date)
    machines = {}

    for event in events:
        if event.machine not in machines:
            machines[event.machine] = set()

        if event.type == "login":
            machines[event.machine].add(event.user)

        elif event.type == "logout":
            machines[event.machine].discard(event.user)

    return machines

Debugging Note

The original version used remove(), which caused an error when a logout event appeared for a user who was not currently logged in. The updated version uses discard(), which safely avoids an error if the user is not present in the set.

Step 3: Generate Machine User Report

This function prints only machines that currently have active users.

def generate_report(machines):
    for machine, users in machines.items():
        if len(users) > 0:
            user_list = ", ".join(users)
            print("{}: {}".format(machine, user_list))

Step 4: Create Event Class

The Event class stores event date, event type, machine name, and username for each activity record.

class Event:
    def __init__(self, event_date, event_type, machine_name, user):
        self.date = event_date
        self.type = event_type
        self.machine = machine_name
        self.user = user

Step 5: Create Event Dataset

A list of login and logout events was created to simulate user activity across different machines.

events = [
    Event('2020-01-21 12:45:56', 'login', 'myworkstation.local', 'jordan'),
    Event('2020-01-22 15:53:42', 'logout', 'webserver.local', 'jordan'),
    Event('2020-01-21 18:53:21', 'login', 'webserver.local', 'lane'),
    Event('2020-01-22 10:25:34', 'logout', 'myworkstation.local', 'jordan'),
    Event('2020-01-21 08:20:01', 'login', 'webserver.local', 'jordan'),
    Event('2020-01-23 11:24:35', 'logout', 'mailserver.local', 'chris'),
]

Step 6: Process Events

The event list was processed to identify which users remained active on each machine.

users = current_users(events)
print(users)
Output
{'webserver.local': {'lane'}, 'myworkstation.local': set(), 'mailserver.local': set()}

Step 7: Generate Final Report

The final report displays only machines with active users.

generate_report(users)
Output
webserver.local: lane

Key Skills Demonstrated

Project Impact

This project demonstrates how Python can be used to process system events, track user activity, identify active sessions, and generate useful reports for administrative or security monitoring purposes.

The debugging improvement from remove() to discard() shows practical problem-solving and safer event handling when processing inconsistent or unexpected activity logs.

Project Conclusion

The project successfully processed machine login and logout events, resolved a runtime error caused by unmatched logout activity, and generated a clean report showing active users by machine.

This project supports core Python automation skills relevant to system administration, security monitoring, event analysis, and cybersecurity workflow development.

Chat Now