Pymonit/app.py

97 lines
3.8 KiB
Python

#!/usr/bin/python3
import paramiko
from config import *
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:
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_boot_time(psboot):
boot_time = datetime.fromtimestamp(psboot - 60)
return str(boot_time)
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)
boottime = get_boot_time(full['boot_time'])
return render_template('index.html', full = full, client = client, titre = flask_conf.title, node = node, boottime = boottime)
@app.route("/n/<node>", methods = ['POST', 'GET'])
def get_node_info(node):
full = get_full(node)
boottime = get_boot_time(full['boot_time'])
return render_template('index.html', full = full, client = client, titre = flask_conf.title, node = node, boottime = boottime)
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_percent": full['cpu']['percent'],
"mem_percent": full['mem']['percent'],
"swap_percent": full['swap']['percent'],
"cpu_time_user": full['cpu']['time_user'],
"cpu_time_nice": full['cpu']['time_nice'],
"cpu_time_system": full['cpu']['time_system'],
"cpu_time_idle": full['cpu']['time_idle'],
"cpu_time_iowait": full['cpu']['time_iowait'],
"cpu_time_irq": full['cpu']['time_irq'],
"cpu_time_softirq": full['cpu']['time_softirq'],
"cpu_time_steal": full['cpu']['time_steal'],
"cpu_time_guest": full['cpu']['time_guest'],
"cpu_time_guest_nice": full['cpu']['time_nice'],
"mem_total": full['mem']['total'],
"mem_available": full['mem']['available'],
"mem_used": full['mem']['used'],
"mem_free": full['mem']['free'],
"mem_active": full['mem']['active'],
"mem_inactive": full['mem']['inactive'],
"mem_buffers": full['mem']['buffers'],
"mem_cached": full['mem']['cached'],
"mem_shared": full['mem']['shared'],
"mem_slab": full['mem']['slab'],
"swap_total": full['swap']['total'],
"swap_used": full['swap']['used'],
"swap_free": full['swap']['free'],
"swap_sin": full['swap']['sin'],
"swap_sout": full['swap']['sout'],
}
)
yield f"data:{json_data}\n\n"
time.sleep(1)
@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
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)