Skip to content

Commit

Permalink
added socket-io impl
Browse files Browse the repository at this point in the history
  • Loading branch information
Amith Koujalgi committed Oct 25, 2023
1 parent 5fce79d commit f4520c0
Show file tree
Hide file tree
Showing 8 changed files with 19 additions and 25 deletions.
15 changes: 10 additions & 5 deletions sys_stats/api/stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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:]:
Expand Down Expand Up @@ -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
Expand All @@ -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
5 changes: 0 additions & 5 deletions sys_stats/app_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion sys_stats/static/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@
<li>
<a href="https://github.com/amithkoujalgi/sys-stats/issues" class="nav-link py-3 px-2" title=""
data-bs-toggle="tooltip"
data-bs-placement="right" data-bs-original-title="Report issues" target="_blank">
data-bs-placement="right" data-bs-original-title="Report issue" target="_blank">
<i class="bi-exclamation-triangle fs-1"></i>
</a>
</li>
Expand Down
2 changes: 1 addition & 1 deletion sys_stats/static/info/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@
<li>
<a href="https://github.com/amithkoujalgi/sys-stats/issues" class="nav-link py-3 px-2" title=""
data-bs-toggle="tooltip"
data-bs-placement="right" data-bs-original-title="Report issues" target="_blank">
data-bs-placement="right" data-bs-original-title="Report issue" target="_blank">
<i class="bi-exclamation-triangle fs-1"></i>
</a>
</li>
Expand Down
3 changes: 3 additions & 0 deletions sys_stats/static/info/info.js
Original file line number Diff line number Diff line change
Expand Up @@ -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!');
Expand Down
2 changes: 1 addition & 1 deletion sys_stats/static/ports/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@
<li>
<a href="https://github.com/amithkoujalgi/sys-stats/issues" class="nav-link py-3 px-2" title=""
data-bs-toggle="tooltip"
data-bs-placement="right" data-bs-original-title="Report issues" target="_blank">
data-bs-placement="right" data-bs-original-title="Report issue" target="_blank">
<i class="bi-exclamation-triangle fs-1"></i>
</a>
</li>
Expand Down
2 changes: 1 addition & 1 deletion sys_stats/static/stats/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@
<li>
<a href="https://github.com/amithkoujalgi/sys-stats/issues" class="nav-link py-3 px-2" title=""
data-bs-toggle="tooltip"
data-bs-placement="right" data-bs-original-title="Report issues" target="_blank">
data-bs-placement="right" data-bs-original-title="Report issue" target="_blank">
<i class="bi-exclamation-triangle fs-1"></i>
</a>
</li>
Expand Down
13 changes: 2 additions & 11 deletions sys_stats/sys_stats_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import logging
import os

import eventlet
import uvicorn

from sys_stats.app_server import app
Expand All @@ -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__":
Expand Down

0 comments on commit f4520c0

Please sign in to comment.