Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FEAT(ws): Containerize the Log Monitoring Tool #336

Open
vfedotovs opened this issue Nov 6, 2024 · 0 comments
Open

FEAT(ws): Containerize the Log Monitoring Tool #336

vfedotovs opened this issue Nov 6, 2024 · 0 comments

Comments

@vfedotovs
Copy link
Owner

A Python script (log_monitor.py) will read the ws_main.log file, search for log levels (INFO, WARNING, ERROR, TRACEBACK), and count the occurrences of each. The results will be stored in an output log file (log_summary.log).

import re
from collections import defaultdict

# Define log file paths
input_log_path = "/logs/ws_main.log"
output_log_path = "/logs/log_summary.log"

# Initialize counts
log_counts = defaultdict(int)

# Define log level keywords
log_levels = ["INFO", "WARNING", "ERROR", "TRACEBACK"]

try:
    with open(input_log_path, "r") as log_file:
        for line in log_file:
            for level in log_levels:
                if re.search(fr"\b{level}\b", line):
                    log_counts[level] += 1

    # Write counts to output file
    with open(output_log_path, "w") as summary_file:
        for level in log_levels:
            summary_file.write(f"{level}: {log_counts[level]}\n")

    print("Log summary written to log_summary.log")
except FileNotFoundError:
    print(f"Error: Log file {input_log_path} not found.")

Dockerfile

FROM python:3.9

# Set working directory
WORKDIR /app

# Copy the Python script and logs directory
COPY log_monitor.py /app/
COPY logs /logs  # Assuming logs are stored in a 'logs' directory in the project

# Install any necessary Python packages
RUN pip install --no-cache-dir -r requirements.txt

# Run the script to monitor logs
CMD ["python", "log_monitor.py"]

Docker-cmpose

version: '3.8'
services:
  log-monitor:
    build: .
    volumes:
      - ./logs:/logs  # Bind the logs directory for persistent access
    restart: on-failure

Build and Run the Container

Build the image:

docker build -t log-monitor .

# Run the container:
docker run --rm -v $(pwd)/logs:/logs log-monitor

Automating the Daily Execution

Using Docker Compose with Restart Policies: Configure Docker Compose with a restart: daily policy.
Using Cron with Docker: Alternatively, you can set up a host-based cron job to restart the container daily:

0 0 * * * docker restart log-monitor
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant