status.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /* Copyright (c) 2010-2012, The Tor Project, Inc. */
  2. /* See LICENSE for licensing information */
  3. /**
  4. * \file status.c
  5. * \brief Keep status information and log the heartbeat messages.
  6. **/
  7. #include "or.h"
  8. #include "config.h"
  9. #include "status.h"
  10. #include "nodelist.h"
  11. #include "router.h"
  12. #include "circuitlist.h"
  13. #include "main.h"
  14. /** Return the total number of circuits. */
  15. static int
  16. count_circuits(void)
  17. {
  18. circuit_t *circ;
  19. int nr=0;
  20. for (circ = circuit_get_global_list_(); circ; circ = circ->next)
  21. nr++;
  22. return nr;
  23. }
  24. /** Take seconds <b>secs</b> and return a newly allocated human-readable
  25. * uptime string */
  26. static char *
  27. secs_to_uptime(long secs)
  28. {
  29. long int days = secs / 86400;
  30. int hours = (int)((secs - (days * 86400)) / 3600);
  31. int minutes = (int)((secs - (days * 86400) - (hours * 3600)) / 60);
  32. char *uptime_string = NULL;
  33. switch (days) {
  34. case 0:
  35. tor_asprintf(&uptime_string, "%d:%02d hours", hours, minutes);
  36. break;
  37. case 1:
  38. tor_asprintf(&uptime_string, "%ld day %d:%02d hours",
  39. days, hours, minutes);
  40. break;
  41. default:
  42. tor_asprintf(&uptime_string, "%ld days %d:%02d hours",
  43. days, hours, minutes);
  44. break;
  45. }
  46. return uptime_string;
  47. }
  48. /** Take <b>bytes</b> and returns a newly allocated human-readable usage
  49. * string. */
  50. static char *
  51. bytes_to_usage(uint64_t bytes)
  52. {
  53. char *bw_string = NULL;
  54. if (bytes < (1<<20)) { /* Less than a megabyte. */
  55. tor_asprintf(&bw_string, U64_FORMAT" kB", U64_PRINTF_ARG(bytes>>10));
  56. } else if (bytes < (1<<30)) { /* Megabytes. Let's add some precision. */
  57. double bw = U64_TO_DBL(bytes);
  58. tor_asprintf(&bw_string, "%.2f MB", bw/(1<<20));
  59. } else { /* Gigabytes. */
  60. double bw = U64_TO_DBL(bytes);
  61. tor_asprintf(&bw_string, "%.2f GB", bw/(1<<30));
  62. }
  63. return bw_string;
  64. }
  65. /** Log a "heartbeat" message describing Tor's status and history so that the
  66. * user can know that there is indeed a running Tor. Return 0 on success and
  67. * -1 on failure. */
  68. int
  69. log_heartbeat(time_t now)
  70. {
  71. char *bw_sent = NULL;
  72. char *bw_rcvd = NULL;
  73. char *uptime = NULL;
  74. const routerinfo_t *me;
  75. const or_options_t *options = get_options();
  76. (void)now;
  77. if (public_server_mode(options)) {
  78. /* Let's check if we are in the current cached consensus. */
  79. if (!(me = router_get_my_routerinfo()))
  80. return -1; /* Something stinks, we won't even attempt this. */
  81. else
  82. if (!node_get_by_id(me->cache_info.identity_digest))
  83. log_fn(LOG_NOTICE, LD_HEARTBEAT, "Heartbeat: It seems like we are not "
  84. "in the cached consensus.");
  85. }
  86. uptime = secs_to_uptime(get_uptime());
  87. bw_rcvd = bytes_to_usage(get_bytes_read());
  88. bw_sent = bytes_to_usage(get_bytes_written());
  89. log_fn(LOG_NOTICE, LD_HEARTBEAT, "Heartbeat: Tor's uptime is %s, with %d "
  90. "circuits open. I've sent %s and received %s.",
  91. uptime, count_circuits(),bw_sent,bw_rcvd);
  92. tor_free(uptime);
  93. tor_free(bw_sent);
  94. tor_free(bw_rcvd);
  95. return 0;
  96. }