LINUX_SRV Telegram 208
Используем библиотеку psutil для Python, что бы получить исчерпывающую информацию о состоянии CPU и системы в целом.



import psutil
import time
from tabulate import tabulate

def get_cpu_usage():

return psutil.cpu_percent(interval=1)

def get_memory_usage():

memory = psutil.virtual_memory()
return {
'Всего': f'{memory.total / (1024**3):.2f} ГБ',
'Используется': f'{memory.used / (1024**3):.2f} ГБ',
'Свободно': f'{memory.available / (1024**3):.2f} ГБ',
'Процент использования': f'{memory.percent}%'
}

def get_top_processes(n=10):

processes = []
for proc in sorted(psutil.process_iter(['pid', 'name', 'cpu_percent', 'memory_percent']),
key=lambda x: x.info['cpu_percent'],
reverse=True)[:n]:
try:
processes.append([
proc.info['pid'],
proc.info['name'],
f"{proc.info['cpu_percent']:.2f}%",
f"{proc.info['memory_percent']:.2f}%"
])
except (psutil.NoSuchProcess, psutil.AccessDenied, psutil.ZombieProcess):
pass

return processes

def main():

print("=== Мониторинг системы ===")


print(f"\nЗагрузка CPU: {get_cpu_usage()}%")


print("\nИспользование памяти:")
for key, value in get_memory_usage().items():
print(f"{key}: {value}")


print("\nТоп процессов по использованию CPU:")
top_processes = get_top_processes()
print(tabulate(top_processes,
headers=['PID', 'Название', 'CPU %', 'Память %'],
tablefmt='grid'))

if __name__ == '__main__':
main()
👍2



tgoop.com/linux_srv/208
Create:
Last Update:

Используем библиотеку psutil для Python, что бы получить исчерпывающую информацию о состоянии CPU и системы в целом.



import psutil
import time
from tabulate import tabulate

def get_cpu_usage():

return psutil.cpu_percent(interval=1)

def get_memory_usage():

memory = psutil.virtual_memory()
return {
'Всего': f'{memory.total / (1024**3):.2f} ГБ',
'Используется': f'{memory.used / (1024**3):.2f} ГБ',
'Свободно': f'{memory.available / (1024**3):.2f} ГБ',
'Процент использования': f'{memory.percent}%'
}

def get_top_processes(n=10):

processes = []
for proc in sorted(psutil.process_iter(['pid', 'name', 'cpu_percent', 'memory_percent']),
key=lambda x: x.info['cpu_percent'],
reverse=True)[:n]:
try:
processes.append([
proc.info['pid'],
proc.info['name'],
f"{proc.info['cpu_percent']:.2f}%",
f"{proc.info['memory_percent']:.2f}%"
])
except (psutil.NoSuchProcess, psutil.AccessDenied, psutil.ZombieProcess):
pass

return processes

def main():

print("=== Мониторинг системы ===")


print(f"\nЗагрузка CPU: {get_cpu_usage()}%")


print("\nИспользование памяти:")
for key, value in get_memory_usage().items():
print(f"{key}: {value}")


print("\nТоп процессов по использованию CPU:")
top_processes = get_top_processes()
print(tabulate(top_processes,
headers=['PID', 'Название', 'CPU %', 'Память %'],
tablefmt='grid'))

if __name__ == '__main__':
main()

BY Типичный Сисадмин




Share with your friend now:
tgoop.com/linux_srv/208

View MORE
Open in Telegram


Telegram News

Date: |

Unlimited number of subscribers per channel Other crimes that the SUCK Channel incited under Ng’s watch included using corrosive chemicals to make explosives and causing grievous bodily harm with intent. The court also found Ng responsible for calling on people to assist protesters who clashed violently with police at several universities in November 2019. How to create a business channel on Telegram? (Tutorial) The creator of the channel becomes its administrator by default. If you need help managing your channel, you can add more administrators from your subscriber base. You can provide each admin with limited or full rights to manage the channel. For example, you can allow an administrator to publish and edit content while withholding the right to add new subscribers. Select: Settings – Manage Channel – Administrators – Add administrator. From your list of subscribers, select the correct user. A new window will appear on the screen. Check the rights you’re willing to give to your administrator.
from us


Telegram Типичный Сисадмин
FROM American