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

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

/* Libowfat */
#include "byte.h"
15
#include "io.h"
16 17 18 19 20 21

/* Opentracker */
#include "trackerlogic.h"
#include "ot_mutex.h"
#include "ot_stats.h"

22 23 24 25 26 27
#ifndef NO_FULLSCRAPE_LOGGING
#define LOG_TO_STDERR( ... ) fprintf( stderr, __VA_ARGS__ )
#else
#define LOG_TO_STDERR( ... )
#endif

28 29 30 31 32 33 34
/* 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;
35 36
static unsigned long long ot_overall_tcp_connects = 0;
static unsigned long long ot_overall_udp_connects = 0;
37
static unsigned long long ot_full_scrape_count = 0;
38
static unsigned long long ot_full_scrape_request_count = 0;
39
static unsigned long long ot_full_scrape_size = 0;
E
erdgeist 已提交
40
static unsigned long long ot_failed_request_counts[CODE_HTTPERROR_COUNT];
41

42 43
static time_t ot_start_time;

44
/* Converter function from memory to human readable hex strings */
45
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;}
46 47 48 49

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

/* Fetches stats from tracker */
50 51
size_t stats_top5_txt( char * reply ) {
  size_t    j;
52
  ot_record top5s[5], top5c[5];
53 54
  char     *r  = reply, hex_out[42];
  int       idx, bucket;
55 56 57 58 59 60 61 62

  byte_zero( top5s, sizeof( top5s ) );
  byte_zero( top5c, sizeof( top5c ) );

  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;
63 64 65 66 67 68 69 70 71 72 73
      int idx = 4; while( (idx >= 0) && ( peer_list->peer_count > top5c[idx].val ) ) --idx;
      if ( idx++ != 4 ) {
        memmove( top5c + idx + 1, top5c + idx, ( 4 - idx ) * sizeof( ot_record ) );
        top5c[idx].val = peer_list->peer_count;
        top5c[idx].torrent = (ot_torrent*)(torrents_list->data) + j;
      }
      idx = 4; while( (idx >= 0) && ( peer_list->seed_count > top5s[idx].val ) ) --idx;
      if ( idx++ != 4 ) {
        memmove( top5s + idx + 1, top5s + idx, ( 4 - idx ) * sizeof( ot_record ) );
        top5s[idx].val = peer_list->seed_count;
        top5s[idx].torrent = (ot_torrent*)(torrents_list->data) + j;
74 75 76 77
      }
    }
    mutex_bucket_unlock( bucket );
  }
78 79 80 81 82 83 84 85 86

  r += sprintf( r, "Top5 torrents by peers:\n" );
  for( idx=0; idx<5; ++idx )
    if( top5c[idx].torrent )
      r += sprintf( r, "\t%zd\t%s\n", top5c[idx].val, to_hex( hex_out, top5c[idx].torrent->hash) );
  r += sprintf( r, "Top5 torrents by seeds:\n" );
  for( idx=0; idx<5; ++idx )
    if( top5s[idx].torrent )
      r += sprintf( r, "\t%zd\t%s\n", top5s[idx].val, to_hex( hex_out, top5s[idx].torrent->hash) );
87 88 89 90 91 92 93

  return r - reply;
}

/* This function collects 4096 /24s in 4096 possible
   malloc blocks
*/
94
static size_t stats_slash24s_txt( char * reply, size_t amount, uint32_t thresh ) {
95 96 97 98 99 100 101

#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)

102 103
  uint32_t *counts[ NUM_BUFS ];
  uint32_t  slash24s[amount*2];  /* first dword amount, second dword subnet */
104 105 106 107 108
  int       bucket;
  size_t    i, j, k, l;
  char     *r  = reply;

  byte_zero( counts, sizeof( counts ) );
109
  byte_zero( slash24s, amount * 2 * sizeof(uint32_t) );
110 111 112 113 114 115 116 117 118 119 120

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

  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 ) {
121 122
          uint32_t s24 = ntohl(*(uint32_t*)(peers+l)) >> 8;
          uint32_t *count = counts[ s24 >> NUM_LOWBITS ];
123
          if( !count ) {
124
            count = malloc( sizeof(uint32_t) * NUM_S24S );
125 126
            if( !count )
              goto bailout_cleanup;
127
            byte_zero( count, sizeof( uint32_t ) * NUM_S24S );
128 129 130 131 132 133 134 135 136 137 138
            counts[ s24 >> NUM_LOWBITS ] = count;
          }
          count[ s24 & MSK_S24S ]++;
        }
      }
    }
    mutex_bucket_unlock( bucket );
  }

  k = l = 0; /* Debug: count allocated bufs */
  for( i=0; i < NUM_BUFS; ++i ) {
139
    uint32_t *count = counts[i];
140 141 142 143 144 145 146 147 148 149
    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;
150
        memmove( slash24s + 2 * ( insert_pos + 1 ), slash24s + 2 * ( insert_pos ), 2 * sizeof( uint32_t ) * ( amount - insert_pos - 1 ) );
151 152 153 154 155 156 157 158 159 160 161 162 163 164
        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 ) {
165
      uint32_t ip = slash24s[ 2*i +1 ];
166 167 168 169 170 171 172 173 174 175 176 177 178
      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;

bailout_cleanup:

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

  return 0;
}

179 180 181 182 183 184 185 186 187
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,
188
    ot_overall_tcp_successfulannounces+ot_overall_udp_successfulannounces+ot_overall_udp_connects,
189 190 191
    (int)t,
    (int)(t / 3600),
    events_per_time( ot_overall_tcp_connections+ot_overall_udp_connections, t ),
192
    events_per_time( ot_overall_tcp_successfulannounces+ot_overall_udp_successfulannounces+ot_overall_udp_connects, t )
193 194 195 196 197 198 199 200
  );
}

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,
201
    ot_overall_udp_successfulannounces+ot_overall_udp_connects,
202 203 204
    (int)t,
    (int)(t / 3600),
    events_per_time( ot_overall_udp_connections, t ),
205
    events_per_time( ot_overall_udp_successfulannounces+ot_overall_udp_connects, t )
206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
  );
}

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 )
  );
}

222 223 224 225 226 227 228 229 230 231 232
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 )
  );
}
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266

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;
    }
    mutex_bucket_unlock( bucket );
  }
  return sprintf( reply, "%zd\n%zd\nopentracker serving %zd torrents\nopentracker",
    peer_count,
    seed_count,
    torrent_count
  );
}

D
denis 已提交
267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308
static size_t stats_startstop_mrtg( char * reply )
{
  size_t    torrent_count = 0;
  int bucket;

  for( bucket=0; bucket<OT_BUCKET_COUNT; ++bucket )
  {
    ot_vector *torrents_list = mutex_bucket_lock( bucket );
    torrent_count += torrents_list->size;
    mutex_bucket_unlock( bucket );
  }

  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;
    }
    mutex_bucket_unlock( bucket );
  }

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

D
denis 已提交
309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327
static size_t stats_torrents_mrtg( char * reply )
{
  size_t torrent_count = 0;
  int bucket;

  for( bucket=0; bucket<OT_BUCKET_COUNT; ++bucket )
  {
    ot_vector *torrents_list = mutex_bucket_lock( bucket );
    torrent_count += torrents_list->size;
    mutex_bucket_unlock( bucket );
  }

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

E
erdgeist 已提交
328
static size_t stats_httperrors_txt ( char * reply ) {
E
erdgeist 已提交
329
  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 已提交
330
  ot_failed_request_counts[0], ot_failed_request_counts[1], ot_failed_request_counts[2], 
E
erdgeist 已提交
331 332
  ot_failed_request_counts[3], ot_failed_request_counts[4], ot_failed_request_counts[5],
  ot_failed_request_counts[6] );
E
erdgeist 已提交
333 334
}

335 336 337
size_t return_stats_for_tracker( char *reply, int mode, int format ) {
  format = format;
  switch( mode ) {
E
erdgeist 已提交
338
    case TASK_STATS_CONNS:
339
      return stats_connections_mrtg( reply );
340 341
    case TASK_STATS_SCRAPE:
      return stats_scrape_mrtg( reply );
E
erdgeist 已提交
342
    case TASK_STATS_UDP:
343
      return stats_udpconnections_mrtg( reply );
E
erdgeist 已提交
344
    case TASK_STATS_TCP:
345
      return stats_tcpconnections_mrtg( reply );
E
erdgeist 已提交
346
    case TASK_STATS_PEERS:
347
      return stats_peers_mrtg( reply );
D
denis 已提交
348 349
    case TASK_STATS_TORRENTS:
      return stats_torrents_mrtg( reply );
D
denis 已提交
350 351 352 353
    case TASK_STATS_TORADDREM:
      return stats_toraddrem_mrtg( reply );
    case TASK_STATS_STARTSTOP:
      return stats_startstop_mrtg( reply );
E
erdgeist 已提交
354
    case TASK_STATS_SLASH24S:
355
      return stats_slash24s_txt( reply, 25, 16 );
E
erdgeist 已提交
356
    case TASK_STATS_TOP5:
357
      return stats_top5_txt( reply );
E
erdgeist 已提交
358
    case TASK_STATS_FULLSCRAPE:
359
      return stats_fullscrapes_mrtg( reply );
E
erdgeist 已提交
360 361
    case TASK_STATS_HTTPERRORS:
      return stats_httperrors_txt( reply );
362 363 364 365 366 367 368 369 370 371 372 373 374
    default:
      return 0;
  }
}

void stats_issue_event( ot_status_event event, int is_tcp, size_t event_data ) {
  switch( event ) {
    case EVENT_ACCEPT:
      if( is_tcp ) ot_overall_tcp_connections++; else ot_overall_udp_connections++;
      break;
    case EVENT_ANNOUNCE:
      if( is_tcp ) ot_overall_tcp_successfulannounces++; else ot_overall_udp_successfulannounces++;
      break;
375 376 377
   case EVENT_CONNECT:
      if( is_tcp ) ot_overall_tcp_connects++; else ot_overall_udp_connects++;
      break;
378 379 380 381 382 383
    case EVENT_SCRAPE:
      if( is_tcp ) ot_overall_tcp_successfulscrapes++; else ot_overall_udp_successfulscrapes++;
    case EVENT_FULLSCRAPE:
      ot_full_scrape_count++;
      ot_full_scrape_size += event_data;
      break;
384 385
    case EVENT_FULLSCRAPE_REQUEST:
      {
E
erdgeist 已提交
386
      unsigned char ip[4]; *(int*)ip = is_tcp; /* ugly hack to transfer ip to stats */
387 388 389 390 391 392
      LOG_TO_STDERR( "[%08d] scrp: %d.%d.%d.%d - FULL SCRAPE\n", (unsigned int)(g_now - ot_start_time), ip[0], ip[1], ip[2], ip[3] );
      ot_full_scrape_request_count++;
      }
      break;
    case EVENT_FULLSCRAPE_REQUEST_GZIP:
      {
E
erdgeist 已提交
393
      unsigned char ip[4]; *(int*)ip = is_tcp; /* ugly hack to transfer ip to stats */
394 395 396
      LOG_TO_STDERR( "[%08d] scrp: %d.%d.%d.%d - FULL SCRAPE GZIP\n", (unsigned int)(g_now - ot_start_time), ip[0], ip[1], ip[2], ip[3] );
      ot_full_scrape_request_count++;
      }
E
erdgeist 已提交
397 398 399 400
      break;
    case EVENT_FAILED:
      ot_failed_request_counts[event_data]++;
      break;
401 402 403 404 405
    case EVENT_SYNC_IN_REQUEST:
    case EVENT_SYNC_IN:
    case EVENT_SYNC_OUT_REQUEST:
    case EVENT_SYNC_OUT:
      break;
406 407 408
    default:
      break;
  }
409
}
410 411 412 413 414 415 416 417 418

void stats_init( ) {
  ot_start_time = g_now;
}

void stats_deinit( ) {

}