status.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. /* Copyright (c) 2010-2017, 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 "statefile.h"
  26. #include "hs_stats.h"
  27. #include "hs_service.h"
  28. #include "dos.h"
  29. static void log_accounting(const time_t now, const or_options_t *options);
  30. #include "geoip.h"
  31. /** Return the total number of circuits. */
  32. STATIC int
  33. count_circuits(void)
  34. {
  35. return smartlist_len(circuit_get_global_list());
  36. }
  37. /** Take seconds <b>secs</b> and return a newly allocated human-readable
  38. * uptime string. */
  39. STATIC char *
  40. secs_to_uptime(long secs)
  41. {
  42. long int days = secs / 86400;
  43. int hours = (int)((secs - (days * 86400)) / 3600);
  44. int minutes = (int)((secs - (days * 86400) - (hours * 3600)) / 60);
  45. char *uptime_string = NULL;
  46. switch (days) {
  47. case 0:
  48. tor_asprintf(&uptime_string, "%d:%02d hours", hours, minutes);
  49. break;
  50. case 1:
  51. tor_asprintf(&uptime_string, "%ld day %d:%02d hours",
  52. days, hours, minutes);
  53. break;
  54. default:
  55. tor_asprintf(&uptime_string, "%ld days %d:%02d hours",
  56. days, hours, minutes);
  57. break;
  58. }
  59. return uptime_string;
  60. }
  61. /** Take <b>bytes</b> and returns a newly allocated human-readable usage
  62. * string. */
  63. STATIC char *
  64. bytes_to_usage(uint64_t bytes)
  65. {
  66. char *bw_string = NULL;
  67. if (bytes < (1<<20)) { /* Less than a megabyte. */
  68. tor_asprintf(&bw_string, U64_FORMAT" kB", U64_PRINTF_ARG(bytes>>10));
  69. } else if (bytes < (1<<30)) { /* Megabytes. Let's add some precision. */
  70. double bw = U64_TO_DBL(bytes);
  71. tor_asprintf(&bw_string, "%.2f MB", bw/(1<<20));
  72. } else { /* Gigabytes. */
  73. double bw = U64_TO_DBL(bytes);
  74. tor_asprintf(&bw_string, "%.2f GB", bw/(1<<30));
  75. }
  76. return bw_string;
  77. }
  78. /** Log some usage info about our hidden service */
  79. static void
  80. log_onion_service_stats(void)
  81. {
  82. unsigned int num_services = hs_service_get_num_services();
  83. /* If there are no active hidden services, no need to print logs */
  84. if (num_services == 0) {
  85. return;
  86. }
  87. log_notice(LD_HEARTBEAT,
  88. "Our hidden service%s received %u v2 and %u v3 INTRODUCE2 cells "
  89. "and attempted to launch %d rendezvous circuits.",
  90. num_services == 1 ? "" : "s",
  91. hs_stats_get_n_introduce2_v2_cells(),
  92. hs_stats_get_n_introduce2_v3_cells(),
  93. hs_stats_get_n_rendezvous_launches());
  94. }
  95. /** Log a "heartbeat" message describing Tor's status and history so that the
  96. * user can know that there is indeed a running Tor. Return 0 on success and
  97. * -1 on failure. */
  98. int
  99. log_heartbeat(time_t now)
  100. {
  101. char *bw_sent = NULL;
  102. char *bw_rcvd = NULL;
  103. char *uptime = NULL;
  104. const routerinfo_t *me;
  105. double r = tls_get_write_overhead_ratio();
  106. const int hibernating = we_are_hibernating();
  107. const or_options_t *options = get_options();
  108. if (public_server_mode(options) && !hibernating) {
  109. /* Let's check if we are in the current cached consensus. */
  110. if (!(me = router_get_my_routerinfo()))
  111. return -1; /* Something stinks, we won't even attempt this. */
  112. else
  113. if (!node_get_by_id(me->cache_info.identity_digest))
  114. log_fn(LOG_NOTICE, LD_HEARTBEAT, "Heartbeat: It seems like we are not "
  115. "in the cached consensus.");
  116. }
  117. uptime = secs_to_uptime(get_uptime());
  118. bw_rcvd = bytes_to_usage(get_bytes_read());
  119. bw_sent = bytes_to_usage(get_bytes_written());
  120. log_fn(LOG_NOTICE, LD_HEARTBEAT, "Heartbeat: Tor's uptime is %s, with %d "
  121. "circuits open. I've sent %s and received %s.%s",
  122. uptime, count_circuits(), bw_sent, bw_rcvd,
  123. hibernating?" We are currently hibernating.":"");
  124. if (server_mode(options) && accounting_is_enabled(options) && !hibernating) {
  125. log_accounting(now, options);
  126. }
  127. double fullness_pct = 100;
  128. if (stats_n_data_cells_packaged && !hibernating) {
  129. fullness_pct =
  130. 100*(U64_TO_DBL(stats_n_data_bytes_packaged) /
  131. U64_TO_DBL(stats_n_data_cells_packaged*RELAY_PAYLOAD_SIZE));
  132. }
  133. const double overhead_pct = ( r - 1.0 ) * 100.0;
  134. #define FULLNESS_PCT_THRESHOLD 80
  135. #define TLS_OVERHEAD_THRESHOLD 15
  136. const int severity = (fullness_pct < FULLNESS_PCT_THRESHOLD ||
  137. overhead_pct > TLS_OVERHEAD_THRESHOLD)
  138. ? LOG_NOTICE : LOG_INFO;
  139. log_fn(severity, LD_HEARTBEAT,
  140. "Average packaged cell fullness: %2.3f%%. "
  141. "TLS write overhead: %.f%%", fullness_pct, overhead_pct);
  142. if (public_server_mode(options)) {
  143. rep_hist_log_circuit_handshake_stats(now);
  144. rep_hist_log_link_protocol_counts();
  145. dos_log_heartbeat();
  146. }
  147. circuit_log_ancient_one_hop_circuits(1800);
  148. if (options->BridgeRelay) {
  149. char *msg = NULL;
  150. msg = format_client_stats_heartbeat(now);
  151. if (msg)
  152. log_notice(LD_HEARTBEAT, "%s", msg);
  153. tor_free(msg);
  154. }
  155. if (options->MainloopStats) {
  156. const uint64_t main_loop_success_count = get_main_loop_success_count();
  157. const uint64_t main_loop_error_count = get_main_loop_error_count();
  158. const uint64_t main_loop_idle_count = get_main_loop_idle_count();
  159. log_fn(LOG_NOTICE, LD_HEARTBEAT, "Main event loop statistics: "
  160. U64_FORMAT " successful returns, "
  161. U64_FORMAT " erroneous returns, and "
  162. U64_FORMAT " idle returns.",
  163. U64_PRINTF_ARG(main_loop_success_count),
  164. U64_PRINTF_ARG(main_loop_error_count),
  165. U64_PRINTF_ARG(main_loop_idle_count));
  166. }
  167. /** Now, if we are an HS service, log some stats about our usage */
  168. log_onion_service_stats();
  169. tor_free(uptime);
  170. tor_free(bw_sent);
  171. tor_free(bw_rcvd);
  172. return 0;
  173. }
  174. static void
  175. log_accounting(const time_t now, const or_options_t *options)
  176. {
  177. or_state_t *state = get_or_state();
  178. char *acc_rcvd = bytes_to_usage(state->AccountingBytesReadInInterval);
  179. char *acc_sent = bytes_to_usage(state->AccountingBytesWrittenInInterval);
  180. char *acc_used = bytes_to_usage(get_accounting_bytes());
  181. uint64_t acc_bytes = options->AccountingMax;
  182. char *acc_max;
  183. time_t interval_end = accounting_get_end_time();
  184. char end_buf[ISO_TIME_LEN + 1];
  185. char *remaining = NULL;
  186. acc_max = bytes_to_usage(acc_bytes);
  187. format_local_iso_time(end_buf, interval_end);
  188. remaining = secs_to_uptime(interval_end - now);
  189. const char *acc_rule;
  190. switch (options->AccountingRule) {
  191. case ACCT_MAX: acc_rule = "max";
  192. break;
  193. case ACCT_SUM: acc_rule = "sum";
  194. break;
  195. case ACCT_OUT: acc_rule = "out";
  196. break;
  197. case ACCT_IN: acc_rule = "in";
  198. break;
  199. default: acc_rule = "max";
  200. break;
  201. }
  202. log_notice(LD_HEARTBEAT, "Heartbeat: Accounting enabled. "
  203. "Sent: %s, Received: %s, Used: %s / %s, Rule: %s. The "
  204. "current accounting interval ends on %s, in %s.",
  205. acc_sent, acc_rcvd, acc_used, acc_max, acc_rule, end_buf, remaining);
  206. tor_free(acc_rcvd);
  207. tor_free(acc_sent);
  208. tor_free(acc_used);
  209. tor_free(acc_max);
  210. tor_free(remaining);
  211. }