status.c 4.7 KB

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