-
Notifications
You must be signed in to change notification settings - Fork 5
/
cpu_info.py
104 lines (81 loc) · 2.29 KB
/
cpu_info.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#!/usr/bin/env python
# -*- coding: utf-8; -*-
import collectd
import time
# Global variables
NAME = 'cpu_info'
VERBOSE = True
# Helper functions
def get_stats():
""" Reads /proc/stat and returns array of cpu stats.
"""
with open('/proc/stat', 'r') as f:
arr = f.readline().split()
arr.pop(0)
return ([float(a) for a in arr ] + [0] * 10)[:10]
def log(t, message):
""" Log messages to collectd logger
"""
if t == 'err':
collectd.error('{0}: {1}'.format(NAME, message))
elif t == 'warn':
collectd.warning('{0}: {1}'.format(NAME, message))
elif t == 'verb':
if VERBOSE:
collectd.info('{0}: {1}'.format(NAME, message))
else:
collectd.info('{0}: {1}'.format(NAME, message))
def configure_callback(conf):
""" Config data from collectd
"""
log('verb', 'configure_callback Running')
global NAME, VERBOSE
for node in conf.children:
if node.key == 'Name':
NAME = node.values[0]
elif node.key == 'Verbose':
if node.values[0] == 'False':
VERBOSE = False
else:
log('warn', 'Unknown config key: {0}'.format(node.key))
def read_callback():
""" Prepare data for collectd
"""
log('verb', 'read_callback Running')
stats = get_cpustats()
if not stats:
log('verb', 'No statistics received')
return
for metric, percent in stats:
log('verb', 'Sending value: {0} {1}'.format(metric, percent))
value = collectd.Values(plugin=NAME)
value.type = 'percent'
value.type_instance = metric
value.values = [ str(percent) ]
value.dispatch()
def get_cpustats():
""" Get CPU stats in percentages
"""
cpu = get_stats()
time.sleep(5)
cpun = get_stats()
deltas = [ n[0] - n[1] for n in zip(cpun, cpu) ]
dj = sum(deltas)
percent = [ ((d / dj) * 100) for d in deltas ]
columns = [
'user',
'nice',
'system',
'idle',
'wait',
'interrupt',
'softirq',
'steal',
'guest',
'guestn'
]
return zip(columns, percent)
# Register to collectd
collectd.register_config(configure_callback)
collectd.warning('Initialising {0}'.format(NAME))
collectd.register_read(read_callback)