updated
This commit is contained in:
91
Flask-monitoring/app.py
Normal file
91
Flask-monitoring/app.py
Normal file
@@ -0,0 +1,91 @@
|
||||
from flask import Flask, request, jsonify, render_template, redirect, url_for
|
||||
from flask_sqlalchemy import SQLAlchemy
|
||||
from datetime import datetime, timedelta
|
||||
import os
|
||||
|
||||
app = Flask(__name__)
|
||||
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///logs.db'
|
||||
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
|
||||
db = SQLAlchemy(app)
|
||||
|
||||
class Log(db.Model):
|
||||
id = db.Column(db.Integer, primary_key=True)
|
||||
hostname = db.Column(db.String(100), nullable=False)
|
||||
message = db.Column(db.String(500), nullable=False)
|
||||
timestamp = db.Column(db.DateTime, default=datetime.utcnow)
|
||||
|
||||
# Create the database if it does not exist
|
||||
if not os.path.exists('instance/logs.db'):
|
||||
with app.app_context():
|
||||
db.create_all()
|
||||
|
||||
# Flag to check if tables are created
|
||||
tables_created = False
|
||||
|
||||
@app.before_request
|
||||
def create_tables():
|
||||
global tables_created
|
||||
if not tables_created:
|
||||
db.create_all()
|
||||
tables_created = True
|
||||
|
||||
@app.route('/log', methods=['POST'])
|
||||
def log_message():
|
||||
data = request.get_json()
|
||||
hostname = data.get('hostname')
|
||||
message = data.get('message')
|
||||
if hostname and message:
|
||||
new_log = Log(hostname=hostname, message=message)
|
||||
db.session.add(new_log)
|
||||
db.session.commit()
|
||||
return jsonify({'status': 'success', 'message': 'Log saved'}), 201
|
||||
return jsonify({'status': 'error', 'message': 'Invalid data'}), 400
|
||||
|
||||
@app.route('/logs', methods=['GET'])
|
||||
def get_logs():
|
||||
logs = Log.query.all()
|
||||
return jsonify([{'id': log.id, 'hostname': log.hostname, 'message': log.message, 'timestamp': log.timestamp} for log in logs])
|
||||
|
||||
@app.route('/cleanup', methods=['DELETE'])
|
||||
def cleanup_logs():
|
||||
cutoff_date = datetime.utcnow() - timedelta(days=10)
|
||||
Log.query.filter(Log.timestamp < cutoff_date).delete()
|
||||
db.session.commit()
|
||||
return jsonify({'status': 'success', 'message': 'Old logs deleted'})
|
||||
|
||||
@app.route('/')
|
||||
def index():
|
||||
# Get the latest log entry for each board
|
||||
latest_logs = db.session.query(Log).order_by(Log.timestamp.desc()).all()
|
||||
boards = {}
|
||||
for log in latest_logs:
|
||||
if log.hostname not in boards:
|
||||
boards[log.hostname] = log
|
||||
|
||||
# Determine the status of each board
|
||||
board_status = []
|
||||
for hostname, log in boards.items():
|
||||
time_diff = datetime.utcnow() - log.timestamp
|
||||
status = 'Online' if time_diff.total_seconds() <= 20 else 'Possible Offline'
|
||||
board_status.append({'hostname': hostname, 'status': status, 'last_seen': log.timestamp})
|
||||
|
||||
return render_template('index.html', boards=board_status)
|
||||
|
||||
@app.route('/board/<hostname>')
|
||||
def view_board(hostname):
|
||||
logs = Log.query.filter_by(hostname=hostname).order_by(Log.timestamp.desc()).all()
|
||||
return render_template('board.html', hostname=hostname, logs=logs)
|
||||
|
||||
@app.route('/delete_board/<hostname>', methods=['POST'])
|
||||
def delete_board(hostname):
|
||||
Log.query.filter_by(hostname=hostname).delete()
|
||||
db.session.commit()
|
||||
return redirect(url_for('index'))
|
||||
|
||||
@app.route('/board/<hostname>/logs', methods=['GET'])
|
||||
def get_board_logs(hostname):
|
||||
logs = Log.query.filter_by(hostname=hostname).order_by(Log.timestamp.desc()).all()
|
||||
return jsonify({'logs': [{'timestamp': log.timestamp, 'message': log.message} for log in logs]})
|
||||
|
||||
if __name__ == '__main__':
|
||||
app.run(host='0.0.0.0', port=5000)
|
||||
Reference in New Issue
Block a user