|
1 | 1 | from flask import Flask, request, jsonify |
2 | 2 | from atproto import Client, client_utils |
| 3 | +from datetime import datetime |
| 4 | +import subprocess |
3 | 5 |
|
4 | 6 | app = Flask(__name__) |
5 | 7 |
|
| 8 | +@app.route('/') |
| 9 | +def index(): |
| 10 | + try: |
| 11 | + # Get uptime using "uptime" command and parse output |
| 12 | + # ["cat", "/proc/uptime", "|", "sed '{print $2}'"], |
| 13 | + uptime_output = subprocess.run( |
| 14 | + ["uptime"], |
| 15 | + capture_output=True, |
| 16 | + text=True, |
| 17 | + shell=True |
| 18 | + ) |
| 19 | + |
| 20 | + uptimeoutput = "N/A" |
| 21 | + |
| 22 | + if uptime_output.returncode == 0: |
| 23 | + uptimeoutput = uptime_output.stdout |
| 24 | + |
| 25 | + # Get current time |
| 26 | + current_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S") |
| 27 | + |
| 28 | + # Read last 50 lines of log file |
| 29 | + log_output = subprocess.run( |
| 30 | + ["tail", "-n", "50", "/var/log/pybsposter.log"], |
| 31 | + capture_output=True, |
| 32 | + text=True |
| 33 | + ) |
| 34 | + |
| 35 | + if log_output.returncode == 0: |
| 36 | + logs = log_output.stdout.split('\n') |
| 37 | + logs = [log.strip() for log in logs if log] |
| 38 | + else: |
| 39 | + logs = ["No log entries found"] |
| 40 | + |
| 41 | + except subprocess.CalledProcessError as e: |
| 42 | + print(f"Error: {e}") |
| 43 | + uptime_seconds, current_time, logs = "N/A", "N/A", ["Error reading metrics or logs"] |
| 44 | + |
| 45 | + return f""" |
| 46 | + <pre> |
| 47 | + ______ ______ ______ _ |
| 48 | + | ___ \ | ___ \ | ___ \ | | |
| 49 | + | |_/ / _ _ | |_/ / ___ | |_/ / ___ ___ | |_ ___ _ __ |
| 50 | + | __/ | | | || ___ \/ __|| __/ / _ \ / __|| __| / _ \| '__| |
| 51 | + | | | |_| || |_/ /\__ \| | | (_) |\__ \| |_ | __/| | |
| 52 | + \_| \__, |\____/ |___/\_| \___/ |___/ \__| \___||_| |
| 53 | + __/ | |
| 54 | + |___/ |
| 55 | + </pre> |
| 56 | + <br/> |
| 57 | + <br/> |
| 58 | + <h1>Container Metrics</h1> |
| 59 | + <p>Uptime: {uptimeoutput}</p> |
| 60 | + <p>Last Updated: {current_time}</p> |
| 61 | + <h2>Recent Logs:</h2> |
| 62 | + <ul>{''.join(f'<li>{log}</li>' for log in logs)}</ul> |
| 63 | + """ |
| 64 | + |
6 | 65 | @app.route('/post', methods=['POST']) |
7 | 66 | def handle_post(): |
8 | 67 | data = request.json |
|
0 commit comments