From f4520c0da52f04a75282f684cb25d530a2fc1461 Mon Sep 17 00:00:00 2001 From: Amith Koujalgi Date: Wed, 25 Oct 2023 23:12:26 +0530 Subject: [PATCH] added socket-io impl --- sys_stats/api/stats.py | 15 ++++++++++----- sys_stats/app_server.py | 5 ----- sys_stats/static/index.html | 2 +- sys_stats/static/info/index.html | 2 +- sys_stats/static/info/info.js | 3 +++ sys_stats/static/ports/index.html | 2 +- sys_stats/static/stats/index.html | 2 +- sys_stats/sys_stats_cli.py | 13 ++----------- 8 files changed, 19 insertions(+), 25 deletions(-) diff --git a/sys_stats/api/stats.py b/sys_stats/api/stats.py index e59940d..5805f0a 100644 --- a/sys_stats/api/stats.py +++ b/sys_stats/api/stats.py @@ -37,7 +37,8 @@ def processes(search_keyword: str): process.pid).lower() or search_keyword in cmdline.lower(): processlist.append(process_dict) except Exception as e: - logger.error(f'Could not add process - {process.name()}. Error: {e}') + pass + # logger.error(f'Could not add process - {process.name()}. Error: {e}') processlist = sorted(processlist, key=lambda x: x['cpu_usage'], reverse=True) return processlist @@ -84,7 +85,6 @@ def __list_open_ports_with_lsof(): open_ports = [] try: cmd = ['lsof', '-i', '-n', '-P'] - # print(f'running: {" ".join(cmd)}') output = subprocess.check_output(cmd, universal_newlines=True) lines = output.split('\n') for line in lines[1:]: @@ -190,6 +190,7 @@ def __list_open_ports_standard(): return port_mappings +# noinspection PyTypeChecker def net_connections(search_keyword: str = ''): """ Lists TCP ports open for listening @@ -207,13 +208,17 @@ def net_connections(search_keyword: str = ''): else: logger.error(f'Unknown platform - {platform}') - if search_keyword == None or search_keyword == '': + if search_keyword is None or search_keyword == '': return _port_list _port_list_filtered = [] for p in _port_list: - if search_keyword in str(p['pid']) or search_keyword in p['laddr']['port'] or search_keyword.lower() in p[ - 'process_name'].lower() or search_keyword.lower() in p['process_cmd'].lower(): + if ( + search_keyword in str(p['pid']) + or search_keyword in p['laddr']['port'] + or search_keyword.lower() in p['process_name'].lower() + or search_keyword.lower() in p['process_cmd'].lower() + ): _port_list_filtered.append(p) return _port_list_filtered diff --git a/sys_stats/app_server.py b/sys_stats/app_server.py index 6bbd1a8..47d1d00 100644 --- a/sys_stats/app_server.py +++ b/sys_stats/app_server.py @@ -23,28 +23,23 @@ def connect(sid, environ): @sio.on('list_processes') def list_processes(sid, data): search_keyword = data['search_keyword'] - # print(search_keyword) sio.emit("process-list", stats.processes(search_keyword=search_keyword)) - # print('Received message:', data) @sio.on('list_ports') def list_ports(sid, data): search_keyword = data['search_keyword'] sio.emit("port-list", stats.net_connections(search_keyword)) - # print('Received message:', data) @sio.on('resource_usage') def resource_usage(sid, data): sio.emit("resource-usage", stats.resource_usage()) - # print('Received message:', data) @sio.on('kill_process') def kill_process(sid, data): _pid_to_kill = int(data['process_id']) - # print('Received message:', data) sio.emit("process-kill-status", { "status": stats.kill_process_by_pid(_pid_to_kill), "pid": _pid_to_kill diff --git a/sys_stats/static/index.html b/sys_stats/static/index.html index ba1372c..73245b3 100644 --- a/sys_stats/static/index.html +++ b/sys_stats/static/index.html @@ -68,7 +68,7 @@
  • + data-bs-placement="right" data-bs-original-title="Report issue" target="_blank">
  • diff --git a/sys_stats/static/info/index.html b/sys_stats/static/info/index.html index 42999bf..a7d3882 100644 --- a/sys_stats/static/info/index.html +++ b/sys_stats/static/info/index.html @@ -68,7 +68,7 @@
  • + data-bs-placement="right" data-bs-original-title="Report issue" target="_blank">
  • diff --git a/sys_stats/static/info/info.js b/sys_stats/static/info/info.js index a4b2bd7..8f98ade 100644 --- a/sys_stats/static/info/info.js +++ b/sys_stats/static/info/info.js @@ -3,6 +3,9 @@ let socket = io(`ws://${window.location.host}`); $(document).ready(function () { + $(function () { + $('[data-bs-toggle="tooltip"]').tooltip(); + }); console.log('Loaded socketio script'); socket.on('connect', () => { console.log('Connected to server!'); diff --git a/sys_stats/static/ports/index.html b/sys_stats/static/ports/index.html index 6a78373..dcb4b0b 100644 --- a/sys_stats/static/ports/index.html +++ b/sys_stats/static/ports/index.html @@ -68,7 +68,7 @@
  • + data-bs-placement="right" data-bs-original-title="Report issue" target="_blank">
  • diff --git a/sys_stats/static/stats/index.html b/sys_stats/static/stats/index.html index 21c2a6b..a6acab1 100644 --- a/sys_stats/static/stats/index.html +++ b/sys_stats/static/stats/index.html @@ -68,7 +68,7 @@
  • + data-bs-placement="right" data-bs-original-title="Report issue" target="_blank">
  • diff --git a/sys_stats/sys_stats_cli.py b/sys_stats/sys_stats_cli.py index dc4db39..084d0d3 100644 --- a/sys_stats/sys_stats_cli.py +++ b/sys_stats/sys_stats_cli.py @@ -4,6 +4,7 @@ import logging import os +import eventlet import uvicorn from sys_stats.app_server import app @@ -29,19 +30,9 @@ def main(): def _start(port: int = 8070): host: str = "0.0.0.0" - HTTP_GATEWAY_TIMEOUT_SECONDS = int(os.getenv("HTTP_GATEWAY_TIMEOUT_SECONDS", 180)) logging.info(f"Starting web server on {host}:{port}") - config = uvicorn.Config( - app, - host=host, - port=port, - timeout_keep_alive=HTTP_GATEWAY_TIMEOUT_SECONDS, - server_header=False, - ) - server_app = uvicorn.Server(config=config) - app.debug = True logging.info(f"Web UI at: http://{host}:{port}") - asyncio.run(server_app.serve()) + eventlet.wsgi.server(eventlet.listen((host, port)), app) if __name__ == "__main__":