diff --git a/tor-daemon/tor-daemon.py b/tor-daemon/tor-daemon.py
--- a/tor-daemon/tor-daemon.py
+++ b/tor-daemon/tor-daemon.py
#
@@ -84,6 +85,134 @@ def get_guard_counts(s):
#
+def get_accounting_info(s):
#
+ ["accounting/bytes", "accounting_bytes", "int", "field"],
#
+ ["accounting/bytes-left", "accounting_bytes_remaining", "int", "field"],
#
+ "name" : "accounting_enabled",
#
+ res = send_and_respond(s, "GETINFO accounting/enabled")
#
+ if len(res) < 1 or not res[0].startswith("250-"):
#
+ v = int(res[0].split("=")[1])
#
+ vals.append(accounting)
#
+ # Accounting is active
#
+ accounting['value'] = "1"
#
+ vals.append(accounting)
#
+ # Current relay state
#
+ res = send_and_respond(s, "GETINFO accounting/hibernating")
#
+ if len(res) > 1 and res[0].startswith("250-"):
#
+ val = res[0].split("=")[1]
#
+ "name" : "accounting_hibernating_state",
#
+ for f in byte_fields:
#
+ res = send_and_respond(s, "GETINFO " + f[0])
#
+ if len(res) > 1 and res[0].startswith("250-"):
#
+ val = res[0].split("=")[1]
#
+ # There's a read and a write value
#
+ cols = val.split(" ")
#
+ "name" : f[1] + "_read",
#
+ "value" : int(cols[0]),
#
+ "name" : f[1] + "_write",
#
+ "value" : int(cols[1]),
#
+ # Calculate durations
#
+ # 2022-05-04 12:31:00
#
+ # We want to convert these into durations
#
+ # How much of the accounting interval is left? how much has elapsed etc
#
+ # strptime pattern to use when parsing tor's date responses
#
+ timepattern = "%Y-%m-%d %H:%M:%S"
#
+ # For some reason, tor uses rfc3339 for the current time, but not for
#
+ nowtimepattern = "%Y-%m-%dT%H:%M:%S"
#
+ # Ask Tor what time it thinks it currently is
#
+ res = send_and_respond(s, "GETINFO current-time/utc")
#
+ if len(res) > 1 and res[0].startswith("250-"):
#
+ val = res[0].split("=")[1]
#
+ now = datetime.datetime.strptime(val, nowtimepattern)
#
+ # Now ask when the accounting period started
#
+ res = send_and_respond(s, "GETINFO accounting/interval-start")
#
+ if len(res) > 1 and res[0].startswith("250-"):
#
+ val = res[0].split("=")[1]
#
+ acc_start = datetime.datetime.strptime(val, timepattern)
#
+ delta = now - acc_start
#
+ "name" : "accounting_period_seconds_elapsed",
#
+ "value" : int(delta.total_seconds()),
#
+ "fieldtype" : "field"
#
+ # When does the accounting period end?
#
+ res = send_and_respond(s, "GETINFO accounting/interval-end")
#
+ if len(res) > 1 and res[0].startswith("250-"):
#
+ val = res[0].split("=")[1]
#
+ acc_stop = datetime.datetime.strptime(val, timepattern)
#
+ delta = acc_stop - now
#
+ "name" : "accounting_period_seconds_remaining",
#
+ "value" : int(delta.total_seconds()),
#
+ "fieldtype" : "field"
#
+ # Calculate length of the accounting period
#
+ delta = acc_stop - acc_start
#
+ "name" : "accounting_period_length",
#
+ "value" : int(delta.total_seconds()),
#
+ "fieldtype" : "field"
#
def build_lp(measurement_name, state):
#
''' Build a Line Protocol response
#
@@ -102,7 +231,7 @@ def build_lp(measurement_name, state):
#
if entry['type'] == "int":
#
- v = entry['name'] + "=" + entry["value"] + "i"
#
+ v = entry['name'] + "=" + str(entry["value"]) + "i"
#
elif entry['type'] == "float":
#
v = entry['name'] + "=" + entry["value"]
#
@@ -191,5 +320,9 @@ for stat in stats:
#
state["counters"].append(["guards", get_guard_counts(s)])
#
+for v in get_accounting_info(s):
#
+ state["stats"].append(v)
#
print(build_lp(MEASUREMENT, state))