status.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /* Copyright (c) 2010, 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. /** Returns the number of open 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. /* Takes seconds <b>secs</b> and returns a human-readable uptime string */
  25. static char *
  26. secs_to_uptime(long secs)
  27. {
  28. long int days = secs / 86400;
  29. int hours = (secs - (days * 86400)) / 3600;
  30. int minutes = (secs - (days * 86400) - (hours * 3600)) / 60;
  31. char *uptime_string = NULL;
  32. switch (days) {
  33. case 0:
  34. tor_asprintf(&uptime_string, "%d:%02d", hours, minutes);
  35. break;
  36. case 1:
  37. tor_asprintf(&uptime_string, "%ld day %d:%02d", days, hours, minutes);
  38. break;
  39. default:
  40. tor_asprintf(&uptime_string, "%ld days %d:%02d", days, hours, minutes);
  41. break;
  42. }
  43. return uptime_string;
  44. }
  45. /* Takes <b>bytes</b> and returns a human-readable bandwidth string. */
  46. static char *
  47. bytes_to_bandwidth(uint64_t bytes)
  48. {
  49. char *bw_string = NULL;
  50. if (bytes < (1<<20)) /* Less than a megabyte. */
  51. tor_asprintf(&bw_string, U64_FORMAT" kB", U64_PRINTF_ARG(bytes>>10));
  52. else if (bytes < (1<<30)) { /* Megabytes. Let's add some precision. */
  53. double bw = U64_TO_DBL(bytes);
  54. tor_asprintf(&bw_string, "%.2f MB", bw/(1<<20));
  55. } else { /* Gigabytes. */
  56. double bw = U64_TO_DBL(bytes);
  57. tor_asprintf(&bw_string, "%.2f GB", bw/(1<<30));
  58. }
  59. return bw_string;
  60. }
  61. /* This function provides the heartbeat log message */
  62. int
  63. log_heartbeat(time_t now)
  64. {
  65. uint64_t in,out;
  66. char *bw_sent = NULL;
  67. char *bw_rcvd = NULL;
  68. char *uptime = NULL;
  69. const routerinfo_t *me;
  70. const node_t *myself;
  71. or_options_t *options = get_options();
  72. int is_server = server_mode(options);
  73. if (is_server) {
  74. /* Let's check if we are in the current cached consensus. */
  75. if (!(me = router_get_my_routerinfo()))
  76. return -1; /* Something stinks, we won't even attempt this. */
  77. else
  78. if (!(myself = node_get_by_id(me->cache_info.identity_digest)))
  79. log_fn(LOG_NOTICE, LD_HEARTBEAT, "Heartbeat: It seems like we are not "
  80. "in the cached consensus.");
  81. }
  82. get_traffic_stats(&in, &out);
  83. uptime = secs_to_uptime(get_uptime());
  84. bw_sent = bytes_to_bandwidth(out);
  85. bw_rcvd = bytes_to_bandwidth(in);
  86. log_fn(LOG_NOTICE, LD_HEARTBEAT, "Heartbeat: Tor's uptime is %s, with %d "
  87. "circuits open, I've pushed %s and received %s.",
  88. uptime, count_circuits(),bw_sent,bw_rcvd);
  89. tor_free(uptime);
  90. tor_free(bw_sent);
  91. tor_free(bw_rcvd);
  92. return 0;
  93. }