@ -1,10 +1,13 @@
import operator
import os
import os
import platform
import platform
import socket
import socket
import subprocess
import subprocess
import psutil
import psutil
import matplotlib . pyplot as plt
import matplotlib . pyplot as plt
from numpy . distutils import cpuinfo
import platform
import cpuinfo
threads_por_nucleo = psutil . cpu_count ( logical = False )
threads_por_nucleo = psutil . cpu_count ( logical = False )
memoria = psutil . virtual_memory ( )
memoria = psutil . virtual_memory ( )
@ -35,7 +38,31 @@ def get_process_states():
other_count + = 1
other_count + = 1
return running_count , sleeping_count , other_count
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 ( ) :
def get_running_processes ( ) :
# Using list comprehension to get information about all running processes
# Using list comprehension to get information about all running processes
return [ proc . info for proc in psutil . process_iter ( [ ' pid ' , ' name ' , ' username ' ] ) ]
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 ' )
connections = psutil . net_connections ( kind = ' inet ' )
return connections
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 ( ) :
def procesesRunning ( ) :
# Get and print information about all running processes
# Get and print information about all running processes
running_processes = get_running_processes ( )
running_processes = get_running_processes ( )
@ -70,6 +131,8 @@ while ans:
10. Socket info
10. Socket info
11. Modulo de plataforma
11. Modulo de plataforma
12. Numero de precesos activos , dormidos y otros
12. Numero de precesos activos , dormidos y otros
13. Mostar Contenedores sorteados por CPU
14. Informacion del CPU
0. Salir
0. Salir
""" )
""" )
ans = input ( " Elige que tipo de informacio deseas saber: " )
ans = input ( " Elige que tipo de informacio deseas saber: " )
@ -130,6 +193,12 @@ while ans:
print ( f " Processos activos: { running } " )
print ( f " Processos activos: { running } " )
print ( f " procesos dormiendo: { sleeping } " )
print ( f " procesos dormiendo: { sleeping } " )
print ( f " Otros procesos: { other } " )
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 " :
elif ans == " 0 " :
print ( " \n Adios " )
print ( " \n Adios " )
ans = None
ans = None