status.c 2.9 KB

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