aboutsummaryrefslogtreecommitdiff
path: root/src/statistics/gnunet-statistics.c
blob: 159afda53c5a7f919421312ad68e8defb1a92286 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
/*
     This file is part of GNUnet.
     Copyright (C) 2001, 2002, 2004-2007, 2009, 2016 GNUnet e.V.

     GNUnet is free software; you can redistribute it and/or modify
     it under the terms of the GNU General Public License as published
     by the Free Software Foundation; either version 3, or (at your
     option) any later version.

     GNUnet is distributed in the hope that it will be useful, but
     WITHOUT ANY WARRANTY; without even the implied warranty of
     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     General Public License for more details.

     You should have received a copy of the GNU General Public License
     along with GNUnet; see the file COPYING.  If not, write to the
     Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
     Boston, MA 02110-1301, USA.
*/

/**
 * @file statistics/gnunet-statistics.c
 * @brief tool to obtain statistics
 * @author Christian Grothoff
 * @author Igor Wronsky
 */
#include "platform.h"
#include "gnunet_util_lib.h"
#include "gnunet_statistics_service.h"
#include "statistics.h"


/**
 * Final status code.
 */
static int ret;

/**
 * Set to subsystem that we're going to get stats for (or NULL for all).
 */
static char *subsystem;

/**
 * Set to the specific stat value that we are after (or NULL for all).
 */
static char *name;

/**
 * Make the value that is being set persistent.
 */
static int persistent;

/**
 * Watch value continuously
 */
static int watch;

/**
 * Quiet mode
 */
static int quiet;

/**
 * Remote host
 */
static char *remote_host;

/**
 * Remote host's port
 */
static unsigned long long remote_port;

/**
 * Value to set
 */
static unsigned long long set_val;

/**
 * Set operation
 */
static int set_value;

/**
 * Handle for pending GET operation.
 */
static struct GNUNET_STATISTICS_GetHandle *gh;


/**
 * Callback function to process statistic values.
 *
 * @param cls closure
 * @param subsystem name of subsystem that created the statistic
 * @param name the name of the datum
 * @param value the current value
 * @param is_persistent #GNUNET_YES if the value is persistent, #GNUNET_NO if not
 * @return #GNUNET_OK to continue, #GNUNET_SYSERR to abort iteration
 */
static int
printer (void *cls,
         const char *subsystem,
         const char *name,
         uint64_t value,
         int is_persistent)
{
  struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get();
  const char *now_str;

  if (quiet == GNUNET_NO)
  {
    if (GNUNET_YES == watch)
    {
      now_str = GNUNET_STRINGS_absolute_time_to_string (now);
      FPRINTF (stdout,
	       "%24s %s%12s %50s: %16llu\n",
               now_str,
               is_persistent ? "!" : " ",
               subsystem,
	       _(name),
	       (unsigned long long) value);
    }
    else
    {
      FPRINTF (stdout,
	       "%s%12s %50s: %16llu\n",
               is_persistent ? "!" : " ",
               subsystem,
	       _(name),
	       (unsigned long long) value);
    }
  }
  else
    FPRINTF (stdout,
	     "%llu\n",
	     (unsigned long long) value);

  return GNUNET_OK;
}


/**
 * Function called last by the statistics code.
 *
 * @param cls closure
 * @param success #GNUNET_OK if statistics were
 *        successfully obtained, #GNUNET_SYSERR if not.
 */
static void
cleanup (void *cls,
         int success)
{
  gh = NULL;
  if (GNUNET_OK != success)
  {
    if (NULL == remote_host)
      FPRINTF (stderr,
               "%s",
               _("Failed to obtain statistics.\n"));
    else
      FPRINTF (stderr,
               _("Failed to obtain statistics from host `%s:%llu'\n"),
               remote_host,
               remote_port);
    ret = 1;
  }
  GNUNET_SCHEDULER_shutdown ();
}


/**
 * Function run on shutdown to clean up.
 *
 * @param cls the statistics handle
 */
static void
shutdown_task (void *cls)
{
  struct GNUNET_STATISTICS_Handle *h = cls;

  if (NULL == h)
    return;
  if (NULL != gh)
  {
    GNUNET_STATISTICS_get_cancel (gh);
    gh = NULL;
  }
  if ( (GNUNET_YES == watch) &&
       (NULL != subsystem) &&
       (NULL != name) )
    GNUNET_assert (GNUNET_OK ==
		   GNUNET_STATISTICS_watch_cancel (h,
                                                   subsystem,
                                                   name,
                                                   &printer,
						   h));
  GNUNET_STATISTICS_destroy (h,
                             GNUNET_NO);
  h = NULL;
}


/**
 * Main task that does the actual work.
 *
 * @param cls closure with our configuration
 */
static void
main_task (void *cls)
{
  const struct GNUNET_CONFIGURATION_Handle *cfg = cls;
  struct GNUNET_STATISTICS_Handle *h;

  if (set_value)
  {
    if (NULL == subsystem)
    {
      FPRINTF (stderr,
	       "%s",
	       _("Missing argument: subsystem \n"));
      ret = 1;
      return;
    }
    if (NULL == name)
    {
      FPRINTF (stderr,
	       "%s",
	       _("Missing argument: name\n"));
      ret = 1;
      return;
    }
    h = GNUNET_STATISTICS_create (subsystem,
				  cfg);
    if (NULL == h)
    {
      ret = 1;
      return;
    }
    GNUNET_STATISTICS_set (h,
                           name,
                           (uint64_t) set_val,
                           persistent);
    GNUNET_STATISTICS_destroy (h,
                               GNUNET_YES);
    h = NULL;
    return;
  }
  if (NULL == (h = GNUNET_STATISTICS_create ("gnunet-statistics",
                                             cfg)))
  {
    ret = 1;
    return;
  }
  if (GNUNET_NO == watch)
  {
    if (NULL ==
        (gh = GNUNET_STATISTICS_get (h,
                                     subsystem,
                                     name,
                                     &cleanup,
                                     &printer,
				     h)) )
      cleanup (h,
	       GNUNET_SYSERR);
  }
  else
  {
    if ( (NULL == subsystem) ||
	 (NULL == name) )
    {
      printf (_("No subsystem or name given\n"));
      GNUNET_STATISTICS_destroy (h,
				 GNUNET_NO);
      h = NULL;
      ret = 1;
      return;
    }
    if (GNUNET_OK !=
        GNUNET_STATISTICS_watch (h,
                                 subsystem,
                                 name,
                                 &printer,
				 h))
    {
      fprintf (stderr,
               _("Failed to initialize watch routine\n"));
      GNUNET_SCHEDULER_add_now (&shutdown_task,
                                h);
      return;
    }
  }
  GNUNET_SCHEDULER_add_shutdown (&shutdown_task,
                                 h);
}


/**
 * Main function that will be run by the scheduler.
 *
 * @param cls closure
 * @param args remaining command-line arguments
 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
 * @param cfg configuration
 */
static void
run (void *cls,
     char *const *args,
     const char *cfgfile,
     const struct GNUNET_CONFIGURATION_Handle *cfg)
{
  struct GNUNET_CONFIGURATION_Handle *c;

  c = (struct GNUNET_CONFIGURATION_Handle *) cfg;
  set_value = GNUNET_NO;
  if (NULL != args[0])
  {
    if (1 != SSCANF (args[0],
		     "%llu",
		     &set_val))
    {
      FPRINTF (stderr,
	       _("Invalid argument `%s'\n"),
	       args[0]);
      ret = 1;
      return;
    }
    set_value = GNUNET_YES;
  }
  if (NULL != remote_host)
  {
    if (0 == remote_port)
    {
      if (GNUNET_SYSERR ==
	  GNUNET_CONFIGURATION_get_value_number (cfg,
						 "statistics",
						 "PORT",
						 &remote_port))
      {
	FPRINTF (stderr,
		 _("A port is required to connect to host `%s'\n"),
		 remote_host);
	return;
      }
    }
    else if (65535 <= remote_port)
    {
      FPRINTF (stderr,
	       _("A port has to be between 1 and 65535 to connect to host `%s'\n"),
	       remote_host);
      return;
    }

    /* Manipulate configuration */
    GNUNET_CONFIGURATION_set_value_string (c,
					   "statistics",
					   "UNIXPATH",
					   "");
    GNUNET_CONFIGURATION_set_value_string (c,
					   "statistics",
					   "HOSTNAME",
					   remote_host);
    GNUNET_CONFIGURATION_set_value_number (c,
					   "statistics",
					   "PORT",
					   remote_port);
  }
  GNUNET_SCHEDULER_add_now (&main_task,
			    c);
}


/**
 * The main function to obtain statistics in GNUnet.
 *
 * @param argc number of arguments from the command line
 * @param argv command line arguments
 * @return 0 ok, 1 on error
 */
int
main (int argc, char *const *argv)
{
  struct GNUNET_GETOPT_CommandLineOption options[] = {
    GNUNET_GETOPT_option_string ('n',
                                 "name",
                                 "NAME",
                                 gettext_noop ("limit output to statistics for the given NAME"),
                                 &name),

    GNUNET_GETOPT_option_flag ('p',
                                  "persistent",
                                  gettext_noop ("make the value being set persistent"),
                                  &persistent),

    GNUNET_GETOPT_option_string ('s',
                                 "subsystem",
                                 "SUBSYSTEM",
                                 gettext_noop ("limit output to the given SUBSYSTEM"),
                                 &subsystem),

    GNUNET_GETOPT_option_flag ('q',
                                  "quiet",
                                  gettext_noop ("just print the statistics value"),
                                  &quiet),

    GNUNET_GETOPT_option_flag ('w',
                                  "watch",
                                  gettext_noop ("watch value continuously"),
                                  &watch),

    GNUNET_GETOPT_option_string ('r',
                                 "remote",
                                 "REMOTE",
                                 gettext_noop ("connect to remote host"),
                                 &remote_host),
    GNUNET_GETOPT_option_ulong ('o',
                                    "port",
                                    "PORT",
                                    gettext_noop ("port for remote host"),
                                    &remote_port),
    GNUNET_GETOPT_OPTION_END
  };
  remote_port = 0;
  remote_host = NULL;
  if (GNUNET_OK !=
      GNUNET_STRINGS_get_utf8_args (argc, argv,
				    &argc, &argv))
    return 2;

  ret = (GNUNET_OK ==
	 GNUNET_PROGRAM_run (argc,
			     argv,
			     "gnunet-statistics [options [value]]",
			     gettext_noop
			     ("Print statistics about GNUnet operations."),
			     options,
			     &run,
			     NULL)) ? ret : 1;
  GNUNET_free_non_null (remote_host);
  GNUNET_free ((void*) argv);
  return ret;
}

/* end of gnunet-statistics.c */