Skip to content

Commit

Permalink
option added to kill process
Browse files Browse the repository at this point in the history
  • Loading branch information
codeperfectplus committed Aug 29, 2024
1 parent f9636f0 commit 555d768
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 8 deletions.
21 changes: 17 additions & 4 deletions src/routes/process.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,29 @@
from flask import request, render_template, blueprints
import os
from flask import request, render_template, blueprints, redirect, url_for, flash

from src.config import app
from src.utils import get_top_processes

process_bp = blueprints.Blueprint("process", __name__)


@app.route("/process", methods=["GET", "POST"])
def process():
number_of_processes = 5 # Default number

if request.method == "POST":
if "kill_pid" in request.form:
# Handle killing the process
pid_to_kill = request.form.get("kill_pid")
process_name = request.form.get("process_name")
try:
os.kill(int(pid_to_kill), 9) # Sends a SIGKILL signal
flash(f"Process '{process_name}' (PID {pid_to_kill}) killed successfully.", "success")
except Exception as e:
flash(f"Failed to kill process '{process_name}' (PID {pid_to_kill}). Error: {e}", "danger")
return redirect(url_for("process")) # Refresh the page after killing process

# Handle the number of processes to display
number_of_processes = int(request.form.get("number", 5))

top_processes = get_top_processes(number_of_processes)
return render_template("process.html", processes=top_processes, number=number_of_processes)
return render_template("process.html", processes=top_processes, number=number_of_processes)
23 changes: 22 additions & 1 deletion src/templates/ext/navbar.html
Original file line number Diff line number Diff line change
@@ -1 +1,22 @@
<nav class="navbar navbar-expand-lg navbar-light bg-light shadow-sm"><a class="navbar-brand d-flex align-items-center{% if request.endpoint=='dashboard' %}active{% endif %}" href="{{ url_for('dashboard')}}"><i class="fas fa-home mr-2"></i>Home </a><button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation"><span class="navbar-toggler-icon"></span></button><div class="collapse navbar-collapse" id="navbarNav"><ul class="navbar-nav ml-auto"><li class="nav-item"><a class="nav-link{% if request.endpoint=='cpu_usage' %}active{% endif %}" href="{{ url_for('cpu_usage')}}"><i class="fas fa-microchip mr-1"></i>CPU Info </a></li><li class="nav-item"><a class="nav-link{% if request.endpoint=='memory_usage' %}active{% endif %}" href="{{ url_for('memory_usage')}}"><i class="fas fa-memory mr-1"></i>Memory Info </a></li><li class="nav-item"><a class="nav-link{% if request.endpoint=='disk_usage' %}active{% endif %}" href="{{ url_for('disk_usage')}}"><i class="fas fa-hdd mr-1"></i>Disk Info </a></li><li class="nav-item"><a class="nav-link{% if request.endpoint=='network_stats' %}active{% endif %}" href="{{ url_for('network_stats')}}"><i class="fas fa-network-wired mr-1"></i>Network Info </a></li><li class="nav-item"><a class="nav-link{% if request.endpoint=='settings' %}active{% endif %}" href="{{ url_for('settings')}}"><i class="fas fa-cog mr-1"></i>Settings </a></li><li class="nav-item"><a class="nav-link{% if request.endpoint=='process' %}active{% endif %}" href="{{ url_for('process')}}"><i class="fas fa-tasks mr-1"></i>Process </a></li></ul></div></nav>
<nav class="navbar navbar-expand-lg navbar-light bg-light shadow-sm"><a
class="navbar-brand d-flex align-items-center{% if request.endpoint=='dashboard' %}active{% endif %}"
href="{{ url_for('dashboard')}}"><i class="fas fa-home mr-2"></i>Home </a><button class="navbar-toggler"
type="button" data-toggle="collapse" data-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false"
aria-label="Toggle navigation"><span class="navbar-toggler-icon"></span></button>
<div class="collapse navbar-collapse" id="navbarNav">
<ul class="navbar-nav ml-auto">
<li class="nav-item"><a class="nav-link{% if request.endpoint=='cpu_usage' %}active{% endif %}"
href="{{ url_for('cpu_usage')}}"><i class="fas fa-microchip mr-1"></i>CPU Info </a></li>
<li class="nav-item"><a class="nav-link{% if request.endpoint=='memory_usage' %}active{% endif %}"
href="{{ url_for('memory_usage')}}"><i class="fas fa-memory mr-1"></i>Memory Info </a></li>
<li class="nav-item"><a class="nav-link{% if request.endpoint=='disk_usage' %}active{% endif %}"
href="{{ url_for('disk_usage')}}"><i class="fas fa-hdd mr-1"></i>Disk Info </a></li>
<li class="nav-item"><a class="nav-link{% if request.endpoint=='network_stats' %}active{% endif %}"
href="{{ url_for('network_stats')}}"><i class="fas fa-network-wired mr-1"></i>Network Info </a></li>
<li class="nav-item"><a class="nav-link{% if request.endpoint=='settings' %}active{% endif %}"
href="{{ url_for('settings')}}"><i class="fas fa-cog mr-1"></i>Settings </a></li>
<li class="nav-item"><a class="nav-link{% if request.endpoint=='process' %}active{% endif %}"
href="{{ url_for('process')}}"><i class="fas fa-tasks mr-1"></i>Process </a></li>
</ul>
</div>
</nav>
47 changes: 46 additions & 1 deletion src/templates/process.html
Original file line number Diff line number Diff line change
@@ -1 +1,46 @@
{% extends 'base.html' %}{% block title %}CPU Usage Details{% endblock %}{% block extra_head %}<link rel="stylesheet" href="{{ url_for('static', filename='css/process.css')}}">{% endblock %}{% block content %}<div class="container"><h1>Top Processes by Memory Usage</h1><form method="post" class="form-control"><label for="number">Number of Processes:</label><input type="number" id="number" name="number" min="1" value="{{ number}}"><button type="submit">Show</button></form><table><thead><tr><th>Process Name</th><th>CPU Usage (%)</th><th>Memory Usage (%)</th></tr></thead><tbody>{% for process in processes %} <tr><td>{{ process[0]}}</td><td>{{ process[1]}}</td><td>{{ process[2]}}</td></tr>{% endfor %} </tbody></table></div>{% endblock %}
{% extends 'base.html' %}
{% block title %}CPU Usage Details{% endblock %}
{% block extra_head %}
<link rel="stylesheet" href="{{ url_for('static', filename='css/process.css') }}">
{% endblock %}
{% block content %}
<div class="container">
<h1>Top Processes by Memory Usage</h1>

<!-- Display flash messages -->
{% include 'ext/message.html' %}

<form method="post" class="form-control">
<label for="number">Number of Processes:</label>
<input type="number" id="number" name="number" min="1" value="{{ number }}">
<button type="submit">Show</button>
</form>

<table>
<thead>
<tr>
<th>Process Name</th>
<th>CPU Usage (%)</th>
<th>Memory Usage (%)</th>
<th>Action</th>
</tr>
</thead>
<tbody>
{% for process in processes %}
<tr>
<td>{{ process[0] }}</td>
<td>{{ process[1] }}</td>
<td>{{ process[2] }}</td>
<td>
<form method="post">
<input type="hidden" name="kill_pid" value="{{ process[3] }}">
<input type="hidden" name="process_name" value="{{ process[0] }}">
<button type="submit">Kill</button>
</form>
</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
{% endblock %}
7 changes: 5 additions & 2 deletions src/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,15 +99,18 @@ def get_cpu_temp():
critical_temp = temp.critical
return current_temp, high_temp, critical_temp

import psutil

def get_top_processes(number=5):
"""Get the top processes by memory usage."""
processes = [
(p.info['name'], p.info['cpu_percent'], round(p.info['memory_percent'], 2))
for p in sorted(psutil.process_iter(['name', 'cpu_percent', 'memory_percent']),
(p.info['name'], p.info['cpu_percent'], round(p.info['memory_percent'], 2), p.info['pid'])
for p in sorted(psutil.process_iter(['name', 'cpu_percent', 'memory_percent', 'pid']),
key=lambda p: p.info['memory_percent'], reverse=True)[:number]
]
return processes


def get_system_info():
""" Get system information and store it in the database. """
print("Getting system information...")
Expand Down

0 comments on commit 555d768

Please sign in to comment.