2 This file is part of GNUnet.
3 Copyright (C) 2001, 2002, 2004-2007, 2009, 2016 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();
109 if (quiet == GNUNET_NO)
111 if (GNUNET_YES == watch)
113 now_str = GNUNET_STRINGS_absolute_time_to_string (now);
115 "%24s %s%12s %50s: %16llu\n",
117 is_persistent ? "!" : " ",
120 (unsigned long long) value);
125 "%s%12s %50s: %16llu\n",
126 is_persistent ? "!" : " ",
129 (unsigned long long) value);
135 (unsigned long long) value);
142 * Function called last by the statistics code.
145 * @param success #GNUNET_OK if statistics were
146 * successfully obtained, #GNUNET_SYSERR if not.
153 if (GNUNET_OK != success)
155 if (NULL == remote_host)
158 _("Failed to obtain statistics.\n"));
161 _("Failed to obtain statistics from host `%s:%llu'\n"),
166 GNUNET_SCHEDULER_shutdown ();
171 * Function run on shutdown to clean up.
173 * @param cls the statistics handle
176 shutdown_task (void *cls)
178 struct GNUNET_STATISTICS_Handle *h = cls;
184 GNUNET_STATISTICS_get_cancel (gh);
187 if ( (GNUNET_YES == watch) &&
188 (NULL != subsystem) &&
190 GNUNET_assert (GNUNET_OK ==
191 GNUNET_STATISTICS_watch_cancel (h,
196 GNUNET_STATISTICS_destroy (h,
203 * Main task that does the actual work.
205 * @param cls closure with our configuration
208 main_task (void *cls)
210 const struct GNUNET_CONFIGURATION_Handle *cfg = cls;
211 struct GNUNET_STATISTICS_Handle *h;
215 if (NULL == subsystem)
219 _("Missing argument: subsystem \n"));
227 _("Missing argument: name\n"));
231 h = GNUNET_STATISTICS_create (subsystem,
238 GNUNET_STATISTICS_set (h,
242 GNUNET_STATISTICS_destroy (h,
247 if (NULL == (h = GNUNET_STATISTICS_create ("gnunet-statistics",
253 if (GNUNET_NO == watch)
256 (gh = GNUNET_STATISTICS_get (h,
267 if ( (NULL == subsystem) ||
270 printf (_("No subsystem or name given\n"));
271 GNUNET_STATISTICS_destroy (h,
278 GNUNET_STATISTICS_watch (h,
285 _("Failed to initialize watch routine\n"));
286 GNUNET_SCHEDULER_add_now (&shutdown_task,
291 GNUNET_SCHEDULER_add_shutdown (&shutdown_task,
297 * Main function that will be run by the scheduler.
300 * @param args remaining command-line arguments
301 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
302 * @param cfg configuration
308 const struct GNUNET_CONFIGURATION_Handle *cfg)
310 struct GNUNET_CONFIGURATION_Handle *c;
312 c = (struct GNUNET_CONFIGURATION_Handle *) cfg;
313 set_value = GNUNET_NO;
316 if (1 != SSCANF (args[0],
321 _("Invalid argument `%s'\n"),
326 set_value = GNUNET_YES;
328 if (NULL != remote_host)
330 if (0 == remote_port)
333 GNUNET_CONFIGURATION_get_value_number (cfg,
339 _("A port is required to connect to host `%s'\n"),
344 else if (65535 <= remote_port)
347 _("A port has to be between 1 and 65535 to connect to host `%s'\n"),
352 /* Manipulate configuration */
353 GNUNET_CONFIGURATION_set_value_string (c,
357 GNUNET_CONFIGURATION_set_value_string (c,
361 GNUNET_CONFIGURATION_set_value_number (c,
366 GNUNET_SCHEDULER_add_now (&main_task,
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 struct GNUNET_GETOPT_CommandLineOption options[] = {
382 GNUNET_GETOPT_option_string ('n',
385 gettext_noop ("limit output to statistics for the given NAME"),
388 GNUNET_GETOPT_option_flag ('p',
390 gettext_noop ("make the value being set persistent"),
393 GNUNET_GETOPT_option_string ('s',
396 gettext_noop ("limit output to the given SUBSYSTEM"),
399 GNUNET_GETOPT_option_flag ('q',
401 gettext_noop ("just print the statistics value"),
404 GNUNET_GETOPT_option_flag ('w',
406 gettext_noop ("watch value continuously"),
409 GNUNET_GETOPT_option_string ('r',
412 gettext_noop ("connect to remote host"),
414 GNUNET_GETOPT_option_ulong ('o',
417 gettext_noop ("port for remote host"),
419 GNUNET_GETOPT_OPTION_END
424 GNUNET_STRINGS_get_utf8_args (argc, argv,
429 GNUNET_PROGRAM_run (argc,
431 "gnunet-statistics [options [value]]",
433 ("Print statistics about GNUnet operations."),
437 GNUNET_free_non_null (remote_host);
438 GNUNET_free ((void*) argv);
442 /* end of gnunet-statistics.c */