status.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /* Copyright (c) 2010-2016, The Tor Project, Inc. */
  2. /* See LICENSE for licensing information */
  3. /**
  4. * \file status.c
  5. * \brief Collect status information and log heartbeat messages.
  6. *
  7. * This module is responsible for implementing the heartbeat log messages,
  8. * which periodically inform users and operators about basic facts to
  9. * do with their Tor instance. The log_heartbeat() function, invoked from
  10. * main.c, is the principle entry point. It collects data from elsewhere
  11. * in Tor, and logs it in a human-readable format.
  12. **/
  13. #define STATUS_PRIVATE
  14. #include "or.h"
  15. #include "circuituse.h"
  16. #include "config.h"
  17. #include "status.h"
  18. #include "nodelist.h"
  19. #include "relay.h"
  20. #include "router.h"
  21. #include "circuitlist.h"
  22. #include "main.h"
  23. #include "rephist.h"
  24. #include "hibernate.h"
  25. #include "rephist.h"
  26. #include "statefile.h"
  27. #include "dos.h"
  28. static void log_accounting(const time_t now, const or_options_t *options);
  29. #include "geoip.h"
  30. /** Return the total number of circuits. */
  31. STATIC int
  32. count_circuits(void)
  33. {
  34. return smartlist_len(circuit_get_global_list());
  35. }
  36. /** Take seconds <b>secs</b> and return a newly allocated human-readable
  37. * uptime string */
  38. STATIC char *
  39. secs_to_uptime(long secs)
  40. {
  41. long int days = secs / 86400;
  42. int hours = (int)((secs - (days * 86400)) / 3600);
  43. int minutes = (int)((secs - (days * 86400) - (hours * 3600)) / 60);
  44. char *uptime_string = NULL;
  45. switch (days) {
  46. case 0:
  47. tor_asprintf(&uptime_string, "%d:%02d hours", hours, minutes);
  48. break;
  49. case 1:
  50. tor_asprintf(&uptime_string, "%ld day %d:%02d hours",
  51. days, hours, minutes);
  52. break;
  53. default:
  54. tor_asprintf(&uptime_string, "%ld days %d:%02d hours",
  55. days, hours, minutes);
  56. break;
  57. }
  58. return uptime_string;
  59. }
  60. /** Take <b>bytes</b> and returns a newly allocated human-readable usage
  61. * string. */
  62. STATIC char *
  63. bytes_to_usage(uint64_t bytes)
  64. {
  65. char *bw_string = NULL;
  66. if (bytes < (1<<20)) { /* Less than a megabyte. */
  67. tor_asprintf(&bw_string, U64_FORMAT" kB", U64_PRINTF_ARG(bytes>>10));
  68. } else if (bytes < (1<<30)) { /* Megabytes. Let's add some precision. */
  69. double bw = U64_TO_DBL(bytes);
  70. tor_asprintf(&bw_string, "%.2f MB", bw/(1<<20));
  71. } else { /* Gigabytes. */
  72. double bw = U64_TO_DBL(bytes);
  73. tor_asprintf(&bw_string, "%.2f GB", bw/(1<<30));
  74. }
  75. return bw_string;
  76. }
  77. /** Log a "heartbeat" message describing Tor's status and history so that the
  78. * user can know that there is indeed a running Tor. Return 0 on success and
  79. * -1 on failure. */
  80. int
  81. log_heartbeat(time_t now)
  82. {
  83. char *bw_sent = NULL;
  84. char *bw_rcvd = NULL;
  85. char *uptime = NULL;
  86. const routerinfo_t *me;
  87. double r = tls_get_write_overhead_ratio();
  88. const int hibernating = we_are_hibernating();
  89. const or_options_t *options = get_options();
  90. if (public_server_mode(options) && !hibernating) {
  91. /* Let's check if we are in the current cached consensus. */
  92. if (!(me = router_get_my_routerinfo()))
  93. return -1; /* Something stinks, we won't even attempt this. */
  94. else
  95. if (!node_get_by_id(me->cache_info.identity_digest))
  96. log_fn(LOG_NOTICE, LD_HEARTBEAT, "Heartbeat: It seems like we are not "
  97. "in the cached consensus.");
  98. }
  99. uptime = secs_to_uptime(get_uptime());
  100. bw_rcvd = bytes_to_usage(get_bytes_read());
  101. bw_sent = bytes_to_usage(get_bytes_written());
  102. log_fn(LOG_NOTICE, LD_HEARTBEAT, "Heartbeat: Tor's uptime is %s, with %d "
  103. "circuits open. I've sent %s and received %s.%s",
  104. uptime, count_circuits(), bw_sent, bw_rcvd,
  105. hibernating?" We are currently hibernating.":"");
  106. if (server_mode(options) && accounting_is_enabled(options) && !hibernating) {
  107. log_accounting(now, options);
  108. }
  109. double fullness_pct = 100;
  110. if (stats_n_data_cells_packaged && !hibernating) {
  111. fullness_pct =
  112. 100*(U64_TO_DBL(stats_n_data_bytes_packaged) /
  113. U64_TO_DBL(stats_n_data_cells_packaged*RELAY_PAYLOAD_SIZE));
  114. }
  115. const double overhead_pct = ( r - 1.0 ) * 100.0;
  116. #define FULLNESS_PCT_THRESHOLD 80
  117. #define TLS_OVERHEAD_THRESHOLD 15
  118. const int severity = (fullness_pct < FULLNESS_PCT_THRESHOLD ||
  119. overhead_pct > TLS_OVERHEAD_THRESHOLD)
  120. ? LOG_NOTICE : LOG_INFO;
  121. log_fn(severity, LD_HEARTBEAT,
  122. "Average packaged cell fullness: %2.3f%%. "
  123. "TLS write overhead: %.f%%", fullness_pct, overhead_pct);
  124. if (public_server_mode(options)) {
  125. rep_hist_log_circuit_handshake_stats(now);
  126. rep_hist_log_link_protocol_counts();
  127. dos_log_heartbeat();
  128. }
  129. circuit_log_ancient_one_hop_circuits(1800);
  130. if (options->BridgeRelay) {
  131. char *msg = NULL;
  132. msg = format_client_stats_heartbeat(now);
  133. if (msg)
  134. log_notice(LD_HEARTBEAT, "%s", msg);
  135. tor_free(msg);
  136. }
  137. tor_free(uptime);
  138. tor_free(bw_sent);
  139. tor_free(bw_rcvd);
  140. return 0;
  141. }
  142. static void
  143. log_accounting(const time_t now, const or_options_t *options)
  144. {
  145. or_state_t *state = get_or_state();
  146. char *acc_rcvd = bytes_to_usage(state->AccountingBytesReadInInterval);
  147. char *acc_sent = bytes_to_usage(state->AccountingBytesWrittenInInterval);
  148. char *acc_used = bytes_to_usage(get_accounting_bytes());
  149. uint64_t acc_bytes = options->AccountingMax;
  150. char *acc_max;
  151. time_t interval_end = accounting_get_end_time();
  152. char end_buf[ISO_TIME_LEN + 1];
  153. char *remaining = NULL;
  154. acc_max = bytes_to_usage(acc_bytes);
  155. format_local_iso_time(end_buf, interval_end);
  156. remaining = secs_to_uptime(interval_end - now);
  157. const char *acc_rule;
  158. switch (options->AccountingRule) {
  159. case ACCT_MAX: acc_rule = "max";
  160. break;
  161. case ACCT_SUM: acc_rule = "sum";
  162. break;
  163. case ACCT_OUT: acc_rule = "out";
  164. break;
  165. case ACCT_IN: acc_rule = "in";
  166. break;
  167. default: acc_rule = "max";
  168. break;
  169. }
  170. log_notice(LD_HEARTBEAT, "Heartbeat: Accounting enabled. "
  171. "Sent: %s, Received: %s, Used: %s / %s, Rule: %s. The "
  172. "current accounting interval ends on %s, in %s.",
  173. acc_sent, acc_rcvd, acc_used, acc_max, acc_rule, end_buf, remaining);
  174. tor_free(acc_rcvd);
  175. tor_free(acc_sent);
  176. tor_free(acc_used);
  177. tor_free(acc_max);
  178. tor_free(remaining);
  179. }