1 /* This software was written by Dirk Engling <erdgeist@erdgeist.org>
2 It is considered beerware. Prost. Skol. Cheers or whatever.
3
4 $id$ */
5
6 /* System */
7 #include <stdlib.h>
8 #include <arpa/inet.h>
9 #include <sys/types.h>
10 #include <sys/uio.h>
11 #include <sys/mman.h>
12 #include <stdio.h>
13 #include <string.h>
14 #include <pthread.h>
15 #include <unistd.h>
16 #include <inttypes.h>
17 #ifdef WANT_SYSLOGS
18 #include <syslog.h>
19 #endif
20
21 /* Libowfat */
22 #include "byte.h"
23 #include "io.h"
24 #include "ip4.h"
25 #include "ip6.h"
26
27 /* Opentracker */
28 #include "trackerlogic.h"
29 #include "ot_mutex.h"
30 #include "ot_iovec.h"
31 #include "ot_stats.h"
32 #include "ot_accesslist.h"
33
34 #ifndef NO_FULLSCRAPE_LOGGING
35 #define LOG_TO_STDERR( ... ) fprintf( stderr, __VA_ARGS__ )
36 #else
37 #define LOG_TO_STDERR( ... )
38 #endif
39
40 /* Forward declaration */
41 static void stats_make( int *iovec_entries, struct iovec **iovector, ot_tasktype mode );
42 #define OT_STATS_TMPSIZE 8192
43
44 /* Clumsy counters... to be rethought */
45 static unsigned long long ot_overall_tcp_connections = 0;
46 static unsigned long long ot_overall_udp_connections = 0;
47 static unsigned long long ot_overall_tcp_successfulannounces = 0;
48 static unsigned long long ot_overall_udp_successfulannounces = 0;
49 static unsigned long long ot_overall_tcp_successfulscrapes = 0;
50 static unsigned long long ot_overall_udp_successfulscrapes = 0;
51 static unsigned long long ot_overall_udp_connectionidmissmatches = 0;
52 static unsigned long long ot_overall_tcp_connects = 0;
53 static unsigned long long ot_overall_udp_connects = 0;
54 static unsigned long long ot_overall_completed = 0;
55 static unsigned long long ot_full_scrape_count = 0;
56 static unsigned long long ot_full_scrape_request_count = 0;
57 static unsigned long long ot_full_scrape_size = 0;
58 static unsigned long long ot_failed_request_counts[CODE_HTTPERROR_COUNT];
59 static char * ot_failed_request_names[] = { "302 Redirect", "400 Parse Error", "400 Invalid Parameter", "400 Invalid Parameter (compact=0)", "400 Not Modest", "402 Payment Required", "403 Access Denied", "404 Not found", "500 Internal Server Error" };
60 static unsigned long long ot_renewed[OT_PEER_TIMEOUT];
61 static unsigned long long ot_overall_sync_count;
62 static unsigned long long ot_overall_stall_count;
63
64 static time_t ot_start_time;
65
66 #define STATS_NETWORK_NODE_BITWIDTH 4
67 #define STATS_NETWORK_NODE_COUNT (1<<STATS_NETWORK_NODE_BITWIDTH)
68
69 #define __BYTE(P,D) (((uint8_t*)P)[D/8])
70 #define __MSK (STATS_NETWORK_NODE_COUNT-1)
71 #define __SHFT(D) ((D^STATS_NETWORK_NODE_BITWIDTH)&STATS_NETWORK_NODE_BITWIDTH)
72
73 #define __LDR(P,D) ((__BYTE((P),(D))>>__SHFT((D)))&__MSK)
74 #define __STR(P,D,V) __BYTE((P),(D))=(__BYTE((P),(D))&~(__MSK<<__SHFT((D))))|((V)<<__SHFT((D)))
75
76 #ifdef WANT_V6
77 #define STATS_NETWORK_NODE_MAXDEPTH (68-STATS_NETWORK_NODE_BITWIDTH)
78 #define STATS_NETWORK_NODE_LIMIT (48-STATS_NETWORK_NODE_BITWIDTH)
79 #else
80 #define STATS_NETWORK_NODE_MAXDEPTH (28-STATS_NETWORK_NODE_BITWIDTH)
81 #define STATS_NETWORK_NODE_LIMIT (24-STATS_NETWORK_NODE_BITWIDTH)
82 #endif
83
84 typedef union stats_network_node stats_network_node;
85 union stats_network_node {
86 size_t counters[STATS_NETWORK_NODE_COUNT];
87 stats_network_node *children[STATS_NETWORK_NODE_COUNT];
88 };
89
90 #ifdef WANT_LOG_NETWORKS
91 static stats_network_node *stats_network_counters_root;
92 #endif
93
stat_increase_network_count(stats_network_node ** pnode,int depth,uintptr_t ip)94 static int stat_increase_network_count( stats_network_node **pnode, int depth, uintptr_t ip ) {
95 int foo = __LDR(ip,depth);
96 stats_network_node *node;
97
98 if( !*pnode ) {
99 *pnode = malloc( sizeof( stats_network_node ) );
100 if( !*pnode )
101 return -1;
102 memset( *pnode, 0, sizeof( stats_network_node ) );
103 }
104 node = *pnode;
105
106 if( depth < STATS_NETWORK_NODE_MAXDEPTH )
107 return stat_increase_network_count( node->children + foo, depth+STATS_NETWORK_NODE_BITWIDTH, ip );
108
109 node->counters[ foo ]++;
110 return 0;
111 }
112
stats_shift_down_network_count(stats_network_node ** node,int depth,int shift)113 static int stats_shift_down_network_count( stats_network_node **node, int depth, int shift ) {
114 int i, rest = 0;
115
116 if( !*node )
117 return 0;
118
119 for( i=0; i<STATS_NETWORK_NODE_COUNT; ++i )
120 if( depth < STATS_NETWORK_NODE_MAXDEPTH )
121 rest += stats_shift_down_network_count( (*node)->children + i, depth+STATS_NETWORK_NODE_BITWIDTH, shift );
122 else
123 rest += (*node)->counters[i] >>= shift;
124
125 if( !rest ) {
126 free( *node );
127 *node = NULL;
128 }
129
130 return rest;
131 }
132
stats_get_highscore_networks(stats_network_node * node,int depth,ot_ip6 node_value,size_t * scores,ot_ip6 * networks,int network_count,int limit)133 static 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, int limit ) {
134 size_t score = 0;
135 int i;
136
137 if( !node ) return 0;
138
139 if( depth < limit ) {
140 for( i=0; i<STATS_NETWORK_NODE_COUNT; ++i )
141 if( node->children[i] ) {
142 __STR(node_value,depth,i);
143 score += stats_get_highscore_networks( node->children[i], depth+STATS_NETWORK_NODE_BITWIDTH, node_value, scores, networks, network_count, limit );
144 }
145 return score;
146 }
147
148 if( depth > limit && depth < STATS_NETWORK_NODE_MAXDEPTH ) {
149 for( i=0; i<STATS_NETWORK_NODE_COUNT; ++i )
150 if( node->children[i] )
151 score += stats_get_highscore_networks( node->children[i], depth+STATS_NETWORK_NODE_BITWIDTH, node_value, scores, networks, network_count, limit );
152 return score;
153 }
154
155 if( depth > limit && depth == STATS_NETWORK_NODE_MAXDEPTH ) {
156 for( i=0; i<STATS_NETWORK_NODE_COUNT; ++i )
157 score += node->counters[i];
158 return score;
159 }
160
161 /* if( depth == limit ) */
162 for( i=0; i<STATS_NETWORK_NODE_COUNT; ++i ) {
163 int j=1;
164 size_t node_score;
165
166 if( depth == STATS_NETWORK_NODE_MAXDEPTH )
167 node_score = node->counters[i];
168 else
169 node_score = stats_get_highscore_networks( node->children[i], depth+STATS_NETWORK_NODE_BITWIDTH, node_value, scores, networks, network_count, limit );
170
171 score += node_score;
172
173 if( node_score <= scores[0] ) continue;
174
175 __STR(node_value,depth,i);
176 while( j < network_count && node_score > scores[j] ) ++j;
177 --j;
178
179 memcpy( scores, scores + 1, j * sizeof( *scores ) );
180 memcpy( networks, networks + 1, j * sizeof( *networks ) );
181 scores[ j ] = node_score;
182 memcpy( networks + j, node_value, sizeof( *networks ) );
183 }
184
185 return score;
186 }
187
stats_return_busy_networks(char * reply,stats_network_node * tree,int amount,int limit)188 static size_t stats_return_busy_networks( char * reply, stats_network_node *tree, int amount, int limit ) {
189 ot_ip6 networks[amount];
190 ot_ip6 node_value;
191 size_t scores[amount];
192 int i;
193 char * r = reply;
194
195 memset( scores, 0, sizeof( scores ) );
196 memset( networks, 0, sizeof( networks ) );
197 memset( node_value, 0, sizeof( node_value ) );
198
199 stats_get_highscore_networks( tree, 0, node_value, scores, networks, amount, limit );
200
201 r += sprintf( r, "Networks, limit /%d:\n", limit+STATS_NETWORK_NODE_BITWIDTH );
202 for( i=amount-1; i>=0; --i) {
203 if( scores[i] ) {
204 r += sprintf( r, "%08zd: ", scores[i] );
205 #ifdef WANT_V6
206 r += fmt_ip6c( r, networks[i] );
207 #else
208 r += fmt_ip4( r, networks[i]);
209 #endif
210 *r++ = '\n';
211 }
212 }
213 *r++ = '\n';
214
215 return r - reply;
216 }
217
stats_slash24s_txt(char * reply,size_t amount)218 static size_t stats_slash24s_txt( char *reply, size_t amount ) {
219 stats_network_node *slash24s_network_counters_root = NULL;
220 char *r=reply;
221 int bucket;
222 size_t i;
223
224 for( bucket=0; bucket<OT_BUCKET_COUNT; ++bucket ) {
225 ot_vector *torrents_list = mutex_bucket_lock( bucket );
226 for( i=0; i<torrents_list->size; ++i ) {
227 ot_peerlist *peer_list = ( ((ot_torrent*)(torrents_list->data))[i] ).peer_list;
228 ot_vector *bucket_list = &peer_list->peers;
229 int num_buckets = 1;
230
231 if( OT_PEERLIST_HASBUCKETS( peer_list ) ) {
232 num_buckets = bucket_list->size;
233 bucket_list = (ot_vector *)bucket_list->data;
234 }
235
236 while( num_buckets-- ) {
237 ot_peer *peers = (ot_peer*)bucket_list->data;
238 size_t numpeers = bucket_list->size;
239 while( numpeers-- )
240 if( stat_increase_network_count( &slash24s_network_counters_root, 0, (uintptr_t)(peers++) ) )
241 goto bailout_unlock;
242 ++bucket_list;
243 }
244 }
245 mutex_bucket_unlock( bucket, 0 );
246 if( !g_opentracker_running )
247 goto bailout_error;
248 }
249
250 /* The tree is built. Now analyze */
251 r += stats_return_busy_networks( r, slash24s_network_counters_root, amount, STATS_NETWORK_NODE_MAXDEPTH );
252 r += stats_return_busy_networks( r, slash24s_network_counters_root, amount, STATS_NETWORK_NODE_LIMIT );
253 goto success;
254
255 bailout_unlock:
256 mutex_bucket_unlock( bucket, 0 );
257 bailout_error:
258 r = reply;
259 success:
260 stats_shift_down_network_count( &slash24s_network_counters_root, 0, sizeof(int)*8-1 );
261
262 return r-reply;
263 }
264
265 #ifdef WANT_SPOT_WOODPECKER
266 static stats_network_node *stats_woodpeckers_tree;
267 static pthread_mutex_t g_woodpeckers_mutex = PTHREAD_MUTEX_INITIALIZER;
268
stats_return_woodpeckers(char * reply,int amount)269 static size_t stats_return_woodpeckers( char * reply, int amount ) {
270 char * r = reply;
271
272 pthread_mutex_lock( &g_woodpeckers_mutex );
273 r += stats_return_busy_networks( r, stats_woodpeckers_tree, amount, STATS_NETWORK_NODE_MAXDEPTH );
274 pthread_mutex_unlock( &g_woodpeckers_mutex );
275 return r-reply;
276 }
277 #endif
278
279 typedef struct {
280 unsigned long long torrent_count;
281 unsigned long long peer_count;
282 unsigned long long seed_count;
283 } torrent_stats;
284
torrent_statter(ot_torrent * torrent,uintptr_t data)285 static int torrent_statter( ot_torrent *torrent, uintptr_t data ) {
286 torrent_stats *stats = (torrent_stats*)data;
287 stats->torrent_count++;
288 stats->peer_count += torrent->peer_list->peer_count;
289 stats->seed_count += torrent->peer_list->seed_count;
290 return 0;
291 }
292
293 /* Converter function from memory to human readable hex strings */
to_hex(char * d,uint8_t * s)294 static char*to_hex(char*d,uint8_t*s){char*m="0123456789ABCDEF";char *t=d;char*e=d+40;while(d<e){*d++=m[*s>>4];*d++=m[*s++&15];}*d=0;return t;}
295
296 typedef struct { size_t val; ot_torrent * torrent; } ot_record;
297
298 /* Fetches stats from tracker */
stats_top_txt(char * reply,int amount)299 size_t stats_top_txt( char * reply, int amount ) {
300 size_t j;
301 ot_record top100s[100], top100c[100];
302 char *r = reply, hex_out[42];
303 int idx, bucket;
304
305 if( amount > 100 )
306 amount = 100;
307
308 byte_zero( top100s, sizeof( top100s ) );
309 byte_zero( top100c, sizeof( top100c ) );
310
311 for( bucket=0; bucket<OT_BUCKET_COUNT; ++bucket ) {
312 ot_vector *torrents_list = mutex_bucket_lock( bucket );
313 for( j=0; j<torrents_list->size; ++j ) {
314 ot_peerlist *peer_list = ( ((ot_torrent*)(torrents_list->data))[j] ).peer_list;
315 int idx = amount - 1; while( (idx >= 0) && ( peer_list->peer_count > top100c[idx].val ) ) --idx;
316 if ( idx++ != amount - 1 ) {
317 memmove( top100c + idx + 1, top100c + idx, ( amount - 1 - idx ) * sizeof( ot_record ) );
318 top100c[idx].val = peer_list->peer_count;
319 top100c[idx].torrent = (ot_torrent*)(torrents_list->data) + j;
320 }
321 idx = amount - 1; while( (idx >= 0) && ( peer_list->seed_count > top100s[idx].val ) ) --idx;
322 if ( idx++ != amount - 1 ) {
323 memmove( top100s + idx + 1, top100s + idx, ( amount - 1 - idx ) * sizeof( ot_record ) );
324 top100s[idx].val = peer_list->seed_count;
325 top100s[idx].torrent = (ot_torrent*)(torrents_list->data) + j;
326 }
327 }
328 mutex_bucket_unlock( bucket, 0 );
329 if( !g_opentracker_running )
330 return 0;
331 }
332
333 r += sprintf( r, "Top %d torrents by peers:\n", amount );
334 for( idx=0; idx<amount; ++idx )
335 if( top100c[idx].torrent )
336 r += sprintf( r, "\t%zd\t%s\n", top100c[idx].val, to_hex( hex_out, top100c[idx].torrent->hash) );
337 r += sprintf( r, "Top %d torrents by seeds:\n", amount );
338 for( idx=0; idx<amount; ++idx )
339 if( top100s[idx].torrent )
340 r += sprintf( r, "\t%zd\t%s\n", top100s[idx].val, to_hex( hex_out, top100s[idx].torrent->hash) );
341
342 return r - reply;
343 }
344
events_per_time(unsigned long long events,time_t t)345 static unsigned long events_per_time( unsigned long long events, time_t t ) {
346 return events / ( (unsigned int)t ? (unsigned int)t : 1 );
347 }
348
stats_connections_mrtg(char * reply)349 static size_t stats_connections_mrtg( char * reply ) {
350 ot_time t = time( NULL ) - ot_start_time;
351 return sprintf( reply,
352 "%llu\n%llu\n%i seconds (%i hours)\nopentracker connections, %lu conns/s :: %lu success/s.",
353 ot_overall_tcp_connections+ot_overall_udp_connections,
354 ot_overall_tcp_successfulannounces+ot_overall_udp_successfulannounces+ot_overall_udp_connects,
355 (int)t,
356 (int)(t / 3600),
357 events_per_time( ot_overall_tcp_connections+ot_overall_udp_connections, t ),
358 events_per_time( ot_overall_tcp_successfulannounces+ot_overall_udp_successfulannounces+ot_overall_udp_connects, t )
359 );
360 }
361
stats_udpconnections_mrtg(char * reply)362 static size_t stats_udpconnections_mrtg( char * reply ) {
363 ot_time t = time( NULL ) - ot_start_time;
364 return sprintf( reply,
365 "%llu\n%llu\n%i seconds (%i hours)\nopentracker udp4 stats, %lu conns/s :: %lu success/s.",
366 ot_overall_udp_connections,
367 ot_overall_udp_successfulannounces+ot_overall_udp_connects,
368 (int)t,
369 (int)(t / 3600),
370 events_per_time( ot_overall_udp_connections, t ),
371 events_per_time( ot_overall_udp_successfulannounces+ot_overall_udp_connects, t )
372 );
373 }
374
stats_tcpconnections_mrtg(char * reply)375 static size_t stats_tcpconnections_mrtg( char * reply ) {
376 time_t t = time( NULL ) - ot_start_time;
377 return sprintf( reply,
378 "%llu\n%llu\n%i seconds (%i hours)\nopentracker tcp4 stats, %lu conns/s :: %lu success/s.",
379 ot_overall_tcp_connections,
380 ot_overall_tcp_successfulannounces,
381 (int)t,
382 (int)(t / 3600),
383 events_per_time( ot_overall_tcp_connections, t ),
384 events_per_time( ot_overall_tcp_successfulannounces, t )
385 );
386 }
387
stats_scrape_mrtg(char * reply)388 static size_t stats_scrape_mrtg( char * reply ) {
389 time_t t = time( NULL ) - ot_start_time;
390 return sprintf( reply,
391 "%llu\n%llu\n%i seconds (%i hours)\nopentracker scrape stats, %lu scrape/s (tcp and udp)",
392 ot_overall_tcp_successfulscrapes,
393 ot_overall_udp_successfulscrapes,
394 (int)t,
395 (int)(t / 3600),
396 events_per_time( (ot_overall_tcp_successfulscrapes+ot_overall_udp_successfulscrapes), t )
397 );
398 }
399
stats_fullscrapes_mrtg(char * reply)400 static size_t stats_fullscrapes_mrtg( char * reply ) {
401 ot_time t = time( NULL ) - ot_start_time;
402 return sprintf( reply,
403 "%llu\n%llu\n%i seconds (%i hours)\nopentracker full scrape stats, %lu conns/s :: %lu bytes/s.",
404 ot_full_scrape_count * 1000,
405 ot_full_scrape_size,
406 (int)t,
407 (int)(t / 3600),
408 events_per_time( ot_full_scrape_count, t ),
409 events_per_time( ot_full_scrape_size, t )
410 );
411 }
412
stats_peers_mrtg(char * reply)413 static size_t stats_peers_mrtg( char * reply ) {
414 torrent_stats stats = {0,0,0};
415
416 iterate_all_torrents( torrent_statter, (uintptr_t)&stats );
417
418 return sprintf( reply, "%llu\n%llu\nopentracker serving %llu torrents\nopentracker",
419 stats.peer_count,
420 stats.seed_count,
421 stats.torrent_count
422 );
423 }
424
stats_torrents_mrtg(char * reply)425 static size_t stats_torrents_mrtg( char * reply )
426 {
427 size_t torrent_count = mutex_get_torrent_count();
428
429 return sprintf( reply, "%zd\n%zd\nopentracker serving %zd torrents\nopentracker",
430 torrent_count,
431 (size_t)0,
432 torrent_count
433 );
434 }
435
stats_httperrors_txt(char * reply)436 static size_t stats_httperrors_txt ( char * reply ) {
437 return sprintf( reply, "302 RED %llu\n400 ... %llu\n400 PAR %llu\n400 COM %llu\n403 IP %llu\n404 INV %llu\n500 SRV %llu\n",
438 ot_failed_request_counts[0], ot_failed_request_counts[1], ot_failed_request_counts[2],
439 ot_failed_request_counts[3], ot_failed_request_counts[4], ot_failed_request_counts[5],
440 ot_failed_request_counts[6] );
441 }
442
stats_return_renew_bucket(char * reply)443 static size_t stats_return_renew_bucket( char * reply ) {
444 char *r = reply;
445 int i;
446
447 for( i=0; i<OT_PEER_TIMEOUT; ++i )
448 r+=sprintf(r,"%02i %llu\n", i, ot_renewed[i] );
449 return r - reply;
450 }
451
stats_return_sync_mrtg(char * reply)452 static size_t stats_return_sync_mrtg( char * reply ) {
453 ot_time t = time( NULL ) - ot_start_time;
454 return sprintf( reply,
455 "%llu\n%llu\n%i seconds (%i hours)\nopentracker connections, %lu conns/s :: %lu success/s.",
456 ot_overall_sync_count,
457 0LL,
458 (int)t,
459 (int)(t / 3600),
460 events_per_time( ot_overall_tcp_connections+ot_overall_udp_connections, t ),
461 events_per_time( ot_overall_tcp_successfulannounces+ot_overall_udp_successfulannounces+ot_overall_udp_connects, t )
462 );
463 }
464
stats_return_completed_mrtg(char * reply)465 static size_t stats_return_completed_mrtg( char * reply ) {
466 ot_time t = time( NULL ) - ot_start_time;
467
468 return sprintf( reply,
469 "%llu\n%llu\n%i seconds (%i hours)\nopentracker, %lu completed/h.",
470 ot_overall_completed,
471 0LL,
472 (int)t,
473 (int)(t / 3600),
474 events_per_time( ot_overall_completed, t / 3600 )
475 );
476 }
477
478 #ifdef WANT_LOG_NUMWANT
479 extern unsigned long long numwants[201];
stats_return_numwants(char * reply)480 static size_t stats_return_numwants( char * reply ) {
481 char * r = reply;
482 int i;
483 for( i=0; i<=200; ++i )
484 r += sprintf( r, "%03d => %lld\n", i, numwants[i] );
485 return r-reply;
486 }
487 #endif
488
489 #ifdef WANT_FULLLOG_NETWORKS
stats_return_fulllog(int * iovec_entries,struct iovec ** iovector,char * r)490 static void stats_return_fulllog( int *iovec_entries, struct iovec **iovector, char *r ) {
491 ot_log *loglist = g_logchain_first, *llnext;
492 char * re = r + OT_STATS_TMPSIZE;
493
494 g_logchain_first = g_logchain_last = 0;
495
496 while( loglist ) {
497 if( r + ( loglist->size + 64 ) >= re ) {
498 r = iovec_fix_increase_or_free( iovec_entries, iovector, r, 32 * OT_STATS_TMPSIZE );
499 if( !r ) return;
500 re = r + 32 * OT_STATS_TMPSIZE;
501 }
502 r += sprintf( r, "%08ld: ", loglist->time );
503 r += fmt_ip6c( r, loglist->ip );
504 *r++ = '\n';
505 memcpy( r, loglist->data, loglist->size );
506 r += loglist->size;
507 *r++ = '\n';
508 *r++ = '*';
509 *r++ = '\n';
510 *r++ = '\n';
511
512 llnext = loglist->next;
513 free( loglist->data );
514 free( loglist );
515 loglist = llnext;
516 }
517 iovec_fixlast( iovec_entries, iovector, r );
518 }
519 #endif
520
stats_return_everything(char * reply)521 static size_t stats_return_everything( char * reply ) {
522 torrent_stats stats = {0,0,0};
523 int i;
524 char * r = reply;
525
526 iterate_all_torrents( torrent_statter, (uintptr_t)&stats );
527
528 r += sprintf( r, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" );
529 r += sprintf( r, "<stats>\n" );
530 r += sprintf( r, " <tracker_id>%" PRIu32 "</tracker_id>\n", g_tracker_id );
531 r += sprintf( r, " <version>\n" ); r += stats_return_tracker_version( r ); r += sprintf( r, " </version>\n" );
532 r += sprintf( r, " <uptime>%llu</uptime>\n", (unsigned long long)(time( NULL ) - ot_start_time) );
533 r += sprintf( r, " <torrents>\n" );
534 r += sprintf( r, " <count_mutex>%zd</count_mutex>\n", mutex_get_torrent_count() );
535 r += sprintf( r, " <count_iterator>%llu</count_iterator>\n", stats.torrent_count );
536 r += sprintf( r, " </torrents>\n" );
537 r += sprintf( r, " <peers>\n <count>%llu</count>\n </peers>\n", stats.peer_count );
538 r += sprintf( r, " <seeds>\n <count>%llu</count>\n </seeds>\n", stats.seed_count );
539 r += sprintf( r, " <completed>\n <count>%llu</count>\n </completed>\n", ot_overall_completed );
540 r += sprintf( r, " <connections>\n" );
541 r += sprintf( r, " <tcp>\n <accept>%llu</accept>\n <announce>%llu</announce>\n <scrape>%llu</scrape>\n </tcp>\n", ot_overall_tcp_connections, ot_overall_tcp_successfulannounces, ot_overall_tcp_successfulscrapes );
542 r += sprintf( r, " <udp>\n <overall>%llu</overall>\n <connect>%llu</connect>\n <announce>%llu</announce>\n <scrape>%llu</scrape>\n <missmatch>%llu</missmatch>\n </udp>\n", ot_overall_udp_connections, ot_overall_udp_connects, ot_overall_udp_successfulannounces, ot_overall_udp_successfulscrapes, ot_overall_udp_connectionidmissmatches );
543 r += sprintf( r, " <livesync>\n <count>%llu</count>\n </livesync>\n", ot_overall_sync_count );
544 r += sprintf( r, " </connections>\n" );
545 r += sprintf( r, " <debug>\n" );
546 r += sprintf( r, " <renew>\n" );
547 for( i=0; i<OT_PEER_TIMEOUT; ++i )
548 r += sprintf( r, " <count interval=\"%02i\">%llu</count>\n", i, ot_renewed[i] );
549 r += sprintf( r, " </renew>\n" );
550 r += sprintf( r, " <http_error>\n" );
551 for( i=0; i<CODE_HTTPERROR_COUNT; ++i )
552 r += sprintf( r, " <count code=\"%s\">%llu</count>\n", ot_failed_request_names[i], ot_failed_request_counts[i] );
553 r += sprintf( r, " </http_error>\n" );
554 r += sprintf( r, " <mutex_stall>\n <count>%llu</count>\n </mutex_stall>\n", ot_overall_stall_count );
555 r += sprintf( r, " </debug>\n" );
556 r += sprintf( r, "</stats>" );
557 return r - reply;
558 }
559
560 extern const char
561 *g_version_opentracker_c, *g_version_accesslist_c, *g_version_clean_c, *g_version_fullscrape_c, *g_version_http_c,
562 *g_version_iovec_c, *g_version_mutex_c, *g_version_stats_c, *g_version_udp_c, *g_version_vector_c,
563 *g_version_scan_urlencoded_query_c, *g_version_trackerlogic_c, *g_version_livesync_c, *g_version_rijndael_c;
564
stats_return_tracker_version(char * reply)565 size_t stats_return_tracker_version( char *reply ) {
566 return sprintf( reply, "%s%s%s%s%s%s%s%s%s%s%s%s%s%s",
567 g_version_opentracker_c, g_version_accesslist_c, g_version_clean_c, g_version_fullscrape_c, g_version_http_c,
568 g_version_iovec_c, g_version_mutex_c, g_version_stats_c, g_version_udp_c, g_version_vector_c,
569 g_version_scan_urlencoded_query_c, g_version_trackerlogic_c, g_version_livesync_c, g_version_rijndael_c );
570 }
571
return_stats_for_tracker(char * reply,int mode,int format)572 size_t return_stats_for_tracker( char *reply, int mode, int format ) {
573 (void) format;
574 switch( mode & TASK_TASK_MASK ) {
575 case TASK_STATS_CONNS:
576 return stats_connections_mrtg( reply );
577 case TASK_STATS_SCRAPE:
578 return stats_scrape_mrtg( reply );
579 case TASK_STATS_UDP:
580 return stats_udpconnections_mrtg( reply );
581 case TASK_STATS_TCP:
582 return stats_tcpconnections_mrtg( reply );
583 case TASK_STATS_FULLSCRAPE:
584 return stats_fullscrapes_mrtg( reply );
585 case TASK_STATS_COMPLETED:
586 return stats_return_completed_mrtg( reply );
587 case TASK_STATS_HTTPERRORS:
588 return stats_httperrors_txt( reply );
589 case TASK_STATS_VERSION:
590 return stats_return_tracker_version( reply );
591 case TASK_STATS_RENEW:
592 return stats_return_renew_bucket( reply );
593 case TASK_STATS_SYNCS:
594 return stats_return_sync_mrtg( reply );
595 #ifdef WANT_LOG_NUMWANT
596 case TASK_STATS_NUMWANTS:
597 return stats_return_numwants( reply );
598 #endif
599 default:
600 return 0;
601 }
602 }
603
stats_make(int * iovec_entries,struct iovec ** iovector,ot_tasktype mode)604 static void stats_make( int *iovec_entries, struct iovec **iovector, ot_tasktype mode ) {
605 char *r;
606
607 *iovec_entries = 0;
608 *iovector = NULL;
609 if( !( r = iovec_increase( iovec_entries, iovector, OT_STATS_TMPSIZE ) ) )
610 return;
611
612 switch( mode & TASK_TASK_MASK ) {
613 case TASK_STATS_TORRENTS: r += stats_torrents_mrtg( r ); break;
614 case TASK_STATS_PEERS: r += stats_peers_mrtg( r ); break;
615 case TASK_STATS_SLASH24S: r += stats_slash24s_txt( r, 128 ); break;
616 case TASK_STATS_TOP10: r += stats_top_txt( r, 10 ); break;
617 case TASK_STATS_TOP100:
618 r = iovec_fix_increase_or_free( iovec_entries, iovector, r, 4 * OT_STATS_TMPSIZE );
619 if( !r ) return;
620 r += stats_top_txt( r, 100 ); break;
621 case TASK_STATS_EVERYTHING: r += stats_return_everything( r ); break;
622 #ifdef WANT_SPOT_WOODPECKER
623 case TASK_STATS_WOODPECKERS: r += stats_return_woodpeckers( r, 128 ); break;
624 #endif
625 #ifdef WANT_FULLLOG_NETWORKS
626 case TASK_STATS_FULLLOG: stats_return_fulllog( iovec_entries, iovector, r );
627 return;
628 #endif
629 default:
630 iovec_free(iovec_entries, iovector);
631 return;
632 }
633 iovec_fixlast( iovec_entries, iovector, r );
634 }
635
stats_issue_event(ot_status_event event,PROTO_FLAG proto,uintptr_t event_data)636 void stats_issue_event( ot_status_event event, PROTO_FLAG proto, uintptr_t event_data ) {
637 switch( event ) {
638 case EVENT_ACCEPT:
639 if( proto == FLAG_TCP ) ot_overall_tcp_connections++; else ot_overall_udp_connections++;
640 #ifdef WANT_LOG_NETWORKS
641 stat_increase_network_count( &stats_network_counters_root, 0, event_data );
642 #endif
643 break;
644 case EVENT_ANNOUNCE:
645 if( proto == FLAG_TCP ) ot_overall_tcp_successfulannounces++; else ot_overall_udp_successfulannounces++;
646 break;
647 case EVENT_CONNECT:
648 if( proto == FLAG_TCP ) ot_overall_tcp_connects++; else ot_overall_udp_connects++;
649 break;
650 case EVENT_COMPLETED:
651 #ifdef WANT_SYSLOGS
652 if( event_data) {
653 struct ot_workstruct *ws = (struct ot_workstruct *)event_data;
654 char timestring[64];
655 char hash_hex[42], peerid_hex[42], ip_readable[64];
656 struct tm time_now;
657 time_t ttt;
658
659 time( &ttt );
660 localtime_r( &ttt, &time_now );
661 strftime( timestring, sizeof( timestring ), "%FT%T%z", &time_now );
662
663 to_hex( hash_hex, *ws->hash );
664 if( ws->peer_id )
665 to_hex( peerid_hex, (uint8_t*)ws->peer_id );
666 else {
667 *peerid_hex=0;
668 }
669
670 #ifdef WANT_V6
671 ip_readable[ fmt_ip6c( ip_readable, (char*)&ws->peer ) ] = 0;
672 #else
673 ip_readable[ fmt_ip4( ip_readable, (char*)&ws->peer ) ] = 0;
674 #endif
675 syslog( LOG_INFO, "time=%s event=completed info_hash=%s peer_id=%s ip=%s", timestring, hash_hex, peerid_hex, ip_readable );
676 }
677 #endif
678 ot_overall_completed++;
679 break;
680 case EVENT_SCRAPE:
681 if( proto == FLAG_TCP ) ot_overall_tcp_successfulscrapes++; else ot_overall_udp_successfulscrapes++;
682 break;
683 case EVENT_FULLSCRAPE:
684 ot_full_scrape_count++;
685 ot_full_scrape_size += event_data;
686 break;
687 case EVENT_FULLSCRAPE_REQUEST:
688 {
689 ot_ip6 *ip = (ot_ip6*)event_data; /* ugly hack to transfer ip to stats */
690 char _debug[512];
691 int off = snprintf( _debug, sizeof(_debug), "[%08d] scrp: ", (unsigned int)(g_now_seconds - ot_start_time)/60 );
692 off += fmt_ip6c( _debug+off, *ip );
693 off += snprintf( _debug+off, sizeof(_debug)-off, " - FULL SCRAPE\n" );
694 write( 2, _debug, off );
695 ot_full_scrape_request_count++;
696 }
697 break;
698 case EVENT_FULLSCRAPE_REQUEST_GZIP:
699 {
700 ot_ip6 *ip = (ot_ip6*)event_data; /* ugly hack to transfer ip to stats */
701 char _debug[512];
702 int off = snprintf( _debug, sizeof(_debug), "[%08d] scrp: ", (unsigned int)(g_now_seconds - ot_start_time)/60 );
703 off += fmt_ip6c(_debug+off, *ip );
704 off += snprintf( _debug+off, sizeof(_debug)-off, " - FULL SCRAPE\n" );
705 write( 2, _debug, off );
706 ot_full_scrape_request_count++;
707 }
708 break;
709 case EVENT_FAILED:
710 ot_failed_request_counts[event_data]++;
711 break;
712 case EVENT_RENEW:
713 ot_renewed[event_data]++;
714 break;
715 case EVENT_SYNC:
716 ot_overall_sync_count+=event_data;
717 break;
718 case EVENT_BUCKET_LOCKED:
719 ot_overall_stall_count++;
720 break;
721 #ifdef WANT_SPOT_WOODPECKER
722 case EVENT_WOODPECKER:
723 pthread_mutex_lock( &g_woodpeckers_mutex );
724 stat_increase_network_count( &stats_woodpeckers_tree, 0, event_data );
725 pthread_mutex_unlock( &g_woodpeckers_mutex );
726 break;
727 #endif
728 case EVENT_CONNID_MISSMATCH:
729 ++ot_overall_udp_connectionidmissmatches;
730 default:
731 break;
732 }
733 }
734
stats_cleanup()735 void stats_cleanup() {
736 #ifdef WANT_SPOT_WOODPECKER
737 pthread_mutex_lock( &g_woodpeckers_mutex );
738 stats_shift_down_network_count( &stats_woodpeckers_tree, 0, 1 );
739 pthread_mutex_unlock( &g_woodpeckers_mutex );
740 #endif
741 }
742
stats_worker(void * args)743 static void * stats_worker( void * args ) {
744 int iovec_entries;
745 struct iovec *iovector;
746
747 (void) args;
748
749 while( 1 ) {
750 ot_tasktype tasktype = TASK_STATS;
751 ot_taskid taskid = mutex_workqueue_poptask( &tasktype );
752 stats_make( &iovec_entries, &iovector, tasktype );
753 if( mutex_workqueue_pushresult( taskid, iovec_entries, iovector ) )
754 iovec_free( &iovec_entries, &iovector );
755 }
756 return NULL;
757 }
758
stats_deliver(int64 sock,int tasktype)759 void stats_deliver( int64 sock, int tasktype ) {
760 mutex_workqueue_pushtask( sock, tasktype );
761 }
762
763 static pthread_t thread_id;
stats_init()764 void stats_init( ) {
765 ot_start_time = g_now_seconds;
766 pthread_create( &thread_id, NULL, stats_worker, NULL );
767 }
768
stats_deinit()769 void stats_deinit( ) {
770 pthread_cancel( thread_id );
771 }
772
773 const char *g_version_stats_c = "$Source$: $Revision$\n";
774