status.c 6.0 KB

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