2018-06-20 14:13:28 +02:00
|
|
|
/* Copyright (c) 2010-2018, The Tor Project, Inc. */
|
2010-12-01 02:24:03 +01:00
|
|
|
/* See LICENSE for licensing information */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* \file status.c
|
2016-10-15 02:08:51 +02:00
|
|
|
* \brief Collect status information and log heartbeat messages.
|
|
|
|
*
|
|
|
|
* This module is responsible for implementing the heartbeat log messages,
|
|
|
|
* which periodically inform users and operators about basic facts to
|
|
|
|
* do with their Tor instance. The log_heartbeat() function, invoked from
|
|
|
|
* main.c, is the principle entry point. It collects data from elsewhere
|
|
|
|
* in Tor, and logs it in a human-readable format.
|
2010-12-01 02:24:03 +01:00
|
|
|
**/
|
|
|
|
|
2014-04-15 14:20:34 +02:00
|
|
|
#define STATUS_PRIVATE
|
|
|
|
|
2018-06-20 15:35:05 +02:00
|
|
|
#include "or/or.h"
|
|
|
|
#include "or/circuituse.h"
|
|
|
|
#include "or/config.h"
|
|
|
|
#include "or/status.h"
|
|
|
|
#include "or/nodelist.h"
|
|
|
|
#include "or/relay.h"
|
|
|
|
#include "or/router.h"
|
|
|
|
#include "or/circuitlist.h"
|
|
|
|
#include "or/main.h"
|
|
|
|
#include "or/rephist.h"
|
|
|
|
#include "or/hibernate.h"
|
|
|
|
#include "or/statefile.h"
|
|
|
|
#include "or/hs_stats.h"
|
|
|
|
#include "or/hs_service.h"
|
|
|
|
#include "or/dos.h"
|
2010-12-01 02:24:03 +01:00
|
|
|
|
2018-06-20 15:35:05 +02:00
|
|
|
#include "or/routerinfo_st.h"
|
2018-06-15 20:49:15 +02:00
|
|
|
|
2013-08-21 19:41:15 +02:00
|
|
|
static void log_accounting(const time_t now, const or_options_t *options);
|
2018-06-20 15:35:05 +02:00
|
|
|
#include "or/geoip.h"
|
2013-08-21 19:41:15 +02:00
|
|
|
|
2011-02-22 18:36:46 +01:00
|
|
|
/** Return the total number of circuits. */
|
2014-04-15 14:20:34 +02:00
|
|
|
STATIC int
|
2010-12-01 02:32:42 +01:00
|
|
|
count_circuits(void)
|
|
|
|
{
|
2014-08-15 22:32:32 +02:00
|
|
|
return smartlist_len(circuit_get_global_list());
|
2010-12-01 02:32:42 +01:00
|
|
|
}
|
|
|
|
|
2011-02-22 18:36:46 +01:00
|
|
|
/** Take seconds <b>secs</b> and return a newly allocated human-readable
|
2018-02-07 18:22:29 +01:00
|
|
|
* uptime string. */
|
2014-04-15 14:20:34 +02:00
|
|
|
STATIC char *
|
2010-12-01 02:32:42 +01:00
|
|
|
secs_to_uptime(long secs)
|
|
|
|
{
|
|
|
|
long int days = secs / 86400;
|
2011-02-24 07:19:09 +01:00
|
|
|
int hours = (int)((secs - (days * 86400)) / 3600);
|
|
|
|
int minutes = (int)((secs - (days * 86400) - (hours * 3600)) / 60);
|
2010-12-01 02:32:42 +01:00
|
|
|
char *uptime_string = NULL;
|
|
|
|
|
|
|
|
switch (days) {
|
|
|
|
case 0:
|
2011-05-16 21:58:01 +02:00
|
|
|
tor_asprintf(&uptime_string, "%d:%02d hours", hours, minutes);
|
2010-12-01 02:32:42 +01:00
|
|
|
break;
|
|
|
|
case 1:
|
2011-05-16 21:58:01 +02:00
|
|
|
tor_asprintf(&uptime_string, "%ld day %d:%02d hours",
|
|
|
|
days, hours, minutes);
|
2010-12-01 02:32:42 +01:00
|
|
|
break;
|
|
|
|
default:
|
2011-05-16 21:58:01 +02:00
|
|
|
tor_asprintf(&uptime_string, "%ld days %d:%02d hours",
|
|
|
|
days, hours, minutes);
|
2010-12-01 02:32:42 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return uptime_string;
|
|
|
|
}
|
|
|
|
|
2011-02-22 18:36:46 +01:00
|
|
|
/** Take <b>bytes</b> and returns a newly allocated human-readable usage
|
|
|
|
* string. */
|
2014-04-15 14:20:34 +02:00
|
|
|
STATIC char *
|
2011-02-22 18:36:46 +01:00
|
|
|
bytes_to_usage(uint64_t bytes)
|
2010-12-01 02:32:42 +01:00
|
|
|
{
|
|
|
|
char *bw_string = NULL;
|
2010-12-01 02:24:03 +01:00
|
|
|
|
2011-02-22 18:36:46 +01:00
|
|
|
if (bytes < (1<<20)) { /* Less than a megabyte. */
|
2018-07-03 16:40:59 +02:00
|
|
|
tor_asprintf(&bw_string, "%"PRIu64" kB", (bytes>>10));
|
2011-02-22 18:36:46 +01:00
|
|
|
} else if (bytes < (1<<30)) { /* Megabytes. Let's add some precision. */
|
2018-07-03 16:43:43 +02:00
|
|
|
double bw = ((double)bytes);
|
2010-12-01 02:32:42 +01:00
|
|
|
tor_asprintf(&bw_string, "%.2f MB", bw/(1<<20));
|
2011-02-22 18:36:46 +01:00
|
|
|
} else { /* Gigabytes. */
|
2018-07-03 16:43:43 +02:00
|
|
|
double bw = ((double)bytes);
|
2010-12-01 02:32:42 +01:00
|
|
|
tor_asprintf(&bw_string, "%.2f GB", bw/(1<<30));
|
|
|
|
}
|
2010-12-01 02:24:03 +01:00
|
|
|
|
2010-12-01 02:32:42 +01:00
|
|
|
return bw_string;
|
|
|
|
}
|
|
|
|
|
2018-05-08 18:21:24 +02:00
|
|
|
/** Log some usage info about our onion service(s). */
|
2018-01-23 11:31:06 +01:00
|
|
|
static void
|
|
|
|
log_onion_service_stats(void)
|
|
|
|
{
|
|
|
|
unsigned int num_services = hs_service_get_num_services();
|
|
|
|
|
2018-05-08 18:21:24 +02:00
|
|
|
/* If there are no active onion services, no need to print logs */
|
2018-01-23 11:31:06 +01:00
|
|
|
if (num_services == 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
log_notice(LD_HEARTBEAT,
|
2018-05-08 18:21:24 +02:00
|
|
|
"Our onion service%s received %u v2 and %u v3 INTRODUCE2 cells "
|
2018-01-23 11:31:06 +01:00
|
|
|
"and attempted to launch %d rendezvous circuits.",
|
|
|
|
num_services == 1 ? "" : "s",
|
|
|
|
hs_stats_get_n_introduce2_v2_cells(),
|
|
|
|
hs_stats_get_n_introduce2_v3_cells(),
|
|
|
|
hs_stats_get_n_rendezvous_launches());
|
|
|
|
}
|
|
|
|
|
2011-02-22 18:36:46 +01:00
|
|
|
/** Log a "heartbeat" message describing Tor's status and history so that the
|
|
|
|
* user can know that there is indeed a running Tor. Return 0 on success and
|
|
|
|
* -1 on failure. */
|
2010-12-01 02:32:42 +01:00
|
|
|
int
|
|
|
|
log_heartbeat(time_t now)
|
|
|
|
{
|
|
|
|
char *bw_sent = NULL;
|
|
|
|
char *bw_rcvd = NULL;
|
|
|
|
char *uptime = NULL;
|
|
|
|
const routerinfo_t *me;
|
2013-03-12 03:06:07 +01:00
|
|
|
double r = tls_get_write_overhead_ratio();
|
2013-03-12 01:50:02 +01:00
|
|
|
const int hibernating = we_are_hibernating();
|
2010-12-01 02:32:42 +01:00
|
|
|
|
2011-06-14 19:01:38 +02:00
|
|
|
const or_options_t *options = get_options();
|
2010-12-01 02:24:03 +01:00
|
|
|
|
2013-03-12 01:50:02 +01:00
|
|
|
if (public_server_mode(options) && !hibernating) {
|
2010-12-01 02:32:42 +01:00
|
|
|
/* 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
|
2011-06-08 21:10:43 +02:00
|
|
|
if (!node_get_by_id(me->cache_info.identity_digest))
|
2010-12-01 02:32:42 +01:00
|
|
|
log_fn(LOG_NOTICE, LD_HEARTBEAT, "Heartbeat: It seems like we are not "
|
|
|
|
"in the cached consensus.");
|
|
|
|
}
|
2010-12-01 02:24:03 +01:00
|
|
|
|
2010-12-01 02:32:42 +01:00
|
|
|
uptime = secs_to_uptime(get_uptime());
|
2011-04-16 14:48:46 +02:00
|
|
|
bw_rcvd = bytes_to_usage(get_bytes_read());
|
|
|
|
bw_sent = bytes_to_usage(get_bytes_written());
|
2010-12-01 02:24:03 +01:00
|
|
|
|
2010-12-01 02:32:42 +01:00
|
|
|
log_fn(LOG_NOTICE, LD_HEARTBEAT, "Heartbeat: Tor's uptime is %s, with %d "
|
2013-03-12 01:50:02 +01:00
|
|
|
"circuits open. I've sent %s and received %s.%s",
|
2015-02-20 07:24:36 +01:00
|
|
|
uptime, count_circuits(), bw_sent, bw_rcvd,
|
2013-03-12 01:50:02 +01:00
|
|
|
hibernating?" We are currently hibernating.":"");
|
2010-12-01 02:32:42 +01:00
|
|
|
|
2013-06-12 10:51:39 +02:00
|
|
|
if (server_mode(options) && accounting_is_enabled(options) && !hibernating) {
|
|
|
|
log_accounting(now, options);
|
|
|
|
}
|
|
|
|
|
2015-02-20 07:24:36 +01:00
|
|
|
double fullness_pct = 100;
|
|
|
|
if (stats_n_data_cells_packaged && !hibernating) {
|
|
|
|
fullness_pct =
|
2018-07-03 16:43:43 +02:00
|
|
|
100*(((double)stats_n_data_bytes_packaged) /
|
|
|
|
((double)stats_n_data_cells_packaged*RELAY_PAYLOAD_SIZE));
|
2013-03-12 03:06:07 +01:00
|
|
|
}
|
2015-02-20 07:24:36 +01:00
|
|
|
const double overhead_pct = ( r - 1.0 ) * 100.0;
|
|
|
|
|
|
|
|
#define FULLNESS_PCT_THRESHOLD 80
|
|
|
|
#define TLS_OVERHEAD_THRESHOLD 15
|
|
|
|
|
|
|
|
const int severity = (fullness_pct < FULLNESS_PCT_THRESHOLD ||
|
|
|
|
overhead_pct > TLS_OVERHEAD_THRESHOLD)
|
|
|
|
? LOG_NOTICE : LOG_INFO;
|
|
|
|
|
|
|
|
log_fn(severity, LD_HEARTBEAT,
|
2015-02-24 16:05:34 +01:00
|
|
|
"Average packaged cell fullness: %2.3f%%. "
|
2015-02-20 07:24:36 +01:00
|
|
|
"TLS write overhead: %.f%%", fullness_pct, overhead_pct);
|
2013-03-12 03:06:07 +01:00
|
|
|
|
2015-03-10 15:07:41 +01:00
|
|
|
if (public_server_mode(options)) {
|
2013-12-24 17:41:48 +01:00
|
|
|
rep_hist_log_circuit_handshake_stats(now);
|
2015-03-10 15:07:41 +01:00
|
|
|
rep_hist_log_link_protocol_counts();
|
2018-01-25 22:36:05 +01:00
|
|
|
dos_log_heartbeat();
|
2015-03-10 15:07:41 +01:00
|
|
|
}
|
2013-12-24 17:41:48 +01:00
|
|
|
|
2014-04-29 20:02:12 +02:00
|
|
|
circuit_log_ancient_one_hop_circuits(1800);
|
|
|
|
|
2015-02-16 21:40:15 +01:00
|
|
|
if (options->BridgeRelay) {
|
2013-02-26 11:43:53 +01:00
|
|
|
char *msg = NULL;
|
|
|
|
msg = format_client_stats_heartbeat(now);
|
|
|
|
if (msg)
|
|
|
|
log_notice(LD_HEARTBEAT, "%s", msg);
|
|
|
|
tor_free(msg);
|
|
|
|
}
|
|
|
|
|
2017-12-16 02:41:21 +01:00
|
|
|
if (options->MainloopStats) {
|
|
|
|
const uint64_t main_loop_success_count = get_main_loop_success_count();
|
|
|
|
const uint64_t main_loop_error_count = get_main_loop_error_count();
|
|
|
|
const uint64_t main_loop_idle_count = get_main_loop_idle_count();
|
|
|
|
|
|
|
|
log_fn(LOG_NOTICE, LD_HEARTBEAT, "Main event loop statistics: "
|
2018-07-03 16:40:59 +02:00
|
|
|
"%"PRIu64 " successful returns, "
|
|
|
|
"%"PRIu64 " erroneous returns, and "
|
|
|
|
"%"PRIu64 " idle returns.",
|
|
|
|
(main_loop_success_count),
|
|
|
|
(main_loop_error_count),
|
|
|
|
(main_loop_idle_count));
|
2017-12-16 02:41:21 +01:00
|
|
|
}
|
|
|
|
|
2018-01-23 11:31:06 +01:00
|
|
|
/** Now, if we are an HS service, log some stats about our usage */
|
|
|
|
log_onion_service_stats();
|
|
|
|
|
2010-12-01 02:32:42 +01:00
|
|
|
tor_free(uptime);
|
|
|
|
tor_free(bw_sent);
|
|
|
|
tor_free(bw_rcvd);
|
|
|
|
|
|
|
|
return 0;
|
2010-12-01 02:24:03 +01:00
|
|
|
}
|
2010-12-01 02:32:42 +01:00
|
|
|
|
2013-08-21 19:41:15 +02:00
|
|
|
static void
|
2013-06-12 10:51:39 +02:00
|
|
|
log_accounting(const time_t now, const or_options_t *options)
|
|
|
|
{
|
|
|
|
or_state_t *state = get_or_state();
|
|
|
|
char *acc_rcvd = bytes_to_usage(state->AccountingBytesReadInInterval);
|
|
|
|
char *acc_sent = bytes_to_usage(state->AccountingBytesWrittenInInterval);
|
2016-01-04 05:02:44 +01:00
|
|
|
char *acc_used = bytes_to_usage(get_accounting_bytes());
|
2014-09-23 14:34:22 +02:00
|
|
|
uint64_t acc_bytes = options->AccountingMax;
|
|
|
|
char *acc_max;
|
2013-06-12 10:51:39 +02:00
|
|
|
time_t interval_end = accounting_get_end_time();
|
|
|
|
char end_buf[ISO_TIME_LEN + 1];
|
|
|
|
char *remaining = NULL;
|
2014-09-23 14:34:22 +02:00
|
|
|
acc_max = bytes_to_usage(acc_bytes);
|
2013-06-12 10:51:39 +02:00
|
|
|
format_local_iso_time(end_buf, interval_end);
|
|
|
|
remaining = secs_to_uptime(interval_end - now);
|
|
|
|
|
2016-01-04 05:02:44 +01:00
|
|
|
const char *acc_rule;
|
|
|
|
switch (options->AccountingRule) {
|
|
|
|
case ACCT_MAX: acc_rule = "max";
|
|
|
|
break;
|
|
|
|
case ACCT_SUM: acc_rule = "sum";
|
|
|
|
break;
|
|
|
|
case ACCT_OUT: acc_rule = "out";
|
|
|
|
break;
|
|
|
|
case ACCT_IN: acc_rule = "in";
|
|
|
|
break;
|
|
|
|
default: acc_rule = "max";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2013-08-21 19:41:15 +02:00
|
|
|
log_notice(LD_HEARTBEAT, "Heartbeat: Accounting enabled. "
|
2016-01-04 05:02:44 +01:00
|
|
|
"Sent: %s, Received: %s, Used: %s / %s, Rule: %s. The "
|
2013-08-21 19:41:15 +02:00
|
|
|
"current accounting interval ends on %s, in %s.",
|
2016-01-04 05:02:44 +01:00
|
|
|
acc_sent, acc_rcvd, acc_used, acc_max, acc_rule, end_buf, remaining);
|
2013-06-12 10:51:39 +02:00
|
|
|
|
|
|
|
tor_free(acc_rcvd);
|
|
|
|
tor_free(acc_sent);
|
2016-01-04 05:02:44 +01:00
|
|
|
tor_free(acc_used);
|
2013-06-12 10:51:39 +02:00
|
|
|
tor_free(acc_max);
|
|
|
|
tor_free(remaining);
|
|
|
|
}
|
2013-08-21 19:41:15 +02:00
|
|
|
|