Add link protocol version counts to the heartbeat message
[tor.git] / src / or / status.c
blob8f7be0aa3c06c70e4e3938b7cd1d2c738cf8b447
1 /* Copyright (c) 2010-2015, The Tor Project, Inc. */
2 /* See LICENSE for licensing information */
4 /**
5 * \file status.c
6 * \brief Keep status information and log the heartbeat messages.
7 **/
9 #define STATUS_PRIVATE
11 #include "or.h"
12 #include "circuituse.h"
13 #include "config.h"
14 #include "status.h"
15 #include "nodelist.h"
16 #include "relay.h"
17 #include "router.h"
18 #include "circuitlist.h"
19 #include "main.h"
20 #include "rephist.h"
21 #include "hibernate.h"
22 #include "rephist.h"
23 #include "statefile.h"
25 static void log_accounting(const time_t now, const or_options_t *options);
26 #include "geoip.h"
28 /** Return the total number of circuits. */
29 STATIC int
30 count_circuits(void)
32 return smartlist_len(circuit_get_global_list());
35 /** Take seconds <b>secs</b> and return a newly allocated human-readable
36 * uptime string */
37 STATIC char *
38 secs_to_uptime(long secs)
40 long int days = secs / 86400;
41 int hours = (int)((secs - (days * 86400)) / 3600);
42 int minutes = (int)((secs - (days * 86400) - (hours * 3600)) / 60);
43 char *uptime_string = NULL;
45 switch (days) {
46 case 0:
47 tor_asprintf(&uptime_string, "%d:%02d hours", hours, minutes);
48 break;
49 case 1:
50 tor_asprintf(&uptime_string, "%ld day %d:%02d hours",
51 days, hours, minutes);
52 break;
53 default:
54 tor_asprintf(&uptime_string, "%ld days %d:%02d hours",
55 days, hours, minutes);
56 break;
59 return uptime_string;
62 /** Take <b>bytes</b> and returns a newly allocated human-readable usage
63 * string. */
64 STATIC char *
65 bytes_to_usage(uint64_t bytes)
67 char *bw_string = NULL;
69 if (bytes < (1<<20)) { /* Less than a megabyte. */
70 tor_asprintf(&bw_string, U64_FORMAT" kB", U64_PRINTF_ARG(bytes>>10));
71 } else if (bytes < (1<<30)) { /* Megabytes. Let's add some precision. */
72 double bw = U64_TO_DBL(bytes);
73 tor_asprintf(&bw_string, "%.2f MB", bw/(1<<20));
74 } else { /* Gigabytes. */
75 double bw = U64_TO_DBL(bytes);
76 tor_asprintf(&bw_string, "%.2f GB", bw/(1<<30));
79 return bw_string;
82 /** Log a "heartbeat" message describing Tor's status and history so that the
83 * user can know that there is indeed a running Tor. Return 0 on success and
84 * -1 on failure. */
85 int
86 log_heartbeat(time_t now)
88 char *bw_sent = NULL;
89 char *bw_rcvd = NULL;
90 char *uptime = NULL;
91 const routerinfo_t *me;
92 double r = tls_get_write_overhead_ratio();
93 const int hibernating = we_are_hibernating();
95 const or_options_t *options = get_options();
97 if (public_server_mode(options) && !hibernating) {
98 /* Let's check if we are in the current cached consensus. */
99 if (!(me = router_get_my_routerinfo()))
100 return -1; /* Something stinks, we won't even attempt this. */
101 else
102 if (!node_get_by_id(me->cache_info.identity_digest))
103 log_fn(LOG_NOTICE, LD_HEARTBEAT, "Heartbeat: It seems like we are not "
104 "in the cached consensus.");
107 uptime = secs_to_uptime(get_uptime());
108 bw_rcvd = bytes_to_usage(get_bytes_read());
109 bw_sent = bytes_to_usage(get_bytes_written());
111 log_fn(LOG_NOTICE, LD_HEARTBEAT, "Heartbeat: Tor's uptime is %s, with %d "
112 "circuits open. I've sent %s and received %s.%s",
113 uptime, count_circuits(), bw_sent, bw_rcvd,
114 hibernating?" We are currently hibernating.":"");
116 if (server_mode(options) && accounting_is_enabled(options) && !hibernating) {
117 log_accounting(now, options);
120 double fullness_pct = 100;
121 if (stats_n_data_cells_packaged && !hibernating) {
122 fullness_pct =
123 100*(U64_TO_DBL(stats_n_data_bytes_packaged) /
124 U64_TO_DBL(stats_n_data_cells_packaged*RELAY_PAYLOAD_SIZE));
126 const double overhead_pct = ( r - 1.0 ) * 100.0;
128 #define FULLNESS_PCT_THRESHOLD 80
129 #define TLS_OVERHEAD_THRESHOLD 15
131 const int severity = (fullness_pct < FULLNESS_PCT_THRESHOLD ||
132 overhead_pct > TLS_OVERHEAD_THRESHOLD)
133 ? LOG_NOTICE : LOG_INFO;
135 log_fn(severity, LD_HEARTBEAT,
136 "Average packaged cell fullness: %2.3f%%. "
137 "TLS write overhead: %.f%%", fullness_pct, overhead_pct);
139 if (public_server_mode(options)) {
140 rep_hist_log_circuit_handshake_stats(now);
141 rep_hist_log_link_protocol_counts();
144 circuit_log_ancient_one_hop_circuits(1800);
146 if (options->BridgeRelay) {
147 char *msg = NULL;
148 msg = format_client_stats_heartbeat(now);
149 if (msg)
150 log_notice(LD_HEARTBEAT, "%s", msg);
151 tor_free(msg);
154 tor_free(uptime);
155 tor_free(bw_sent);
156 tor_free(bw_rcvd);
158 return 0;
161 static void
162 log_accounting(const time_t now, const or_options_t *options)
164 or_state_t *state = get_or_state();
165 char *acc_rcvd = bytes_to_usage(state->AccountingBytesReadInInterval);
166 char *acc_sent = bytes_to_usage(state->AccountingBytesWrittenInInterval);
167 uint64_t acc_bytes = options->AccountingMax;
168 char *acc_max;
169 time_t interval_end = accounting_get_end_time();
170 char end_buf[ISO_TIME_LEN + 1];
171 char *remaining = NULL;
172 if (options->AccountingRule == ACCT_SUM)
173 acc_bytes *= 2;
174 acc_max = bytes_to_usage(acc_bytes);
175 format_local_iso_time(end_buf, interval_end);
176 remaining = secs_to_uptime(interval_end - now);
178 log_notice(LD_HEARTBEAT, "Heartbeat: Accounting enabled. "
179 "Sent: %s / %s, Received: %s / %s. The "
180 "current accounting interval ends on %s, in %s.",
181 acc_sent, acc_max, acc_rcvd, acc_max, end_buf, remaining);
183 tor_free(acc_rcvd);
184 tor_free(acc_sent);
185 tor_free(acc_max);
186 tor_free(remaining);