13468592a84b9611425332b4b6eec9cd15474490
[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  * Callback function to process statistic values.
78  *
79  * @param cls closure
80  * @param subsystem name of subsystem that created the statistic
81  * @param name the name of the datum
82  * @param value the current value
83  * @param is_persistent GNUNET_YES if the value is persistent, GNUNET_NO if not
84  * @return GNUNET_OK to continue, GNUNET_SYSERR to abort iteration
85  */
86 static int
87 printer (void *cls, const char *subsystem, const char *name, uint64_t value,
88          int is_persistent)
89 {
90   struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get();
91   const char * now_str;
92
93   if (quiet == GNUNET_NO)
94   {
95     if (GNUNET_YES == watch)
96     {
97       now_str = GNUNET_STRINGS_absolute_time_to_string(now);
98       FPRINTF (stdout, "%24s %s%12s %50s: %16llu \n",
99                now_str,
100                is_persistent ? "!" : " ",
101                subsystem, _(name), (unsigned long long) value);
102     }
103     else
104     {
105       FPRINTF (stdout, "%s%12s %50s: %16llu \n",
106                is_persistent ? "!" : " ",
107                subsystem, _(name), (unsigned long long) value);
108     }
109   }
110   else
111     FPRINTF (stdout, "%llu\n", (unsigned long long) value);
112
113   return GNUNET_OK;
114 }
115
116
117 /**
118  * Function called last by the statistics code.
119  *
120  * @param cls closure
121  * @param success GNUNET_OK if statistics were
122  *        successfully obtained, GNUNET_SYSERR if not.
123  */
124 static void
125 cleanup (void *cls, int success)
126 {
127
128   if (success != GNUNET_OK)
129   {
130     if (NULL == remote_host)
131       FPRINTF (stderr, "%s", _("Failed to obtain statistics.\n"));
132     else
133       FPRINTF (stderr,  _("Failed to obtain statistics from host `%s:%llu'\n"),
134               remote_host, remote_port);
135     ret = 1;
136   }
137   GNUNET_SCHEDULER_shutdown ();
138 }
139
140
141 static void
142 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
143 {
144   struct GNUNET_STATISTICS_Handle *h = cls;
145
146   if (NULL != h)
147   {
148     GNUNET_STATISTICS_watch_cancel (h, subsystem, name, &printer, h);
149     GNUNET_STATISTICS_destroy (h, GNUNET_NO);
150     h = NULL;
151   }
152 }
153
154
155 /**
156  * Main function that will be run by the scheduler.
157  *
158  * @param cls closure
159  * @param args remaining command-line arguments
160  * @param cfgfile name of the configuration file used (for saving, can be NULL!)
161  * @param cfg configuration
162  */
163 static void
164 run (void *cls, char *const *args, const char *cfgfile,
165      const struct GNUNET_CONFIGURATION_Handle *cfg)
166 {
167   struct GNUNET_STATISTICS_Handle *h;
168   unsigned long long val;
169
170   if (NULL != remote_host)
171   {
172       /* connect to a remote host */
173       if (0 == remote_port)
174       {
175         if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_number (cfg, "statistics", "PORT", &remote_port))
176         {
177           FPRINTF (stderr, _("A port is required to connect to host `%s'\n"), remote_host);
178           return;
179         }
180       }
181       else if (65535 <= remote_port)
182       {
183           FPRINTF (stderr, _("A port has to be between 1 and 65535 to connect to host `%s'\n"), remote_host);
184           return;
185       }
186
187       /* Manipulate configuration */
188       GNUNET_CONFIGURATION_set_value_string ((struct GNUNET_CONFIGURATION_Handle *) cfg, "statistics", "UNIXPATH", "");
189       GNUNET_CONFIGURATION_set_value_string ((struct GNUNET_CONFIGURATION_Handle *) cfg, "statistics", "HOSTNAME", remote_host);
190       GNUNET_CONFIGURATION_set_value_number ((struct GNUNET_CONFIGURATION_Handle *) cfg, "statistics", "PORT", remote_port);
191   }
192
193   if (args[0] != NULL)
194   {
195     if ((1 != SSCANF (args[0], "%llu", &val)) || (subsystem == NULL) ||
196         (name == NULL))
197     {
198       FPRINTF (stderr, _("Invalid argument `%s'\n"), args[0]);
199       ret = 1;
200       return;
201     }
202     h = GNUNET_STATISTICS_create (subsystem, cfg);
203     if (NULL == h)
204     {
205       ret = 1;
206       return;
207     }
208     GNUNET_STATISTICS_set (h, name, (uint64_t) val, persistent);
209     GNUNET_STATISTICS_destroy (h, GNUNET_YES);
210     h = NULL;
211     return;
212   }
213   h = GNUNET_STATISTICS_create ("gnunet-statistics", cfg);
214   GNUNET_SCHEDULER_add_delayed(GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task, h);
215   if (NULL == h)
216   {
217     ret = 1;
218     return;
219   }
220   if (GNUNET_NO == watch)
221   {
222     if (NULL ==
223       GNUNET_STATISTICS_get (h, subsystem, name, GET_TIMEOUT, &cleanup,
224                              &printer, h))
225     cleanup (h, GNUNET_SYSERR);
226   }
227   else
228   {
229     if ((NULL == subsystem) || (NULL == name))
230     {
231       printf (_("No subsystem or name given\n"));
232       if (h != NULL)
233         GNUNET_STATISTICS_destroy (h, GNUNET_NO);
234       ret = 1;
235       return;
236     }
237     if (GNUNET_OK != GNUNET_STATISTICS_watch (h, subsystem, name, &printer, h))
238     {
239       fprintf (stderr, _("Failed to initialize watch routine\n"));
240       GNUNET_SCHEDULER_add_now (&shutdown_task, h);
241       return;
242     }
243   }
244 }
245
246 /**
247  * The main function to obtain statistics in GNUnet.
248  *
249  * @param argc number of arguments from the command line
250  * @param argv command line arguments
251  * @return 0 ok, 1 on error
252  */
253 int
254 main (int argc, char *const *argv)
255 {
256   static const struct GNUNET_GETOPT_CommandLineOption options[] = {
257     {'n', "name", "NAME",
258      gettext_noop ("limit output to statistics for the given NAME"), 1,
259      &GNUNET_GETOPT_set_string, &name},
260     {'p', "persistent", NULL,
261      gettext_noop ("make the value being set persistent"), 0,
262      &GNUNET_GETOPT_set_one, &persistent},
263     {'s', "subsystem", "SUBSYSTEM",
264      gettext_noop ("limit output to the given SUBSYSTEM"), 1,
265      &GNUNET_GETOPT_set_string, &subsystem},
266     {'q', "quiet", NULL,
267      gettext_noop ("just print the statistics value"), 0,
268      &GNUNET_GETOPT_set_one, &quiet},
269     {'w', "watch", NULL,
270      gettext_noop ("watch value continuously"), 0,
271      &GNUNET_GETOPT_set_one, &watch},
272      {'r', "remote", NULL,
273       gettext_noop ("connect to remote host"), 1,
274       &GNUNET_GETOPT_set_string, &remote_host},
275     {'o', "port", NULL,
276      gettext_noop ("port for remote host"), 1,
277      &GNUNET_GETOPT_set_uint, &remote_port},
278     GNUNET_GETOPT_OPTION_END
279   };
280
281   remote_port = 0;
282   remote_host = NULL;
283   if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
284     return 2;
285
286   return (GNUNET_OK ==
287           GNUNET_PROGRAM_run (argc, argv, "gnunet-statistics [options [value]]",
288                               gettext_noop
289                               ("Print statistics about GNUnet operations."),
290                               options, &run, NULL)) ? ret : 1;
291
292   GNUNET_free_non_null(remote_host);
293 }
294
295 /* end of gnunet-statistics.c */