Network streaming support
[lttng-tools.git] / src / bin / lttng / commands / create.c
1 /*
2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
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 _GNU_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 <time.h>
26 #include <unistd.h>
27
28 #include "../command.h"
29 #include "../utils.h"
30
31 #include <common/defaults.h>
32 #include <common/sessiond-comm/sessiond-comm.h>
33 #include <common/uri.h>
34
35 static char *opt_output_path;
36 static char *opt_session_name;
37 static char *opt_uris;
38 static char *opt_ctrl_uris;
39 static char *opt_data_uris;
40 static int opt_no_consumer = 1;
41
42 enum {
43 OPT_HELP = 1,
44 OPT_LIST_OPTIONS,
45 };
46
47 static struct poptOption long_options[] = {
48 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
49 {"help", 'h', POPT_ARG_NONE, NULL, OPT_HELP, NULL, NULL},
50 {"output", 'o', POPT_ARG_STRING, &opt_output_path, 0, NULL, NULL},
51 {"list-options", 0, POPT_ARG_NONE, NULL, OPT_LIST_OPTIONS, NULL, NULL},
52 {"set-uri", 'U', POPT_ARG_STRING, &opt_uris, 0, 0, 0},
53 {"ctrl-uri", 'C', POPT_ARG_STRING, &opt_ctrl_uris, 0, 0, 0},
54 {"data-uri", 'D', POPT_ARG_STRING, &opt_data_uris, 0, 0, 0},
55 {"no-consumer", 0, POPT_ARG_NONE, &opt_no_consumer, 0, 0, 0},
56 {0, 0, 0, 0, 0, 0, 0}
57 };
58
59 /*
60 * usage
61 */
62 static void usage(FILE *ofp)
63 {
64 fprintf(ofp, "usage: lttng create [options] [NAME]\n");
65 fprintf(ofp, "\n");
66 fprintf(ofp, " The default NAME is 'auto-yyyymmdd-hhmmss'\n");
67 fprintf(ofp, " -h, --help Show this help\n");
68 fprintf(ofp, " --list-options Simple listing of options\n");
69 fprintf(ofp, " -o, --output PATH Specify output path for traces\n");
70 fprintf(ofp, " -U, --set-uri=URI Set URI for the enable-consumer destination.\n");
71 fprintf(ofp, " It is persistent for the session lifetime.\n");
72 fprintf(ofp, " Redo the command to change it.\n");
73 fprintf(ofp, " This will set both data and control URI for network.\n");
74 fprintf(ofp, " -C, --ctrl-uri=URI Set control path URI.\n");
75 fprintf(ofp, " -D, --data-uri=URI Set data path URI.\n");
76 fprintf(ofp, " --no-consumer Disable consumer for entire tracing session.\n");
77 fprintf(ofp, "\n");
78 }
79
80 /*
81 * Parse URI from string to lttng_uri object array.
82 */
83 static ssize_t parse_uri_from_str(const char *str_uri, struct lttng_uri **uris)
84 {
85 int i;
86 ssize_t size;
87 struct lttng_uri *uri;
88
89 if (*uris != NULL) {
90 free(*uris);
91 }
92
93 size = uri_parse(str_uri, uris);
94 if (size < 1) {
95 ERR("Bad URI %s. Either the hostname or IP is invalid", str_uri);
96 size = -1;
97 }
98
99 for (i = 0; i < size; i++) {
100 uri = (struct lttng_uri *) &uris[i];
101 /* Set default port if none was given */
102 if (uri->port == 0) {
103 if (uri->stype == LTTNG_STREAM_CONTROL) {
104 uri->port = DEFAULT_NETWORK_CONTROL_PORT;
105 } else if (uri->stype == LTTNG_STREAM_DATA) {
106 uri->port = DEFAULT_NETWORK_DATA_PORT;
107 }
108 }
109 }
110
111 return size;
112 }
113
114 /*
115 * Print URI message.
116 */
117 static void print_uri_msg(struct lttng_uri *uri)
118 {
119 char *dst;
120
121 switch (uri->dtype) {
122 case LTTNG_DST_IPV4:
123 dst = uri->dst.ipv4;
124 break;
125 case LTTNG_DST_IPV6:
126 dst = uri->dst.ipv6;
127 break;
128 case LTTNG_DST_PATH:
129 dst = uri->dst.path;
130 MSG("Consumer destination set to %s", dst);
131 goto end;
132 default:
133 DBG("Unknown URI destination");
134 goto end;
135 }
136
137 MSG("Consumer %s stream set to %s with the %s protocol on port %d",
138 uri->stype == LTTNG_STREAM_CONTROL ? "control" : "data",
139 dst, uri->proto == LTTNG_TCP ? "TCP" : "UNK", uri->port);
140
141 end:
142 return;
143 }
144
145 /*
146 * Create a tracing session.
147 * If no name is specified, a default name is generated.
148 *
149 * Returns one of the CMD_* result constants.
150 */
151 static int create_session()
152 {
153 int ret, have_name = 0, i;
154 char datetime[16];
155 char *session_name, *traces_path = NULL, *alloc_path = NULL;
156 time_t rawtime;
157 ssize_t size;
158 struct tm *timeinfo;
159 struct lttng_uri *uris = NULL, *ctrl_uri = NULL, *data_uri = NULL;
160
161 /* Get date and time for automatic session name/path */
162 time(&rawtime);
163 timeinfo = localtime(&rawtime);
164 strftime(datetime, sizeof(datetime), "%Y%m%d-%H%M%S", timeinfo);
165
166 /* Auto session name creation */
167 if (opt_session_name == NULL) {
168 ret = asprintf(&session_name, "auto-%s", datetime);
169 if (ret < 0) {
170 perror("asprintf session name");
171 goto error;
172 }
173 DBG("Auto session name set to %s", session_name);
174 } else {
175 session_name = opt_session_name;
176 have_name = 1;
177 }
178
179 if (opt_output_path != NULL) {
180 traces_path = expand_full_path(opt_output_path);
181 if (traces_path == NULL) {
182 ret = CMD_ERROR;
183 goto error;
184 }
185
186 ret = asprintf(&alloc_path, "file://%s", traces_path);
187 if (ret < 0) {
188 PERROR("asprintf expand path");
189 ret = CMD_FATAL;
190 goto error;
191 }
192
193 ret = uri_parse(alloc_path, &ctrl_uri);
194 if (ret < 1) {
195 ret = CMD_FATAL;
196 goto error;
197 }
198 } else if (opt_uris) { /* Handling URIs (-U opt) */
199 size = parse_uri_from_str(opt_uris, &uris);
200 if (size < 1) {
201 ret = CMD_ERROR;
202 goto error;
203 } else if (size == 1 && uris[0].dtype != LTTNG_DST_PATH) {
204 ERR("Only net:// and file:// are supported. "
205 "Use -C and -D for more fine grained control");
206 ret = CMD_ERROR;
207 goto error;
208 } else if (size == 2) {
209 uris[0].stype = LTTNG_STREAM_CONTROL;
210 uris[1].stype = LTTNG_STREAM_DATA;
211
212 for (i = 0; i < size; i++) {
213 /* Set default port if none was given */
214 if (uris[i].port == 0) {
215 if (uris[i].stype == LTTNG_STREAM_CONTROL) {
216 uris[i].port = DEFAULT_NETWORK_CONTROL_PORT;
217 } else {
218 uris[i].port = DEFAULT_NETWORK_DATA_PORT;
219 }
220 }
221 }
222
223 ctrl_uri = &uris[0];
224 print_uri_msg(ctrl_uri);
225 data_uri = &uris[1];
226 print_uri_msg(data_uri);
227 } else {
228 ctrl_uri = &uris[0];
229 print_uri_msg(ctrl_uri);
230 }
231 } else if (opt_ctrl_uris || opt_data_uris) {
232 /* Setting up control URI (-C opt) */
233 if (opt_ctrl_uris) {
234 size = parse_uri_from_str(opt_ctrl_uris, &uris);
235 if (size < 1) {
236 ret = CMD_ERROR;
237 goto error;
238 }
239 ctrl_uri = &uris[0];
240 ctrl_uri->stype = LTTNG_STREAM_CONTROL;
241 /* Set default port if none specified */
242 if (ctrl_uri->port == 0) {
243 ctrl_uri->port = DEFAULT_NETWORK_CONTROL_PORT;
244 }
245 print_uri_msg(ctrl_uri);
246 }
247
248 /* Setting up data URI (-D opt) */
249 if (opt_data_uris) {
250 size = parse_uri_from_str(opt_data_uris, &uris);
251 if (size < 1) {
252 ret = CMD_ERROR;
253 goto error;
254 }
255 data_uri = &uris[0];
256 data_uri->stype = LTTNG_STREAM_DATA;
257 /* Set default port if none specified */
258 if (data_uri->port == 0) {
259 data_uri->port = DEFAULT_NETWORK_DATA_PORT;
260 }
261 print_uri_msg(data_uri);
262 }
263 } else {
264 /* Auto output path */
265 alloc_path = config_get_default_path();
266 if (alloc_path == NULL) {
267 ERR("HOME path not found.\n \
268 Please specify an output path using -o, --output PATH");
269 ret = CMD_FATAL;
270 goto error;
271 }
272 alloc_path = strdup(alloc_path);
273
274 if (have_name) {
275 ret = asprintf(&traces_path, "file://%s/" DEFAULT_TRACE_DIR_NAME
276 "/%s-%s", alloc_path, session_name, datetime);
277 } else {
278 ret = asprintf(&traces_path, "file://%s/" DEFAULT_TRACE_DIR_NAME
279 "/%s", alloc_path, session_name);
280 }
281 if (ret < 0) {
282 PERROR("asprintf trace dir name");
283 ret = CMD_FATAL;
284 goto error;
285 }
286
287 ret = uri_parse(traces_path, &ctrl_uri);
288 if (ret < 1) {
289 ret = CMD_FATAL;
290 goto error;
291 }
292 }
293
294 /* If there is no subdir specified and the URI are network */
295 if (strlen(ctrl_uri->subdir) == 0) {
296 if (have_name) {
297 ret = snprintf(ctrl_uri->subdir, sizeof(ctrl_uri->subdir), "%s-%s",
298 session_name, datetime);
299 } else {
300 ret = snprintf(ctrl_uri->subdir, sizeof(ctrl_uri->subdir), "%s",
301 session_name);
302 }
303 if (ret < 0) {
304 PERROR("snprintf subdir");
305 goto error;
306 }
307 DBG("Subdir update to %s", ctrl_uri->subdir);
308 }
309
310 ret = lttng_create_session_uri(session_name, ctrl_uri, data_uri,
311 opt_no_consumer);
312 if (ret < 0) {
313 /* Don't set ret so lttng can interpret the sessiond error. */
314 switch (-ret) {
315 case LTTCOMM_EXIST_SESS:
316 WARN("Session %s already exists", session_name);
317 break;
318 }
319 goto error;
320 }
321
322 /* Init lttng session config */
323 ret = config_init(session_name);
324 if (ret < 0) {
325 ret = CMD_ERROR;
326 goto error;
327 }
328
329 MSG("Session %s created.", session_name);
330 if (ctrl_uri->dtype == LTTNG_DST_PATH) {
331 MSG("Traces will be written in %s" , ctrl_uri->dst.path);
332 }
333
334 ret = CMD_SUCCESS;
335
336 error:
337 if (opt_session_name == NULL) {
338 free(session_name);
339 }
340
341 if (alloc_path) {
342 free(alloc_path);
343 }
344
345 if (traces_path) {
346 free(traces_path);
347 }
348 return ret;
349 }
350
351 /*
352 * The 'create <options>' first level command
353 *
354 * Returns one of the CMD_* result constants.
355 */
356 int cmd_create(int argc, const char **argv)
357 {
358 int opt, ret = CMD_SUCCESS;
359 static poptContext pc;
360
361 pc = poptGetContext(NULL, argc, argv, long_options, 0);
362 poptReadDefaultConfig(pc, 0);
363
364 while ((opt = poptGetNextOpt(pc)) != -1) {
365 switch (opt) {
366 case OPT_HELP:
367 usage(stdout);
368 goto end;
369 case OPT_LIST_OPTIONS:
370 list_cmd_options(stdout, long_options);
371 goto end;
372 default:
373 usage(stderr);
374 ret = CMD_UNDEFINED;
375 goto end;
376 }
377 }
378
379 opt_session_name = (char*) poptGetArg(pc);
380
381 ret = create_session();
382
383 end:
384 poptFreeContext(pc);
385 return ret;
386 }
This page took 0.056373 seconds and 4 git commands to generate.