-
Notifications
You must be signed in to change notification settings - Fork 70
Expand file tree
/
Copy path60_stats_per_cpu_core.py
More file actions
executable file
·42 lines (33 loc) · 1001 Bytes
/
60_stats_per_cpu_core.py
File metadata and controls
executable file
·42 lines (33 loc) · 1001 Bytes
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
#! /usr/bin/env python
# -*- coding: utf-8 -*-
import time, os, socket
import json
metric = ['usr', 'nice', 'sys', 'idle', 'iowait', 'irq', 'soft', 'steal', 'guest']
host = socket.gethostname()
def get_cpu_core_stat(num):
data = []
for x in range(num):
try:
handler = os.popen("cat /proc/stat | grep cpu%d " % x)
except:
continue
output = handler.read().split('\n')[0]
output = output.split(' ')[1:10]
if len(output) != 9:
continue
index=0
for m in output:
t = {}
t['metric'] = 'cpu.core.%s' % metric[index]
t['endpoint'] = host
t['timestamp'] = int(time.time())
t['step'] = 60
t['counterType'] = 'COUNTER'
t['tags'] = 'core=%s' % str(x)
t['value'] = m
index += 1
data.append(t)
return data
if __name__ == "__main__":
core_total = int(os.popen("cat /proc/cpuinfo | grep processor | tail -1 | cut -d' ' -f2").read().strip()) + 1
print json.dumps(get_cpu_core_stat(core_total))