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