2023-06-30 11:40:48 +00:00
|
|
|
#!/usr/bin/python3
|
|
|
|
import paramiko
|
|
|
|
from config import *
|
2023-07-02 17:44:37 +00:00
|
|
|
from flask import Flask, redirect, render_template, request, url_for, Response, stream_with_context
|
|
|
|
import time
|
|
|
|
from datetime import datetime
|
|
|
|
import json
|
2023-06-30 11:40:48 +00:00
|
|
|
|
|
|
|
app = Flask(__name__)
|
|
|
|
|
|
|
|
def get_remote(hostname, port, username, password, local_script_path, remote_script_path):
|
|
|
|
ssh = paramiko.SSHClient()
|
|
|
|
try:
|
|
|
|
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
|
|
|
|
ssh.connect(hostname, port, username, password)
|
|
|
|
sftp = ssh.open_sftp()
|
|
|
|
sftp.put(local_script_path, remote_script_path)
|
|
|
|
sftp.close()
|
|
|
|
stdin, stdout, stderr = ssh.exec_command(f"python3 {remote_script_path}")
|
|
|
|
output = stdout.read().decode()
|
|
|
|
return(output)
|
|
|
|
|
|
|
|
finally:
|
|
|
|
ssh.close()
|
|
|
|
|
|
|
|
def get_full(node):
|
|
|
|
remote_info = get_remote(node, ssh_conf.port, ssh_conf.username, ssh_conf.password, paths.agent_path, paths.remote_path)
|
|
|
|
full = eval(remote_info)
|
|
|
|
return full
|
|
|
|
|
|
|
|
@app.route("/", methods = ['GET'])
|
|
|
|
def get_info():
|
|
|
|
node = 'localhost'
|
|
|
|
full = get_full(node)
|
2023-07-02 17:44:37 +00:00
|
|
|
return render_template('index.html', full = full, client = client, titre = flask_conf.title, node = node)
|
2023-06-30 11:40:48 +00:00
|
|
|
|
|
|
|
@app.route("/n/<node>", methods = ['POST', 'GET'])
|
|
|
|
def get_node_info(node):
|
|
|
|
full = get_full(node)
|
2023-07-02 17:44:37 +00:00
|
|
|
return render_template('index.html', full = full, client = client, titre = flask_conf.title, node = node)
|
2023-06-30 11:40:48 +00:00
|
|
|
|
2023-07-02 17:44:37 +00:00
|
|
|
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)
|
2023-06-30 11:40:48 +00:00
|
|
|
|
2023-07-02 17:44:37 +00:00
|
|
|
@app.route("/chart-ressources/<nodename>")
|
|
|
|
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
|
2023-06-30 11:40:48 +00:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2023-07-02 17:44:37 +00:00
|
|
|
app.run(host=flask_conf.host, port=flask_conf.port, use_reloader=flask_conf.reloader, threaded=flask_conf.thread, debug=flask_conf.debug)
|