ot_stats.c 20.3 KB
Newer Older
1
/* This software was written by Dirk Engling <erdgeist@erdgeist.org>
E
erdgeist 已提交
2
   It is considered beerware. Prost. Skol. Cheers or whatever.
E
erdgeist 已提交
3

E
erdgeist 已提交
4
   $id$ */
5 6 7

/* System */
#include <stdlib.h>
E
erdgeist 已提交
8
#include <arpa/inet.h>
9
#include <sys/types.h>
E
erdgeist 已提交
10
#include <sys/uio.h>
11 12 13
#include <sys/mman.h>
#include <stdio.h>
#include <string.h>
14
#include <pthread.h>
15 16 17

/* Libowfat */
#include "byte.h"
18
#include "io.h"
19 20 21 22

/* Opentracker */
#include "trackerlogic.h"
#include "ot_mutex.h"
23
#include "ot_iovec.h"
24 25
#include "ot_stats.h"

26 27 28 29 30 31
#ifndef NO_FULLSCRAPE_LOGGING
#define LOG_TO_STDERR( ... ) fprintf( stderr, __VA_ARGS__ )
#else
#define LOG_TO_STDERR( ... )
#endif

32 33 34 35
/* Forward declaration */
static void stats_make( int *iovec_entries, struct iovec **iovector, ot_tasktype mode );
#define OT_STATS_TMPSIZE 8192

36 37 38 39 40 41 42
/* Clumsy counters... to be rethought */
static unsigned long long ot_overall_tcp_connections = 0;
static unsigned long long ot_overall_udp_connections = 0;
static unsigned long long ot_overall_tcp_successfulannounces = 0;
static unsigned long long ot_overall_udp_successfulannounces = 0;
static unsigned long long ot_overall_tcp_successfulscrapes = 0;
static unsigned long long ot_overall_udp_successfulscrapes = 0;
43 44
static unsigned long long ot_overall_tcp_connects = 0;
static unsigned long long ot_overall_udp_connects = 0;
45
static unsigned long long ot_full_scrape_count = 0;
46
static unsigned long long ot_full_scrape_request_count = 0;
47
static unsigned long long ot_full_scrape_size = 0;
E
erdgeist 已提交
48
static unsigned long long ot_failed_request_counts[CODE_HTTPERROR_COUNT];
49
static unsigned long long ot_renewed[OT_PEER_TIMEOUT];
D
denis 已提交
50
static unsigned long long ot_overall_sync_count;
51

52 53
static time_t ot_start_time;

54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
#ifdef WANT_LOG_NETWORKS
#define STATS_NETWORK_NODE_BITWIDTH  8
#define STATS_NETWORK_NODE_MAXDEPTH  3

#define STATS_NETWORK_NODE_BITMASK ((1<<STATS_NETWORK_NODE_BITWIDTH)-1)
#define STATS_NETWORK_NODE_COUNT    (1<<STATS_NETWORK_NODE_BITWIDTH)

typedef union stats_network_node stats_network_node;
union stats_network_node {
  int                 counters[STATS_NETWORK_NODE_COUNT];
  stats_network_node *children[STATS_NETWORK_NODE_COUNT];
};

static stats_network_node *stats_network_counters_root = NULL;

static int stat_increase_network_count( stats_network_node **node, int depth, uint32_t ip ) {
  int foo = ( ip >> ( 32 - STATS_NETWORK_NODE_BITWIDTH * ( ++depth ) ) ) & STATS_NETWORK_NODE_BITMASK;

  if( !*node ) {
    *node = malloc( sizeof( stats_network_node ) );
    if( !*node )
      return -1;
    memset( *node, 0, sizeof( stats_network_node ) );
  }

  if( depth < STATS_NETWORK_NODE_MAXDEPTH )
    return stat_increase_network_count( &(*node)->children[ foo ], depth, ip );

  (*node)->counters[ foo ]++;
  return 0;
}

static int stats_shift_down_network_count( stats_network_node **node, int depth, int shift ) {
  int i, rest = 0;
  if( !*node ) return 0;

E
erdgeist 已提交
90
  if( ++depth == STATS_NETWORK_NODE_MAXDEPTH )
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
    for( i=0; i<STATS_NETWORK_NODE_COUNT; ++i ) {
      rest += ((*node)->counters[i]>>=shift);
    return rest;
  }

  for( i=0; i<STATS_NETWORK_NODE_COUNT; ++i ) {
    stats_network_node **childnode = &(*node)->children[i];
    int rest_val;

    if( !*childnode ) continue;

    rest += rest_val = stats_shift_down_network_count( childnode, depth, shift );

    if( rest_val ) continue;

    free( (*node)->children[i] );
    (*node)->children[i] = NULL;
  }

  return rest;
}

static void stats_get_highscore_networks( stats_network_node *node, int depth, uint32_t node_value, int *scores, uint32_t *networks, int network_count ) {
  int i;

  if( !node ) return;

  if( !depth++ ) {
    memset( scores, 0, sizeof( *scores ) * network_count );
    memset( networks, 0, sizeof( *networks ) * network_count );
  }

  if( depth < STATS_NETWORK_NODE_MAXDEPTH ) {
    for( i=0; i<STATS_NETWORK_NODE_COUNT; ++i )
      if( node->children[i] )
        stats_get_highscore_networks( node->children[i], depth, node_value | ( i << ( 32 - depth * STATS_NETWORK_NODE_BITWIDTH ) ), scores, networks, network_count );
  } else
    for( i=0; i<STATS_NETWORK_NODE_COUNT; ++i ) {
      int j=1;
      if( node->counters[i] <= scores[0] ) continue;

      while( (j<network_count) && (node->counters[i]>scores[j] ) ) ++j;
      --j;

      memmove( scores, scores + 1, j * sizeof( *scores ) );
      memmove( networks, networks + 1, j * sizeof( *networks ) );
      scores[ j ] = node->counters[ i ];
      networks[ j ] = node_value | ( i << ( 32 - depth * STATS_NETWORK_NODE_BITWIDTH ) );
  }
}

static size_t stats_return_busy_networks( char * reply ) {
  uint32_t networks[16];
  int      scores[16];
  int      i;
  char   * r = reply;

  stats_get_highscore_networks( stats_network_counters_root, 0, 0, scores, networks, 16 );

E
erdgeist 已提交
150
  for( i=15; i>=0; --i)
151 152 153 154 155 156 157
    r += sprintf( r, "%08i: %d.%d.%d.0/24\n", scores[i], (networks[i]>>24)&0xff, (networks[i]>>16)&0xff, (networks[i]>>8)&0xff );

  return r - reply;
}

#endif

158
/* Converter function from memory to human readable hex strings */
159
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;}
160 161 162 163

typedef struct { size_t val; ot_torrent * torrent; } ot_record;

/* Fetches stats from tracker */
164
size_t stats_top10_txt( char * reply ) {
165
  size_t    j;
166
  ot_record top10s[10], top10c[10];
167 168
  char     *r  = reply, hex_out[42];
  int       idx, bucket;
169

170 171
  byte_zero( top10s, sizeof( top10s ) );
  byte_zero( top10c, sizeof( top10c ) );
172 173 174 175 176

  for( bucket=0; bucket<OT_BUCKET_COUNT; ++bucket ) {
    ot_vector *torrents_list = mutex_bucket_lock( bucket );
    for( j=0; j<torrents_list->size; ++j ) {
      ot_peerlist *peer_list = ( ((ot_torrent*)(torrents_list->data))[j] ).peer_list;
177 178 179 180 181
      int idx = 9; while( (idx >= 0) && ( peer_list->peer_count > top10c[idx].val ) ) --idx;
      if ( idx++ != 9 ) {
        memmove( top10c + idx + 1, top10c + idx, ( 9 - idx ) * sizeof( ot_record ) );
        top10c[idx].val = peer_list->peer_count;
        top10c[idx].torrent = (ot_torrent*)(torrents_list->data) + j;
182
      }
183 184 185 186 187
      idx = 9; while( (idx >= 0) && ( peer_list->seed_count > top10s[idx].val ) ) --idx;
      if ( idx++ != 9 ) {
        memmove( top10s + idx + 1, top10s + idx, ( 9 - idx ) * sizeof( ot_record ) );
        top10s[idx].val = peer_list->seed_count;
        top10s[idx].torrent = (ot_torrent*)(torrents_list->data) + j;
188 189
      }
    }
190
    mutex_bucket_unlock( bucket, 0 );
E
erdgeist 已提交
191 192
    if( !g_opentracker_running )
      return 0;
193
  }
194

195 196 197 198 199 200 201 202
  r += sprintf( r, "Top 10 torrents by peers:\n" );
  for( idx=0; idx<10; ++idx )
    if( top10c[idx].torrent )
      r += sprintf( r, "\t%zd\t%s\n", top10c[idx].val, to_hex( hex_out, top10c[idx].torrent->hash) );
  r += sprintf( r, "Top 10 torrents by seeds:\n" );
  for( idx=0; idx<10; ++idx )
    if( top10s[idx].torrent )
      r += sprintf( r, "\t%zd\t%s\n", top10s[idx].val, to_hex( hex_out, top10s[idx].torrent->hash) );
203 204 205 206 207 208 209

  return r - reply;
}

/* This function collects 4096 /24s in 4096 possible
   malloc blocks
*/
210
static size_t stats_slash24s_txt( char * reply, size_t amount, uint32_t thresh ) {
211 212 213 214 215 216 217

#define NUM_TOPBITS 12
#define NUM_LOWBITS (24-NUM_TOPBITS)
#define NUM_BUFS    (1<<NUM_TOPBITS)
#define NUM_S24S    (1<<NUM_LOWBITS)
#define MSK_S24S    (NUM_S24S-1)

218 219
  uint32_t *counts[ NUM_BUFS ];
  uint32_t  slash24s[amount*2];  /* first dword amount, second dword subnet */
220
//  int       bucket;
221 222 223 224
  size_t    i, j, k, l;
  char     *r  = reply;

  byte_zero( counts, sizeof( counts ) );
225
  byte_zero( slash24s, amount * 2 * sizeof(uint32_t) );
226 227 228

  r += sprintf( r, "Stats for all /24s with more than %u announced torrents:\n\n", thresh );

229 230
#if 0
  /* XXX: TOOD: Doesn't work yet with new peer storage model */
231 232 233 234 235 236 237 238
  for( bucket=0; bucket<OT_BUCKET_COUNT; ++bucket ) {
    ot_vector *torrents_list = mutex_bucket_lock( bucket );
    for( j=0; j<torrents_list->size; ++j ) {
      ot_peerlist *peer_list = ( ((ot_torrent*)(torrents_list->data))[j] ).peer_list;
      for( k=0; k<OT_POOLS_COUNT; ++k ) {
        ot_peer *peers =    peer_list->peers[k].data;
        size_t   numpeers = peer_list->peers[k].size;
        for( l=0; l<numpeers; ++l ) {
239 240
          uint32_t s24 = ntohl(*(uint32_t*)(peers+l)) >> 8;
          uint32_t *count = counts[ s24 >> NUM_LOWBITS ];
241
          if( !count ) {
242
            count = malloc( sizeof(uint32_t) * NUM_S24S );
243
            if( !count ) {
244
              mutex_bucket_unlock( bucket, 0 );
245
              goto bailout_cleanup;
246
            }
247
            byte_zero( count, sizeof( uint32_t ) * NUM_S24S );
248 249 250 251 252 253
            counts[ s24 >> NUM_LOWBITS ] = count;
          }
          count[ s24 & MSK_S24S ]++;
        }
      }
    }
254
    mutex_bucket_unlock( bucket, 0 );
E
erdgeist 已提交
255 256
    if( !g_opentracker_running )
      goto bailout_cleanup;
257
  }
258
#endif
259 260 261

  k = l = 0; /* Debug: count allocated bufs */
  for( i=0; i < NUM_BUFS; ++i ) {
262
    uint32_t *count = counts[i];
263 264 265 266 267 268 269 270 271 272
    if( !counts[i] )
      continue;
    ++k; /* Debug: count allocated bufs */
    for( j=0; j < NUM_S24S; ++j ) {
      if( count[j] > thresh ) {
        /* This subnet seems to announce more torrents than the last in our list */
        int insert_pos = amount - 1;
        while( ( insert_pos >= 0 ) && ( count[j] > slash24s[ 2 * insert_pos ] ) )
          --insert_pos;
        ++insert_pos;
273
        memmove( slash24s + 2 * ( insert_pos + 1 ), slash24s + 2 * ( insert_pos ), 2 * sizeof( uint32_t ) * ( amount - insert_pos - 1 ) );
274 275 276 277 278 279 280 281 282 283 284 285 286 287
        slash24s[ 2 * insert_pos     ] = count[j];
        slash24s[ 2 * insert_pos + 1 ] = ( i << NUM_TOPBITS ) + j;
        if( slash24s[ 2 * amount - 2 ] > thresh )
          thresh = slash24s[ 2 * amount - 2 ];
      }
      if( count[j] ) ++l;
    }
    free( count );
  }

  r += sprintf( r, "Allocated bufs: %zd, used s24s: %zd\n", k, l );

  for( i=0; i < amount; ++i )
    if( slash24s[ 2*i ] >= thresh ) {
288
      uint32_t ip = slash24s[ 2*i +1 ];
289 290 291 292 293 294 295 296 297 298 299
      r += sprintf( r, "% 10ld %d.%d.%d.0/24\n", (long)slash24s[ 2*i ], (int)(ip >> 16), (int)(255 & ( ip >> 8 )), (int)(ip & 255) );
    }

  return r - reply;

  for( i=0; i < NUM_BUFS; ++i )
    free( counts[i] );

  return 0;
}

300 301 302 303 304 305 306 307
/*
 struct {
   size_t size
   size_t space
   size_t count
 }
 */

308 309 310 311 312 313 314 315 316
static unsigned long events_per_time( unsigned long long events, time_t t ) {
  return events / ( (unsigned int)t ? (unsigned int)t : 1 );
}

static size_t stats_connections_mrtg( char * reply ) {
  ot_time t = time( NULL ) - ot_start_time;
  return sprintf( reply,
    "%llu\n%llu\n%i seconds (%i hours)\nopentracker connections, %lu conns/s :: %lu success/s.",
    ot_overall_tcp_connections+ot_overall_udp_connections,
317
    ot_overall_tcp_successfulannounces+ot_overall_udp_successfulannounces+ot_overall_udp_connects,
318 319 320
    (int)t,
    (int)(t / 3600),
    events_per_time( ot_overall_tcp_connections+ot_overall_udp_connections, t ),
321
    events_per_time( ot_overall_tcp_successfulannounces+ot_overall_udp_successfulannounces+ot_overall_udp_connects, t )
322 323 324 325 326 327 328 329
  );
}

static size_t stats_udpconnections_mrtg( char * reply ) {
  ot_time t = time( NULL ) - ot_start_time;
  return sprintf( reply,
    "%llu\n%llu\n%i seconds (%i hours)\nopentracker udp4 stats, %lu conns/s :: %lu success/s.",
    ot_overall_udp_connections,
330
    ot_overall_udp_successfulannounces+ot_overall_udp_connects,
331 332 333
    (int)t,
    (int)(t / 3600),
    events_per_time( ot_overall_udp_connections, t ),
334
    events_per_time( ot_overall_udp_successfulannounces+ot_overall_udp_connects, t )
335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350
  );
}

static size_t stats_tcpconnections_mrtg( char * reply ) {
  time_t t = time( NULL ) - ot_start_time;
  return sprintf( reply,
    "%llu\n%llu\n%i seconds (%i hours)\nopentracker tcp4 stats, %lu conns/s :: %lu success/s.",
    ot_overall_tcp_connections,
    ot_overall_tcp_successfulannounces,
    (int)t,
    (int)(t / 3600),
    events_per_time( ot_overall_tcp_connections, t ),
    events_per_time( ot_overall_tcp_successfulannounces, t )
  );
}

351 352 353 354 355 356 357 358 359 360 361
static size_t stats_scrape_mrtg( char * reply ) {
  time_t t = time( NULL ) - ot_start_time;
  return sprintf( reply,
    "%llu\n%llu\n%i seconds (%i hours)\nopentracker scrape stats, %lu scrape/s (tcp and udp)",
    ot_overall_tcp_successfulscrapes,
    ot_overall_udp_successfulscrapes,
    (int)t,
    (int)(t / 3600),
    events_per_time( (ot_overall_tcp_successfulscrapes+ot_overall_udp_successfulscrapes), t )
  );
}
362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386

static size_t stats_fullscrapes_mrtg( char * reply ) {
  ot_time t = time( NULL ) - ot_start_time;
  return sprintf( reply,
    "%llu\n%llu\n%i seconds (%i hours)\nopentracker full scrape stats, %lu conns/s :: %lu bytes/s.",
    ot_full_scrape_count * 1000,
    ot_full_scrape_size,
    (int)t,
    (int)(t / 3600),
    events_per_time( ot_full_scrape_count, t ),
    events_per_time( ot_full_scrape_size, t )
  );
}

static size_t stats_peers_mrtg( char * reply ) {
  size_t    torrent_count = 0, peer_count = 0, seed_count = 0, j;
  int bucket;

  for( bucket=0; bucket<OT_BUCKET_COUNT; ++bucket ) {
    ot_vector *torrents_list = mutex_bucket_lock( bucket );
    torrent_count += torrents_list->size;
    for( j=0; j<torrents_list->size; ++j ) {
      ot_peerlist *peer_list = ( ((ot_torrent*)(torrents_list->data))[j] ).peer_list;
      peer_count += peer_list->peer_count; seed_count += peer_list->seed_count;
    }
387
    mutex_bucket_unlock( bucket, 0 );
E
erdgeist 已提交
388 389
    if( !g_opentracker_running )
      return 0;
390 391 392 393 394 395 396 397
  }
  return sprintf( reply, "%zd\n%zd\nopentracker serving %zd torrents\nopentracker",
    peer_count,
    seed_count,
    torrent_count
  );
}

D
denis 已提交
398 399
static size_t stats_startstop_mrtg( char * reply )
{
400
  size_t    torrent_count = mutex_get_torrent_count();
D
denis 已提交
401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421

  return sprintf( reply, "%zd\n%zd\nopentracker handling %zd torrents\nopentracker",
    (size_t)0,
    (size_t)0,
    torrent_count
  );
}

static size_t stats_toraddrem_mrtg( char * reply )
{
  size_t    peer_count = 0, j;
  int bucket;

  for( bucket=0; bucket<OT_BUCKET_COUNT; ++bucket )
  {
    ot_vector *torrents_list = mutex_bucket_lock( bucket );
    for( j=0; j<torrents_list->size; ++j )
    {
      ot_peerlist *peer_list = ( ((ot_torrent*)(torrents_list->data))[j] ).peer_list;
      peer_count += peer_list->peer_count;
    }
422
    mutex_bucket_unlock( bucket, 0 );
E
erdgeist 已提交
423 424
    if( !g_opentracker_running )
      return 0;
D
denis 已提交
425 426 427 428 429 430 431 432 433
  }

  return sprintf( reply, "%zd\n%zd\nopentracker handling %zd peers\nopentracker",
    (size_t)0,
    (size_t)0,
    peer_count
  );
}

D
denis 已提交
434 435
static size_t stats_torrents_mrtg( char * reply )
{
436
  size_t torrent_count = mutex_get_torrent_count();
D
denis 已提交
437 438 439 440 441 442 443 444

  return sprintf( reply, "%zd\n%zd\nopentracker serving %zd torrents\nopentracker",
    torrent_count,
    (size_t)0,
    torrent_count
  );
}

E
erdgeist 已提交
445
static size_t stats_httperrors_txt ( char * reply ) {
E
erdgeist 已提交
446
  return sprintf( reply, "302 RED %llu\n400 ... %llu\n400 PAR %llu\n400 COM %llu\n403 IP  %llu\n404 INV %llu\n500 SRV %llu\n",
E
erdgeist 已提交
447
  ot_failed_request_counts[0], ot_failed_request_counts[1], ot_failed_request_counts[2],
E
erdgeist 已提交
448 449
  ot_failed_request_counts[3], ot_failed_request_counts[4], ot_failed_request_counts[5],
  ot_failed_request_counts[6] );
E
erdgeist 已提交
450 451
}

452 453 454 455
static size_t stats_return_renew_bucket( char * reply ) {
  char *r = reply;
  int i;

456
  for( i=0; i<OT_PEER_TIMEOUT; ++i )
457 458 459 460
    r+=sprintf(r,"%02i %llu\n", i, ot_renewed[i] );
  return r - reply;
}

D
denis 已提交
461 462 463 464 465 466
static size_t stats_return_sync_mrtg( char * reply )
{
	ot_time t = time( NULL ) - ot_start_time;
	return sprintf( reply,
				   "%llu\n%llu\n%i seconds (%i hours)\nopentracker connections, %lu conns/s :: %lu success/s.",
				   ot_overall_sync_count,
E
erdgeist 已提交
467
				   0LL,
D
denis 已提交
468 469 470 471 472 473 474
				   (int)t,
				   (int)(t / 3600),
				   events_per_time( ot_overall_tcp_connections+ot_overall_udp_connections, t ),
				   events_per_time( ot_overall_tcp_successfulannounces+ot_overall_udp_successfulannounces+ot_overall_udp_connects, t )
				   );
}

E
erdgeist 已提交
475 476
extern const char
*g_version_opentracker_c, *g_version_accesslist_c, *g_version_clean_c, *g_version_fullscrape_c, *g_version_http_c,
477
*g_version_iovec_c, *g_version_mutex_c, *g_version_stats_c, *g_version_udp_c, *g_version_vector_c,
E
erdgeist 已提交
478
*g_version_scan_urlencoded_query_c, *g_version_trackerlogic_c, *g_version_livesync_c;
E
erdgeist 已提交
479 480

size_t stats_return_tracker_version( char *reply ) {
481
  return sprintf( reply, "%s%s%s%s%s%s%s%s%s%s%s%s%s",
E
erdgeist 已提交
482
  g_version_opentracker_c, g_version_accesslist_c, g_version_clean_c, g_version_fullscrape_c, g_version_http_c,
483
  g_version_iovec_c, g_version_mutex_c, g_version_stats_c, g_version_udp_c, g_version_vector_c,
E
erdgeist 已提交
484
  g_version_scan_urlencoded_query_c, g_version_trackerlogic_c, g_version_livesync_c );
E
erdgeist 已提交
485 486
}

487 488
size_t return_stats_for_tracker( char *reply, int mode, int format ) {
  format = format;
489
  switch( mode & TASK_TASK_MASK ) {
E
erdgeist 已提交
490
    case TASK_STATS_CONNS:
491
      return stats_connections_mrtg( reply );
492 493
    case TASK_STATS_SCRAPE:
      return stats_scrape_mrtg( reply );
E
erdgeist 已提交
494
    case TASK_STATS_UDP:
495
      return stats_udpconnections_mrtg( reply );
E
erdgeist 已提交
496
    case TASK_STATS_TCP:
497
      return stats_tcpconnections_mrtg( reply );
D
denis 已提交
498 499 500 501
    case TASK_STATS_TORADDREM:
      return stats_toraddrem_mrtg( reply );
    case TASK_STATS_STARTSTOP:
      return stats_startstop_mrtg( reply );
E
erdgeist 已提交
502
    case TASK_STATS_FULLSCRAPE:
503
      return stats_fullscrapes_mrtg( reply );
E
erdgeist 已提交
504 505
    case TASK_STATS_HTTPERRORS:
      return stats_httperrors_txt( reply );
506 507
    case TASK_STATS_VERSION:
      return stats_return_tracker_version( reply );
508 509
    case TASK_STATS_RENEW:
      return stats_return_renew_bucket( reply );
D
denis 已提交
510 511
    case TASK_STATS_SYNCS:
	  return stats_return_sync_mrtg( reply );
512
#ifdef WANT_LOG_NETWORKS
513 514
    case TASK_STATS_BUSY_NETWORKS:
      return stats_return_busy_networks( reply );
515
#endif
516 517 518 519 520
    default:
      return 0;
  }
}

521 522 523 524 525 526 527
static void stats_make( int *iovec_entries, struct iovec **iovector, ot_tasktype mode ) {
  char *r;

  *iovec_entries = 0;
  *iovector      = NULL;
  if( !( r = iovec_increase( iovec_entries, iovector, OT_STATS_TMPSIZE ) ) )
    return;
E
erdgeist 已提交
528

529 530
  switch( mode & TASK_TASK_MASK ) {
    case TASK_STATS_TORRENTS:    r += stats_torrents_mrtg( r );             break;
E
erdgeist 已提交
531
    case TASK_STATS_PEERS:       r += stats_peers_mrtg( r );                break;
532 533 534 535 536 537 538 539 540
    case TASK_STATS_SLASH24S:    r += stats_slash24s_txt( r, 25, 16 );      break;
    case TASK_STATS_TOP10:       r += stats_top10_txt( r );                 break;
    default:
      iovec_free(iovec_entries, iovector);
      return;
  }
  iovec_fixlast( iovec_entries, iovector, r );
}

E
erdgeist 已提交
541
void stats_issue_event( ot_status_event event, PROTO_FLAG proto, uint32_t event_data ) {
542 543
  switch( event ) {
    case EVENT_ACCEPT:
E
erdgeist 已提交
544
      if( proto == FLAG_TCP ) ot_overall_tcp_connections++; else ot_overall_udp_connections++;
545 546 547
#ifdef WANT_LOG_NETWORKS
      stat_increase_network_count( &stats_network_counters_root, 0, event_data );
#endif
548 549
      break;
    case EVENT_ANNOUNCE:
E
erdgeist 已提交
550
      if( proto == FLAG_TCP ) ot_overall_tcp_successfulannounces++; else ot_overall_udp_successfulannounces++;
551
      break;
552
   case EVENT_CONNECT:
E
erdgeist 已提交
553
      if( proto == FLAG_TCP ) ot_overall_tcp_connects++; else ot_overall_udp_connects++;
554
      break;
555
    case EVENT_SCRAPE:
E
erdgeist 已提交
556
      if( proto == FLAG_TCP ) ot_overall_tcp_successfulscrapes++; else ot_overall_udp_successfulscrapes++;
557 558 559 560
    case EVENT_FULLSCRAPE:
      ot_full_scrape_count++;
      ot_full_scrape_size += event_data;
      break;
561 562
    case EVENT_FULLSCRAPE_REQUEST:
      {
E
erdgeist 已提交
563
      uint8_t ip[4]; *(uint32_t*)ip = (uint32_t)proto; /* ugly hack to transfer ip to stats */
564
      LOG_TO_STDERR( "[%08d] scrp: %d.%d.%d.%d - FULL SCRAPE\n", (unsigned int)(g_now_seconds - ot_start_time)/60, ip[0], ip[1], ip[2], ip[3] );
565 566 567 568 569
      ot_full_scrape_request_count++;
      }
      break;
    case EVENT_FULLSCRAPE_REQUEST_GZIP:
      {
E
erdgeist 已提交
570
      uint8_t ip[4]; *(uint32_t*)ip = (uint32_t)proto; /* ugly hack to transfer ip to stats */
571
      LOG_TO_STDERR( "[%08d] scrp: %d.%d.%d.%d - FULL SCRAPE GZIP\n", (unsigned int)(g_now_seconds - ot_start_time)/60, ip[0], ip[1], ip[2], ip[3] );
572 573
      ot_full_scrape_request_count++;
      }
E
erdgeist 已提交
574 575 576 577
      break;
    case EVENT_FAILED:
      ot_failed_request_counts[event_data]++;
      break;
D
denis 已提交
578
	case EVENT_RENEW:
579 580
      ot_renewed[event_data]++;
      break;
D
denis 已提交
581 582 583
    case EVENT_SYNC:
      ot_overall_sync_count+=event_data;
	  break;
584 585 586
    default:
      break;
  }
587
}
588

589 590 591
static void * stats_worker( void * args ) {
  int iovec_entries;
  struct iovec *iovector;
E
erdgeist 已提交
592

593
  args = args;
E
erdgeist 已提交
594

595 596 597 598 599 600 601 602 603 604 605 606 607 608 609
  while( 1 ) {
    ot_tasktype tasktype = TASK_STATS;
    ot_taskid   taskid   = mutex_workqueue_poptask( &tasktype );
    stats_make( &iovec_entries, &iovector, tasktype );
    if( mutex_workqueue_pushresult( taskid, iovec_entries, iovector ) )
      iovec_free( &iovec_entries, &iovector );
  }
  return NULL;
}

void stats_deliver( int64 socket, int tasktype ) {
  mutex_workqueue_pushtask( socket, tasktype );
}

static pthread_t thread_id;
610
void stats_init( ) {
611
  ot_start_time = g_now_seconds;
612
  pthread_create( &thread_id, NULL, stats_worker, NULL );
613 614 615
}

void stats_deinit( ) {
616
  pthread_cancel( thread_id );
617 618
}

E
erdgeist 已提交
619
const char *g_version_stats_c = "$Source$: $Revision$\n";