2 This file is part of GNUnet.
3 Copyright (C) 2001, 2002, 2004, 2005, 2006, 2007, 2009 GNUnet e.V.
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
22 * @file statistics/gnunet-statistics.c
23 * @brief tool to obtain statistics
24 * @author Christian Grothoff
25 * @author Igor Wronsky
28 #include "gnunet_util_lib.h"
29 #include "gnunet_statistics_service.h"
30 #include "statistics.h"
39 * Set to subsystem that we're going to get stats for (or NULL for all).
41 static char *subsystem;
44 * Set to the specific stat value that we are after (or NULL for all).
49 * Make the value that is being set persistent.
51 static int persistent;
54 * Watch value continuously
66 static char *remote_host;
71 static unsigned long long remote_port;
76 static unsigned long long set_val;
84 * Handle for pending GET operation.
86 static struct GNUNET_STATISTICS_GetHandle *gh;
90 * Callback function to process statistic values.
93 * @param subsystem name of subsystem that created the statistic
94 * @param name the name of the datum
95 * @param value the current value
96 * @param is_persistent #GNUNET_YES if the value is persistent, #GNUNET_NO if not
97 * @return #GNUNET_OK to continue, #GNUNET_SYSERR to abort iteration
101 const char *subsystem,
106 struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get();
107 const char * now_str;
109 if (quiet == GNUNET_NO)
111 if (GNUNET_YES == watch)
113 now_str = GNUNET_STRINGS_absolute_time_to_string(now);
114 FPRINTF (stdout, "%24s %s%12s %50s: %16llu \n",
116 is_persistent ? "!" : " ",
117 subsystem, _(name), (unsigned long long) value);
121 FPRINTF (stdout, "%s%12s %50s: %16llu \n",
122 is_persistent ? "!" : " ",
123 subsystem, _(name), (unsigned long long) value);
127 FPRINTF (stdout, "%llu\n", (unsigned long long) value);
134 * Function called last by the statistics code.
137 * @param success #GNUNET_OK if statistics were
138 * successfully obtained, #GNUNET_SYSERR if not.
145 if (GNUNET_OK != success)
147 if (NULL == remote_host)
150 _("Failed to obtain statistics.\n"));
153 _("Failed to obtain statistics from host `%s:%llu'\n"),
158 GNUNET_SCHEDULER_shutdown ();
163 * Function run on shutdown to clean up.
165 * @param cls the statistics handle
168 shutdown_task (void *cls)
170 struct GNUNET_STATISTICS_Handle *h = cls;
176 GNUNET_STATISTICS_get_cancel (gh);
179 if ( (GNUNET_YES == watch) &&
180 (NULL != subsystem) &&
182 GNUNET_assert (GNUNET_OK ==
183 GNUNET_STATISTICS_watch_cancel (h,
187 GNUNET_STATISTICS_destroy (h,
194 * Main task that does the actual work.
196 * @param cls closure with our configuration
199 main_task (void *cls)
201 const struct GNUNET_CONFIGURATION_Handle *cfg = cls;
202 struct GNUNET_STATISTICS_Handle *h;
206 if (NULL == subsystem)
208 FPRINTF (stderr, "%s", _("Missing argument: subsystem \n"));
214 FPRINTF (stderr, "%s", _("Missing argument: name\n"));
218 h = GNUNET_STATISTICS_create (subsystem, cfg);
224 GNUNET_STATISTICS_set (h,
228 GNUNET_STATISTICS_destroy (h,
233 if (NULL == (h = GNUNET_STATISTICS_create ("gnunet-statistics",
239 if (GNUNET_NO == watch)
242 (gh = GNUNET_STATISTICS_get (h,
247 cleanup (h, GNUNET_SYSERR);
251 if ((NULL == subsystem) || (NULL == name))
253 printf (_("No subsystem or name given\n"));
254 GNUNET_STATISTICS_destroy (h, GNUNET_NO);
260 GNUNET_STATISTICS_watch (h,
266 _("Failed to initialize watch routine\n"));
267 GNUNET_SCHEDULER_add_now (&shutdown_task,
272 GNUNET_SCHEDULER_add_shutdown (&shutdown_task,
278 * Function called with th test result to see if the resolver is
281 * @param cls closure with our configuration
282 * @param result #GNUNET_YES if the resolver is running
285 resolver_test_task (void *cls,
288 struct GNUNET_CONFIGURATION_Handle *cfg = cls;
290 if (GNUNET_YES != result)
293 _("Trying to connect to remote host, but service `%s' is not running\n"),
297 /* connect to a remote host */
298 if (0 == remote_port)
301 GNUNET_CONFIGURATION_get_value_number (cfg, "statistics",
306 _("A port is required to connect to host `%s'\n"),
311 else if (65535 <= remote_port)
314 _("A port has to be between 1 and 65535 to connect to host `%s'\n"),
319 /* Manipulate configuration */
320 GNUNET_CONFIGURATION_set_value_string (cfg,
324 GNUNET_CONFIGURATION_set_value_string (cfg,
328 GNUNET_CONFIGURATION_set_value_number (cfg,
332 GNUNET_SCHEDULER_add_now (&main_task, cfg);
337 * Main function that will be run by the scheduler.
340 * @param args remaining command-line arguments
341 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
342 * @param cfg configuration
348 const struct GNUNET_CONFIGURATION_Handle *cfg)
350 set_value = GNUNET_NO;
353 if (1 != SSCANF (args[0], "%llu", &set_val))
355 FPRINTF (stderr, _("Invalid argument `%s'\n"), args[0]);
359 set_value = GNUNET_YES;
361 if (NULL != remote_host)
362 GNUNET_CLIENT_service_test ("resolver",
364 GNUNET_TIME_UNIT_SECONDS,
365 &resolver_test_task, (void *) cfg);
367 GNUNET_SCHEDULER_add_now (&main_task, (void *) cfg);
372 * The main function to obtain statistics in GNUnet.
374 * @param argc number of arguments from the command line
375 * @param argv command line arguments
376 * @return 0 ok, 1 on error
379 main (int argc, char *const *argv)
381 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
382 {'n', "name", "NAME",
383 gettext_noop ("limit output to statistics for the given NAME"), 1,
384 &GNUNET_GETOPT_set_string, &name},
385 {'p', "persistent", NULL,
386 gettext_noop ("make the value being set persistent"), 0,
387 &GNUNET_GETOPT_set_one, &persistent},
388 {'s', "subsystem", "SUBSYSTEM",
389 gettext_noop ("limit output to the given SUBSYSTEM"), 1,
390 &GNUNET_GETOPT_set_string, &subsystem},
392 gettext_noop ("just print the statistics value"), 0,
393 &GNUNET_GETOPT_set_one, &quiet},
395 gettext_noop ("watch value continuously"), 0,
396 &GNUNET_GETOPT_set_one, &watch},
397 {'r', "remote", NULL,
398 gettext_noop ("connect to remote host"), 1,
399 &GNUNET_GETOPT_set_string, &remote_host},
401 gettext_noop ("port for remote host"), 1,
402 &GNUNET_GETOPT_set_uint, &remote_port},
403 GNUNET_GETOPT_OPTION_END
407 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv,
412 GNUNET_PROGRAM_run (argc, argv, "gnunet-statistics [options [value]]",
414 ("Print statistics about GNUnet operations."),
415 options, &run, NULL)) ? ret : 1;
416 GNUNET_free_non_null (remote_host);
417 GNUNET_free ((void*) argv);
421 /* end of gnunet-statistics.c */