status.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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 = (secs - (days * 86400)) / 3600;
  31. int minutes = (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. uint64_t in,out;
  70. char *bw_sent = NULL;
  71. char *bw_rcvd = NULL;
  72. char *uptime = NULL;
  73. const routerinfo_t *me;
  74. const node_t *myself;
  75. or_options_t *options = get_options();
  76. int is_server = server_mode(options);
  77. (void)now;
  78. if (is_server) {
  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 (!(myself = 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. get_traffic_stats(&in, &out);
  88. uptime = secs_to_uptime(get_uptime());
  89. bw_sent = bytes_to_usage(out);
  90. bw_rcvd = bytes_to_usage(in);
  91. log_fn(LOG_NOTICE, LD_HEARTBEAT, "Heartbeat: Tor's uptime is %s, with %d "
  92. "circuits open. I've pushed %s and received %s.",
  93. uptime, count_circuits(),bw_sent,bw_rcvd);
  94. tor_free(uptime);
  95. tor_free(bw_sent);
  96. tor_free(bw_rcvd);
  97. return 0;
  98. }