[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

New GETINFO option, "bytes"



Hi. I added a new option for GETINFO, that will return the total
number of bytes that's gone through Tor since process startup. Just
exporting the internal stats_n_bytes_read/written.

This is very useful for retrieving statistics like bandwidth over
time, for use with tools like arm, vidalia, munin, and other
monitoring applications. The current method that use events is
difficult to use, since you have to listen all the time. With the new
method you can for example poll every minute to see how many bytes was
transferred in total since last you checked.

I wrote a plugin for munin that use this new feature, and it works great.

The patch is trivial, and you probably want to change the name of the
command if you want to use it. There might also be reasons that you
don't want to export and print uint64_t variables. I didn't take time
to check any tor internals guidelines.

// pipe
From d557fc9bc2ec749d4743e3e918289e55c4b9e459 Mon Sep 17 00:00:00 2001
From: Anders Andersson <pipatron@xxxxxxxxx>
Date: Tue, 23 Mar 2010 02:07:37 +0100
Subject: [PATCH] Added a new GETINFO item 'bytes'

---
 src/or/control.c |    7 +++++++
 src/or/main.c    |    4 ++--
 src/or/or.h      |    2 ++
 3 files changed, 11 insertions(+), 2 deletions(-)

diff --git a/src/or/control.c b/src/or/control.c
index 771beae..d591065 100644
--- a/src/or/control.c
+++ b/src/or/control.c
@@ -1328,6 +1328,11 @@ getinfo_helper_misc(control_connection_t *conn, const char *question,
     *answer = tor_malloc(HEX_DIGEST_LEN+1);
     base16_encode(*answer, HEX_DIGEST_LEN+1, me->cache_info.identity_digest,
                   DIGEST_LEN);
+  } else if (!strcmp(question, "bytes")) {
+    *answer = tor_malloc(42);
+    tor_snprintf(*answer, 42, U64_FORMAT" "U64_FORMAT,
+                 U64_PRINTF_ARG(stats_n_bytes_read),
+                 U64_PRINTF_ARG(stats_n_bytes_written));
   }
   return 0;
 }
@@ -1810,6 +1815,8 @@ static const getinfo_item_t getinfo_items[] = {
        "Time when the accounting period ends."),
   ITEM("accounting/interval-wake", accounting,
        "Time to wake up in this accounting period."),
+  ITEM("bytes", misc,
+       "Number of bytes read/written so far since Tor started."),
   ITEM("helper-nodes", entry_guards, NULL), /* deprecated */
   ITEM("entry-guards", entry_guards,
        "Which nodes are we using as entry guards?"),
diff --git a/src/or/main.c b/src/or/main.c
index 74075b6..0e2b755 100644
--- a/src/or/main.c
+++ b/src/or/main.c
@@ -56,9 +56,9 @@ static int stats_prev_global_read_bucket;
 static int stats_prev_global_write_bucket;
 /* XXX we might want to keep stats about global_relayed_*_bucket too. Or not.*/
 /** How many bytes have we read since we started the process? */
-static uint64_t stats_n_bytes_read = 0;
+uint64_t stats_n_bytes_read = 0;
 /** How many bytes have we written since we started the process? */
-static uint64_t stats_n_bytes_written = 0;
+uint64_t stats_n_bytes_written = 0;
 /** What time did this process start up? */
 time_t time_of_process_start = 0;
 /** How many seconds have we been running? */
diff --git a/src/or/or.h b/src/or/or.h
index 737c197..75c43f9 100644
--- a/src/or/or.h
+++ b/src/or/or.h
@@ -4192,6 +4192,8 @@ void accounting_set_bandwidth_usage_from_state(or_state_t *state);
 /********************************* main.c ***************************/
 
 extern int has_completed_circuit;
+extern uint64_t stats_n_bytes_read;
+extern uint64_t stats_n_bytes_written;
 
 int connection_add(connection_t *conn);
 int connection_remove(connection_t *conn);
-- 
1.5.6.5

From 94b4451ff20ac8951ba7fa43edba1d4faa053505 Mon Sep 17 00:00:00 2001
From: Anders Andersson <pipatron@xxxxxxxxx>
Date: Tue, 23 Mar 2010 22:21:04 +0100
Subject: [PATCH] Documented the "bytes" option for GETINFO

---
 doc/spec/control-spec.txt |    5 +++++
 1 files changed, 5 insertions(+), 0 deletions(-)

diff --git a/doc/spec/control-spec.txt b/doc/spec/control-spec.txt
index b60baba..17c59f6 100644
--- a/doc/spec/control-spec.txt
+++ b/doc/spec/control-spec.txt
@@ -498,6 +498,11 @@
        with a $.  This is an implementation error.  It would be nice to add
        the $ back in if we can do so without breaking compatibility.]
 
+    "bytes"
+      Total number of bytes passed through the Tor node since startup, in the
+      form:
+        read-bytes SP write-bytes CRLF
+
     "accounting/enabled"
     "accounting/hibernating"
     "accounting/bytes"
-- 
1.5.6.5