status.c 4.8 KB

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