Debian Codegraph @

gmond/python_modules/cpu/cpu_stats.py

raw
1 import sys
2 import traceback
3 import os
4 import re
5 import time
6 import copy
7
8 METRICS = {
9 'time': 0,
10 'data': {}
11 }
12
13 # Got these from /proc/softirqs
14 softirq_pos = {
15 'hi': 1,
16 'timer': 2,
17 'nettx': 3,
18 'netrx': 4,
19 'block': 5,
20 'blockiopoll': 6,
21 'tasklet': 7,
22 'sched': 8,
23 'hrtimer': 9,
24 'rcu': 10
25 }
26
27 LAST_METRICS = copy.deepcopy(METRICS)
28 METRICS_CACHE_MAX = 5
29
30
31 stat_file = "/proc/stat"
32
33 ###############################################################################
34 #
35 ###############################################################################
36
37
38 def get_metrics():
39 """Return all metrics"""
40
41 global METRICS, LAST_METRICS
42
43 if (time.time() - METRICS['time']) > METRICS_CACHE_MAX:
44
45 try:
46 file = open(stat_file, 'r')
47
48 except IOError:
49 return 0
50
51 # convert to dict
52 metrics = {}
53 for line in file:
54 parts = re.split("\s+", line)
55 metrics[parts[0]] = list(parts[1:])
56
57 # update cache
58 LAST_METRICS = copy.deepcopy(METRICS)
59 METRICS = {
60 'time': time.time(),
61 'data': metrics
62 }
63
64 return [METRICS, LAST_METRICS]
65
66
67 def get_value(name):
68 """Return a value for the requested metric"""
69
70 metrics = get_metrics()[0]
71
72 NAME_PREFIX = "cpu_"
73
74 name = name.replace(NAME_PREFIX, "") # remove prefix from name
75
76 try:
77 result = metrics['data'][name][0]
78 except StandardError:
79 result = 0
80
81 return result
82
83
84 def get_delta(name):
85 """Return change over time for the requested metric"""
86
87 # get metrics
88 [curr_metrics, last_metrics] = get_metrics()
89
90 NAME_PREFIX = "cpu_"
91
92 name = name.replace(NAME_PREFIX, "") # remove prefix from name
93
94 if name == "procs_created":
95 name = "processes"
96
97 try:
98 delta = (float(curr_metrics['data'][name][0]) - float(last_metrics['data'][name][0])) / (curr_metrics['time'] - last_metrics['time'])
99 if delta < 0:
100 print name + " is less 0"
101 delta = 0
102 except KeyError:
103 delta = 0.0
104
105 return delta
106
107 ##############################################################################
108 # SoftIRQ has multiple values which are defined in a dictionary at the top
109 ##############################################################################
110
111
112 def get_softirq_delta(name):
113 """Return change over time for the requested metric"""
114
115 # get metrics
116 [curr_metrics, last_metrics] = get_metrics()
117
118 NAME_PREFIX = "softirq_"
119
120 name = name[len(NAME_PREFIX):] # remove prefix from name
121
122 index = softirq_pos[name]
123
124 try:
125 delta = (float(curr_metrics['data']['softirq'][index]) - float(last_metrics['data']['softirq'][index])) / (curr_metrics['time'] - last_metrics['time'])
126 if delta < 0:
127 print name + " is less 0"
128 delta = 0
129 except KeyError:
130 delta = 0.0
131
132 return delta
133
134
135 def create_desc(skel, prop):
136 d = skel.copy()
137 for k, v in prop.iteritems():
138 d[k] = v
139 return d
140
141
142 def metric_init(params):
143 global descriptors, metric_map, Desc_Skel
144
145 descriptors = []
146
147 Desc_Skel = {
148 'name' : 'XXX',
149 'orig_name' : 'XXX',
150 'call_back' : get_delta,
151 'time_max' : 60,
152 'value_type' : 'float',
153 'format' : '%.0f',
154 'units' : 'XXX',
155 'slope' : 'both', # zero|positive|negative|both
156 'description' : '',
157 'groups' : 'cpu',
158 }
159
160 descriptors.append(create_desc(Desc_Skel, {
161 "name" : "cpu_ctxt",
162 "units" : "ctxs/sec",
163 "description": "Context Switches",
164 }))
165
166 descriptors.append(create_desc(Desc_Skel, {
167 "name" : "procs_created",
168 "units" : "proc/sec",
169 "description": "Number of processes and threads created",
170 }))
171
172 descriptors.append(create_desc(Desc_Skel, {
173 "name" : "cpu_intr",
174 "units" : "intr/sec",
175 "description": "Interrupts serviced",
176 }))
177
178 descriptors.append(create_desc(Desc_Skel, {
179 "name" : "procs_blocked",
180 "units" : "processes",
181 "call_back" : get_value,
182 "description": "Processes blocked",
183 }))
184
185 descriptors.append(create_desc(Desc_Skel, {
186 "name" : "softirq",
187 "units" : "ops/s",
188 "description": "Soft IRQs",
189 }))
190
191 descriptors.append(create_desc(Desc_Skel, {
192 "name" : "softirq_hi",
193 "units" : "ops/s",
194 'groups' : 'softirq',
195 "call_back" : get_softirq_delta
196 }))
197
198 descriptors.append(create_desc(Desc_Skel, {
199 "name" : "softirq_timer",
200 "units" : "ops/s",
201 'groups' : 'softirq',
202 "call_back" : get_softirq_delta
203 }))
204
205 descriptors.append(create_desc(Desc_Skel, {
206 "name" : "softirq_nettx",
207 "units" : "ops/s",
208 'groups' : 'softirq',
209 "call_back" : get_softirq_delta
210 }))
211
212 descriptors.append(create_desc(Desc_Skel, {
213 "name" : "softirq_netrx",
214 "units" : "ops/s",
215 'groups' : 'softirq',
216 "call_back" : get_softirq_delta
217 }))
218
219 descriptors.append(create_desc(Desc_Skel, {
220 "name" : "softirq_block",
221 "units" : "ops/s",
222 'groups' : 'softirq',
223 "call_back" : get_softirq_delta
224 }))
225
226 descriptors.append(create_desc(Desc_Skel, {
227 "name" : "softirq_blockiopoll",
228 "units" : "ops/s",
229 'groups' : 'softirq',
230 "call_back" : get_softirq_delta
231 }))
232
233 descriptors.append(create_desc(Desc_Skel, {
234 "name" : "softirq_tasklet",
235 "units" : "ops/s",
236 'groups' : 'softirq',
237 "call_back" : get_softirq_delta
238 }))
239
240 descriptors.append(create_desc(Desc_Skel, {
241 "name" : "softirq_sched",
242 "units" : "ops/s",
243 'groups' : 'softirq',
244 "call_back" : get_softirq_delta
245 }))
246
247 descriptors.append(create_desc(Desc_Skel, {
248 "name" : "softirq_hrtimer",
249 "units" : "ops/s",
250 'groups' : 'softirq',
251 "call_back" : get_softirq_delta
252 }))
253
254 descriptors.append(create_desc(Desc_Skel, {
255 "name" : "softirq_rcu",
256 "units" : "ops/s",
257 'groups' : 'softirq',
258 "call_back" : get_softirq_delta
259 }))
260
261 # We need a metric_map that maps metric_name to the index in /proc/meminfo
262 metric_map = {}
263
264 for d in descriptors:
265 metric_name = d['name']
266 metric_map[metric_name] = {"name": d['orig_name'], "units": d['units']}
267
268 return descriptors
269
270
271 def metric_cleanup():
272 '''Clean up the metric module.'''
273 pass
274
275
276 #This code is for debugging and unit testing
277 if __name__ == '__main__':
278 metric_init({})
279 while True:
280 for d in descriptors:
281 v = d['call_back'](d['name'])
282 print '%s = %s' % (d['name'], v)
283 print 'Sleeping 15 seconds'
284 time.sleep(5)