status.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /* Copyright (c) 2010-2013, 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. #define STATUS_PRIVATE
  8. #include "or.h"
  9. #include "config.h"
  10. #include "status.h"
  11. #include "nodelist.h"
  12. #include "relay.h"
  13. #include "router.h"
  14. #include "circuitlist.h"
  15. #include "main.h"
  16. #include "rephist.h"
  17. #include "hibernate.h"
  18. #include "rephist.h"
  19. #include "statefile.h"
  20. static void log_accounting(const time_t now, const or_options_t *options);
  21. /** Return the total number of circuits. */
  22. STATIC int
  23. count_circuits(void)
  24. {
  25. circuit_t *circ;
  26. int nr=0;
  27. TOR_LIST_FOREACH(circ, circuit_get_global_list(), head)
  28. nr++;
  29. return nr;
  30. }
  31. /** Take seconds <b>secs</b> and return a newly allocated human-readable
  32. * uptime string */
  33. STATIC char *
  34. secs_to_uptime(long secs)
  35. {
  36. long int days = secs / 86400;
  37. int hours = (int)((secs - (days * 86400)) / 3600);
  38. int minutes = (int)((secs - (days * 86400) - (hours * 3600)) / 60);
  39. char *uptime_string = NULL;
  40. switch (days) {
  41. case 0:
  42. tor_asprintf(&uptime_string, "%d:%02d hours", hours, minutes);
  43. break;
  44. case 1:
  45. tor_asprintf(&uptime_string, "%ld day %d:%02d hours",
  46. days, hours, minutes);
  47. break;
  48. default:
  49. tor_asprintf(&uptime_string, "%ld days %d:%02d hours",
  50. days, hours, minutes);
  51. break;
  52. }
  53. return uptime_string;
  54. }
  55. /** Take <b>bytes</b> and returns a newly allocated human-readable usage
  56. * string. */
  57. STATIC char *
  58. bytes_to_usage(uint64_t bytes)
  59. {
  60. char *bw_string = NULL;
  61. if (bytes < (1<<20)) { /* Less than a megabyte. */
  62. tor_asprintf(&bw_string, U64_FORMAT" kB", U64_PRINTF_ARG(bytes>>10));
  63. } else if (bytes < (1<<30)) { /* Megabytes. Let's add some precision. */
  64. double bw = U64_TO_DBL(bytes);
  65. tor_asprintf(&bw_string, "%.2f MB", bw/(1<<20));
  66. } else { /* Gigabytes. */
  67. double bw = U64_TO_DBL(bytes);
  68. tor_asprintf(&bw_string, "%.2f GB", bw/(1<<30));
  69. }
  70. return bw_string;
  71. }
  72. /** Log a "heartbeat" message describing Tor's status and history so that the
  73. * user can know that there is indeed a running Tor. Return 0 on success and
  74. * -1 on failure. */
  75. int
  76. log_heartbeat(time_t now)
  77. {
  78. char *bw_sent = NULL;
  79. char *bw_rcvd = NULL;
  80. char *uptime = NULL;
  81. const routerinfo_t *me;
  82. double r = tls_get_write_overhead_ratio();
  83. const int hibernating = we_are_hibernating();
  84. const or_options_t *options = get_options();
  85. (void)now;
  86. if (public_server_mode(options) && !hibernating) {
  87. /* Let's check if we are in the current cached consensus. */
  88. if (!(me = router_get_my_routerinfo()))
  89. return -1; /* Something stinks, we won't even attempt this. */
  90. else
  91. if (!node_get_by_id(me->cache_info.identity_digest))
  92. log_fn(LOG_NOTICE, LD_HEARTBEAT, "Heartbeat: It seems like we are not "
  93. "in the cached consensus.");
  94. }
  95. uptime = secs_to_uptime(get_uptime());
  96. bw_rcvd = bytes_to_usage(get_bytes_read());
  97. bw_sent = bytes_to_usage(get_bytes_written());
  98. log_fn(LOG_NOTICE, LD_HEARTBEAT, "Heartbeat: Tor's uptime is %s, with %d "
  99. "circuits open. I've sent %s and received %s.%s",
  100. uptime, count_circuits(),bw_sent,bw_rcvd,
  101. hibernating?" We are currently hibernating.":"");
  102. if (server_mode(options) && accounting_is_enabled(options) && !hibernating) {
  103. log_accounting(now, options);
  104. }
  105. if (stats_n_data_cells_packaged && !hibernating)
  106. log_notice(LD_HEARTBEAT, "Average packaged cell fullness: %2.3f%%",
  107. 100*(U64_TO_DBL(stats_n_data_bytes_packaged) /
  108. U64_TO_DBL(stats_n_data_cells_packaged*RELAY_PAYLOAD_SIZE)) );
  109. if (r > 1.0) {
  110. double overhead = ( r - 1.0 ) * 100.0;
  111. log_notice(LD_HEARTBEAT, "TLS write overhead: %.f%%", overhead);
  112. }
  113. if (public_server_mode(options))
  114. rep_hist_log_circuit_handshake_stats(now);
  115. tor_free(uptime);
  116. tor_free(bw_sent);
  117. tor_free(bw_rcvd);
  118. return 0;
  119. }
  120. static void
  121. log_accounting(const time_t now, const or_options_t *options)
  122. {
  123. or_state_t *state = get_or_state();
  124. char *acc_rcvd = bytes_to_usage(state->AccountingBytesReadInInterval);
  125. char *acc_sent = bytes_to_usage(state->AccountingBytesWrittenInInterval);
  126. char *acc_max = bytes_to_usage(options->AccountingMax);
  127. time_t interval_end = accounting_get_end_time();
  128. char end_buf[ISO_TIME_LEN + 1];
  129. char *remaining = NULL;
  130. format_local_iso_time(end_buf, interval_end);
  131. remaining = secs_to_uptime(interval_end - now);
  132. log_notice(LD_HEARTBEAT, "Heartbeat: Accounting enabled. "
  133. "Sent: %s / %s, Received: %s / %s. The "
  134. "current accounting interval ends on %s, in %s.",
  135. acc_sent, acc_max, acc_rcvd, acc_max, end_buf, remaining);
  136. tor_free(acc_rcvd);
  137. tor_free(acc_sent);
  138. tor_free(acc_max);
  139. tor_free(remaining);
  140. }