status.c 7.4 KB

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