status.c 7.5 KB

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