Back to index

tor  0.2.3.18-rc
status.c
Go to the documentation of this file.
00001 /* Copyright (c) 2010-2012, The Tor Project, Inc. */
00002 /* See LICENSE for licensing information */
00003 
00009 #include "or.h"
00010 #include "config.h"
00011 #include "status.h"
00012 #include "nodelist.h"
00013 #include "router.h"
00014 #include "circuitlist.h"
00015 #include "main.h"
00016 
00018 static int
00019 count_circuits(void)
00020 {
00021   circuit_t *circ;
00022   int nr=0;
00023 
00024   for (circ = _circuit_get_global_list(); circ; circ = circ->next)
00025     nr++;
00026 
00027   return nr;
00028 }
00029 
00032 static char *
00033 secs_to_uptime(long secs)
00034 {
00035   long int days = secs / 86400;
00036   int hours = (int)((secs - (days * 86400)) / 3600);
00037   int minutes = (int)((secs - (days * 86400) - (hours * 3600)) / 60);
00038   char *uptime_string = NULL;
00039 
00040   switch (days) {
00041   case 0:
00042     tor_asprintf(&uptime_string, "%d:%02d hours", hours, minutes);
00043     break;
00044   case 1:
00045     tor_asprintf(&uptime_string, "%ld day %d:%02d hours",
00046                  days, hours, minutes);
00047     break;
00048   default:
00049     tor_asprintf(&uptime_string, "%ld days %d:%02d hours",
00050                  days, hours, minutes);
00051     break;
00052   }
00053 
00054   return uptime_string;
00055 }
00056 
00059 static char *
00060 bytes_to_usage(uint64_t bytes)
00061 {
00062   char *bw_string = NULL;
00063 
00064   if (bytes < (1<<20)) { /* Less than a megabyte. */
00065     tor_asprintf(&bw_string, U64_FORMAT" kB", U64_PRINTF_ARG(bytes>>10));
00066   } else if (bytes < (1<<30)) { /* Megabytes. Let's add some precision. */
00067     double bw = U64_TO_DBL(bytes);
00068     tor_asprintf(&bw_string, "%.2f MB", bw/(1<<20));
00069   } else { /* Gigabytes. */
00070     double bw = U64_TO_DBL(bytes);
00071     tor_asprintf(&bw_string, "%.2f GB", bw/(1<<30));
00072   }
00073 
00074   return bw_string;
00075 }
00076 
00080 int
00081 log_heartbeat(time_t now)
00082 {
00083   char *bw_sent = NULL;
00084   char *bw_rcvd = NULL;
00085   char *uptime = NULL;
00086   const routerinfo_t *me;
00087 
00088   const or_options_t *options = get_options();
00089   (void)now;
00090 
00091   if (public_server_mode(options)) {
00092     /* Let's check if we are in the current cached consensus. */
00093     if (!(me = router_get_my_routerinfo()))
00094       return -1; /* Something stinks, we won't even attempt this. */
00095     else
00096       if (!node_get_by_id(me->cache_info.identity_digest))
00097         log_fn(LOG_NOTICE, LD_HEARTBEAT, "Heartbeat: It seems like we are not "
00098                "in the cached consensus.");
00099   }
00100 
00101   uptime = secs_to_uptime(get_uptime());
00102   bw_rcvd = bytes_to_usage(get_bytes_read());
00103   bw_sent = bytes_to_usage(get_bytes_written());
00104 
00105   log_fn(LOG_NOTICE, LD_HEARTBEAT, "Heartbeat: Tor's uptime is %s, with %d "
00106          "circuits open. I've sent %s and received %s.",
00107          uptime, count_circuits(),bw_sent,bw_rcvd);
00108 
00109   tor_free(uptime);
00110   tor_free(bw_sent);
00111   tor_free(bw_rcvd);
00112 
00113   return 0;
00114 }
00115