gnunet-statistics.c 9.3 KB

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