- debug info
[oweals/gnunet.git] / src / statistics / gnunet-statistics.c
1 /*
2      This file is part of GNUnet.
3      (C) 2001, 2002, 2004, 2005, 2006, 2007, 2009 Christian Grothoff (and other contributing authors)
4
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.
9
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.
14
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.
19 */
20
21 /**
22  * @file statistics/gnunet-statistics.c
23  * @brief tool to obtain statistics
24  * @author Christian Grothoff
25  * @author Igor Wronsky
26  */
27 #include "platform.h"
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"
33
34 #define GET_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 1)
35
36 /**
37  * Final status code.
38  */
39 static int ret;
40
41 /**
42  * Set to subsystem that we're going to get stats for (or NULL for all).
43  */
44 static char *subsystem;
45
46 /**
47  * Set to the specific stat value that we are after (or NULL for all).
48  */
49 static char *name;
50
51 /**
52  * Make the value that is being set persistent.
53  */
54 static int persistent;
55
56 /**
57  * Watch value continuously
58  */
59 static int watch;
60
61 /**
62  * Quiet mode
63  */
64 static int quiet;
65
66 /**
67  * Remote host
68  */
69 static char *remote_host;
70
71 /**
72  * Remote host's port
73  */
74 static unsigned long long  remote_port;
75
76 /**
77  * Value to set
78  */
79 static unsigned long long set_val;
80
81 /**
82  * Set operation
83  */
84 static int set_value;
85
86
87 /**
88  * Callback function to process statistic values.
89  *
90  * @param cls closure
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
96  */
97 static int
98 printer (void *cls, const char *subsystem, const char *name, uint64_t value,
99          int is_persistent)
100 {
101   struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get();
102   const char * now_str;
103
104   if (quiet == GNUNET_NO)
105   {
106     if (GNUNET_YES == watch)
107     {
108       now_str = GNUNET_STRINGS_absolute_time_to_string(now);
109       FPRINTF (stdout, "%24s %s%12s %50s: %16llu \n",
110                now_str,
111                is_persistent ? "!" : " ",
112                subsystem, _(name), (unsigned long long) value);
113     }
114     else
115     {
116       FPRINTF (stdout, "%s%12s %50s: %16llu \n",
117                is_persistent ? "!" : " ",
118                subsystem, _(name), (unsigned long long) value);
119     }
120   }
121   else
122     FPRINTF (stdout, "%llu\n", (unsigned long long) value);
123
124   return GNUNET_OK;
125 }
126
127
128 /**
129  * Function called last by the statistics code.
130  *
131  * @param cls closure
132  * @param success #GNUNET_OK if statistics were
133  *        successfully obtained, #GNUNET_SYSERR if not.
134  */
135 static void
136 cleanup (void *cls, int success)
137 {
138
139   if (success != GNUNET_OK)
140   {
141     if (NULL == remote_host)
142       FPRINTF (stderr, "%s", _("Failed to obtain statistics.\n"));
143     else
144       FPRINTF (stderr,  _("Failed to obtain statistics from host `%s:%llu'\n"),
145               remote_host, remote_port);
146     ret = 1;
147   }
148   GNUNET_SCHEDULER_shutdown ();
149 }
150
151
152 /**
153  * Function run on shutdown to clean up.
154  *
155  * @param cls the statistics handle
156  * @param tc scheduler context
157  */
158 static void
159 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
160 {
161   struct GNUNET_STATISTICS_Handle *h = cls;
162
163   if (NULL == h)
164     return;
165   if ( (GNUNET_YES == watch) &&
166        (NULL != subsystem) &&
167        (NULL != name) )
168     GNUNET_assert (GNUNET_OK ==
169                    GNUNET_STATISTICS_watch_cancel (h, subsystem, name, &printer, h));
170   GNUNET_STATISTICS_destroy (h, GNUNET_NO);
171   h = NULL;  
172 }
173
174
175 /**
176  * Main task that does the actual work.
177  *
178  * @param cls closure with our configuration
179  * @param tc schedueler context
180  */
181 static void
182 main_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
183 {
184   const struct GNUNET_CONFIGURATION_Handle *cfg = cls;
185   struct GNUNET_STATISTICS_Handle *h;
186
187   if (set_value)
188   {
189     if (subsystem == NULL)
190     {
191       FPRINTF (stderr, "%s", _("Missing argument: subsystem \n"));
192       ret = 1;
193       return;
194     }
195     if (name == NULL)
196     {
197       FPRINTF (stderr, "%s", _("Missing argument: name\n"));
198       ret = 1;
199       return;
200     }
201     h = GNUNET_STATISTICS_create (subsystem, cfg);
202     if (NULL == h)
203     {
204       ret = 1;
205       return;
206     }
207     GNUNET_STATISTICS_set (h, name, (uint64_t) set_val, persistent);
208     GNUNET_STATISTICS_destroy (h, GNUNET_YES);
209     h = NULL;
210     return;
211   }
212   if (NULL == (h = GNUNET_STATISTICS_create ("gnunet-statistics", cfg)))
213   {
214     ret = 1;
215     return;
216   }
217   if (GNUNET_NO == watch)
218   {
219     if (NULL ==
220       GNUNET_STATISTICS_get (h, subsystem, name, GET_TIMEOUT, &cleanup,
221                              &printer, h))
222     cleanup (h, GNUNET_SYSERR);
223   }
224   else
225   {
226     if ((NULL == subsystem) || (NULL == name))
227     {
228       printf (_("No subsystem or name given\n"));
229       GNUNET_STATISTICS_destroy (h, GNUNET_NO);
230       h = NULL;
231       ret = 1;
232       return;
233     }
234     if (GNUNET_OK != GNUNET_STATISTICS_watch (h, subsystem, name, &printer, h))
235     {
236       fprintf (stderr, _("Failed to initialize watch routine\n"));
237       GNUNET_SCHEDULER_add_now (&shutdown_task, h);
238       return;
239     }
240   }
241   GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
242                                 &shutdown_task, h);
243
244 }
245
246
247 /**
248  * Function called with th test result to see if the resolver is
249  * running.
250  *
251  * @param cls closure with our configuration
252  * @param result #GNUNET_YES if the resolver is running
253  */ 
254 static void
255 resolver_test_task (void *cls, 
256                     int result)
257 {
258   struct GNUNET_CONFIGURATION_Handle *cfg = cls;
259
260   if (GNUNET_YES != result)
261    {
262      FPRINTF (stderr, 
263               _("Trying to connect to remote host, but service `%s' is not running\n"), "resolver");
264      return;
265    }
266   /* connect to a remote host */
267   if (0 == remote_port)
268   {
269     if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_number (cfg, "statistics", "PORT", &remote_port))
270     {
271       FPRINTF (stderr, _("A port is required to connect to host `%s'\n"), remote_host);
272       return;
273     }
274   }
275   else if (65535 <= remote_port)
276   {
277     FPRINTF (stderr, 
278              _("A port has to be between 1 and 65535 to connect to host `%s'\n"), remote_host);
279     return;
280   }
281
282   /* Manipulate configuration */
283   GNUNET_CONFIGURATION_set_value_string (cfg, 
284                                          "statistics", "UNIXPATH", "");
285   GNUNET_CONFIGURATION_set_value_string (cfg,
286                                          "statistics", "HOSTNAME", remote_host);
287   GNUNET_CONFIGURATION_set_value_number (cfg,
288                                          "statistics", "PORT", remote_port);
289   GNUNET_SCHEDULER_add_now (&main_task, cfg);
290 }
291
292
293 /**
294  * Main function that will be run by the scheduler.
295  *
296  * @param cls closure
297  * @param args remaining command-line arguments
298  * @param cfgfile name of the configuration file used (for saving, can be NULL!)
299  * @param cfg configuration
300  */
301 static void
302 run (void *cls, char *const *args, const char *cfgfile,
303      const struct GNUNET_CONFIGURATION_Handle *cfg)
304 {
305   set_value = GNUNET_NO;
306   if (NULL != args[0])
307   {
308     if (1 != SSCANF (args[0], "%llu", &set_val))
309     {
310       FPRINTF (stderr, _("Invalid argument `%s'\n"), args[0]);
311       ret = 1;
312       return;
313     }
314     set_value = GNUNET_YES;
315   }
316   if (NULL != remote_host)
317     GNUNET_CLIENT_service_test ("resolver", cfg, GNUNET_TIME_UNIT_SECONDS, 
318                                 &resolver_test_task, (void *) cfg);
319   else
320     GNUNET_SCHEDULER_add_now (&main_task, (void *) cfg);
321 }
322
323
324 /**
325  * The main function to obtain statistics in GNUnet.
326  *
327  * @param argc number of arguments from the command line
328  * @param argv command line arguments
329  * @return 0 ok, 1 on error
330  */
331 int
332 main (int argc, char *const *argv)
333 {
334   static const struct GNUNET_GETOPT_CommandLineOption options[] = {
335     {'n', "name", "NAME",
336      gettext_noop ("limit output to statistics for the given NAME"), 1,
337      &GNUNET_GETOPT_set_string, &name},
338     {'p', "persistent", NULL,
339      gettext_noop ("make the value being set persistent"), 0,
340      &GNUNET_GETOPT_set_one, &persistent},
341     {'s', "subsystem", "SUBSYSTEM",
342      gettext_noop ("limit output to the given SUBSYSTEM"), 1,
343      &GNUNET_GETOPT_set_string, &subsystem},
344     {'q', "quiet", NULL,
345      gettext_noop ("just print the statistics value"), 0,
346      &GNUNET_GETOPT_set_one, &quiet},
347     {'w', "watch", NULL,
348      gettext_noop ("watch value continuously"), 0,
349      &GNUNET_GETOPT_set_one, &watch},
350      {'r', "remote", NULL,
351       gettext_noop ("connect to remote host"), 1,
352       &GNUNET_GETOPT_set_string, &remote_host},
353     {'o', "port", NULL,
354      gettext_noop ("port for remote host"), 1,
355      &GNUNET_GETOPT_set_uint, &remote_port},
356     GNUNET_GETOPT_OPTION_END
357   };
358   remote_port = 0;
359   remote_host = NULL;
360   if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
361     return 2;
362
363   ret = (GNUNET_OK ==
364          GNUNET_PROGRAM_run (argc, argv, "gnunet-statistics [options [value]]",
365                              gettext_noop
366                              ("Print statistics about GNUnet operations."),
367                              options, &run, NULL)) ? ret : 1;
368   GNUNET_free_non_null (remote_host);
369   GNUNET_free ((void*) argv);
370   return ret;
371 }
372
373 /* end of gnunet-statistics.c */