2 This file is part of GNUnet.
3 (C) 2001, 2002, 2004, 2005, 2006, 2007, 2009 Christian Grothoff (and other contributing authors)
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., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file statistics/gnunet-statistics.c
23 * @brief tool to obtain statistics
24 * @author Christian Grothoff
25 * @author Igor Wronsky
28 #include "gnunet_getopt_lib.h"
29 #include "gnunet_program_lib.h"
30 #include "gnunet_util_lib.h"
31 #include "gnunet_statistics_service.h"
32 #include "statistics.h"
34 #define GET_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 1)
42 * Set to subsystem that we're going to get stats for (or NULL for all).
44 static char *subsystem;
47 * Set to the specific stat value that we are after (or NULL for all).
52 * Make the value that is being set persistent.
54 static int persistent;
57 * Watch value continuously
69 static char *remote_host;
74 static unsigned long long remote_port;
79 static unsigned long long set_val;
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
98 printer (void *cls, const char *subsystem, const char *name, uint64_t value,
101 struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get();
102 const char * now_str;
104 if (quiet == GNUNET_NO)
106 if (GNUNET_YES == watch)
108 now_str = GNUNET_STRINGS_absolute_time_to_string(now);
109 FPRINTF (stdout, "%24s %s%12s %50s: %16llu \n",
111 is_persistent ? "!" : " ",
112 subsystem, _(name), (unsigned long long) value);
116 FPRINTF (stdout, "%s%12s %50s: %16llu \n",
117 is_persistent ? "!" : " ",
118 subsystem, _(name), (unsigned long long) value);
122 FPRINTF (stdout, "%llu\n", (unsigned long long) value);
129 * Function called last by the statistics code.
132 * @param success #GNUNET_OK if statistics were
133 * successfully obtained, #GNUNET_SYSERR if not.
136 cleanup (void *cls, int success)
139 if (success != GNUNET_OK)
141 if (NULL == remote_host)
142 FPRINTF (stderr, "%s", _("Failed to obtain statistics.\n"));
144 FPRINTF (stderr, _("Failed to obtain statistics from host `%s:%llu'\n"),
145 remote_host, remote_port);
148 GNUNET_SCHEDULER_shutdown ();
153 * Function run on shutdown to clean up.
155 * @param cls the statistics handle
156 * @param tc scheduler context
159 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
161 struct GNUNET_STATISTICS_Handle *h = cls;
165 GNUNET_assert (GNUNET_OK ==
166 GNUNET_STATISTICS_watch_cancel (h, subsystem, name, &printer, h));
167 GNUNET_STATISTICS_destroy (h, GNUNET_NO);
173 * Main task that does the actual work.
175 * @param cls closure with our configuration
176 * @param tc schedueler context
179 main_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
181 const struct GNUNET_CONFIGURATION_Handle *cfg = cls;
182 struct GNUNET_STATISTICS_Handle *h;
186 if (subsystem == NULL)
188 FPRINTF (stderr, "%s", _("Missing argument: subsystem \n"));
194 FPRINTF (stderr, "%s", _("Missing argument: name\n"));
198 h = GNUNET_STATISTICS_create (subsystem, cfg);
204 GNUNET_STATISTICS_set (h, name, (uint64_t) set_val, persistent);
205 GNUNET_STATISTICS_destroy (h, GNUNET_YES);
209 if (NULL == (h = GNUNET_STATISTICS_create ("gnunet-statistics", cfg)))
214 if (GNUNET_NO == watch)
217 GNUNET_STATISTICS_get (h, subsystem, name, GET_TIMEOUT, &cleanup,
219 cleanup (h, GNUNET_SYSERR);
223 if ((NULL == subsystem) || (NULL == name))
225 printf (_("No subsystem or name given\n"));
226 GNUNET_STATISTICS_destroy (h, GNUNET_NO);
231 if (GNUNET_OK != GNUNET_STATISTICS_watch (h, subsystem, name, &printer, h))
233 fprintf (stderr, _("Failed to initialize watch routine\n"));
234 GNUNET_SCHEDULER_add_now (&shutdown_task, h);
238 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
245 * Function called with th test result to see if the resolver is
248 * @param cls closure with our configuration
249 * @param result #GNUNET_YES if the resolver is running
252 resolver_test_task (void *cls,
255 struct GNUNET_CONFIGURATION_Handle *cfg = cls;
257 if (GNUNET_YES != result)
260 _("Trying to connect to remote host, but service `%s' is not running\n"), "resolver");
263 /* connect to a remote host */
264 if (0 == remote_port)
266 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_number (cfg, "statistics", "PORT", &remote_port))
268 FPRINTF (stderr, _("A port is required to connect to host `%s'\n"), remote_host);
272 else if (65535 <= remote_port)
275 _("A port has to be between 1 and 65535 to connect to host `%s'\n"), remote_host);
279 /* Manipulate configuration */
280 GNUNET_CONFIGURATION_set_value_string (cfg,
281 "statistics", "UNIXPATH", "");
282 GNUNET_CONFIGURATION_set_value_string (cfg,
283 "statistics", "HOSTNAME", remote_host);
284 GNUNET_CONFIGURATION_set_value_number (cfg,
285 "statistics", "PORT", remote_port);
286 GNUNET_SCHEDULER_add_now (&main_task, cfg);
291 * Main function that will be run by the scheduler.
294 * @param args remaining command-line arguments
295 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
296 * @param cfg configuration
299 run (void *cls, char *const *args, const char *cfgfile,
300 const struct GNUNET_CONFIGURATION_Handle *cfg)
302 set_value = GNUNET_NO;
305 if (1 != SSCANF (args[0], "%llu", &set_val))
307 FPRINTF (stderr, _("Invalid argument `%s'\n"), args[0]);
311 set_value = GNUNET_YES;
313 if (NULL != remote_host)
314 GNUNET_CLIENT_service_test ("resolver", cfg, GNUNET_TIME_UNIT_SECONDS,
315 &resolver_test_task, (void *) cfg);
317 GNUNET_SCHEDULER_add_now (&main_task, (void *) cfg);
322 * The main function to obtain statistics in GNUnet.
324 * @param argc number of arguments from the command line
325 * @param argv command line arguments
326 * @return 0 ok, 1 on error
329 main (int argc, char *const *argv)
331 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
332 {'n', "name", "NAME",
333 gettext_noop ("limit output to statistics for the given NAME"), 1,
334 &GNUNET_GETOPT_set_string, &name},
335 {'p', "persistent", NULL,
336 gettext_noop ("make the value being set persistent"), 0,
337 &GNUNET_GETOPT_set_one, &persistent},
338 {'s', "subsystem", "SUBSYSTEM",
339 gettext_noop ("limit output to the given SUBSYSTEM"), 1,
340 &GNUNET_GETOPT_set_string, &subsystem},
342 gettext_noop ("just print the statistics value"), 0,
343 &GNUNET_GETOPT_set_one, &quiet},
345 gettext_noop ("watch value continuously"), 0,
346 &GNUNET_GETOPT_set_one, &watch},
347 {'r', "remote", NULL,
348 gettext_noop ("connect to remote host"), 1,
349 &GNUNET_GETOPT_set_string, &remote_host},
351 gettext_noop ("port for remote host"), 1,
352 &GNUNET_GETOPT_set_uint, &remote_port},
353 GNUNET_GETOPT_OPTION_END
357 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
361 GNUNET_PROGRAM_run (argc, argv, "gnunet-statistics [options [value]]",
363 ("Print statistics about GNUnet operations."),
364 options, &run, NULL)) ? ret : 1;
365 GNUNET_free_non_null (remote_host);
366 GNUNET_free ((void*) argv);
370 /* end of gnunet-statistics.c */