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 it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your 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 Affero General Public License for more details.
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
20 * @file statistics/gnunet-statistics.c
21 * @brief tool to obtain statistics
22 * @author Christian Grothoff
23 * @author Igor Wronsky
26 #include "gnunet_util_lib.h"
27 #include "gnunet_statistics_service.h"
28 #include "statistics.h"
37 * Set to subsystem that we're going to get stats for (or NULL for all).
39 static char *subsystem;
42 * Set to the specific stat value that we are after (or NULL for all).
47 * Make the value that is being set persistent.
49 static int persistent;
52 * Watch value continuously
64 static char *remote_host;
69 static unsigned long long remote_port;
74 static unsigned long long set_val;
82 * Handle for pending GET operation.
84 static struct GNUNET_STATISTICS_GetHandle *gh;
88 * Callback function to process statistic values.
91 * @param subsystem name of subsystem that created the statistic
92 * @param name the name of the datum
93 * @param value the current value
94 * @param is_persistent #GNUNET_YES if the value is persistent, #GNUNET_NO if not
95 * @return #GNUNET_OK to continue, #GNUNET_SYSERR to abort iteration
99 const char *subsystem,
104 struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get();
107 if (quiet == GNUNET_NO)
109 if (GNUNET_YES == watch)
111 now_str = GNUNET_STRINGS_absolute_time_to_string (now);
113 "%24s %s%12s %50s: %16llu\n",
115 is_persistent ? "!" : " ",
118 (unsigned long long) value);
123 "%s%12s %50s: %16llu\n",
124 is_persistent ? "!" : " ",
127 (unsigned long long) value);
133 (unsigned long long) value);
140 * Function called last by the statistics code.
143 * @param success #GNUNET_OK if statistics were
144 * successfully obtained, #GNUNET_SYSERR if not.
151 if (GNUNET_OK != success)
153 if (NULL == remote_host)
156 _("Failed to obtain statistics.\n"));
159 _("Failed to obtain statistics from host `%s:%llu'\n"),
164 GNUNET_SCHEDULER_shutdown ();
169 * Function run on shutdown to clean up.
171 * @param cls the statistics handle
174 shutdown_task (void *cls)
176 struct GNUNET_STATISTICS_Handle *h = cls;
182 GNUNET_STATISTICS_get_cancel (gh);
185 if ( (GNUNET_YES == watch) &&
186 (NULL != subsystem) &&
188 GNUNET_assert (GNUNET_OK ==
189 GNUNET_STATISTICS_watch_cancel (h,
194 GNUNET_STATISTICS_destroy (h,
201 * Main task that does the actual work.
203 * @param cls closure with our configuration
206 main_task (void *cls)
208 const struct GNUNET_CONFIGURATION_Handle *cfg = cls;
209 struct GNUNET_STATISTICS_Handle *h;
213 if (NULL == subsystem)
217 _("Missing argument: subsystem \n"));
225 _("Missing argument: name\n"));
229 h = GNUNET_STATISTICS_create (subsystem,
236 GNUNET_STATISTICS_set (h,
240 GNUNET_STATISTICS_destroy (h,
245 if (NULL == (h = GNUNET_STATISTICS_create ("gnunet-statistics",
251 if (GNUNET_NO == watch)
254 (gh = GNUNET_STATISTICS_get (h,
265 if ( (NULL == subsystem) ||
268 printf (_("No subsystem or name given\n"));
269 GNUNET_STATISTICS_destroy (h,
276 GNUNET_STATISTICS_watch (h,
283 _("Failed to initialize watch routine\n"));
284 GNUNET_SCHEDULER_add_now (&shutdown_task,
289 GNUNET_SCHEDULER_add_shutdown (&shutdown_task,
295 * Main function that will be run by the scheduler.
298 * @param args remaining command-line arguments
299 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
300 * @param cfg configuration
306 const struct GNUNET_CONFIGURATION_Handle *cfg)
308 struct GNUNET_CONFIGURATION_Handle *c;
310 c = (struct GNUNET_CONFIGURATION_Handle *) cfg;
311 set_value = GNUNET_NO;
314 if (1 != SSCANF (args[0],
319 _("Invalid argument `%s'\n"),
324 set_value = GNUNET_YES;
326 if (NULL != remote_host)
328 if (0 == remote_port)
331 GNUNET_CONFIGURATION_get_value_number (cfg,
337 _("A port is required to connect to host `%s'\n"),
342 else if (65535 <= remote_port)
345 _("A port has to be between 1 and 65535 to connect to host `%s'\n"),
350 /* Manipulate configuration */
351 GNUNET_CONFIGURATION_set_value_string (c,
355 GNUNET_CONFIGURATION_set_value_string (c,
359 GNUNET_CONFIGURATION_set_value_number (c,
364 GNUNET_SCHEDULER_add_now (&main_task,
370 * The main function to obtain statistics in GNUnet.
372 * @param argc number of arguments from the command line
373 * @param argv command line arguments
374 * @return 0 ok, 1 on error
377 main (int argc, char *const *argv)
379 struct GNUNET_GETOPT_CommandLineOption options[] = {
380 GNUNET_GETOPT_option_string ('n',
383 gettext_noop ("limit output to statistics for the given NAME"),
386 GNUNET_GETOPT_option_flag ('p',
388 gettext_noop ("make the value being set persistent"),
391 GNUNET_GETOPT_option_string ('s',
394 gettext_noop ("limit output to the given SUBSYSTEM"),
397 GNUNET_GETOPT_option_flag ('q',
399 gettext_noop ("just print the statistics value"),
402 GNUNET_GETOPT_option_flag ('w',
404 gettext_noop ("watch value continuously"),
407 GNUNET_GETOPT_option_string ('r',
410 gettext_noop ("connect to remote host"),
412 GNUNET_GETOPT_option_ulong ('o',
415 gettext_noop ("port for remote host"),
417 GNUNET_GETOPT_OPTION_END
422 GNUNET_STRINGS_get_utf8_args (argc, argv,
427 GNUNET_PROGRAM_run (argc,
429 "gnunet-statistics [options [value]]",
431 ("Print statistics about GNUnet operations."),
435 GNUNET_free_non_null (remote_host);
436 GNUNET_free ((void*) argv);
440 /* end of gnunet-statistics.c */