summaryrefslogtreecommitdiff
path: root/src/ot_stats.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/ot_stats.c')
-rw-r--r--src/ot_stats.c826
1 files changed, 826 insertions, 0 deletions
diff --git a/src/ot_stats.c b/src/ot_stats.c
new file mode 100644
index 0000000..95f3cff
--- /dev/null
+++ b/src/ot_stats.c
@@ -0,0 +1,826 @@
1/* This software was written by Dirk Engling <erdgeist@erdgeist.org>
2 It is considered beerware. Prost. Skol. Cheers or whatever. */
3
4/* System */
5#include <pthread.h>
6#include <stdatomic.h>
7#include <stdio.h>
8#include <stdlib.h>
9#include <string.h>
10#include <sys/types.h>
11#include <sys/uio.h>
12#include <unistd.h>
13#ifdef WANT_SYSLOGS
14#include <syslog.h>
15#endif
16
17/* Opentracker */
18#include "ot_accesslist.h"
19#include "ot_event.h"
20#include "ot_hex.h"
21#include "ot_iovec.h"
22#include "ot_ip.h"
23#include "ot_mutex.h"
24#include "ot_workqueue.h"
25#include "ot_fmt.h"
26#include "ot_stats.h"
27#include "trackerlogic.h"
28
29#ifndef NO_FULLSCRAPE_LOGGING
30#define LOG_TO_STDERR(...) fprintf(stderr, __VA_ARGS__)
31#else
32#define LOG_TO_STDERR(...)
33#endif
34
35/* Forward declaration */
36static void stats_make(size_t *iovec_entries, struct iovec **iovector, ot_tasktype mode);
37#define OT_STATS_TMPSIZE 8192
38
39/* Each serving thread keeps its own statistics tally. The list is linked
40 through intrusive next pointers within the workstruct.
41
42 Registration happens once per thread and the adding up once per statistics
43 request, so this lock is never on anybody's hot path. */
44static struct ot_workstruct *g_counter_contexts;
45static ot_counter g_retired[OT_COUNTER_COUNT];
46static pthread_mutex_t g_counter_mutex = PTHREAD_MUTEX_INITIALIZER;
47
48void stats_counters_register(struct ot_workstruct *ws) {
49 pthread_mutex_lock(&g_counter_mutex);
50 ws->counters_next = g_counter_contexts;
51 g_counter_contexts = ws;
52 pthread_mutex_unlock(&g_counter_mutex);
53}
54
55void stats_counters_retire(struct ot_workstruct *ws) {
56 struct ot_workstruct **link;
57 size_t i;
58
59 pthread_mutex_lock(&g_counter_mutex);
60 for (link = &g_counter_contexts; *link; link = &(*link)->counters_next)
61 if (*link == ws) {
62 *link = ws->counters_next;
63 break;
64 }
65
66 /* When retiring a worker thread, collect the counter tally in the global */
67 for (i = 0; i < OT_COUNTER_COUNT; ++i)
68 atomic_fetch_add_explicit(g_retired + i, atomic_load_explicit(ws->counters + i, memory_order_relaxed), memory_order_relaxed);
69 pthread_mutex_unlock(&g_counter_mutex);
70}
71
72/* What every thread has counted so far, added together */
73static void counters_total(unsigned long long *total) {
74 struct ot_workstruct *ws;
75 size_t i;
76
77 pthread_mutex_lock(&g_counter_mutex);
78 for (i = 0; i < OT_COUNTER_COUNT; ++i)
79 total[i] = atomic_load_explicit(g_retired + i, memory_order_relaxed);
80 for (ws = g_counter_contexts; ws; ws = ws->counters_next)
81 for (i = 0; i < OT_COUNTER_COUNT; ++i)
82 total[i] += atomic_load_explicit(ws->counters + i, memory_order_relaxed);
83 pthread_mutex_unlock(&g_counter_mutex);
84}
85
86#define COUNT(index, by) atomic_fetch_add_explicit(ws->counters + (index), (unsigned long)(by), memory_order_relaxed)
87
88static const char *ot_failed_request_names[] = {
89 "302 Redirect", "400 Parse Error", "400 Invalid Parameter", "400 Invalid Parameter (compact=0)", "400 Not Modest",
90 "402 Payment Required", "403 Access Denied", "404 Not found", "500 Internal Server Error"};
91
92static time_t ot_start_time;
93
94#ifdef WANT_SPOT_WOODPECKER
95#define STATS_NETWORK_NODE_BITWIDTH 4
96#define STATS_NETWORK_NODE_COUNT (1 << STATS_NETWORK_NODE_BITWIDTH)
97
98#define __BYTE(P, D) (((uint8_t *)(P))[(D) / 8])
99#define __MSK (STATS_NETWORK_NODE_COUNT - 1)
100#define __SHFT(D) (((D) ^ STATS_NETWORK_NODE_BITWIDTH) & STATS_NETWORK_NODE_BITWIDTH)
101
102#define __LDR(P, D) ((__BYTE((P), (D)) >> __SHFT((D))) & __MSK)
103#define __STR(P, D, V) __BYTE((P), (D)) = (__BYTE((P), (D)) & ~(__MSK << __SHFT((D)))) | ((V) << __SHFT((D)))
104
105#if 0
106// XXX
107#define STATS_NETWORK_NODE_MAXDEPTH (68 - STATS_NETWORK_NODE_BITWIDTH)
108#define STATS_NETWORK_NODE_LIMIT (48 - STATS_NETWORK_NODE_BITWIDTH)
109#endif
110#define STATS_NETWORK_NODE_MAXDEPTH (28 - STATS_NETWORK_NODE_BITWIDTH)
111#define STATS_NETWORK_NODE_LIMIT (24 - STATS_NETWORK_NODE_BITWIDTH)
112
113typedef union stats_network_node stats_network_node;
114union stats_network_node {
115 size_t counters[STATS_NETWORK_NODE_COUNT];
116 stats_network_node *children[STATS_NETWORK_NODE_COUNT];
117};
118
119static int stat_increase_network_count(stats_network_node **pnode, int depth, uintptr_t ip) {
120 int foo = __LDR(ip, depth);
121 stats_network_node *node;
122
123 if (!*pnode) {
124 *pnode = malloc(sizeof(stats_network_node));
125 if (!*pnode)
126 return -1;
127 memset(*pnode, 0, sizeof(stats_network_node));
128 }
129 node = *pnode;
130
131 if (depth < STATS_NETWORK_NODE_MAXDEPTH)
132 return stat_increase_network_count(node->children + foo, depth + STATS_NETWORK_NODE_BITWIDTH, ip);
133
134 node->counters[foo]++;
135 return 0;
136}
137
138static int stats_shift_down_network_count(stats_network_node **node, int depth, int shift) {
139 size_t i, rest = 0;
140
141 if (!*node)
142 return 0;
143
144 for (i = 0; i < STATS_NETWORK_NODE_COUNT; ++i)
145 if (depth < STATS_NETWORK_NODE_MAXDEPTH)
146 rest += stats_shift_down_network_count((*node)->children + i, depth + STATS_NETWORK_NODE_BITWIDTH, shift);
147 else
148 rest += (*node)->counters[i] >>= shift;
149
150 if (!rest) {
151 free(*node);
152 *node = NULL;
153 }
154
155 return rest;
156}
157
158static size_t stats_get_highscore_networks(stats_network_node *node, int depth, ot_ip6 node_value, size_t *scores, ot_ip6 *networks, int network_count,
159 int limit) {
160 size_t i, score = 0;
161
162 if (!node)
163 return 0;
164
165 if (depth < limit) {
166 for (i = 0; i < STATS_NETWORK_NODE_COUNT; ++i)
167 if (node->children[i]) {
168 __STR(node_value, depth, i);
169 score += stats_get_highscore_networks(node->children[i], depth + STATS_NETWORK_NODE_BITWIDTH, node_value, scores, networks, network_count, limit);
170 }
171 return score;
172 }
173
174 if (depth > limit && depth < STATS_NETWORK_NODE_MAXDEPTH) {
175 for (i = 0; i < STATS_NETWORK_NODE_COUNT; ++i)
176 if (node->children[i])
177 score += stats_get_highscore_networks(node->children[i], depth + STATS_NETWORK_NODE_BITWIDTH, node_value, scores, networks, network_count, limit);
178 return score;
179 }
180
181 if (depth > limit && depth == STATS_NETWORK_NODE_MAXDEPTH) {
182 for (i = 0; i < STATS_NETWORK_NODE_COUNT; ++i)
183 score += node->counters[i];
184 return score;
185 }
186
187 /* if( depth == limit ) */
188 for (i = 0; i < STATS_NETWORK_NODE_COUNT; ++i) {
189 int j = 1;
190 size_t node_score;
191
192 if (depth == STATS_NETWORK_NODE_MAXDEPTH)
193 node_score = node->counters[i];
194 else
195 node_score = stats_get_highscore_networks(node->children[i], depth + STATS_NETWORK_NODE_BITWIDTH, node_value, scores, networks, network_count, limit);
196
197 score += node_score;
198
199 if (node_score <= scores[0])
200 continue;
201
202 __STR(node_value, depth, i);
203 while (j < network_count && node_score > scores[j])
204 ++j;
205 --j;
206
207 memmove(scores, scores + 1, j * sizeof(*scores));
208 memmove(networks, networks + 1, j * sizeof(*networks));
209 scores[j] = node_score;
210 memcpy(networks + j, node_value, sizeof(*networks));
211 }
212
213 return score;
214}
215
216static void stats_return_busy_networks(char **reply, const char *reply_end, stats_network_node *tree, size_t amount, int limit) {
217 ot_ip6 networks[amount];
218 ot_ip6 node_value;
219 size_t scores[amount];
220 size_t i;
221
222 memset(scores, 0, sizeof(scores));
223 memset(networks, 0, sizeof(networks));
224 memset(node_value, 0, sizeof(node_value));
225
226 stats_get_highscore_networks(tree, 0, node_value, scores, networks, amount, limit);
227
228 fmt_append(reply, reply_end, "Networks, limit /%d:\n", limit + STATS_NETWORK_NODE_BITWIDTH);
229 for (i = amount; i > 0; --i) {
230 if (scores[i - 1]) {
231 char ip_readable[64];
232
233 ip_readable[ip_format(ip_readable, networks[i - 1])] = 0;
234 fmt_append(reply, reply_end, "%08zd: %s\n", scores[i - 1], ip_readable);
235 }
236 }
237 fmt_append(reply, reply_end, "\n");
238}
239
240static stats_network_node *stats_woodpeckers_tree;
241static pthread_mutex_t g_woodpeckers_mutex = PTHREAD_MUTEX_INITIALIZER;
242
243static void stats_return_woodpeckers(char **reply, const char *reply_end, int amount) {
244 pthread_mutex_lock(&g_woodpeckers_mutex);
245 stats_return_busy_networks(reply, reply_end, stats_woodpeckers_tree, amount, STATS_NETWORK_NODE_MAXDEPTH);
246 pthread_mutex_unlock(&g_woodpeckers_mutex);
247}
248#endif
249
250typedef struct {
251 unsigned long long torrent_count;
252 unsigned long long peer_count;
253 unsigned long long seed_count;
254} torrent_stats;
255
256static int torrent_statter(ot_torrent *torrent, uintptr_t data) {
257 torrent_stats *stats = (torrent_stats *)data;
258 stats->torrent_count++;
259 stats->peer_count += torrent_peer_count(torrent);
260 stats->seed_count += torrent_seed_count(torrent);
261 return 0;
262}
263
264/* Converter function from memory to human readable hex strings */
265typedef struct {
266 size_t val;
267 ot_hash hash;
268} ot_record;
269
270static inline void stats_insert_into_highscore(size_t val, ot_record* list, size_t amount, ot_hash* hash) {
271 size_t i;
272 for (i = 0; (i < amount) && (list[i].val > val); ++i)
273 {}
274 /* if we score better than a record in the list, we need to make room in the high
275 score array by moving lower scoring entries up */
276 if (i < amount) {
277 memmove(list + i + 1, list + i, (amount - i - 1) * sizeof(ot_record));
278
279 memcpy(&list[i].hash, hash, sizeof(ot_hash));
280 list[i].val = val;
281 }
282}
283
284
285/* Fetches stats from tracker */
286void stats_top_txt(char **reply, const char *reply_end, size_t amount) {
287 size_t j, idx, bucket;
288 ot_record top100s[100], top100c[100], top100l[100];
289 char hex_out[42];
290
291 if (amount > 100)
292 amount = 100;
293
294 memset(top100s, 0, sizeof(top100s));
295 memset(top100c, 0, sizeof(top100c));
296 memset(top100l, 0, sizeof(top100l));
297
298 /* Iterate over complete torrent list */
299 for (bucket = 0; bucket < OT_BUCKET_COUNT; ++bucket) {
300 ot_vector *torrents_list = mutex_bucket_lock(bucket);
301 for (j = 0; j < torrents_list->size; ++j) {
302 ot_torrent *torrent = (ot_torrent *)(torrents_list->data) + j;
303 size_t peer_count = torrent_peer_count(torrent);
304 size_t seed_count = torrent_seed_count(torrent);
305 size_t leech_count = peer_count - seed_count;
306
307 stats_insert_into_highscore(peer_count, top100c, amount, &torrent->hash);
308 stats_insert_into_highscore(seed_count, top100s, amount, &torrent->hash);
309 stats_insert_into_highscore(leech_count, top100l, amount, &torrent->hash);
310 }
311 mutex_bucket_unlock(bucket, 0);
312 if (!g_opentracker_running)
313 return;
314 }
315
316 fmt_append(reply, reply_end, "Top %zd torrents by peers:\n", amount);
317 for (idx = 0; idx < amount; ++idx)
318 if (top100c[idx].val)
319 fmt_append(reply, reply_end, "\t%zd\t%s\n", top100c[idx].val, hex_from_hash(hex_out, top100c[idx].hash));
320 fmt_append(reply, reply_end, "Top %zd torrents by seeds:\n", amount);
321 for (idx = 0; idx < amount; ++idx)
322 if (top100s[idx].val)
323 fmt_append(reply, reply_end, "\t%zd\t%s\n", top100s[idx].val, hex_from_hash(hex_out, top100s[idx].hash));
324 fmt_append(reply, reply_end, "Top %zd torrents by leechers:\n", amount);
325 for (idx = 0; idx < amount; ++idx)
326 if (top100l[idx].val)
327 fmt_append(reply, reply_end, "\t%zd\t%s\n", top100l[idx].val, hex_from_hash(hex_out, top100l[idx].hash));
328}
329
330static unsigned long events_per_time(unsigned long long events, time_t t) { return events / ((unsigned int)t ? (unsigned int)t : 1); }
331
332static void stats_connections_mrtg(char **reply, const char *reply_end) {
333 ot_time t = time_since_seconds(ot_start_time);
334 unsigned long long total[OT_COUNTER_COUNT];
335
336 counters_total(total);
337
338 fmt_append(reply, reply_end, "%llu\n%llu\n%i seconds (%i hours)\nopentracker connections, %lu conns/s :: %lu success/s.",
339 total[OT_COUNTER_TCP_CONNECTIONS] + total[OT_COUNTER_UDP_CONNECTIONS],
340 total[OT_COUNTER_TCP_ANNOUNCES] + total[OT_COUNTER_UDP_ANNOUNCES] + total[OT_COUNTER_UDP_CONNECTS], (int)t, (int)(t / 3600),
341 events_per_time(total[OT_COUNTER_TCP_CONNECTIONS] + total[OT_COUNTER_UDP_CONNECTIONS], t),
342 events_per_time(total[OT_COUNTER_TCP_ANNOUNCES] + total[OT_COUNTER_UDP_ANNOUNCES] + total[OT_COUNTER_UDP_CONNECTS], t));
343}
344
345static void stats_udpconnections_mrtg(char **reply, const char *reply_end) {
346 ot_time t = time_since_seconds(ot_start_time);
347 unsigned long long total[OT_COUNTER_COUNT];
348
349 counters_total(total);
350
351 fmt_append(reply, reply_end, "%llu\n%llu\n%i seconds (%i hours)\nopentracker udp4 stats, %lu conns/s :: %lu success/s.", total[OT_COUNTER_UDP_CONNECTIONS],
352 total[OT_COUNTER_UDP_ANNOUNCES] + total[OT_COUNTER_UDP_CONNECTS], (int)t, (int)(t / 3600),
353 events_per_time(total[OT_COUNTER_UDP_CONNECTIONS], t),
354 events_per_time(total[OT_COUNTER_UDP_ANNOUNCES] + total[OT_COUNTER_UDP_CONNECTS], t));
355}
356
357static void stats_tcpconnections_mrtg(char **reply, const char *reply_end) {
358 time_t t = time_since_seconds(ot_start_time);
359 unsigned long long total[OT_COUNTER_COUNT];
360
361 counters_total(total);
362
363 fmt_append(reply, reply_end, "%llu\n%llu\n%i seconds (%i hours)\nopentracker tcp4 stats, %lu conns/s :: %lu success/s.", total[OT_COUNTER_TCP_CONNECTIONS],
364 total[OT_COUNTER_TCP_ANNOUNCES], (int)t, (int)(t / 3600), events_per_time(total[OT_COUNTER_TCP_CONNECTIONS], t),
365 events_per_time(total[OT_COUNTER_TCP_ANNOUNCES], t));
366}
367
368static void stats_scrape_mrtg(char **reply, const char *reply_end) {
369 time_t t = time_since_seconds(ot_start_time);
370 unsigned long long total[OT_COUNTER_COUNT];
371
372 counters_total(total);
373
374 fmt_append(reply, reply_end, "%llu\n%llu\n%i seconds (%i hours)\nopentracker scrape stats, %lu scrape/s (tcp and udp)",
375 total[OT_COUNTER_TCP_SCRAPES], total[OT_COUNTER_UDP_SCRAPES], (int)t, (int)(t / 3600),
376 events_per_time((total[OT_COUNTER_TCP_SCRAPES] + total[OT_COUNTER_UDP_SCRAPES]), t));
377}
378
379static void stats_fullscrapes_mrtg(char **reply, const char *reply_end) {
380 ot_time t = time_since_seconds(ot_start_time);
381 unsigned long long total[OT_COUNTER_COUNT];
382
383 counters_total(total);
384
385 fmt_append(reply, reply_end, "%llu\n%llu\n%i seconds (%i hours)\nopentracker full scrape stats, %lu conns/s :: %lu bytes/s.",
386 total[OT_COUNTER_FULLSCRAPES] * 1000, total[OT_COUNTER_FULLSCRAPE_BYTES], (int)t, (int)(t / 3600),
387 events_per_time(total[OT_COUNTER_FULLSCRAPES], t),
388 events_per_time(total[OT_COUNTER_FULLSCRAPE_BYTES], t));
389}
390
391static void stats_peers_mrtg(char **reply, const char *reply_end) {
392 torrent_stats stats = {0, 0, 0};
393
394 iterate_all_torrents(torrent_statter, (uintptr_t)&stats);
395
396 fmt_append(reply, reply_end, "%llu\n%llu\nopentracker serving %llu torrents\nopentracker", stats.peer_count, stats.seed_count,
397 stats.torrent_count);
398}
399
400static void stats_torrents_mrtg(char **reply, const char *reply_end) {
401 size_t torrent_count = mutex_get_torrent_count();
402
403 fmt_append(reply, reply_end, "%zd\n%zd\nopentracker serving %zd torrents\nopentracker", torrent_count, (size_t)0, torrent_count);
404}
405
406static void stats_httperrors_txt(char **reply, const char *reply_end) {
407 unsigned long long total[OT_COUNTER_COUNT];
408
409 counters_total(total);
410
411 fmt_append(reply, reply_end, "302 RED %llu\n400 ... %llu\n400 PAR %llu\n400 COM %llu\n403 IP %llu\n404 INV %llu\n500 SRV %llu\n",
412 total[OT_COUNTER_FAILED + 0], total[OT_COUNTER_FAILED + 1], total[OT_COUNTER_FAILED + 2], total[OT_COUNTER_FAILED + 3],
413 total[OT_COUNTER_FAILED + 4], total[OT_COUNTER_FAILED + 5], total[OT_COUNTER_FAILED + 6]);
414}
415
416static void stats_return_renew_bucket(char **reply, const char *reply_end) {
417 unsigned long long total[OT_COUNTER_COUNT];
418
419 counters_total(total);
420
421 int i;
422
423 for (i = 0; i < OT_PEER_TIMEOUT; ++i)
424 fmt_append(reply, reply_end, "%02i %llu\n", i, total[OT_COUNTER_RENEWED + i]);
425}
426
427static void stats_return_sync_mrtg(char **reply, const char *reply_end) {
428 ot_time t = time_since_seconds(ot_start_time);
429 unsigned long long total[OT_COUNTER_COUNT];
430
431 counters_total(total);
432
433 fmt_append(reply, reply_end, "%llu\n%llu\n%i seconds (%i hours)\nopentracker connections, %lu conns/s :: %lu success/s.", total[OT_COUNTER_SYNCS], 0LL,
434 (int)t, (int)(t / 3600), events_per_time(total[OT_COUNTER_TCP_CONNECTIONS] + total[OT_COUNTER_UDP_CONNECTIONS], t),
435 events_per_time(total[OT_COUNTER_TCP_ANNOUNCES] + total[OT_COUNTER_UDP_ANNOUNCES] + total[OT_COUNTER_UDP_CONNECTS], t));
436}
437
438static void stats_return_completed_mrtg(char **reply, const char *reply_end) {
439 ot_time t = time_since_seconds(ot_start_time);
440 unsigned long long total[OT_COUNTER_COUNT];
441
442 counters_total(total);
443
444
445 fmt_append(reply, reply_end, "%llu\n%llu\n%i seconds (%i hours)\nopentracker, %lu completed/h.", total[OT_COUNTER_COMPLETED], 0LL, (int)t, (int)(t / 3600),
446 events_per_time(total[OT_COUNTER_COMPLETED], t / 3600));
447}
448
449#ifdef WANT_LOG_NUMWANT
450extern unsigned long long numwants[201];
451static void stats_return_numwants(char **reply, const char *reply_end) {
452 int i;
453 for (i = 0; i <= 200; ++i)
454 fmt_append(reply, reply_end, "%03d => %llu\n", i, numwants[i]);
455}
456#endif
457
458static void stats_return_everything(char **reply, const char *reply_end) {
459 unsigned long long total[OT_COUNTER_COUNT];
460
461 counters_total(total);
462
463 torrent_stats stats = {0, 0, 0};
464 int i;
465
466 iterate_all_torrents(torrent_statter, (uintptr_t)&stats);
467
468 fmt_append(reply, reply_end, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
469 fmt_append(reply, reply_end, "<stats>\n");
470 fmt_append(reply, reply_end, " <uptime>%llu</uptime>\n", (unsigned long long)time_since_seconds(ot_start_time));
471 fmt_append(reply, reply_end, " <torrents>\n");
472 fmt_append(reply, reply_end, " <count_mutex>%zd</count_mutex>\n", mutex_get_torrent_count());
473 fmt_append(reply, reply_end, " <count_iterator>%llu</count_iterator>\n", stats.torrent_count);
474 fmt_append(reply, reply_end, " </torrents>\n");
475 fmt_append(reply, reply_end, " <peers>\n <count>%llu</count>\n </peers>\n", stats.peer_count);
476 fmt_append(reply, reply_end, " <seeds>\n <count>%llu</count>\n </seeds>\n", stats.seed_count);
477 fmt_append(reply, reply_end, " <completed>\n <count>%llu</count>\n </completed>\n", total[OT_COUNTER_COMPLETED]);
478 fmt_append(reply, reply_end, " <connections>\n");
479 fmt_append(reply, reply_end, " <tcp>\n <accept>%llu</accept>\n <announce>%llu</announce>\n <scrape>%llu</scrape>\n </tcp>\n",
480 total[OT_COUNTER_TCP_CONNECTIONS], total[OT_COUNTER_TCP_ANNOUNCES], total[OT_COUNTER_TCP_SCRAPES]);
481 fmt_append(reply, reply_end, " <udp>\n <overall>%llu</overall>\n <connect>%llu</connect>\n <announce>%llu</announce>\n <scrape>%llu</scrape>\n <missmatch>%llu</missmatch>\n </udp>\n",
482 total[OT_COUNTER_UDP_CONNECTIONS], total[OT_COUNTER_UDP_CONNECTS], total[OT_COUNTER_UDP_ANNOUNCES], total[OT_COUNTER_UDP_SCRAPES],
483 total[OT_COUNTER_UDP_CONNID_MISSMATCH]);
484 fmt_append(reply, reply_end, " <livesync>\n <count>%llu</count>\n </livesync>\n", total[OT_COUNTER_SYNCS]);
485 fmt_append(reply, reply_end, " </connections>\n");
486 fmt_append(reply, reply_end, " <debug>\n");
487 fmt_append(reply, reply_end, " <renew>\n");
488 for (i = 0; i < OT_PEER_TIMEOUT; ++i)
489 fmt_append(reply, reply_end, " <count interval=\"%02i\">%llu</count>\n", i, total[OT_COUNTER_RENEWED + i]);
490 fmt_append(reply, reply_end, " </renew>\n");
491 fmt_append(reply, reply_end, " <http_error>\n");
492 for (i = 0; i < CODE_HTTPERROR_COUNT; ++i)
493 fmt_append(reply, reply_end, " <count code=\"%s\">%llu</count>\n", ot_failed_request_names[i], total[OT_COUNTER_FAILED + i]);
494 fmt_append(reply, reply_end, " </http_error>\n");
495 fmt_append(reply, reply_end, " <mutex_stall>\n <count>%llu</count>\n </mutex_stall>\n", total[OT_COUNTER_STALLS]);
496 fmt_append(reply, reply_end, " </debug>\n");
497 fmt_append(reply, reply_end, "</stats>");
498}
499
500/* The names as the Makefile spells them, so that what a binary reports maps
501 straight back onto the FEATURES lines it was built from */
502static const char *const build_features[] = {
503#ifdef WANT_V4_ONLY
504 "WANT_V4_ONLY",
505#endif
506#ifdef WANT_ACCESSLIST_BLACK
507 "WANT_ACCESSLIST_BLACK",
508#endif
509#ifdef WANT_ACCESSLIST_WHITE
510 "WANT_ACCESSLIST_WHITE",
511#endif
512#ifdef WANT_DYNAMIC_ACCESSLIST
513 "WANT_DYNAMIC_ACCESSLIST",
514#endif
515#ifdef WANT_SYNC_LIVE
516 "WANT_SYNC_LIVE",
517#endif
518#ifdef WANT_IP_FROM_QUERY_STRING
519 "WANT_IP_FROM_QUERY_STRING",
520#endif
521#ifdef WANT_IP_FROM_PROXY
522 "WANT_IP_FROM_PROXY",
523#endif
524#ifdef WANT_COMPRESSION_GZIP
525 "WANT_COMPRESSION_GZIP",
526#endif
527#ifdef WANT_COMPRESSION_GZIP_ALWAYS
528 "WANT_COMPRESSION_GZIP_ALWAYS",
529#endif
530#ifdef WANT_COMPRESSION_ZSTD
531 "WANT_COMPRESSION_ZSTD",
532#endif
533#ifdef WANT_COMPRESSION_ZSTD_ALWAYS
534 "WANT_COMPRESSION_ZSTD_ALWAYS",
535#endif
536#ifdef WANT_FULLSCRAPE
537 "WANT_FULLSCRAPE",
538#endif
539#ifdef WANT_MODEST_FULLSCRAPES
540 "WANT_MODEST_FULLSCRAPES",
541#endif
542#ifdef WANT_KEEPALIVE
543 "WANT_KEEPALIVE",
544#endif
545#ifdef WANT_RESTRICT_STATS
546 "WANT_RESTRICT_STATS",
547#endif
548#ifdef WANT_RESTRICT_FULLSCRAPE
549 "WANT_RESTRICT_FULLSCRAPE",
550#endif
551#ifdef WANT_LOG_NUMWANT
552 "WANT_LOG_NUMWANT",
553#endif
554#ifdef WANT_SPOT_WOODPECKER
555 "WANT_SPOT_WOODPECKER",
556#endif
557#ifdef WANT_SYSLOGS
558 "WANT_SYSLOGS",
559#endif
560#ifdef WANT_DEV_RANDOM
561 "WANT_DEV_RANDOM",
562#endif
563#ifdef WANT_INSECURE_RANDOM_FALLBACK
564 "WANT_INSECURE_RANDOM_FALLBACK",
565#endif
566#ifdef _DEBUG
567 "_DEBUG",
568#endif
569#ifdef _DEBUG_HTTPERROR
570 "_DEBUG_HTTPERROR",
571#endif
572#ifdef _DEBUG_RANDOMTORRENTS
573 "_DEBUG_RANDOMTORRENTS",
574#endif
575 NULL};
576
577void stats_return_tracker_features(char **reply, const char *reply_end) {
578 size_t i;
579
580 fmt_append(reply, reply_end, "events: %s\n", event_mechanism());
581 fmt_append(reply, reply_end, "features:");
582 for (i = 0; build_features[i]; ++i)
583 fmt_append(reply, reply_end, " %s", build_features[i]);
584 if (!build_features[0])
585 fmt_append(reply, reply_end, " none");
586 fmt_append(reply, reply_end, "\n");
587}
588
589void stats_return_tracker_version(char **reply, const char *reply_end) {
590 fmt_append(reply, reply_end, "opentracker %s", OT_VERSION);
591 if (*GIT_VERSION && strcmp(GIT_VERSION, OT_TAG))
592 fmt_append(reply, reply_end, " (%s)", GIT_VERSION);
593 fmt_append(reply, reply_end, "\n");
594 if (*OT_TAG)
595 fmt_append(reply, reply_end, "https://erdgeist.org/gitweb/opentracker/tag/?h=%s\n", OT_TAG);
596}
597
598void return_stats_for_tracker(char **reply, const char *reply_end, int mode, int format) {
599 (void)format;
600 switch (mode & TASK_TASK_MASK) {
601 case TASK_STATS_CONNS:
602 stats_connections_mrtg(reply, reply_end);
603 break;
604 case TASK_STATS_SCRAPE:
605 stats_scrape_mrtg(reply, reply_end);
606 break;
607 case TASK_STATS_UDP:
608 stats_udpconnections_mrtg(reply, reply_end);
609 break;
610 case TASK_STATS_TCP:
611 stats_tcpconnections_mrtg(reply, reply_end);
612 break;
613 case TASK_STATS_FULLSCRAPE:
614 stats_fullscrapes_mrtg(reply, reply_end);
615 break;
616 case TASK_STATS_COMPLETED:
617 stats_return_completed_mrtg(reply, reply_end);
618 break;
619 case TASK_STATS_HTTPERRORS:
620 stats_httperrors_txt(reply, reply_end);
621 break;
622 case TASK_STATS_RENEW:
623 stats_return_renew_bucket(reply, reply_end);
624 break;
625 case TASK_STATS_SYNCS:
626 stats_return_sync_mrtg(reply, reply_end);
627 break;
628#ifdef WANT_LOG_NUMWANT
629 case TASK_STATS_NUMWANTS:
630 stats_return_numwants(reply, reply_end);
631 break;
632#endif
633 default:
634 break;
635 }
636}
637
638static void stats_make(size_t *iovec_entries, struct iovec **iovector, ot_tasktype mode) {
639 char *r;
640 const char *r_end;
641
642 *iovec_entries = 0;
643 *iovector = NULL;
644 if (!(r = iovec_new_chunk(iovec_entries, iovector, OT_STATS_TMPSIZE)))
645 return;
646 r_end = r + OT_STATS_TMPSIZE;
647
648 switch (mode & TASK_TASK_MASK) {
649 case TASK_STATS_TORRENTS:
650 stats_torrents_mrtg(&r, r_end);
651 break;
652 case TASK_STATS_PEERS:
653 stats_peers_mrtg(&r, r_end);
654 break;
655 case TASK_STATS_TOP10:
656 stats_top_txt(&r, r_end, 10);
657 break;
658 case TASK_STATS_TOP100:
659 r = iovec_next_chunk(iovec_entries, iovector, r, 4 * OT_STATS_TMPSIZE);
660 if (!r)
661 return;
662 r_end = r + 4 * OT_STATS_TMPSIZE;
663 stats_top_txt(&r, r_end, 100);
664 break;
665 case TASK_STATS_EVERYTHING:
666 r = iovec_next_chunk(iovec_entries, iovector, r, OT_STATS_TMPSIZE + 64 * OT_PEER_TIMEOUT);
667 if (!r)
668 return;
669 r_end = r + OT_STATS_TMPSIZE + 64 * OT_PEER_TIMEOUT;
670 stats_return_everything(&r, r_end);
671 break;
672#ifdef WANT_SPOT_WOODPECKER
673 case TASK_STATS_WOODPECKERS:
674 stats_return_woodpeckers(&r, r_end, 128);
675 break;
676#endif
677 default:
678 iovec_free(iovec_entries, iovector);
679 return;
680 }
681 iovec_trim_last(iovec_entries, iovector, r);
682}
683
684void stats_issue_event(struct ot_workstruct *ws, ot_status_event event, PROTO_FLAG proto, uintptr_t event_data) {
685 if (!ws)
686 return;
687
688 switch (event) {
689 case EVENT_ACCEPT:
690 if (proto == FLAG_TCP)
691 COUNT(OT_COUNTER_TCP_CONNECTIONS, 1);
692 else
693 COUNT(OT_COUNTER_UDP_CONNECTIONS, 1);
694 break;
695 case EVENT_ANNOUNCE:
696 if (proto == FLAG_TCP)
697 COUNT(OT_COUNTER_TCP_ANNOUNCES, 1);
698 else
699 COUNT(OT_COUNTER_UDP_ANNOUNCES, 1);
700 break;
701 case EVENT_CONNECT:
702 if (proto == FLAG_TCP)
703 COUNT(OT_COUNTER_TCP_CONNECTS, 1);
704 else
705 COUNT(OT_COUNTER_UDP_CONNECTS, 1);
706 break;
707 case EVENT_COMPLETED:
708#ifdef WANT_SYSLOGS
709 {
710 char timestring[64];
711 char hash_hex[42], peerid_hex[42], ip_readable[64];
712 struct tm time_now;
713 time_t ttt;
714
715 time(&ttt);
716 localtime_r(&ttt, &time_now);
717 if (!strftime(timestring, sizeof(timestring), "%FT%T%z", &time_now))
718 timestring[0] = 0;
719
720 hex_from_hash(hash_hex, *ws->hash);
721 if (ws->peer_id)
722 hex_from_hash(peerid_hex, (uint8_t *)ws->peer_id);
723 else {
724 *peerid_hex = 0;
725 }
726
727 ip_readable[ip_format(ip_readable, (char *)&ws->peer)] = 0;
728 syslog(LOG_INFO, "time=%s event=completed info_hash=%s peer_id=%s ip=%s", timestring, hash_hex, peerid_hex, ip_readable);
729 }
730#endif
731 COUNT(OT_COUNTER_COMPLETED, 1);
732 break;
733 case EVENT_SCRAPE:
734 if (proto == FLAG_TCP)
735 COUNT(OT_COUNTER_TCP_SCRAPES, 1);
736 else
737 COUNT(OT_COUNTER_UDP_SCRAPES, 1);
738 break;
739 case EVENT_FULLSCRAPE:
740 COUNT(OT_COUNTER_FULLSCRAPES, 1);
741 COUNT(OT_COUNTER_FULLSCRAPE_BYTES, event_data);
742 break;
743 case EVENT_FULLSCRAPE_REQUEST: {
744 ot_ip6 *ip = (ot_ip6 *)event_data; /* ugly hack to transfer ip to stats */
745 char _debug[512];
746 int off = snprintf(_debug, sizeof(_debug), "[%08d] scrp: ", (unsigned int)time_since_seconds(ot_start_time) / 60);
747 off += ip_format(_debug + off, *ip);
748 off += snprintf(_debug + off, sizeof(_debug) - off, " - FULL SCRAPE\n");
749 (void)!write(2, _debug, off);
750 COUNT(OT_COUNTER_FULLSCRAPE_REQUESTS, 1);
751 } break;
752 case EVENT_FULLSCRAPE_REQUEST_GZIP: {
753 ot_ip6 *ip = (ot_ip6 *)event_data; /* ugly hack to transfer ip to stats */
754 char _debug[512];
755 int off = snprintf(_debug, sizeof(_debug), "[%08d] scrp: ", (unsigned int)time_since_seconds(ot_start_time) / 60);
756 off += ip_format(_debug + off, *ip);
757 off += snprintf(_debug + off, sizeof(_debug) - off, " - FULL SCRAPE\n");
758 (void)!write(2, _debug, off);
759 COUNT(OT_COUNTER_FULLSCRAPE_REQUESTS, 1);
760 } break;
761 case EVENT_FAILED:
762 if (event_data < CODE_HTTPERROR_COUNT)
763 COUNT(OT_COUNTER_FAILED + event_data, 1);
764 break;
765 case EVENT_RENEW:
766 if (event_data < OT_PEER_TIMEOUT)
767 COUNT(OT_COUNTER_RENEWED + event_data, 1);
768 break;
769 case EVENT_SYNC:
770 COUNT(OT_COUNTER_SYNCS, event_data);
771 break;
772 case EVENT_BUCKET_LOCKED:
773 COUNT(OT_COUNTER_STALLS, 1);
774 break;
775#ifdef WANT_SPOT_WOODPECKER
776 case EVENT_WOODPECKER:
777 pthread_mutex_lock(&g_woodpeckers_mutex);
778 stat_increase_network_count(&stats_woodpeckers_tree, 0, event_data);
779 pthread_mutex_unlock(&g_woodpeckers_mutex);
780 break;
781#endif
782 case EVENT_CONNID_MISSMATCH:
783 COUNT(OT_COUNTER_UDP_CONNID_MISSMATCH, 1);
784 default:
785 break;
786 }
787}
788
789void stats_cleanup(void) {
790#ifdef WANT_SPOT_WOODPECKER
791 pthread_mutex_lock(&g_woodpeckers_mutex);
792 stats_shift_down_network_count(&stats_woodpeckers_tree, 0, 1);
793 pthread_mutex_unlock(&g_woodpeckers_mutex);
794#endif
795}
796
797static void *stats_worker(void *args) {
798 size_t iovec_entries;
799 struct iovec *iovector;
800
801 (void)args;
802
803 while (g_opentracker_running) {
804 ot_tasktype tasktype = TASK_STATS;
805 ot_taskid taskid = workqueue_poptask(&tasktype);
806 if (!taskid)
807 break;
808 stats_make(&iovec_entries, &iovector, tasktype);
809 if (workqueue_pushresult(taskid, iovec_entries, iovector))
810 iovec_free(&iovec_entries, &iovector);
811 }
812 return NULL;
813}
814
815void stats_deliver(int64_t sock, int tasktype) { workqueue_pushtask(sock, tasktype); }
816
817static pthread_t thread_id;
818void stats_init(void) {
819 ot_start_time = time_now_seconds();
820 pthread_create(&thread_id, NULL, stats_worker, NULL);
821}
822
823void stats_deinit(void) {
824 workqueue_wakeall();
825 pthread_join(thread_id, NULL);
826}