status.c 7.6 KB

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