status.c 5.7 KB

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