abw_info.c 10.5 KB
Newer Older
's avatar
committed
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
/*
 * Copyright (c) 2006, CESNET
 * All rights reserved.
 *
 * LICENSE TERMS
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of the company nor the names of its contributors 
 *       may be used to endorse or promote products derived from this 
 *       software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, 
 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY 
 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 
 * THE COMPANY OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * $Id$
 *
 */

's avatar
committed
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "abw_conf.h"
#include "abw_rrd.h"
#include "abw_info.h"

extern configuration_t configuration;

char *progname="abw_info";
int debug=0;

void abw_info_usage() {
	fprintf(stderr, "Usage: %s -h (help)\n", progname);
	fprintf(stderr, "                -v or --verbose\n");
	fprintf(stderr, "                -f or --conf_filename <conf_filename>\n");
	fprintf(stderr, "                --subject_id <subject_id> or --label <label>\n");
	fprintf(stderr, "                --parameters_id <parameters_id>\n");
	fprintf(stderr, "                --direction { in | out }\n");
	fprintf(stderr, "\nTo print all subject_id, parameters_id tuples:\n\n");
	fprintf(stderr, "%s -f abw.cfg\n", progname);
	fprintf(stderr, "\nTo print information about a specific subject_id, label or parameters_id,\nexamples:\n\n");
	fprintf(stderr, "%s -f abw.cfg --subject_id=1\n", progname);
	fprintf(stderr, "%s -f abw.cfg -v --label=Prague_PoP --parameters_id=2\n", progname);
} /* usage() */

int main(int argc, char *argv[])
{ 
	int i;
	int verbose=0;
	char *label=NULL;
	int subject_id=0, parameters_id=0;
	char *direction=NULL;
	char *conf_filename=NULL;
	measurement_t *measurement;
	subject_t *subject_in=NULL, *subject_out=NULL;
	parameters_t *parameters=NULL;
	char *prefix_in=NULL, *prefix_out=NULL;
	int opt;
   int opt_idx;

	if (argc<2) {
		abw_info_usage();
		return -1;
	}

	while ((opt=getopt_long(argc, argv, ABW_INFO_ARGS, abw_info_long_options, 
		&opt_idx)) != -1) {

  		switch(opt) {

			/* General options */
			case 'h':
				abw_info_usage();
				return 0;
			case 'v':
			case LO_ABW_INFO_VERBOSE:
				verbose=1;
				break;
			case 'f':
			case LO_ABW_INFO_CONF_FILENAME:
				if (conf_filename) {
					fprintf(stderr, "%s: only one -f or --conf_filename argument can be specified\n", progname);
					return -1;
				}
				conf_filename=optarg;
				break;
			case LO_SUBJECT_ID:
				if (subject_id || label) {
					fprintf(stderr, "%s: only one --subject or --label argument can be specified\n", progname);
					return -1;
				}
         	subject_id=atoi(optarg);
				if (subject_id<=0) {
					fprintf(stderr, "%s: subject_id must be positive\n", progname);
					return -1;
				}
         	break;
			case LO_LABEL:
				if (subject_id || label) {
					fprintf(stderr, "%s: only one --subject or --label argument can be specified\n", progname);
					return -1;
				}
				label=optarg;
				break;
			case LO_PARAMETERS_ID:
				if (parameters_id) {
					fprintf(stderr, "%s: only one --parameters_id argument can be specified\n", progname);
					return -1;
				}
				parameters_id=atoi(optarg);
				if (parameters_id<=0) {
         	   fprintf(stderr, "%s: parameters_id must be positive\n", 
						progname);
            	return -1;
         	}
				break;
			case LO_DIRECTION:
				if (direction) {
					fprintf(stderr, "%s: only one --direction argument can be specified\n", progname);
					return -1;
				}
				direction=optarg;
				if (strcmp(direction, "in") && strcmp(direction, "out")) {
					fprintf(stderr, "%s: direction must be in our out\n", progname);
					return -1;
				}
				break;
			default:
				fprintf(stderr, "%s: unknown argument\n", __func__);
         	abw_info_usage();
         	return -1;
		} /* switch(opt) */
	}

	if (verbose) {
		if (!subject_id && !label && !parameters_id) {	
			fprintf(stderr, "%s: verbose mode can be used only when subject_id or label or parameters_id is specified\n", __func__);
			return -1;
		}
	}

	/* Create global configuration */
	if ((configuration.global=malloc(sizeof(global_t)))==NULL) {
		fprintf(stderr, "%s: malloc() failed\n", __func__);
		return -1;
	}
	memset(configuration.global, 0, sizeof(global_t));

	configuration.global->conf_filename=conf_filename;

	/* Read configuration file */

	if (read_conf_file(&configuration)<0) {
		fprintf(stderr, "%s: read_conf_file() failed\n", __func__);
		return -1;
	}
	if (read_const_conf_file(&configuration)<0) {
		fprintf(stderr, "%s: read-const_conf_file() failed\n", __func__);
		return -1;
	}

	/* Check if specified values are within acceptable limits */

	if (check_conf(&configuration)<0) {
      fprintf(stderr, "%s: check_conf() failed\n", __func__);
      return -1;
	}

	/* Go over all measurements */

	measurement=configuration.measurement;
   while (measurement) {

		/* Go over all subjects */

		i=0;
		while (measurement->scope->subject[i] && i<MAX_SUBJECTS) {

			/* 
			 * If no --subject_id or --label or --parameters_id was specified,
			 *	then just print all valid tuples <subject_id, parameters_id> 
			 */

			if (!subject_id && !label && !parameters_id)
				 printf("%d %d\n", measurement->scope->subject[i]->id,
                               measurement->parameters_id);

			/*
			 * If this is specified --subject_id, then remember it along with
			 * its direction (in or out) given in the configuration file
			 */

			if (subject_id && subject_id == measurement->scope->subject[i]->id) {
				if (measurement->scope->subject[i]->out)
					subject_out=measurement->scope->subject[i];
				else
					subject_in=measurement->scope->subject[i];
				break; /* we do not need to search remaining subjects */
			}

			/*
			 * If this is specified --label, then remember it, there can be two
			 * subjects corresponding to this label, so remember which was this one
			 */

			if (label && !strcmp(label, measurement->scope->subject[i]->label)) {
				if (measurement->scope->subject[i]->out)
               subject_out=measurement->scope->subject[i];
            else
               subject_in=measurement->scope->subject[i];
				if (subject_in && subject_out)
					break; /* we do not need to search remaining subjects */
			}

			i++;

		} /* while (measurement->scope->subject[i] && i<MAX_SUBJECTS) */

		if (parameters_id && parameters_id == measurement->parameters_id)
			parameters=measurement->parameters;

		measurement=measurement->next;

	} /* while (measurement) */

	if (!subject_id && !label && !parameters_id)
		return 0;

244
245
246
247
248
249
250
251
252
253
	if (subject_id && !subject_in && !subject_out)
		printf("unknown subject_id\n");

	if (label && !direction && !subject_in && !subject_out)
		printf("unknown label\n");

	if (label && direction && 
			((!strcmp(direction, "in") && !subject_in) ||
		 	(!strcmp(direction, "out") && !subject_out)))
		printf("unknown label and direction combination\n");
's avatar
committed
254
255
256
257
258
259

	if (parameters_id && !parameters)
		printf("unknown parameters_id\n");

	if (subject_in) {
		if ((prefix_in=abw_rrd_create_filename(subject_in->hostname,
260
			subject_in->interface, (subject_in->port >=0 )?subject_in->port:0, (parameters)?parameters->id:0, NULL))==NULL){
's avatar
committed
261
262
263
264
265
266
267
				fprintf(stderr, "%s: abw_rrd_create_filename() failed\n", __func__);
				return -1;
			}
	}

	if (subject_out) {
		if ((prefix_out=abw_rrd_create_filename(subject_out->hostname,
268
			subject_out->interface, (subject_out->port >=0 )?subject_out->port:0, (parameters)?parameters->id:0, NULL))==NULL){
's avatar
committed
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
				fprintf(stderr, "%s: abw_rrd_create_filename() failed\n", __func__);
				return -1;
			}
	}

	if (verbose) {
		if (subject_in && (!direction || !strcmp(direction, "in"))) {
			printf("hostname_in: %s\n", subject_in->hostname);
			printf("interface_in: %s\n", subject_in->interface);
			printf("prefix_in: %s\n", prefix_in);
			printf("label_in: %s\n", subject_in->label);
			printf("description_in: %s\n",
				(subject_in->description)?subject_in->description:"");
		}
		if (subject_out && (!direction || !strcmp(direction, "out"))) {
			printf("hostname_out: %s\n", subject_out->hostname);
			printf("interface_out: %s\n", subject_out->interface);
			printf("prefix_out: %s\n", prefix_out);
			printf("label_out: %s\n", subject_out->label);
			printf("description_out: %s\n",
				(subject_out->description)?subject_out->description:"");
		}
		if (parameters) {
			printf("header_filter: %s\n",
				(parameters->header_filter)?parameters->header_filter:"");
			printf("sau_mode: %c\n", parameters->sau_mode);
			printf("sau_threshold: %.02f\n", parameters->sau_threshold);
			printf("payload_string: %s\n",
				(parameters->payload_strings[0])?parameters->payload_strings[0]:"");
			printf("interval_sec: %u\n",
				(unsigned int)(parameters->interval.tv_sec));
			printf("interval_usec: %u\n",
                (unsigned int)(parameters->interval.tv_usec));
		}
	}
	else {
		if (subject_in && (!direction || !strcmp(direction, "in"))) {
        	printf("%s %s %s %s \"%s\"\n", subject_in->hostname,
           	subject_in->interface, prefix_in, subject_in->label,
              (subject_in->description)?(subject_in->description):"");
		}
		if (subject_out && (!direction || !strcmp(direction, "out"))) {
        	printf("%s %s %s %s \"%s\"\n", subject_out->hostname,
           	subject_out->interface, prefix_out, subject_out->label,
              (subject_out->description)?(subject_out->description):"");
		}
		if (parameters) {
			printf("%c %.02f %s %s\n", parameters->sau_mode,
317
318
319
				parameters->sau_threshold, 
				(parameters->payload_strings[0])?parameters->payload_strings[0]:"-",
				(parameters->header_filter)?parameters->header_filter:"-");
's avatar
committed
320
321
322
323
324
		}
	}

	return 0;
} /* main() */