Python3 – simple script to watch CPU usage

Python3 – simple script to watch CPU usage

yum install gcc python3-devel
yum install python3
pip3 install psutil

Command: watch -n 2 python3 cpu.py


def tell_system_status():
import psutil
import platform
import datetime

os, name, version, _, _, _ = platform.uname()
version = version.split(‘-‘)[0]
cores = psutil.cpu_count()
cpu_percent = psutil.cpu_percent()
memory_percent = psutil.virtual_memory()[2]
disk_percent = psutil.disk_usage(‘/’)[3]
boot_time = datetime.datetime.fromtimestamp(psutil.boot_time())
running_since = boot_time.strftime(“%A %d. %B %Y”)
response = “I am currently running on %s version %s. ” % (os, version)
response += “\nThis system is named %s and has %s CPU cores. ” % (name, cores)
response += “\nCurrent disk_percent is %s percent. ” % disk_percent
response += “\nCurrent CPU utilization is %s percent. ” % cpu_percent
response += “\nCurrent memory utilization is %s percent. ” % memory_perc ent
response += “\nit’s running since %s.” % running_since
return response

print (tell_system_status())

Leave a Reply

Your email address will not be published. Required fields are marked *