missing check
[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  * Callback function to process statistic values.
88  *
89  * @param cls closure
90  * @param subsystem name of subsystem that created the statistic
91  * @param name the name of the datum
92  * @param value the current value
93  * @param is_persistent GNUNET_YES if the value is persistent, GNUNET_NO if not
94  * @return GNUNET_OK to continue, GNUNET_SYSERR to abort iteration
95  */
96 static int
97 printer (void *cls, const char *subsystem, const char *name, uint64_t value,
98          int is_persistent)
99 {
100   struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get();
101   const char * now_str;
102
103   if (quiet == GNUNET_NO)
104   {
105     if (GNUNET_YES == watch)
106     {
107       now_str = GNUNET_STRINGS_absolute_time_to_string(now);
108       FPRINTF (stdout, "%24s %s%12s %50s: %16llu \n",
109                now_str,
110                is_persistent ? "!" : " ",
111                subsystem, _(name), (unsigned long long) value);
112     }
113     else
114     {
115       FPRINTF (stdout, "%s%12s %50s: %16llu \n",
116                is_persistent ? "!" : " ",
117                subsystem, _(name), (unsigned long long) value);
118     }
119   }
120   else
121     FPRINTF (stdout, "%llu\n", (unsigned long long) value);
122
123   return GNUNET_OK;
124 }
125
126
127 /**
128  * Function called last by the statistics code.
129  *
130  * @param cls closure
131  * @param success GNUNET_OK if statistics were
132  *        successfully obtained, GNUNET_SYSERR if not.
133  */
134 static void
135 cleanup (void *cls, int success)
136 {
137
138   if (success != GNUNET_OK)
139   {
140     if (NULL == remote_host)
141       FPRINTF (stderr, "%s", _("Failed to obtain statistics.\n"));
142     else
143       FPRINTF (stderr,  _("Failed to obtain statistics from host `%s:%llu'\n"),
144               remote_host, remote_port);
145     ret = 1;
146   }
147   GNUNET_SCHEDULER_shutdown ();
148 }
149
150
151 /**
152  * Function run on shutdown to clean up.
153  *
154  * @param cls the statistics handle
155  * @param tc scheduler context
156  */
157 static void
158 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
159 {
160   struct GNUNET_STATISTICS_Handle *h = cls;
161
162   if (NULL == h)
163     return;
164   GNUNET_STATISTICS_watch_cancel (h, subsystem, name, &printer, h);
165   GNUNET_STATISTICS_destroy (h, GNUNET_NO);
166   h = NULL;  
167 }
168
169
170 /**
171  * Main task that does the actual work.
172  *
173  * @param cls closure with our configuration
174  * @param tc schedueler context
175  */
176 static void
177 main_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
178 {
179   const struct GNUNET_CONFIGURATION_Handle *cfg = cls;
180   struct GNUNET_STATISTICS_Handle *h;
181
182   if (set_value)
183   {
184     if (subsystem == NULL)
185     {
186       FPRINTF (stderr, "%s", _("Missing argument: subsystem \n"));
187       ret = 1;
188       return;
189     }
190     if (name == NULL)
191     {
192       FPRINTF (stderr, "%s", _("Missing argument: name\n"));
193       ret = 1;
194       return;
195     }
196     h = GNUNET_STATISTICS_create (subsystem, cfg);
197     if (NULL == h)
198     {
199       ret = 1;
200       return;
201     }
202     GNUNET_STATISTICS_set (h, name, (uint64_t) set_val, persistent);
203     GNUNET_STATISTICS_destroy (h, GNUNET_YES);
204     h = NULL;
205     return;
206   }
207   if (NULL == (h = GNUNET_STATISTICS_create ("gnunet-statistics", cfg)))
208   {
209     ret = 1;
210     return;
211   }
212   if (GNUNET_NO == watch)
213   {
214     if (NULL ==
215       GNUNET_STATISTICS_get (h, subsystem, name, GET_TIMEOUT, &cleanup,
216                              &printer, h))
217     cleanup (h, GNUNET_SYSERR);
218   }
219   else
220   {
221     if ((NULL == subsystem) || (NULL == name))
222     {
223       printf (_("No subsystem or name given\n"));
224       GNUNET_STATISTICS_destroy (h, GNUNET_NO);
225       h = NULL;
226       ret = 1;
227       return;
228     }
229     if (GNUNET_OK != GNUNET_STATISTICS_watch (h, subsystem, name, &printer, h))
230     {
231       fprintf (stderr, _("Failed to initialize watch routine\n"));
232       GNUNET_SCHEDULER_add_now (&shutdown_task, h);
233       return;
234     }
235   }
236   GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
237                                 &shutdown_task, h);
238
239 }
240
241
242 /**
243  * Function called with th test result to see if the resolver is
244  * running.
245  *
246  * @param cls closure with our configuration
247  * @param result GNUNET_YES if the resolver is running
248  */ 
249 static void
250 resolver_test_task (void *cls, 
251                     int result)
252 {
253   struct GNUNET_CONFIGURATION_Handle *cfg = cls;
254
255   if (GNUNET_YES != result)
256    {
257      FPRINTF (stderr, 
258               _("Trying to connect to remote host, but service `%s' is not running\n"), "resolver");
259      return;
260    }
261   /* connect to a remote host */
262   if (0 == remote_port)
263   {
264     if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_number (cfg, "statistics", "PORT", &remote_port))
265     {
266       FPRINTF (stderr, _("A port is required to connect to host `%s'\n"), remote_host);
267       return;
268     }
269   }
270   else if (65535 <= remote_port)
271   {
272     FPRINTF (stderr, 
273              _("A port has to be between 1 and 65535 to connect to host `%s'\n"), remote_host);
274     return;
275   }
276
277   /* Manipulate configuration */
278   GNUNET_CONFIGURATION_set_value_string (cfg, 
279                                          "statistics", "UNIXPATH", "");
280   GNUNET_CONFIGURATION_set_value_string (cfg,
281                                          "statistics", "HOSTNAME", remote_host);
282   GNUNET_CONFIGURATION_set_value_number (cfg,
283                                          "statistics", "PORT", remote_port);
284   GNUNET_SCHEDULER_add_now (&main_task, cfg);
285 }
286
287
288 /**
289  * Main function that will be run by the scheduler.
290  *
291  * @param cls closure
292  * @param args remaining command-line arguments
293  * @param cfgfile name of the configuration file used (for saving, can be NULL!)
294  * @param cfg configuration
295  */
296 static void
297 run (void *cls, char *const *args, const char *cfgfile,
298      const struct GNUNET_CONFIGURATION_Handle *cfg)
299 {
300   set_value = GNUNET_NO;
301   if (NULL != args[0])
302   {
303     if (1 != SSCANF (args[0], "%llu", &set_val))
304     {
305       FPRINTF (stderr, _("Invalid argument `%s'\n"), args[0]);
306       ret = 1;
307       return;
308     }
309     set_value = GNUNET_YES;
310   }
311   if (NULL != remote_host)
312     GNUNET_CLIENT_service_test ("resolver", cfg, GNUNET_TIME_UNIT_SECONDS, 
313                                 &resolver_test_task, (void *) cfg);
314   else
315     GNUNET_SCHEDULER_add_now (&main_task, (void *) cfg);
316 }
317
318
319 /**
320  * The main function to obtain statistics in GNUnet.
321  *
322  * @param argc number of arguments from the command line
323  * @param argv command line arguments
324  * @return 0 ok, 1 on error
325  */
326 int
327 main (int argc, char *const *argv)
328 {
329   static const struct GNUNET_GETOPT_CommandLineOption options[] = {
330     {'n', "name", "NAME",
331      gettext_noop ("limit output to statistics for the given NAME"), 1,
332      &GNUNET_GETOPT_set_string, &name},
333     {'p', "persistent", NULL,
334      gettext_noop ("make the value being set persistent"), 0,
335      &GNUNET_GETOPT_set_one, &persistent},
336     {'s', "subsystem", "SUBSYSTEM",
337      gettext_noop ("limit output to the given SUBSYSTEM"), 1,
338      &GNUNET_GETOPT_set_string, &subsystem},
339     {'q', "quiet", NULL,
340      gettext_noop ("just print the statistics value"), 0,
341      &GNUNET_GETOPT_set_one, &quiet},
342     {'w', "watch", NULL,
343      gettext_noop ("watch value continuously"), 0,
344      &GNUNET_GETOPT_set_one, &watch},
345      {'r', "remote", NULL,
346       gettext_noop ("connect to remote host"), 1,
347       &GNUNET_GETOPT_set_string, &remote_host},
348     {'o', "port", NULL,
349      gettext_noop ("port for remote host"), 1,
350      &GNUNET_GETOPT_set_uint, &remote_port},
351     GNUNET_GETOPT_OPTION_END
352   };
353   remote_port = 0;
354   remote_host = NULL;
355   if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
356     return 2;
357
358   ret = (GNUNET_OK ==
359          GNUNET_PROGRAM_run (argc, argv, "gnunet-statistics [options [value]]",
360                              gettext_noop
361                              ("Print statistics about GNUnet operations."),
362                              options, &run, NULL)) ? ret : 1;
363   GNUNET_free_non_null (remote_host);
364   GNUNET_free ((void*) argv);
365   return ret;
366 }
367
368 /* end of gnunet-statistics.c */