forked from aarnaud/collectd-lxc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcollectd_lxc.py
executable file
·338 lines (283 loc) · 11.9 KB
/
collectd_lxc.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
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
#!/usr/bin/env python2.7
import glob
import os
import re
import subprocess
from nsenter import Namespace
CONFIG = {'separator': '_',
'dsname': ('lxc', '%USERID%', '%CONTAINER%'),
'collectblkio': True,
'collectcpu': True,
'collectmemory': True,
'collectnet': True, }
def config_callback(cfg):
'''Used as callback when collectd configures plugins'''
collectd.info('Configuring lxc plugin')
global CONFIG
for node in cfg.children:
key = node.key.lower()
val = node.values
if key == 'separator':
CONFIG[key] = val[0]
elif key == 'dsname':
CONFIG[key] = val
elif key in ['collectblkio', 'collectcpu',
'collectmemory', 'collectnet']:
if isinstance(val[0], bool):
CONFIG[key] = val[0]
else:
CONFIG[key] = str_to_bool(val[0])
for key in CONFIG:
collectd.debug("Config: {0} = {1}".format(key, str(CONFIG[key])))
def init_callback():
'''Used as callback when collectd initializes this plugin'''
collectd.info('Initializing lxc plugin')
def str_to_bool(string):
'''Convert string to boolean'''
truevals = ['true', '1', 'yes']
return (string is not None and string.lower() in truevals)
def collect_anything():
'''Return True if at least one metric is to be collected'''
items = ['collectblkio', 'collectcpu', 'collectmemory', 'collectnet']
return sum([CONFIG[item] for item in items]) > 0
def get_blkdev_name(minmaj):
'''Given major:minor device, return a name as friendly as possible'''
devname = minmaj
# Try "friendly" names first, if this is LVM, we might have
# vgname-lvname in dm/name
try:
with open('/sys/dev/block/{0}/dm/name'.format(minmaj), 'r') as f:
devname = f.readline().rstrip()
except:
re_dev = re.compile('^DEVNAME=(?P<devname>.+)$', re.MULTILINE)
try:
with open('/sys/dev/block/{0}/uevent'.format(minmaj), 'r') as f:
devname = re_dev.search(f.read()).group('devname')
except:
devname = minmaj
devname = re.sub("[^a-zA-Z0-9]", '_', devname)
return devname
def get_task_id_by_cgroup(cgroup_path):
'''Return first ID from /tasks or /init.scope/tasks'''
tasks_paths = []
tasks_paths.append(os.path.join(cgroup_path, 'tasks'))
tasks_paths.append(os.path.join(cgroup_path, 'init.scope', 'tasks'))
for tasks in tasks_paths:
try:
with open(tasks, 'r') as f:
# First PID is as good as any other
task_id = f.readline().rstrip()
return task_id
except:
continue
collectd.debug("cannot get task_id " +
"from cgroup path {0}".format(cgroup_path))
return None
def get_proc_net_dev_by_task_id(task_id):
'''Get /proc/net/dev contents from inside a container via its net ns'''
if not task_id:
return None
try:
with Namespace(task_id, 'net'):
# To read network metric in namespace,
# "open" method don't work with namespaces
network_data = subprocess.check_output(['/bin/cat', '/proc/net/dev'])
return network_data.split('\n')
except:
collectd.debug("cannot get /proc/net/dev " +
"from netns for pid {0}".format(task_id))
return None
def get_ds_name(user_id, container_name):
'''Build data source name based on configured template'''
dscomponents = []
for string in CONFIG['dsname']:
if string == '%USERID%':
dscomponents.append('{0}'.format(user_id))
elif string == '%CONTAINER%':
dscomponents.append(container_name)
else:
dscomponents.append(str(string))
return CONFIG['separator'].join(dscomponents)
def collect_net(metric_root, dsn):
'''Collect network stats and dispatch them'''
task_id = get_task_id_by_cgroup(metric_root)
network_data = get_proc_net_dev_by_task_id(task_id)
if not network_data:
return
# HEAD OF /proc/net/dev :
# Inter-|Receive |Transmit
# face |bytes packets errs drop fifo frame compressed multicast|bytes packets errs drop fifo colls carrier compressed
for line in network_data[2:]:
if line.strip() == "":
continue
iface = line.strip().split(':')[0]
rx_data = line.strip().split(':')[1].split()[0:7]
tx_data = line.strip().split(':')[1].split()[8:15]
rx_bytes = int(rx_data[0])
tx_bytes = int(tx_data[0])
rx_packets = int(rx_data[1])
tx_packets = int(tx_data[1])
rx_errors = int(rx_data[2])
tx_errors = int(tx_data[2])
values = collectd.Values(plugin_instance="net", type="if_octets",
plugin=dsn)
values.dispatch(type_instance=iface, values=[rx_bytes, tx_bytes])
values = collectd.Values(plugin_instance="net", type="if_packets",
plugin=dsn)
values.dispatch(type_instance=iface, values=[rx_packets, tx_packets])
values = collectd.Values(plugin_instance="net", type="if_errors",
plugin=dsn)
values.dispatch(type_instance=iface, values=[rx_errors, tx_errors])
return
def collect_cpu(metric_root, dsn):
'''Collect CPU stats and dispatch them'''
srcfile = os.path.join(metric_root, 'cpuacct.stat')
with open(srcfile, 'r') as f:
lines = f.read().splitlines()
cpu_user = 0
cpu_system = 0
for line in lines:
data = line.split()
if data[0] == "user":
cpu_user = int(data[1])
elif data[0] == "system":
cpu_system = int(data[1])
values = collectd.Values(plugin_instance="cpu", type="cpu", plugin=dsn)
values.dispatch(type_instance="user", values=[cpu_user])
values.dispatch(type_instance="system", values=[cpu_system])
def collect_memory(metric_root, dsn):
'''Collect memory stats and dispatch them'''
srcfile = os.path.join(metric_root, 'memory.stat')
with open(srcfile, 'r') as f:
lines = f.read().splitlines()
mem_rss = 0
mem_cache = 0
mem_swap = 0
for line in lines:
data = line.split()
if data[0] == "total_rss":
mem_rss = int(data[1])
elif data[0] == "total_cache":
mem_cache = int(data[1])
elif data[0] == "total_swap":
mem_swap = int(data[1])
values = collectd.Values(plugin_instance="memory", type="memory",
plugin=dsn)
values.dispatch(type_instance="rss", values=[mem_rss])
values.dispatch(type_instance="cache", values=[mem_cache])
values.dispatch(type_instance="swap", values=[mem_swap])
def collect_blkio(metric_root, dsn):
'''Collect blkio stats and dispatch them'''
rgxp = r'^ (?P<dev> [0-9:]+ ) \s {0} \s (?P<val> [0-9]+ ) $'
re_templ = lambda kw: re.compile(rgxp.format(kw),
flags=re.VERBOSE | re.MULTILINE)
def parse(regexp, s):
d = {}
intify = lambda dev_val: (dev_val[0], int(dev_val[1]))
d.update(map(intify, regexp.findall(s)))
return d
parse_reads = lambda s: parse(re_templ("Read"), s)
parse_writes = lambda s: parse(re_templ("Write"), s)
# write metrics are slightly irrelevant actually, because writes
# are only accounted when they are not buffered. So meaningful
# results are only obtained for direct/unbuffered IO.
srcfile = os.path.join(metric_root, 'blkio.throttle.io_service_bytes')
try:
with open(srcfile, 'r') as f:
byte_lines = f.read()
all_bytes_read = parse_reads(byte_lines)
all_bytes_write = parse_writes(byte_lines)
for k in all_bytes_read:
devname = get_blkdev_name(k)
values = collectd.Values(plugin_instance="blkio",
type="disk_octets",
plugin=dsn)
values.dispatch(type_instance=devname,
values=[all_bytes_read[k], all_bytes_write[k]])
except:
collectd.debug("cannot parse {0}".format(srcfile))
pass
srcfile = os.path.join(metric_root, 'blkio.throttle.io_serviced')
try:
with open(srcfile, 'r') as f:
ops_lines = f.read()
all_ops_read = parse_reads(ops_lines)
all_ops_write = parse_writes(ops_lines)
for k in all_bytes_read:
devname = get_blkdev_name(k)
values = collectd.Values(plugin_instance="blkio",
type="disk_ops",
plugin=dsn)
values.dispatch(type_instance=devname,
values=[all_ops_read[k], all_ops_write[k]])
except:
collectd.debug("cannot parse {0}".format(srcfile))
pass
def read_callback(input_data=None):
'''Used every time collectd polls for data'''
# Avoid doing expensive stuff below if there's nothing to collect
if not collect_anything():
return
root_lxc_cgroup = glob.glob("/sys/fs/cgroup/*/lxc/*/")
unprivilege_lxc_cgroup = glob.glob("/sys/fs/cgroup/*/*/*/*/lxc/*/")
cgroup_lxc = root_lxc_cgroup + unprivilege_lxc_cgroup
metrics = dict()
# Get all stats by container group by user
rgxp = r'/sys/fs/cgroup/'
rgxp += r'(?P<type>[a-zA-Z_,]+)/'
rgxp += r'(?:user/(?P<user_id>[0-9]+)\.user/[a-zA-Z0-9]+\.session/)?lxc/'
rgxp += r'(?P<container_name>.*)/'
for cgroup_lxc_metrics in cgroup_lxc:
m = re.search(rgxp, cgroup_lxc_metrics)
user_id = int(m.group("user_id") or 0)
stat_type = m.group("type")
container_name = m.group("container_name")
if user_id not in metrics:
metrics[user_id] = dict()
if container_name not in metrics[user_id]:
metrics[user_id][container_name] = dict()
metrics[user_id][container_name][stat_type] = cgroup_lxc_metrics
for user_id in metrics:
for container_name in metrics[user_id]:
dsn = get_ds_name(user_id, container_name)
processed_network = False
for metric in metrics[user_id][container_name]:
metric_root = metrics[user_id][container_name][metric]
# there is no separate cgroup for network (the way we're
# interested in it), but we need container->PID mapping,
# so we will reuse metric_root of whatever metric that
# happens to be the first one;
if not processed_network and CONFIG['collectnet']:
# we should only do it once per container
processed_network = True
collect_net(metric_root, dsn)
if metric == "memory" and CONFIG['collectmemory']:
collect_memory(metric_root, dsn)
if metric == "cpuacct" and CONFIG['collectcpu']:
collect_cpu(metric_root, dsn)
if metric == "blkio" and CONFIG['collectblkio']:
collect_blkio(metric_root, dsn)
if __name__ == '__main__':
# for commandline debugging
def print_to_stdout(string):
print(string)
# Mimic Collectd Values object
class Values(object):
def __init__(self, **kwargs):
self.__dict__["_values"] = kwargs
def __setattr__(self, key, value):
self.__dict__["_values"][key] = value
def dispatch(self, **kwargs):
values = self._values.copy()
values.update(kwargs)
print(values)
import types
collectd = types.ModuleType("collectd")
collectd.Values = Values
collectd.info = print_to_stdout
read_callback()
else:
import collectd
collectd.register_config(config_callback)
collectd.register_init(init_callback)
collectd.register_read(read_callback)