-
Notifications
You must be signed in to change notification settings - Fork 142
/
monitoring.py
231 lines (193 loc) · 9.07 KB
/
monitoring.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
from contextlib import contextmanager
import glob
import os.path
import common
import settings
import logging
logger = logging.getLogger("cbt")
class Monitoring(object):
def __init__(self, mconfig):
# the initializers should be the very single places interrogating
# settings for the sake of explicitness
nodes_list = mconfig.get('nodes', self._get_default_nodes())
self.nodes = settings.getnodes(*nodes_list)
@staticmethod
def _get_all():
for monitoring, mconfig in sorted(settings.monitoring_profiles.items()):
yield Monitoring._get_object(monitoring, mconfig)
@staticmethod
def _get_object(monitoring, mconfig):
if monitoring == 'collectl':
return CollectlMonitoring(mconfig)
if monitoring == 'perf':
return PerfMonitoring(mconfig)
if monitoring == 'blktrace':
return BlktraceMonitoring(mconfig)
if monitoring == 'top':
return TopMonitoring(mconfig)
class CollectlMonitoring(Monitoring):
def __init__(self, mconfig):
super(CollectlMonitoring, self).__init__(mconfig)
self.args = mconfig.get('args', '-s+mYZ -i 1:10 -F0 -f {collectl_dir} '
r'--rawdskfilt \"+cciss/c\d+d\d+ |hd[ab] | sd[a-z]+ |dm-\d+ |xvd[a-z] |fio[a-z]+ | vd[a-z]+ |emcpower[a-z]+ |psv\d+ |nvme[0-9]n[0-9]+p[0-9]+ \"')
def start(self, directory):
collectl_dir = '%s/collectl' % directory
common.pdsh(self.nodes, 'mkdir -p -m0755 -- %s' % collectl_dir).communicate()
common.pdsh(self.nodes, ['collectl', self.args.format(collectl_dir=collectl_dir)])
def stop(self, directory):
common.pdsh(self.nodes, 'pkill -SIGINT -f collectl').communicate()
@staticmethod
def _get_default_nodes():
return ['clients', 'osds', 'mons', 'rgws']
class PerfMonitoring(Monitoring):
def __init__(self, mconfig):
super(PerfMonitoring, self).__init__(mconfig)
self.pid_dir = settings.cluster.get('pid_dir')
self.pid_glob = mconfig.get('pid_glob', 'osd.*.pid')
self.user = settings.cluster.get('user')
self.perf_cmd = mconfig.get('perf_cmd', 'sudo perf')
self.args_template = mconfig.get('args')
self.perf_runners = []
self.perf_dir = '' # we need the output file to extract data
def start(self, directory):
perf_dir = '%s/perf' % directory
self.perf_dir = perf_dir
common.pdsh(self.nodes, 'mkdir -p -m0755 -- %s' % perf_dir).communicate()
perf_template = '{} {} &'.format(self.perf_cmd, self.args_template)
local_node = common.get_localnode(self.nodes)
if local_node:
logger.debug("PerfMonitoring: in local_node");
logger.debug("pid_dir: %s" % self.pid_dir);
for pid_path in glob.glob(os.path.join(self.pid_dir, self.pid_glob)):
logger.debug("PerfMonitoring pid_path: %s" % pid_path);
with open(pid_path) as pidfile:
pid = pidfile.read().strip()
perf_cmd = perf_template.format(perf_dir=perf_dir, pid=pid)
runner = common.sh(local_node, perf_cmd)
self.perf_runners.append(runner)
else:
logger.debug("PerfMonitoring: remote_node");
# ${pid} will be handled by remote's sh
perf_cmd = perf_template.format(perf_dir=perf_dir, pid='${pid}')
common.pdsh(self.nodes, ['for pid in `cat %s/%s`;' % (self.pid_dir, self.pid_glob),
'do', perf_cmd,
'done'])
def stop(self, directory):
if self.perf_runners:
for runner in self.perf_runners:
runner.kill()
else:
common.pdsh(self.nodes, 'sudo pkill -SIGINT -f perf\ ').communicate()
if directory:
common.pdsh(self.nodes, 'sudo chown {user}.{user} {dir}/perf/perf.data'.format(
user=self.user, dir=directory))
common.pdsh(self.nodes, 'sudo chown {user}.{user} {dir}/perf/perf_stat.*'.format(
user=self.user, dir=directory))
def get_cpu_cycles(self, out_dir):
import re
total_cpu_cycles = 0
perf_dir_name = str(glob.glob(out_dir + "/perf*")[0])
perf_stat_fnames = os.listdir(perf_dir_name)
for perf_out_fname in perf_stat_fnames:
perf_output_file = open(perf_dir_name + "/" + perf_out_fname, "rt")
match = re.search(r'(.*) cycles(.*?) .*', perf_output_file.read(), re.M | re.I)
if match:
cpu_cycles = match.group(1).strip()
else:
return None
total_cpu_cycles = total_cpu_cycles + int(cpu_cycles.replace(',', ''))
return total_cpu_cycles
@staticmethod
def _get_default_nodes():
return ['osds']
class BlktraceMonitoring(Monitoring):
def __init__(self, mconfig):
super(BlktraceMonitoring, self).__init__(mconfig)
self.osds_per_node = settings.cluster.get('osds_per_node')
self.use_existing = settings.cluster.get('use_existing', True)
self.user = settings.cluster.get('user')
def start(self, directory):
blktrace_dir = '%s/blktrace' % directory
common.pdsh(self.nodes, 'mkdir -p -m0755 -- %s' % blktrace_dir).communicate()
for device in range(0, self.osds_per_node):
common.pdsh(self.nodes, 'cd %s;sudo blktrace -o device%s -d /dev/disk/by-partlabel/osd-device-%s-data'
% (blktrace_dir, device, device))
def stop(self, directory):
common.pdsh(self.nodes, 'sudo pkill -SIGINT -f blktrace').communicate()
if directory and not self.use_existing:
self._make_movies(directory)
def _make_movies(self, directory):
seekwatcher = '/home/%s/bin/seekwatcher' % self.user
blktrace_dir = '%s/blktrace' % directory
for device in range(self.osds_per_node):
common.pdsh(self.nodes, 'cd %s;%s -t device%s -o device%s.mpg --movie' %
(blktrace_dir, seekwatcher, device, device)).communicate()
@staticmethod
def _get_default_nodes():
return ['osds']
class TopMonitoring(Monitoring):
def __init__(self, mconfig):
super(TopMonitoring, self).__init__(mconfig)
self.pid_dir = settings.cluster.get('pid_dir')
self.pid_glob = mconfig.get('pid_glob', 'osd.*.pid')
self.user = settings.cluster.get('user')
self.top_cmd = mconfig.get('top_cmd', 'top')
self.args = mconfig.get('args', '-b -H -1 -p {pid} -n 30 > {top_dir}/{pid}_osd_top.out')
self.top_runners = []
def start(self, directory):
top_dir = '%s/top' % directory
common.pdsh(self.nodes, 'mkdir -p -m0755 -- %s' % top_dir).communicate()
top_template = '{} {}'.format(self.top_cmd, self.args)
local_node = common.get_localnode(self.nodes)
if local_node:
logger.debug("TopMonitoring: in local_node");
logger.debug("pid_dir: %s" % self.pid_dir);
for pid_path in glob.glob(os.path.join(self.pid_dir, self.pid_glob)):
logger.debug("TopMonitoring pid_path: %s" % pid_path);
with open(pid_path) as pidfile:
pid = pidfile.read().strip()
top_cmd = top_template.format(top_dir=top_dir, pid=pid)
runner = common.sh(local_node, top_cmd)
self.top_runners.append(runner)
else:
logger.debug("TopMonitoring: remote_node");
# ${pid} will be handled by remote's sh
top_cmd = top_template.format(top_dir=top_dir, pid='${pid}')
common.pdsh(self.nodes, ['for pid in `cat %s/%s`;' % (self.pid_dir, self.pid_glob),
'do', top_cmd,
'done'])
def stop(self, directory):
#common.pdsh(self.nodes, 'pkill -SIGINT -f top').communicate()
if self.top_runners:
for runner in self.top_runners:
runner.kill()
else:
#ToDO: find the pid of the correct top instance process
common.pdsh(self.nodes, 'sudo pkill -SIGINT -f top\ ').communicate()
if directory:
common.pdsh(self.nodes, 'sudo chown {user}.{user} {dir}/top/*top.out'.format(
user=self.user, dir=directory))
@staticmethod
def _get_default_nodes():
return ['osds']
def start(directory):
for m in Monitoring._get_all():
m.start(directory)
def stop(directory=None):
for m in Monitoring._get_all():
m.stop(directory)
@contextmanager
def monitor(directory):
monitors = []
for m in Monitoring._get_all():
m.start(directory)
monitors.append(m)
yield
for m in monitors:
m.stop(directory)
def get_cpu_cycles(out_dir):
# check if perf stat is configured
for monitoring_profile in Monitoring._get_all():
if(isinstance(monitoring_profile, PerfMonitoring)):
return monitoring_profile.get_cpu_cycles(out_dir) # if it is, then return the number of cycle
return None