Local OS monitoring client with Python

From PedrosBrainDump
Revision as of 17:48, 10 October 2024 by 413vhcu1lq0463ob (talk | contribs) (Created page with "This script monitores: * CPU usage (percent) * RAM usage (percent) * Swap usage (percent) * Root disk usage (percent) It's all saved on a CSV file named 'consume-metrics-YEAR-MONTH-DAY.csv' the csv structure is: year-month-day,hout:minute:second,cpu usage,ram usage,swap usage,root disk usage Here is the script import psutil import datetime import time while True: momment = datetime.datetime.now() year = momment.year month = momment.month day...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

This script monitores:

  • CPU usage (percent)
  • RAM usage (percent)
  • Swap usage (percent)
  • Root disk usage (percent)

It's all saved on a CSV file named 'consume-metrics-YEAR-MONTH-DAY.csv' the csv structure is:

year-month-day,hout:minute:second,cpu usage,ram usage,swap usage,root disk usage

Here is the script

import psutil
import datetime
import time

while True:
    momment = datetime.datetime.now()
    year = momment.year
    month = momment.month
    day = momment.day
    hour = momment.hour
    minute = momment.minute
    second = momment.second
    cpu = psutil.cpu_percent()
    ram = psutil.virtual_memory().percent
    swap = psutil.swap_memory().percent
    root_disk = psutil.disk_usage('/').percent
    print(f'{year}-{month}-{day},{hour}:{minute}:{second},{cpu},{ram},{swap},{root_disk}')
    with open(f'consume-metrics-{year}-{month}-{day}.csv', 'a+') as logfile:
        logfile.write(f'{year}-{month}-{day},{hour}:{minute}:{second},{cpu},{ram},{swap},{root_disk}\n')
    time.sleep(1)