status.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. /** Return the total number of circuits. */
  16. static int
  17. count_circuits(void)
  18. {
  19. circuit_t *circ;
  20. int nr=0;
  21. for (circ = circuit_get_global_list_(); circ; circ = circ->next)
  22. nr++;
  23. return nr;
  24. }
  25. /** Take seconds <b>secs</b> and return a newly allocated human-readable
  26. * uptime string */
  27. static char *
  28. secs_to_uptime(long secs)
  29. {
  30. long int days = secs / 86400;
  31. int hours = (int)((secs - (days * 86400)) / 3600);
  32. int minutes = (int)((secs - (days * 86400) - (hours * 3600)) / 60);
  33. char *uptime_string = NULL;
  34. switch (days) {
  35. case 0:
  36. tor_asprintf(&uptime_string, "%d:%02d hours", hours, minutes);
  37. break;
  38. case 1:
  39. tor_asprintf(&uptime_string, "%ld day %d:%02d hours",
  40. days, hours, minutes);
  41. break;
  42. default:
  43. tor_asprintf(&uptime_string, "%ld days %d:%02d hours",
  44. days, hours, minutes);
  45. break;
  46. }
  47. return uptime_string;
  48. }
  49. /** Take <b>bytes</b> and returns a newly allocated human-readable usage
  50. * string. */
  51. static char *
  52. bytes_to_usage(uint64_t bytes)
  53. {
  54. char *bw_string = NULL;
  55. if (bytes < (1<<20)) { /* Less than a megabyte. */
  56. tor_asprintf(&bw_string, U64_FORMAT" kB", U64_PRINTF_ARG(bytes>>10));
  57. } else if (bytes < (1<<30)) { /* Megabytes. Let's add some precision. */
  58. double bw = U64_TO_DBL(bytes);
  59. tor_asprintf(&bw_string, "%.2f MB", bw/(1<<20));
  60. } else { /* Gigabytes. */
  61. double bw = U64_TO_DBL(bytes);
  62. tor_asprintf(&bw_string, "%.2f GB", bw/(1<<30));
  63. }
  64. return bw_string;
  65. }
  66. /** Log a "heartbeat" message describing Tor's status and history so that the
  67. * user can know that there is indeed a running Tor. Return 0 on success and
  68. * -1 on failure. */
  69. int
  70. log_heartbeat(time_t now)
  71. {
  72. char *bw_sent = NULL;
  73. char *bw_rcvd = NULL;
  74. char *uptime = NULL;
  75. const routerinfo_t *me;
  76. const or_options_t *options = get_options();
  77. (void)now;
  78. if (public_server_mode(options)) {
  79. /* Let's check if we are in the current cached consensus. */
  80. if (!(me = router_get_my_routerinfo()))
  81. return -1; /* Something stinks, we won't even attempt this. */
  82. else
  83. if (!node_get_by_id(me->cache_info.identity_digest))
  84. log_fn(LOG_NOTICE, LD_HEARTBEAT, "Heartbeat: It seems like we are not "
  85. "in the cached consensus.");
  86. }
  87. uptime = secs_to_uptime(get_uptime());
  88. bw_rcvd = bytes_to_usage(get_bytes_read());
  89. bw_sent = bytes_to_usage(get_bytes_written());
  90. log_fn(LOG_NOTICE, LD_HEARTBEAT, "Heartbeat: Tor's uptime is %s, with %d "
  91. "circuits open. I've sent %s and received %s.",
  92. uptime, count_circuits(),bw_sent,bw_rcvd);
  93. if (stats_n_data_cells_packaged)
  94. log_notice(LD_HEARTBEAT, "Average packaged cell fullness: %2.3f%%",
  95. 100*(U64_TO_DBL(stats_n_data_bytes_packaged) /
  96. U64_TO_DBL(stats_n_data_cells_packaged*RELAY_PAYLOAD_SIZE)) );
  97. tor_free(uptime);
  98. tor_free(bw_sent);
  99. tor_free(bw_rcvd);
  100. return 0;
  101. }