Real-time log watching and containerized metrics app
# log_watcher.py
import time
import os
LOG_FILE = "/var/log/syslog" # Change to your log path
KEYWORDS = ["ERROR", "CRITICAL"]
def tail(file):
file.seek(0, os.SEEK_END)
while True:
line = file.readline()
if not line:
time.sleep(0.5)
continue
if any(kw in line for kw in KEYWORDS):
print(f"[ALERT] {line.strip()}")
if __name__ == "__main__":
try:
with open(LOG_FILE, "r") as f:
print(f"Monitoring {LOG_FILE} for {KEYWORDS}...")
tail(f)
except FileNotFoundError:
print("Log file not found!")
# app.py (Flask with Prometheus metrics)
from flask import Flask
from prometheus_client import Counter, generate_latest
app = Flask(__name__)
REQUESTS = Counter('app_requests_total', 'Total number of requests')
@app.route('/')
def home():
REQUESTS.inc()
return "Hello from DevOps Flask App!"
@app.route('/metrics')
def metrics():
return generate_latest(), 200, {'Content-Type': 'text/plain'}
if __name__ == '__main__':
app.run(host='0.0.0.0', port=5000)
# Dockerfile
FROM python:3.10-slim
WORKDIR /app
COPY app.py requirements.txt ./
RUN pip install -r requirements.txt
EXPOSE 5000
CMD ["python", "app.py"]
# requirements.txt
flask
prometheus_client
# docker-compose.yaml
version: '3'
services:
web:
build: .
ports:
- "5000:5000"
prometheus:
image: prom/prometheus
volumes:
- ./prometheus.yml:/etc/prometheus/prometheus.yml
ports:
- "9090:9090"
# prometheus.yml
scrape_configs:
- job_name: 'flask_app'
static_configs:
- targets: ['web:5000']