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"
32 #define GET_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 1)
40 * Set to subsystem that we're going to get stats for (or NULL for all).
42 static char *subsystem;
45 * Set to the specific stat value that we are after (or NULL for all).
50 * Make the value that is being set persistent.
52 static int persistent;
55 * Watch value continuously
67 static char *remote_host;
72 static unsigned long long remote_port;
77 static unsigned long long set_val;
86 * Callback function to process statistic values.
89 * @param subsystem name of subsystem that created the statistic
90 * @param name the name of the datum
91 * @param value the current value
92 * @param is_persistent #GNUNET_YES if the value is persistent, #GNUNET_NO if not
93 * @return #GNUNET_OK to continue, #GNUNET_SYSERR to abort iteration
97 const char *subsystem,
102 struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get();
103 const char * now_str;
105 if (quiet == GNUNET_NO)
107 if (GNUNET_YES == watch)
109 now_str = GNUNET_STRINGS_absolute_time_to_string(now);
110 FPRINTF (stdout, "%24s %s%12s %50s: %16llu \n",
112 is_persistent ? "!" : " ",
113 subsystem, _(name), (unsigned long long) value);
117 FPRINTF (stdout, "%s%12s %50s: %16llu \n",
118 is_persistent ? "!" : " ",
119 subsystem, _(name), (unsigned long long) value);
123 FPRINTF (stdout, "%llu\n", (unsigned long long) value);
130 * Function called last by the statistics code.
133 * @param success #GNUNET_OK if statistics were
134 * successfully obtained, #GNUNET_SYSERR if not.
137 cleanup (void *cls, int success)
140 if (success != GNUNET_OK)
142 if (NULL == remote_host)
145 _("Failed to obtain statistics.\n"));
148 _("Failed to obtain statistics from host `%s:%llu'\n"),
153 GNUNET_SCHEDULER_shutdown ();
158 * Function run on shutdown to clean up.
160 * @param cls the statistics handle
163 shutdown_task (void *cls)
165 struct GNUNET_STATISTICS_Handle *h = cls;
169 if ( (GNUNET_YES == watch) &&
170 (NULL != subsystem) &&
172 GNUNET_assert (GNUNET_OK ==
173 GNUNET_STATISTICS_watch_cancel (h, subsystem, name, &printer, h));
174 GNUNET_STATISTICS_destroy (h, GNUNET_NO);
180 * Main task that does the actual work.
182 * @param cls closure with our configuration
185 main_task (void *cls)
187 const struct GNUNET_CONFIGURATION_Handle *cfg = cls;
188 struct GNUNET_STATISTICS_Handle *h;
192 if (NULL == subsystem)
194 FPRINTF (stderr, "%s", _("Missing argument: subsystem \n"));
200 FPRINTF (stderr, "%s", _("Missing argument: name\n"));
204 h = GNUNET_STATISTICS_create (subsystem, cfg);
210 GNUNET_STATISTICS_set (h, name, (uint64_t) set_val, persistent);
211 GNUNET_STATISTICS_destroy (h, GNUNET_YES);
215 if (NULL == (h = GNUNET_STATISTICS_create ("gnunet-statistics", cfg)))
220 if (GNUNET_NO == watch)
223 GNUNET_STATISTICS_get (h, subsystem, name, GET_TIMEOUT,
226 cleanup (h, GNUNET_SYSERR);
230 if ((NULL == subsystem) || (NULL == name))
232 printf (_("No subsystem or name given\n"));
233 GNUNET_STATISTICS_destroy (h, GNUNET_NO);
238 if (GNUNET_OK != GNUNET_STATISTICS_watch (h, subsystem, name,
241 fprintf (stderr, _("Failed to initialize watch routine\n"));
242 GNUNET_SCHEDULER_add_now (&shutdown_task, h);
246 GNUNET_SCHEDULER_add_shutdown (&shutdown_task, h);
251 * Function called with th test result to see if the resolver is
254 * @param cls closure with our configuration
255 * @param result #GNUNET_YES if the resolver is running
258 resolver_test_task (void *cls,
261 struct GNUNET_CONFIGURATION_Handle *cfg = cls;
263 if (GNUNET_YES != result)
266 _("Trying to connect to remote host, but service `%s' is not running\n"),
270 /* connect to a remote host */
271 if (0 == remote_port)
274 GNUNET_CONFIGURATION_get_value_number (cfg, "statistics",
279 _("A port is required to connect to host `%s'\n"),
284 else if (65535 <= remote_port)
287 _("A port has to be between 1 and 65535 to connect to host `%s'\n"),
292 /* Manipulate configuration */
293 GNUNET_CONFIGURATION_set_value_string (cfg,
294 "statistics", "UNIXPATH", "");
295 GNUNET_CONFIGURATION_set_value_string (cfg,
296 "statistics", "HOSTNAME", remote_host);
297 GNUNET_CONFIGURATION_set_value_number (cfg,
298 "statistics", "PORT", remote_port);
299 GNUNET_SCHEDULER_add_now (&main_task, cfg);
304 * Main function that will be run by the scheduler.
307 * @param args remaining command-line arguments
308 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
309 * @param cfg configuration
312 run (void *cls, char *const *args, const char *cfgfile,
313 const struct GNUNET_CONFIGURATION_Handle *cfg)
315 set_value = GNUNET_NO;
318 if (1 != SSCANF (args[0], "%llu", &set_val))
320 FPRINTF (stderr, _("Invalid argument `%s'\n"), args[0]);
324 set_value = GNUNET_YES;
326 if (NULL != remote_host)
327 GNUNET_CLIENT_service_test ("resolver", cfg, GNUNET_TIME_UNIT_SECONDS,
328 &resolver_test_task, (void *) cfg);
330 GNUNET_SCHEDULER_add_now (&main_task, (void *) cfg);
335 * The main function to obtain statistics in GNUnet.
337 * @param argc number of arguments from the command line
338 * @param argv command line arguments
339 * @return 0 ok, 1 on error
342 main (int argc, char *const *argv)
344 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
345 {'n', "name", "NAME",
346 gettext_noop ("limit output to statistics for the given NAME"), 1,
347 &GNUNET_GETOPT_set_string, &name},
348 {'p', "persistent", NULL,
349 gettext_noop ("make the value being set persistent"), 0,
350 &GNUNET_GETOPT_set_one, &persistent},
351 {'s', "subsystem", "SUBSYSTEM",
352 gettext_noop ("limit output to the given SUBSYSTEM"), 1,
353 &GNUNET_GETOPT_set_string, &subsystem},
355 gettext_noop ("just print the statistics value"), 0,
356 &GNUNET_GETOPT_set_one, &quiet},
358 gettext_noop ("watch value continuously"), 0,
359 &GNUNET_GETOPT_set_one, &watch},
360 {'r', "remote", NULL,
361 gettext_noop ("connect to remote host"), 1,
362 &GNUNET_GETOPT_set_string, &remote_host},
364 gettext_noop ("port for remote host"), 1,
365 &GNUNET_GETOPT_set_uint, &remote_port},
366 GNUNET_GETOPT_OPTION_END
370 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
374 GNUNET_PROGRAM_run (argc, argv, "gnunet-statistics [options [value]]",
376 ("Print statistics about GNUnet operations."),
377 options, &run, NULL)) ? ret : 1;
378 GNUNET_free_non_null (remote_host);
379 GNUNET_free ((void*) argv);
383 /* end of gnunet-statistics.c */