Local OS monitoring client with Python: Difference between revisions

From PedrosBrainDump
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..."
 
No edit summary
 
(4 intermediate revisions by the same user not shown)
Line 5: Line 5:
* Swap usage (percent)
* Swap usage (percent)
* Root disk usage (percent)
* Root disk usage (percent)
*All processes runing (PID, name, cpu usage, mem usage)
IT consumes something around 10MB/day of storage,


It's all saved on a CSV file named 'consume-metrics-YEAR-MONTH-DAY.csv' the csv structure is:
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
  year-month-day,hout:minute:second,cpu usage,ram usage,swap usage,root disk usage,process1 pid-process1 name-process1 cpu usage-process1 mem usage|process2...
Here is the script
Here is the script
  import psutil
  import psutil
  import datetime
  import datetime
  import time
  import time
ITERATION_TIME = 10 # seconds
   
   
  while True:
  while True:
Line 21: Line 25:
     minute = momment.minute
     minute = momment.minute
     second = momment.second
     second = momment.second
     cpu = psutil.cpu_percent()
     cpu = psutil.cpu_percent()
     ram = psutil.virtual_memory().percent
     ram = psutil.virtual_memory().percent
     swap = psutil.swap_memory().percent
     swap = psutil.swap_memory().percent
     root_disk = psutil.disk_usage('/').percent
     root_disk = psutil.disk_usage('/').percent
     print(f'{year}-{month}-{day},{hour}:{minute}:{second},{cpu},{ram},{swap},{root_disk}')
    all_processes = list(psutil.process_iter(['pid', 'name']))
    sleeptime = ITERATION_TIME / len(all_processes)
    processes = []
    for process in all_processes:
        try:
            cpu_usage = process.cpu_percent(interval=sleeptime)
            memory_info = process.memory_info().rss
        except Exception as e:
            print(f'ERROR : {e}')
        else:
            processes.append(f"{process.info['pid']}-{process.info['name']}-{cpu_usage}-{memory_info}")
    processes = '|'.join(processes)
     print(f'{year}-{month}-{day},{hour}:{minute}:{second},{cpu},{ram},{swap},{root_disk},{processes}')
     with open(f'consume-metrics-{year}-{month}-{day}.csv', 'a+') as logfile:
     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')
         logfile.write(f'{year}-{month}-{day},{hour}:{minute}:{second},{cpu},{ram},{swap},{root_disk},{processes}\n')
    time.sleep(1)

Latest revision as of 16:16, 11 October 2024

This script monitores:

  • CPU usage (percent)
  • RAM usage (percent)
  • Swap usage (percent)
  • Root disk usage (percent)
  • All processes runing (PID, name, cpu usage, mem usage)

IT consumes something around 10MB/day of storage,

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,process1 pid-process1 name-process1 cpu usage-process1 mem usage|process2...

Here is the script

import psutil
import datetime
import time

ITERATION_TIME = 10 # seconds

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

    all_processes = list(psutil.process_iter(['pid', 'name']))
    sleeptime = ITERATION_TIME / len(all_processes)

    processes = []
    for process in all_processes:
        try:
            cpu_usage = process.cpu_percent(interval=sleeptime)
            memory_info = process.memory_info().rss
        except Exception as e:
            print(f'ERROR : {e}')
        else:
            processes.append(f"{process.info['pid']}-{process.info['name']}-{cpu_usage}-{memory_info}")
    processes = '|'.join(processes)

    print(f'{year}-{month}-{day},{hour}:{minute}:{second},{cpu},{ram},{swap},{root_disk},{processes}')
    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},{processes}\n')