2013-01-16 07:54:56 +01:00
|
|
|
/* Copyright (c) 2010-2013, The Tor Project, Inc. */
|
2010-12-01 02:24:03 +01:00
|
|
|
/* See LICENSE for licensing information */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* \file status.c
|
|
|
|
* \brief Keep status information and log the heartbeat messages.
|
|
|
|
**/
|
|
|
|
|
|
|
|
#include "or.h"
|
2010-12-01 02:32:42 +01:00
|
|
|
#include "config.h"
|
|
|
|
#include "status.h"
|
|
|
|
#include "nodelist.h"
|
2012-12-18 21:16:35 +01:00
|
|
|
#include "relay.h"
|
2010-12-01 02:32:42 +01:00
|
|
|
#include "router.h"
|
|
|
|
#include "circuitlist.h"
|
|
|
|
#include "main.h"
|
2010-12-01 02:24:03 +01:00
|
|
|
|
2011-02-22 18:36:46 +01:00
|
|
|
/** Return the total number of circuits. */
|
2010-12-01 02:32:42 +01:00
|
|
|
static int
|
|
|
|
count_circuits(void)
|
|
|
|
{
|
|
|
|
circuit_t *circ;
|
|
|
|
int nr=0;
|
2010-12-01 02:24:03 +01:00
|
|
|
|
2012-10-12 18:22:13 +02:00
|
|
|
for (circ = circuit_get_global_list_(); circ; circ = circ->next)
|
2010-12-01 02:32:42 +01:00
|
|
|
nr++;
|
2010-12-01 02:24:03 +01:00
|
|
|
|
2010-12-01 02:32:42 +01:00
|
|
|
return nr;
|
|
|
|
}
|
|
|
|
|
2011-02-22 18:36:46 +01:00
|
|
|
/** Take seconds <b>secs</b> and return a newly allocated human-readable
|
|
|
|
* uptime string */
|
2010-12-01 02:32:42 +01:00
|
|
|
static char *
|
|
|
|
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. */
|
2010-12-01 02:32:42 +01: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. */
|
2010-12-01 02:32:42 +01:00
|
|
|
tor_asprintf(&bw_string, U64_FORMAT" kB", U64_PRINTF_ARG(bytes>>10));
|
2011-02-22 18:36:46 +01:00
|
|
|
} else if (bytes < (1<<30)) { /* Megabytes. Let's add some precision. */
|
2010-12-01 02:32:42 +01:00
|
|
|
double bw = U64_TO_DBL(bytes);
|
|
|
|
tor_asprintf(&bw_string, "%.2f MB", bw/(1<<20));
|
2011-02-22 18:36:46 +01:00
|
|
|
} else { /* Gigabytes. */
|
2010-12-01 02:32:42 +01:00
|
|
|
double bw = U64_TO_DBL(bytes);
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
|
2011-06-14 19:01:38 +02:00
|
|
|
const or_options_t *options = get_options();
|
2011-02-22 18:36:46 +01:00
|
|
|
(void)now;
|
2010-12-01 02:24:03 +01:00
|
|
|
|
2011-05-15 03:13:52 +02:00
|
|
|
if (public_server_mode(options)) {
|
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 "
|
2011-08-27 18:45:54 +02:00
|
|
|
"circuits open. I've sent %s and received %s.",
|
2010-12-01 02:32:42 +01:00
|
|
|
uptime, count_circuits(),bw_sent,bw_rcvd);
|
|
|
|
|
2012-12-18 21:16:35 +01:00
|
|
|
if (stats_n_data_cells_packaged)
|
|
|
|
log(LOG_NOTICE, LD_HEARTBEAT, "Average packaged cell fullness: %2.3f%%",
|
|
|
|
100*(U64_TO_DBL(stats_n_data_bytes_packaged) /
|
|
|
|
U64_TO_DBL(stats_n_data_cells_packaged*RELAY_PAYLOAD_SIZE)) );
|
|
|
|
|
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
|
|
|
|