analysis-cpu-mem.py 1.71 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# -*- coding: utf-8 -*-
"""
Created on Tue Jun 19 10:34:31 2018

@author: imdea
"""

import csv
from collections import defaultdict
import numpy as np
import matplotlib.pyplot as plt
from datetime import datetime, timedelta
import dateutil.parser
import matplotlib.dates as mdates

cpu_vector = []
memory_vector = []
time_vector = []

columns = defaultdict(list) # each value in each column is appended to a list

with open('monitor-cpu-memory-load.csv') as f: # obtained through glances
    reader = csv.DictReader(f) # read rows into a dictionary format
    for row in reader: # read a row as {column1: value1, column2: value2,...}
        for (k,v) in row.items(): # go over each column name and value 
            columns[k].append(v) # append the value into the appropriate list
                                 # based on column name k

#print(columns['now'])
#print(columns['cpu.user'])
#print(columns['mem.used'])
#print(len(columns['now']),len(columns['mem.used']))

for j in range (len(columns['now'])):
    d = dateutil.parser.parse(columns['now'][j])
    time_vector.append(d.strftime('%H:%M:%S'))
    #print(d.strftime('%H:%M:%S'))  #==> '09/26/2008'

for k in range (len(columns['mem.used'])):
    memory_vector.append(int(columns['mem.used'][k]))
    print(time_vector[k],int(columns['mem.used'][k]))# printing the file for the mem.used

for i in range (len(columns['cpu.user'])):
    memory_vector.append(float(columns['cpu.user'][i]))
    #print(time_vector[i],float(columns['cpu.user'][i]))# printing the file for the cpu.user

for l in range (len(columns['cpu.system'])):
    memory_vector.append(float(columns['cpu.system'][l]))
    #print(time_vector[l],float(columns['cpu.system'][l]))# printing the file for the cpu.system