Tor  0.4.4.0-alpha-dev
status.c
Go to the documentation of this file.
1 /* Copyright (c) 2010-2020, The Tor Project, Inc. */
2 /* See LICENSE for licensing information */
3 
4 /**
5  * \file status.c
6  * \brief Collect status information and log heartbeat messages.
7  *
8  * This module is responsible for implementing the heartbeat log messages,
9  * which periodically inform users and operators about basic facts to
10  * do with their Tor instance. The log_heartbeat() function, invoked from
11  * main.c, is the principle entry point. It collects data from elsewhere
12  * in Tor, and logs it in a human-readable format.
13  **/
14 
15 #define STATUS_PRIVATE
16 
17 #include "core/or/or.h"
18 #include "core/or/circuituse.h"
19 #include "app/config/config.h"
21 #include "core/or/status.h"
23 #include "core/or/relay.h"
24 #include "feature/relay/router.h"
26 #include "core/or/circuitlist.h"
27 #include "core/mainloop/mainloop.h"
28 #include "feature/stats/rephist.h"
30 #include "app/config/statefile.h"
31 #include "feature/hs/hs_stats.h"
32 #include "feature/hs/hs_service.h"
33 #include "core/or/dos.h"
35 
36 #include "app/config/or_state_st.h"
38 #include "lib/tls/tortls.h"
39 
40 static void log_accounting(const time_t now, const or_options_t *options);
41 
42 /** Return the total number of circuits. */
43 STATIC int
45 {
46  return smartlist_len(circuit_get_global_list());
47 }
48 
49 /** Take seconds <b>secs</b> and return a newly allocated human-readable
50  * uptime string. */
51 STATIC char *
52 secs_to_uptime(long secs)
53 {
54  long int days = secs / 86400;
55  int hours = (int)((secs - (days * 86400)) / 3600);
56  int minutes = (int)((secs - (days * 86400) - (hours * 3600)) / 60);
57  char *uptime_string = NULL;
58 
59  switch (days) {
60  case 0:
61  tor_asprintf(&uptime_string, "%d:%02d hours", hours, minutes);
62  break;
63  case 1:
64  tor_asprintf(&uptime_string, "%ld day %d:%02d hours",
65  days, hours, minutes);
66  break;
67  default:
68  tor_asprintf(&uptime_string, "%ld days %d:%02d hours",
69  days, hours, minutes);
70  break;
71  }
72 
73  return uptime_string;
74 }
75 
76 /** Take <b>bytes</b> and returns a newly allocated human-readable usage
77  * string. */
78 STATIC char *
79 bytes_to_usage(uint64_t bytes)
80 {
81  char *bw_string = NULL;
82 
83  if (bytes < (1<<20)) { /* Less than a megabyte. */
84  tor_asprintf(&bw_string, "%"PRIu64" kB", (bytes>>10));
85  } else if (bytes < (1<<30)) { /* Megabytes. Let's add some precision. */
86  double bw = ((double)bytes);
87  tor_asprintf(&bw_string, "%.2f MB", bw/(1<<20));
88  } else { /* Gigabytes. */
89  double bw = ((double)bytes);
90  tor_asprintf(&bw_string, "%.2f GB", bw/(1<<30));
91  }
92 
93  return bw_string;
94 }
95 
96 /** Log some usage info about our onion service(s). */
97 static void
99 {
100  unsigned int num_services = hs_service_get_num_services();
101 
102  /* If there are no active onion services, no need to print logs */
103  if (num_services == 0) {
104  return;
105  }
106 
107  log_notice(LD_HEARTBEAT,
108  "Our onion service%s received %u v2 and %u v3 INTRODUCE2 cells "
109  "and attempted to launch %d rendezvous circuits.",
110  num_services == 1 ? "" : "s",
114 }
115 
116 /** Log a "heartbeat" message describing Tor's status and history so that the
117  * user can know that there is indeed a running Tor. Return 0 on success and
118  * -1 on failure. */
119 int
120 log_heartbeat(time_t now)
121 {
122  char *bw_sent = NULL;
123  char *bw_rcvd = NULL;
124  char *uptime = NULL;
125  const routerinfo_t *me;
126  double r = tls_get_write_overhead_ratio();
127  const int hibernating = we_are_hibernating();
128 
129  const or_options_t *options = get_options();
130 
131  if (public_server_mode(options) && !hibernating) {
132  /* Let's check if we are in the current cached consensus. */
133  if (!(me = router_get_my_routerinfo()))
134  return -1; /* Something stinks, we won't even attempt this. */
135  else
136  if (!node_get_by_id(me->cache_info.identity_digest))
137  log_fn(LOG_NOTICE, LD_HEARTBEAT, "Heartbeat: It seems like we are not "
138  "in the cached consensus.");
139  }
140 
141  uptime = secs_to_uptime(get_uptime());
142  bw_rcvd = bytes_to_usage(get_bytes_read());
143  bw_sent = bytes_to_usage(get_bytes_written());
144 
145  log_fn(LOG_NOTICE, LD_HEARTBEAT, "Heartbeat: Tor's uptime is %s, with %d "
146  "circuits open. I've sent %s and received %s.%s",
147  uptime, count_circuits(), bw_sent, bw_rcvd,
148  hibernating?" We are currently hibernating.":"");
149 
151 
152  if (server_mode(options) && accounting_is_enabled(options) && !hibernating) {
153  log_accounting(now, options);
154  }
155 
156  double fullness_pct = 100;
157  if (stats_n_data_cells_packaged && !hibernating) {
158  fullness_pct =
159  100*(((double)stats_n_data_bytes_packaged) /
161  }
162  const double overhead_pct = ( r - 1.0 ) * 100.0;
163 
164 #define FULLNESS_PCT_THRESHOLD 80
165 #define TLS_OVERHEAD_THRESHOLD 15
166 
167  const int severity = (fullness_pct < FULLNESS_PCT_THRESHOLD ||
168  overhead_pct > TLS_OVERHEAD_THRESHOLD)
169  ? LOG_NOTICE : LOG_INFO;
170 
171  log_fn(severity, LD_HEARTBEAT,
172  "Average packaged cell fullness: %2.3f%%. "
173  "TLS write overhead: %.f%%", fullness_pct, overhead_pct);
174 
175  if (public_server_mode(options)) {
178  dos_log_heartbeat();
179  }
180 
182 
183  if (options->BridgeRelay) {
184  char *msg = NULL;
186  if (msg)
187  log_notice(LD_HEARTBEAT, "%s", msg);
188  tor_free(msg);
189  }
190 
191  if (options->MainloopStats) {
192  const uint64_t main_loop_success_count = get_main_loop_success_count();
193  const uint64_t main_loop_error_count = get_main_loop_error_count();
194  const uint64_t main_loop_idle_count = get_main_loop_idle_count();
195 
196  log_fn(LOG_NOTICE, LD_HEARTBEAT, "Main event loop statistics: "
197  "%"PRIu64 " successful returns, "
198  "%"PRIu64 " erroneous returns, and "
199  "%"PRIu64 " idle returns.",
200  (main_loop_success_count),
201  (main_loop_error_count),
202  (main_loop_idle_count));
203  }
204 
205  /** Now, if we are an HS service, log some stats about our usage */
207 
208  tor_free(uptime);
209  tor_free(bw_sent);
210  tor_free(bw_rcvd);
211 
212  return 0;
213 }
214 
215 static void
216 log_accounting(const time_t now, const or_options_t *options)
217 {
218  or_state_t *state = get_or_state();
219  char *acc_rcvd = bytes_to_usage(state->AccountingBytesReadInInterval);
220  char *acc_sent = bytes_to_usage(state->AccountingBytesWrittenInInterval);
221  char *acc_used = bytes_to_usage(get_accounting_bytes());
222  uint64_t acc_bytes = options->AccountingMax;
223  char *acc_max;
224  time_t interval_end = accounting_get_end_time();
225  char end_buf[ISO_TIME_LEN + 1];
226  char *remaining = NULL;
227  acc_max = bytes_to_usage(acc_bytes);
228  format_local_iso_time(end_buf, interval_end);
229  remaining = secs_to_uptime(interval_end - now);
230 
231  const char *acc_rule;
232  switch (options->AccountingRule) {
233  case ACCT_MAX: acc_rule = "max";
234  break;
235  case ACCT_SUM: acc_rule = "sum";
236  break;
237  case ACCT_OUT: acc_rule = "out";
238  break;
239  case ACCT_IN: acc_rule = "in";
240  break;
241  default: acc_rule = "max";
242  break;
243  }
244 
245  log_notice(LD_HEARTBEAT, "Heartbeat: Accounting enabled. "
246  "Sent: %s, Received: %s, Used: %s / %s, Rule: %s. The "
247  "current accounting interval ends on %s, in %s.",
248  acc_sent, acc_rcvd, acc_used, acc_max, acc_rule, end_buf, remaining);
249 
250  tor_free(acc_rcvd);
251  tor_free(acc_sent);
252  tor_free(acc_used);
253  tor_free(acc_max);
254  tor_free(remaining);
255 }
#define RELAY_PAYLOAD_SIZE
Definition: or.h:605
Header for statefile.c.
Header file for dirclient.c.
uint64_t get_main_loop_error_count(void)
Definition: mainloop.c:549
Router descriptor structure.
uint32_t hs_stats_get_n_rendezvous_launches(void)
Definition: hs_stats.c:54
Header file containing service data for the HS subsytem.
uint64_t get_main_loop_idle_count(void)
Definition: mainloop.c:563
Header file for geoip_stats.c.
void format_local_iso_time(char *buf, time_t t)
Definition: time_fmt.c:285
long get_uptime(void)
Definition: mainloop.c:2525
uint64_t get_accounting_bytes(void)
Definition: hibernate.c:478
#define LOG_INFO
Definition: log.h:45
Header file for nodelist.c.
static void log_onion_service_stats(void)
Definition: status.c:98
int accounting_is_enabled(const or_options_t *options)
Definition: hibernate.c:305
Header file for config.c.
char identity_digest[DIGEST_LEN]
const or_options_t * get_options(void)
Definition: config.c:925
void rep_hist_log_link_protocol_counts(void)
Definition: rephist.c:2875
uint64_t get_bytes_written(void)
Definition: mainloop.c:474
#define tor_free(p)
Definition: malloc.h:52
uint32_t hs_stats_get_n_introduce2_v2_cells(void)
Definition: hs_stats.c:40
#define LOG_NOTICE
Definition: log.h:50
Header file for mainloop.c.
Header file for hs_stats.c.
uint64_t get_main_loop_success_count(void)
Definition: mainloop.c:535
#define STATIC
Definition: testsupport.h:32
smartlist_t * circuit_get_global_list(void)
Definition: circuitlist.c:691
Header file for hibernate.c.
Header file for routermode.c.
int tor_asprintf(char **strp, const char *fmt,...)
Definition: printf.c:75
uint64_t stats_n_data_bytes_packaged
Definition: relay.c:2046
uint32_t hs_stats_get_n_introduce2_v3_cells(void)
Definition: hs_stats.c:33
int log_heartbeat(time_t now)
Definition: status.c:120
double tls_get_write_overhead_ratio(void)
Definition: tortls_nss.c:689
Master header file for Tor-specific functionality.
const node_t * node_get_by_id(const char *identity_digest)
Definition: nodelist.c:223
Header file for rephist.c.
Header file for circuituse.c.
Header file for circuitlist.c.
uint64_t get_bytes_read(void)
Definition: mainloop.c:464
time_t accounting_get_end_time(void)
Definition: hibernate.c:322
uint64_t AccountingMax
void dirclient_dump_total_dls(void)
Definition: dirclient.c:1981
void circuit_log_ancient_one_hop_circuits(int age)
Definition: circuituse.c:878
uint64_t stats_n_data_cells_packaged
Definition: relay.c:2042
Header for status.c.
const routerinfo_t * router_get_my_routerinfo(void)
Definition: router.c:1668
Header file for relay.c.
The or_state_t structure, which represents Tor's state file.
Header file for router.c.
#define log_fn(severity, domain, args,...)
Definition: log.h:287
Headers for tortls.c.
int we_are_hibernating(void)
Definition: hibernate.c:937
STATIC int count_circuits(void)
Definition: status.c:44
unsigned int hs_service_get_num_services(void)
Definition: hs_service.c:3795
int public_server_mode(const or_options_t *options)
Definition: routermode.c:43
STATIC char * bytes_to_usage(uint64_t bytes)
Definition: status.c:79
char * format_client_stats_heartbeat(time_t now)
Definition: geoip_stats.c:1205
STATIC char * secs_to_uptime(long secs)
Definition: status.c:52
or_state_t * get_or_state(void)
Definition: statefile.c:200
int server_mode(const or_options_t *options)
Definition: routermode.c:34
void rep_hist_log_circuit_handshake_stats(time_t now)
Definition: rephist.c:2460
#define LD_HEARTBEAT
Definition: log.h:103