aboutsummaryrefslogtreecommitdiff
path: root/src/or/status.c
diff options
context:
space:
mode:
authorGeorge Kadianakis <desnacked@gmail.com>2010-12-01 03:32:42 +0200
committerNick Mathewson <nickm@torproject.org>2011-02-22 12:40:38 -0500
commitee95430d397d2382227cdeac8c678c7345df594d (patch)
treef946eeac09e632650c1b038ce6a203c4ab194a6d /src/or/status.c
parent098b6ba72d636cf00cfa091e745c25a649fba493 (diff)
downloadtor-ee95430d397d2382227cdeac8c678c7345df594d.tar.gz
tor-ee95430d397d2382227cdeac8c678c7345df594d.zip
Implement more heartbeat message stuff.
(This squashes multiple commits: * Adds uptime monitoring support. * Adds circuit counting code. * Trivially tweaks the documentation. * Trivial run_scheduled_events() code tweaking. * Adds a status.h to export functions. * Added bandwidth monitoring code. * Added consensus presense detection code. * Restricts the precision of the bandwidth output. * Various fixes. * Fixed style and spacing problems. * Tidied up src/or/Makefile.am * Couple of minor fixes on status.c functions. * 'Implemented' client heartbeat support )
Diffstat (limited to 'src/or/status.c')
-rw-r--r--src/or/status.c103
1 files changed, 94 insertions, 9 deletions
diff --git a/src/or/status.c b/src/or/status.c
index 0cd9a07226..c6a007b06c 100644
--- a/src/or/status.c
+++ b/src/or/status.c
@@ -7,21 +7,106 @@
**/
#include "or.h"
+#include "config.h"
+#include "status.h"
+#include "nodelist.h"
+#include "router.h"
+#include "circuitlist.h"
+#include "main.h"
+/** Returns the number of open circuits. */
+static int
+count_circuits(void)
+{
+ circuit_t *circ;
+ int nr=0;
-/****************************************************************************/
+ for (circ = _circuit_get_global_list(); circ; circ = circ->next)
+ nr++;
+ return nr;
+}
+
+/* Takes seconds <b>secs</b> and returns a human-readable uptime string */
+static char *
+secs_to_uptime(long secs)
+{
+ long int days = secs / 86400;
+ int hours = (secs - (days * 86400)) / 3600;
+ int minutes = (secs - (days * 86400) - (hours * 3600)) / 60;
+ char *uptime_string = NULL;
+
+ switch (days) {
+ case 0:
+ tor_asprintf(&uptime_string, "%d:%02d", hours, minutes);
+ break;
+ case 1:
+ tor_asprintf(&uptime_string, "%ld day %d:%02d", days, hours, minutes);
+ break;
+ default:
+ tor_asprintf(&uptime_string, "%ld days %d:%02d", days, hours, minutes);
+ break;
+ }
+
+ return uptime_string;
+}
+
+/* Takes <b>bytes</b> and returns a human-readable bandwidth string. */
+static char *
+bytes_to_bandwidth(uint64_t bytes)
+{
+ char *bw_string = NULL;
+ if (bytes < (1<<20)) /* Less than a megabyte. */
+ tor_asprintf(&bw_string, U64_FORMAT" kB", U64_PRINTF_ARG(bytes>>10));
+ else if (bytes < (1<<30)) { /* Megabytes. Let's add some precision. */
+ double bw = U64_TO_DBL(bytes);
+ tor_asprintf(&bw_string, "%.2f MB", bw/(1<<20));
+ } else { /* Gigabytes. */
+ double bw = U64_TO_DBL(bytes);
+ tor_asprintf(&bw_string, "%.2f GB", bw/(1<<30));
+ }
-#define BEAT(x) log_fn(LOG_NOTICE, LD_HEARTBEAT, (x) )
+ return bw_string;
+}
+
+/* This function provides the heartbeat log message */
+int
+log_heartbeat(time_t now)
+{
+ uint64_t in,out;
+ char *bw_sent = NULL;
+ char *bw_rcvd = NULL;
+ char *uptime = NULL;
+ const routerinfo_t *me;
+ const node_t *myself;
+
+ or_options_t *options = get_options();
+ int is_server = server_mode(options);
-void
-log_heartbeat(time_t now) {
- or_options_t *opt = get_options();
+ if (is_server) {
+ /* Let's check if we are in the current cached consensus. */
+ if (!(me = router_get_my_routerinfo()))
+ return -1; /* Something stinks, we won't even attempt this. */
+ else
+ if (!(myself = node_get_by_id(me->cache_info.identity_digest)))
+ log_fn(LOG_NOTICE, LD_HEARTBEAT, "Heartbeat: It seems like we are not "
+ "in the cached consensus.");
+ }
- (void) now;
- log_fn(LOG_NOTICE, LD_HEARTBEAT, "This is the Tor heartbeat message.");
- if (!server_mode(opt))
- BEAT("you are a client, hahaha");
+ get_traffic_stats(&in, &out);
+ uptime = secs_to_uptime(get_uptime());
+ bw_sent = bytes_to_bandwidth(out);
+ bw_rcvd = bytes_to_bandwidth(in);
+ log_fn(LOG_NOTICE, LD_HEARTBEAT, "Heartbeat: Tor's uptime is %s, with %d "
+ "circuits open, I've pushed %s and received %s.",
+ uptime, count_circuits(),bw_sent,bw_rcvd);
+
+ tor_free(uptime);
+ tor_free(bw_sent);
+ tor_free(bw_rcvd);
+
+ return 0;
}
+