forked from ganglia/gmond_python_modules
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcpu_temp.py
More file actions
79 lines (73 loc) · 2.45 KB
/
cpu_temp.py
File metadata and controls
79 lines (73 loc) · 2.45 KB
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
#!/usr/bin/env python2
import os
descriptors = list()
sysdir = '/sys/devices/platform/'
handler_dict = dict()
def metric_init(params):
global descriptors
try:
coretemp_list = [i for i in os.listdir(sysdir) if i.startswith('coretemp')]
except OSError:
print 'No dir named' + sysdir
os._exit(1)
if not coretemp_list:
print 'No dir name starts with coretemp'
os._exit(1)
for coretemp in coretemp_list:
coreinput_list = [i for i in os.listdir(sysdir + coretemp) if i.endswith('_input')]
try:
with open(sysdir + coretemp + '/temp1_label','r') as f:
phy_id_prefix = f.read().split()[-1]
except IOError:
print 'No temp1_label file'
os._exit(1)
for coreinput in coreinput_list:
build_descriptor(coretemp,coreinput,phy_id_prefix)
return descriptors
def build_descriptor(coretemp,coreinput,phy_id_prefix):
global handler_dict
if coreinput == 'temp1_input':
name = 'cpu_temp_physical_' + phy_id_prefix
description = 'Physical CPU id ' + phy_id_prefix + ' Temperature'
groups = 'cpu_temp_physical'
handler_dict[name] = sysdir + coretemp + '/temp1_input'
else:
with open(sysdir + coretemp + '/' + coreinput[:-6] + '_label','r') as f:
coreid = f.read().split()[-1]
name = 'cpu_temp_core_' + phy_id_prefix + '_' + coreid
description = 'Physical CPU id ' + phy_id_prefix + ' Core ' + coreid + ' Temperature'
groups = 'cpu_temp_core'
handler_dict[name] = sysdir + coretemp + '/' + coreinput
call_back = metric_handler
d = {'name': name,
'call_back': call_back,
'time_max': 60,
'value_type': 'float',
'units': 'C',
'slope': 'both',
'format': '%.1f',
'description': description,
'groups': groups
}
try:
call_back(name)
descriptors.append(d)
except:
print 'Build descriptor Failed'
def metric_handler(name):
try:
with open(handler_dict.get(name),'r') as f:
temp = f.read()
except:
temp = 0
temp_float = int(temp) / 1000.0
return temp_float
def metric_cleanup():
pass
if __name__ == '__main__':
metric_init({})
for d in descriptors:
v = d['call_back'](d['name'])
print 'value for %s is %.1f %s' % (d['name'],v,d['units'])
for k,v in d.iteritems():
print k,v