forked from ganglia/gmond_python_modules
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathipmi.py
More file actions
145 lines (109 loc) · 3.18 KB
/
Copy pathipmi.py
File metadata and controls
145 lines (109 loc) · 3.18 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
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import sys
import re
import time
import copy
import string
import subprocess
METRICS = {
'time' : 0,
'data' : {}
}
METRICS_CACHE_MAX = 5
stats_pos = {}
def get_metrics(params):
"""Return all metrics"""
global METRICS
if (time.time() - METRICS['time']) > METRICS_CACHE_MAX:
new_metrics = {}
units = {}
command = [ params['timeout_bin'],
"3", params['ipmitool_bin'],
"-H", params['ipmi_ip'],
"-U", params['username'],
'-P', params['password'],
'-L', params['level'],
'sensor']
p = subprocess.Popen(command,
stdout=subprocess.PIPE).communicate()[0][:-1]
for i, v in enumerate(p.split("\n")):
data = v.split("|")
try:
metric_name = data[0].strip().lower().replace("+", "").replace(" ", "_")
value = data[1].strip()
# Skip missing sensors
if re.search("(0x)", value ) or value == 'na':
continue
# Extract out a float value
vmatch = re.search("([0-9.]+)", value)
if not vmatch:
continue
metric_value = float(vmatch.group(1))
new_metrics[metric_name] = metric_value
units[metric_name] = data[2].strip().replace("degrees C", "C")
except ValueError:
continue
except IndexError:
continue
METRICS = {
'time': time.time(),
'data': new_metrics,
'units': units
}
return [METRICS]
def get_value(name):
"""Return a value for the requested metric"""
try:
metrics = get_metrics()[0]
name = name.lstrip('ipmi_')
result = metrics['data'][name]
except Exception:
result = 0
return result
def create_desc(skel, prop):
d = skel.copy()
for k,v in prop.iteritems():
d[k] = v
return d
def metric_init(params):
global descriptors, metric_map, Desc_Skel
descriptors = []
Desc_Skel = {
'name' : 'XXX',
'call_back' : get_value,
'time_max' : 60,
'value_type' : 'float',
'format' : '%.5f',
'units' : 'count/s',
'slope' : 'both', # zero|positive|negative|both
'description' : 'XXX',
'groups' : 'XXX',
}
metrics = get_metrics(params)[0]
for item in metrics['data']:
descriptors.append(create_desc(Desc_Skel, {
"name" : params['metric_prefix'] + "_" + item,
'groups' : params['metric_prefix'],
'units' : metrics['units'][item]
}))
return descriptors
def metric_cleanup():
'''Clean up the metric module.'''
pass
#This code is for debugging and unit testing
if __name__ == '__main__':
params = {
"metric_prefix" : "ipmi",
"ipmi_ip" : "10.1.2.3",
"username" : "ADMIN",
"password" : "secret",
"level" : "USER",
"ipmitool_bin" : "/usr/bin/ipmitool",
"timeout_bin" : "/usr/bin/timeout"
}
descriptors = metric_init(params)
while True:
for d in descriptors:
v = d['call_back'](d['name'])
print '%s = %s' % (d['name'], v)
print 'Sleeping 15 seconds'
time.sleep(15)