From ad007ffc71c10c1ace8ed820dc195d92c9bda0c7 Mon Sep 17 00:00:00 2001 From: Florian Forster Date: Thu, 19 Aug 2010 09:01:25 +0200 Subject: [PATCH] collectd-tg: Add option parsing and usage information. It's now possible to configure the parameters on the command line. --- src/collectd-tg.c | 126 ++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 121 insertions(+), 5 deletions(-) diff --git a/src/collectd-tg.c b/src/collectd-tg.c index 7aadecf5..7b86deb2 100644 --- a/src/collectd-tg.c +++ b/src/collectd-tg.c @@ -1,5 +1,5 @@ /** - * collectd - src/collectd-tg.c + * collectd-td - collectd traffic generator * Copyright (C) 2010 Florian octo Forster * * This program is free software; you can redistribute it and/or modify it @@ -23,7 +23,24 @@ # include "config.h" #endif +#ifndef _ISOC99_SOURCE +# define _ISOC99_SOURCE +#endif + +#ifndef _POSIX_C_SOURCE +# define _POSIX_C_SOURCE 200809L +#endif + +#ifndef _XOPEN_SOURCE +# define _XOPEN_SOURCE 700 +#endif + +#if !__GNUC__ +# define __attribute__(x) /**/ +#endif + #include +#include #include #include #include @@ -35,9 +52,15 @@ #include "libcollectdclient/collectd/network.h" #include "libcollectdclient/collectd/network_buffer.h" -static int conf_num_hosts = 1000; -static int conf_num_plugins = 20; -static int conf_num_values = 100000; +#define DEF_NUM_HOSTS 1000 +#define DEF_NUM_PLUGINS 20 +#define DEF_NUM_VALUES 100000 + +static int conf_num_hosts = DEF_NUM_HOSTS; +static int conf_num_plugins = DEF_NUM_PLUGINS; +static int conf_num_values = DEF_NUM_VALUES; +static const char *conf_destination = NET_DEFAULT_V6_ADDR; +static const char *conf_service = NET_DEFAULT_PORT; static lcc_network_t *net; @@ -48,6 +71,31 @@ static struct sigaction sigterm_action; static _Bool loop = 1; +__attribute__((noreturn)) +static void exit_usage (int exit_status) /* {{{ */ +{ + fprintf ((exit_status == EXIT_FAILURE) ? stderr : stdout, + "collectd-tg -- collectd traffic generator\n" + "\n" + " Usage: collectd-ng [OPTION]\n" + "\n" + " Valid options:\n" + " -n Number of value lists. (Default: %i)\n" + " -H Number of hosts to emulate. (Default: %i)\n" + " -p Number of plugins to emulate. (Default: %i)\n" + " -d Destination address of the network packets.\n" + " (Default: %s)\n" + " -D Destination port of the network packets.\n" + " (Default: %s)\n" + " -h Print usage information (this output).\n" + "\n" + "Copyright (C) 2010 Florian Forster\n" + "Licensed under the GNU General Public License, version 2 (GPLv2)\n", + DEF_NUM_VALUES, DEF_NUM_HOSTS, DEF_NUM_PLUGINS, + NET_DEFAULT_V6_ADDR, NET_DEFAULT_PORT); + exit (exit_status); +} /* }}} void exit_usage */ + static void signal_handler (int signal) /* {{{ */ { loop = 0; @@ -212,12 +260,80 @@ static int send_value (lcc_value_list_t *vl) /* {{{ */ return (0); } /* }}} int send_value */ +static int read_options (int argc, char **argv) /* {{{ */ +{ + int opt; + + while ((opt = getopt (argc, argv, "n:H:p:d:D:h")) != -1) + { + switch (opt) + { + case 'n': + { + int tmp = atoi (optarg); + if (tmp < 1) + { + fprintf (stderr, "Unable to parse option as a number: \"%s\"\n", + optarg); + exit (EXIT_FAILURE); + } + conf_num_values = tmp; + } + break; + + case 'H': + { + int tmp = atoi (optarg); + if (tmp < 1) + { + fprintf (stderr, "Unable to parse option as a number: \"%s\"\n", + optarg); + exit (EXIT_FAILURE); + } + conf_num_hosts = tmp; + } + break; + + case 'p': + { + int tmp = atoi (optarg); + if (tmp < 1) + { + fprintf (stderr, "Unable to parse option as a number: \"%s\"\n", + optarg); + exit (EXIT_FAILURE); + } + conf_num_plugins = tmp; + } + break; + + case 'd': + conf_destination = optarg; + break; + + case 'D': + conf_service = optarg; + break; + + case 'h': + exit_usage (EXIT_SUCCESS); + + default: + exit_usage (EXIT_FAILURE); + } /* switch (opt) */ + } /* while (getopt) */ + + return (0); +} /* }}} int read_options */ + int main (int argc, char **argv) /* {{{ */ { int i; time_t last_time; int values_sent = 0; + read_options (argc, argv); + sigint_action.sa_handler = signal_handler; sigaction (SIGINT, &sigint_action, /* old = */ NULL); @@ -242,7 +358,7 @@ int main (int argc, char **argv) /* {{{ */ { lcc_server_t *srv; - srv = lcc_server_create (net, NET_DEFAULT_V6_ADDR, NET_DEFAULT_PORT); + srv = lcc_server_create (net, conf_destination, conf_service); if (srv == NULL) { fprintf (stderr, "lcc_server_create failed.\n"); -- 2.30.2