diff --git a/README.md b/README.md index 99b90a4..d30a135 100644 --- a/README.md +++ b/README.md @@ -107,17 +107,11 @@ Configure the flask configuration according to your needs (port,host,...) in con class flask_conf: port = 8090 host = "0.0.0.0" - thread = False + thread = True debug = False reloader = False - ssl = 'adhoc' -``` -NB: the parameter ssl = 'adhoc' will activate the HTTPS with a self certificate. -If you get an error, you should add this package : - -```bash -pip3 install pyopenssl ``` +NB: Let thread = True, else this will take a time to switch from node to another node ## Path configuration ```bash diff --git a/app.py b/app.py index cbdd759..254f2a6 100755 --- a/app.py +++ b/app.py @@ -1,11 +1,13 @@ #!/usr/bin/python3 import paramiko from config import * -from flask import Flask, redirect, render_template, request, url_for, Response +from flask import Flask, redirect, render_template, request, url_for, Response, stream_with_context +import time +from datetime import datetime +import json app = Flask(__name__) - def get_remote(hostname, port, username, password, local_script_path, remote_script_path): ssh = paramiko.SSHClient() try: @@ -30,15 +32,34 @@ def get_full(node): def get_info(): node = 'localhost' full = get_full(node) - return render_template('index.html', full = full, client = client, titre = flask_conf.title) + return render_template('index.html', full = full, client = client, titre = flask_conf.title, node = node) @app.route("/n/", methods = ['POST', 'GET']) def get_node_info(node): full = get_full(node) - return render_template('index.html', full = full, client = client, titre = flask_conf.title) + return render_template('index.html', full = full, client = client, titre = flask_conf.title, node = node) +def get_ressources(nodename): + while True: + full = get_full(nodename) + json_data = json.dumps( + { + "time": datetime.now().strftime("%Y-%m-%d %H:%M:%S"), + "cpu": full['cpu']['percent'], + "mem": full['mem']['percent'], + "swap": full['swap']['percent'], + } + ) + yield f"data:{json_data}\n\n" + time.sleep(1) +@app.route("/chart-ressources/") +def chart_data(nodename) -> Response: + response = Response(stream_with_context(get_ressources(nodename)), mimetype="text/event-stream") + response.headers["Cache-Control"] = "no-cache" + response.headers["X-Accel-Buffering"] = "no" + return response if __name__ == "__main__": - app.run(host=flask_conf.host, port=flask_conf.port, use_reloader=flask_conf.reloader, threaded=flask_conf.thread, debug=flask_conf.debug, ssl_context=flask_conf.ssl) + app.run(host=flask_conf.host, port=flask_conf.port, use_reloader=flask_conf.reloader, threaded=flask_conf.thread, debug=flask_conf.debug) diff --git a/config.py b/config.py index d305e5a..a762e7b 100644 --- a/config.py +++ b/config.py @@ -13,10 +13,9 @@ class ssh_conf: class flask_conf: port = 8090 host = "0.0.0.0" - thread = False + thread = True debug = False reloader = False - ssl = 'adhoc' title = 'Pymonit' #Path configuration diff --git a/templates/index.html b/templates/index.html index d6f8dc4..0e37c14 100644 --- a/templates/index.html +++ b/templates/index.html @@ -7,6 +7,9 @@ + + + @@ -15,6 +18,8 @@
+ +