forked from cemersoz/dockerStats
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstats.py
83 lines (69 loc) · 2.6 KB
/
stats.py
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
from threading import Thread, Lock
import time
cpu_15 = None
cpu_10 = None
cpu_5 = None
class Stats(Thread):
def __init__(self):
super().__init__()
self.daemon = True
self._lock = Lock()
self.stats = {}
self.cpu_5 = None
self.cpu_10 = None
self.cpu_15 = None
def getCPUStats(self):
stats_file = open('/host-proc/stat','r')
cpus = dict()
for line in stats_file:
if 'cpu' in line:
sp = line.strip().split(' ')
spl = [x for x in sp if x]
cpu = {'user': float(spl[1]),
'nice': float(spl[2]),
'system': float(spl[3]),
'idle': float(spl[4]),
'iowait': float(spl[5]),
'irq': float(spl[6]),
'softirq': float(spl[7]),
'steal': float(spl[8])}
total = cpu['user'] + cpu['nice'] + cpu['system'] + cpu['idle'] + cpu['iowait'] + cpu['irq'] + cpu['softirq'] + cpu['steal']
total_idle = cpu['idle'] + cpu['iowait']
cpu['total'] = total
cpu['total_idle'] = total_idle
cpu['avg'] = 1 - total_idle / total
if self.cpu_5:
cpu['5_avg'] = 1 - (total_idle - self.cpu_5[spl[0]]['total_idle']) / (total - self.cpu_5[spl[0]]['total'])
if self.cpu_10:
cpu['10_avg'] = 1 - (total_idle - self.cpu_10[spl[0]]['total_idle']) / (total - self.cpu_10[spl[0]]['total'])
if self.cpu_15:
cpu['15_avg'] = 1 - (total_idle - self.cpu_15[spl[0]]['total_idle']) / (total - self.cpu_15[spl[0]]['total'])
cpus[spl[0]] = cpu
self.cpu_15 = cpu_10
self.cpu_10 = cpu_5
self.cpu_5 = cpus
return cpus
def getMemStats(self):
mem_file = open('/host-proc/meminfo', 'r')
mem = dict()
for line in mem_file:
spl = [x for x in line.split(' ') if x]
mem[spl[0][:-1]] = float(spl[1])
return {'total_used': mem['MemTotal'] - mem['MemFree'],
'non_trivial': mem['MemTotal'] - mem['MemFree'] - mem['Buffers'] - mem['Cached'] - mem['SReclaimable'] - mem['Shmem'],
'buffers': mem['Buffers'],
'cached': mem['Cached'] + mem['SReclaimable'] + mem['Shmem'],
'pct_used': 1 - mem['MemFree'] / mem['MemTotal'],
'pct_real': 1 - (mem['MemFree'] + mem['Buffers'] + mem['Cached'] + mem['SReclaimable'] + mem['Shmem']) / mem['MemTotal']
}
def run(self):
while True:
self.updateStats()
time.sleep(5)
def updateStats(self):
with self._lock:
self.stats = {'mem': self.getMemStats(),
'cpu': self.getCPUStats()}
def getStats(self):
with self._lock:
return self.stats