From 90158f5a6ebc7179c0c3e627211f96691bdd41f2 Mon Sep 17 00:00:00 2001 From: Victor Date: Fri, 15 Dec 2023 22:54:51 +0100 Subject: [PATCH] =?UTF-8?q?Probando=20hacer=20elemntos=20dinamicos=20sin?= =?UTF-8?q?=20exito=20y=20a=C3=B1adiendo=202=20metodo=20adicionals=20para?= =?UTF-8?q?=20informacion=20del=20cpu=20y=20contenedores?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 ++ main.py | 71 ++++++++++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 72 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 72b7d15..38ded22 100644 --- a/README.md +++ b/README.md @@ -4,3 +4,5 @@ El proyecto consiste de crear un menu que permite al usuario a ver informacion s Puede ver informacion como Hilo por nucleo, sobre la memoria, sockets, modulo de plataforma y sistema operativa, conexiones tcp y otras; Para mostar la informacion usamos metodos para que el menu es facil de entender , como por ejemplo el medotodo de mostar la informaciĆ³n sobre el uso de cpu, memoria y swap en forma de barra de diferentes colores. + +No tenia exito en hacer elmentos dinamicos, como El uso del CPU, memoria y swap, que encontre problemas en limpiar la entrada anterior o en salir de bucle que imprime la informaciĆ³n diff --git a/main.py b/main.py index a9a82df..6fe9340 100644 --- a/main.py +++ b/main.py @@ -1,10 +1,13 @@ +import operator import os import platform import socket import subprocess import psutil import matplotlib.pyplot as plt - +from numpy.distutils import cpuinfo +import platform +import cpuinfo threads_por_nucleo = psutil.cpu_count(logical=False) memoria = psutil.virtual_memory() @@ -35,7 +38,31 @@ def get_process_states(): other_count += 1 return running_count, sleeping_count, other_count +def get_container_info(): + container_info = [] + + # Iterate through all running processes + for process in psutil.process_iter(['pid', 'name', 'cpu_percent']): + try: + # Extract information for each process + pid = process.info['pid'] + name = process.info['name'] + cpu_percent = process.info['cpu_percent'] + + # Append the information to the list + container_info.append({'pid': pid, 'name': name, 'cpu_percent': cpu_percent}) + except (psutil.NoSuchProcess, psutil.AccessDenied, psutil.ZombieProcess): + pass + return container_info +def display_top_containers(container_info, n=3): + # Sort the container_info list based on CPU percent + sorted_containers = sorted(container_info, key=operator.itemgetter('cpu_percent'), reverse=True) + + # Display information for the top N containers + print(f"Top {n} containers by CPU usage:") + for i, container in enumerate(sorted_containers[:n], 1): + print(f"{i}. PID: {container['pid']}, Name: {container['name']}, CPU Usage: {container['cpu_percent']:.2f}%") def get_running_processes(): # Using list comprehension to get information about all running processes return [proc.info for proc in psutil.process_iter(['pid', 'name', 'username'])] @@ -43,6 +70,40 @@ def get_tcp_connections(): connections = psutil.net_connections(kind='inet') return connections + +def get_cpu_info(): + cpu_info = {} + + # Get generic information about the CPU using platform + cpu_info['architecture'] = platform.architecture() + cpu_info['machine'] = platform.machine() + cpu_info['processor'] = platform.processor() + + # Get detailed CPU information using cpuinfo + cpu_info.update(cpuinfo.get_cpu_info()) + + return cpu_info + + +def display_cpu_info(cpu_info): + print("CPU Information:") + print(f"Architecture: {cpu_info.get('architecture', 'N/A')}") + print(f"Machine: {cpu_info.get('machine', 'N/A')}") + print(f"Processor: {cpu_info.get('processor', 'N/A')}") + print(f"Brand: {cpu_info.get('brand_raw', 'N/A')}") + print( + f"Model: {cpu_info.get('brand_raw', 'N/A')} {cpu_info.get('family', 'N/A')} {cpu_info.get('model', 'N/A')} ({cpu_info.get('arch', 'N/A')})") + + # Check if the key 'hz_advertised_raw' exists before accessing it + if 'hz_advertised_raw' in cpu_info: + clock_speed_ghz = cpu_info['hz_advertised_raw'][0] / 1e6 + print(f"Clock Speed: {clock_speed_ghz:.2f} GHz") + else: + print("Clock Speed: N/A") + + print(f"Cores: {cpu_info.get('count', 'N/A')}") + + def procesesRunning(): # Get and print information about all running processes running_processes = get_running_processes() @@ -70,6 +131,8 @@ while ans: 10. Socket info 11. Modulo de plataforma 12. Numero de precesos activos,dormidos y otros + 13. Mostar Contenedores sorteados por CPU + 14. Informacion del CPU 0.Salir """) ans=input("Elige que tipo de informacio deseas saber:") @@ -130,6 +193,12 @@ while ans: print(f"Processos activos: {running}") print(f"procesos dormiendo: {sleeping}") print(f"Otros procesos: {other}") + elif ans =="13": + container_info = get_container_info() + display_top_containers(container_info) + elif ans =="14": + cpu_info = get_cpu_info() + display_cpu_info(cpu_info) elif ans =="0": print("\n Adios") ans = None