Extend the rotation API to provide network trace archive locations
[lttng-tools.git] / src / bin / lttng / commands / rotate.c
1 /*
2 * Copyright (C) 2017 - Julien Desfossez <jdesfossez@efficios.com>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License, version 2 only,
6 * as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
16 */
17
18 #define _LGPL_SOURCE
19 #include <popt.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <sys/stat.h>
24 #include <sys/types.h>
25 #include <unistd.h>
26 #include <inttypes.h>
27 #include <ctype.h>
28 #include <assert.h>
29
30 #include <common/sessiond-comm/sessiond-comm.h>
31 #include <common/mi-lttng.h>
32
33 #include "../command.h"
34 #include <lttng/rotation.h>
35 #include <lttng/location.h>
36
37 static char *opt_session_name;
38 static int opt_no_wait;
39 static struct mi_writer *writer;
40
41 #ifdef LTTNG_EMBED_HELP
42 static const char help_msg[] =
43 #include <lttng-rotate.1.h>
44 ;
45 #endif
46
47 enum {
48 OPT_HELP = 1,
49 OPT_LIST_OPTIONS,
50 };
51
52 static struct poptOption long_options[] = {
53 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
54 {"help", 'h', POPT_ARG_NONE, 0, OPT_HELP, 0, 0},
55 {"list-options", 0, POPT_ARG_NONE, NULL, OPT_LIST_OPTIONS, NULL, NULL},
56 {"no-wait", 'n', POPT_ARG_VAL, &opt_no_wait, 1, 0, 0},
57 {0, 0, 0, 0, 0, 0, 0}
58 };
59
60 static int mi_output_rotate(const char *status, const char *path,
61 const char *session_name)
62 {
63 int ret;
64
65 if (!lttng_opt_mi) {
66 ret = 0;
67 goto end;
68 }
69
70 ret = mi_lttng_writer_open_element(writer,
71 mi_lttng_element_rotation);
72 if (ret) {
73 goto end;
74 }
75
76 ret = mi_lttng_writer_write_element_string(writer,
77 mi_lttng_element_session_name, session_name);
78 if (ret) {
79 goto end;
80 }
81
82 ret = mi_lttng_writer_write_element_string(writer,
83 mi_lttng_element_rotate_status, status);
84 if (ret) {
85 goto end;
86 }
87 if (path) {
88 ret = mi_lttng_writer_write_element_string(writer,
89 config_element_path, path);
90 if (ret) {
91 goto end;
92 }
93 }
94 /* Close rotation element */
95 ret = mi_lttng_writer_close_element(writer);
96 if (ret) {
97 goto end;
98 }
99
100 end:
101 return ret;
102 }
103
104 static int output_trace_archive_location(
105 const struct lttng_trace_archive_location *location,
106 const char *session_name)
107 {
108 int ret = 0;
109 enum lttng_trace_archive_location_type location_type;
110 enum lttng_trace_archive_location_status status;
111 bool printed_location = false;
112
113 location_type = lttng_trace_archive_location_get_type(location);
114
115 _MSG("Trace chunk archive for session %s is now readable",
116 session_name);
117 switch (location_type) {
118 case LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_LOCAL:
119 {
120 const char *absolute_path;
121
122 status = lttng_trace_archive_location_local_get_absolute_path(
123 location, &absolute_path);
124 if (status != LTTNG_TRACE_ARCHIVE_LOCATION_STATUS_OK) {
125 ret = -1;
126 goto end;
127 }
128 MSG(" at %s", absolute_path);
129 ret = mi_output_rotate("completed", absolute_path,
130 session_name);
131 if (ret) {
132 goto end;
133 }
134 printed_location = true;
135 break;
136 }
137 case LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_RELAY:
138 {
139 uint16_t control_port, data_port;
140 const char *host, *relative_path, *protocol_str;
141 enum lttng_trace_archive_location_relay_protocol_type protocol;
142
143 /* Fetch all relay location parameters. */
144 status = lttng_trace_archive_location_relay_get_protocol_type(
145 location, &protocol);
146 if (status != LTTNG_TRACE_ARCHIVE_LOCATION_STATUS_OK) {
147 ret = -1;
148 goto end;
149 }
150
151 status = lttng_trace_archive_location_relay_get_host(
152 location, &host);
153 if (status != LTTNG_TRACE_ARCHIVE_LOCATION_STATUS_OK) {
154 ret = -1;
155 goto end;
156 }
157
158 status = lttng_trace_archive_location_relay_get_control_port(
159 location, &control_port);
160 if (status != LTTNG_TRACE_ARCHIVE_LOCATION_STATUS_OK) {
161 ret = -1;
162 goto end;
163 }
164
165 status = lttng_trace_archive_location_relay_get_data_port(
166 location, &data_port);
167 if (status != LTTNG_TRACE_ARCHIVE_LOCATION_STATUS_OK) {
168 ret = -1;
169 goto end;
170 }
171
172 status = lttng_trace_archive_location_relay_get_relative_path(
173 location, &relative_path);
174 if (status != LTTNG_TRACE_ARCHIVE_LOCATION_STATUS_OK) {
175 ret = -1;
176 goto end;
177 }
178
179 switch (protocol) {
180 case LTTNG_TRACE_ARCHIVE_LOCATION_RELAY_PROTOCOL_TYPE_TCP:
181 protocol_str = "tcp";
182 break;
183 default:
184 protocol_str = "unknown";
185 break;
186 }
187
188 MSG(" on relay %s://%s/%s [control port %" PRIu16 ", data port %"
189 PRIu16 "]", protocol_str, host,
190 relative_path, control_port, data_port);
191 printed_location = true;
192 ret = mi_output_rotate("completed", relative_path,
193 session_name);
194 if (ret) {
195 goto end;
196 }
197 break;
198 }
199 default:
200 break;
201 }
202 end:
203 if (!printed_location) {
204 MSG(" at an unknown location");
205 }
206 return ret;
207 }
208
209 static int rotate_tracing(char *session_name)
210 {
211 int ret;
212 struct lttng_rotation_immediate_attr *attr = NULL;
213 struct lttng_rotation_handle *handle = NULL;
214 enum lttng_rotation_status rotation_status;
215 enum lttng_rotation_state rotation_state = LTTNG_ROTATION_STATE_ONGOING;
216
217 DBG("Rotating the output files of session %s", session_name);
218
219 attr = lttng_rotation_immediate_attr_create();
220 if (!attr) {
221 goto error;
222 }
223
224 ret = lttng_rotation_immediate_attr_set_session_name(attr, session_name);
225 if (ret < 0) {
226 ERR("Session name exceeds the maximal allowed length");
227 goto error;
228 }
229
230 ret = lttng_rotate_session(attr, &handle);
231 if (ret < 0) {
232 switch (-ret) {
233 case LTTNG_ERR_SESSION_NOT_STARTED:
234 WARN("Tracing session %s not started yet", session_name);
235 break;
236 default:
237 ERR("%s", lttng_strerror(ret));
238 break;
239 }
240 goto error;
241 }
242
243 if (!opt_no_wait) {
244 _MSG("Waiting for rotation to complete");
245 ret = fflush(stdout);
246 if (ret) {
247 PERROR("fflush");
248 goto error;
249 }
250
251 do {
252 rotation_status = lttng_rotation_handle_get_state(handle,
253 &rotation_state);
254 if (rotation_status != LTTNG_ROTATION_STATUS_OK) {
255 ERR("Failed to query the state of the rotation");
256 goto error;
257 }
258
259 /*
260 * Data sleep time before retrying (in usec). Don't
261 * sleep if the call returned value indicates
262 * availability.
263 */
264 if (rotation_state == LTTNG_ROTATION_STATE_ONGOING) {
265 ret = usleep(DEFAULT_DATA_AVAILABILITY_WAIT_TIME);
266 if (ret) {
267 PERROR("usleep");
268 goto error;
269 }
270 _MSG(".");
271
272 ret = fflush(stdout);
273 if (ret) {
274 PERROR("fflush");
275 goto error;
276 }
277 }
278 } while (rotation_state == LTTNG_ROTATION_STATE_ONGOING);
279 MSG("");
280 }
281
282 switch (rotation_state) {
283 case LTTNG_ROTATION_STATE_COMPLETED:
284 {
285 const struct lttng_trace_archive_location *location;
286
287 rotation_status = lttng_rotation_handle_get_archive_location(
288 handle, &location);
289 if (rotation_status != LTTNG_ROTATION_STATUS_OK) {
290 ERR("Failed to retrieve the rotation's completed chunk archive location");
291 goto error;
292 }
293 ret = output_trace_archive_location(location, session_name);
294 if (ret) {
295 goto error;
296 }
297 ret = CMD_SUCCESS;
298 goto end;
299 }
300 case LTTNG_ROTATION_STATE_EXPIRED:
301 MSG("Session %s rotated, but handle expired", session_name);
302 ret = mi_output_rotate("expired", NULL, session_name);
303 if (ret) {
304 goto error;
305 }
306 ret = CMD_SUCCESS;
307 goto end;
308 default:
309 ERR("Unexpected rotation state received, aborting...");
310 goto error;
311 }
312
313 error:
314 ret = CMD_ERROR;
315 end:
316 lttng_rotation_handle_destroy(handle);
317 lttng_rotation_immediate_attr_destroy(attr);
318 return ret;
319 }
320
321 /*
322 * cmd_rotate
323 *
324 * The 'rotate <options>' first level command
325 */
326 int cmd_rotate(int argc, const char **argv)
327 {
328 int opt, ret = CMD_SUCCESS, command_ret = CMD_SUCCESS, success = 1;
329 int popt_ret;
330 static poptContext pc;
331 char *session_name = NULL;
332 bool free_session_name = false;
333
334 pc = poptGetContext(NULL, argc, argv, long_options, 0);
335 popt_ret = poptReadDefaultConfig(pc, 0);
336 if (popt_ret) {
337 ret = CMD_ERROR;
338 ERR("poptReadDefaultConfig");
339 goto end;
340 }
341
342 while ((opt = poptGetNextOpt(pc)) != -1) {
343 switch (opt) {
344 case OPT_HELP:
345 SHOW_HELP();
346 goto end;
347 case OPT_LIST_OPTIONS:
348 list_cmd_options(stdout, long_options);
349 goto end;
350 default:
351 ret = CMD_UNDEFINED;
352 goto end;
353 }
354 }
355
356 opt_session_name = (char*) poptGetArg(pc);
357
358 if (!opt_session_name) {
359 session_name = get_session_name();
360 if (!session_name) {
361 goto end;
362 }
363 free_session_name = true;
364 } else {
365 session_name = opt_session_name;
366 }
367
368 /* Mi check */
369 if (lttng_opt_mi) {
370 writer = mi_lttng_writer_create(fileno(stdout), lttng_opt_mi);
371 if (!writer) {
372 ret = -LTTNG_ERR_NOMEM;
373 goto end;
374 }
375
376 /* Open rotate command */
377 ret = mi_lttng_writer_command_open(writer,
378 mi_lttng_element_command_rotate);
379 if (ret) {
380 ret = CMD_ERROR;
381 goto end;
382 }
383
384 /* Open output element */
385 ret = mi_lttng_writer_open_element(writer,
386 mi_lttng_element_command_output);
387 if (ret) {
388 goto end;
389 }
390
391 /* Open rotations element */
392 ret = mi_lttng_writer_open_element(writer,
393 mi_lttng_element_rotations);
394 if (ret) {
395 goto end;
396 }
397
398 }
399
400 command_ret = rotate_tracing(session_name);
401 if (command_ret) {
402 success = 0;
403 }
404
405 /* Mi closing */
406 if (lttng_opt_mi) {
407 /* Close rotations element */
408 ret = mi_lttng_writer_close_element(writer);
409 if (ret) {
410 goto end;
411 }
412 /* Close output element */
413 ret = mi_lttng_writer_close_element(writer);
414 if (ret) {
415 goto end;
416 }
417 /* Success ? */
418 ret = mi_lttng_writer_write_element_bool(writer,
419 mi_lttng_element_command_success, success);
420 if (ret) {
421 ret = CMD_ERROR;
422 goto end;
423 }
424
425 /* Command element close */
426 ret = mi_lttng_writer_command_close(writer);
427 if (ret) {
428 ret = CMD_ERROR;
429 goto end;
430 }
431 }
432
433 end:
434 /* Mi clean-up */
435 if (writer && mi_lttng_writer_destroy(writer)) {
436 /* Preserve original error code */
437 ret = ret ? ret : -LTTNG_ERR_MI_IO_FAIL;
438 }
439
440 /* Overwrite ret if an error occurred with start_tracing */
441 ret = command_ret ? command_ret : ret;
442 poptFreeContext(pc);
443 if (free_session_name) {
444 free(session_name);
445 }
446 return ret;
447 }
This page took 0.056329 seconds and 4 git commands to generate.