summaryrefslogtreecommitdiff
path: root/ot_stats.c
diff options
context:
space:
mode:
Diffstat (limited to 'ot_stats.c')
-rw-r--r--ot_stats.c50
1 files changed, 28 insertions, 22 deletions
diff --git a/ot_stats.c b/ot_stats.c
index fa456c3..b2eaec9 100644
--- a/ot_stats.c
+++ b/ot_stats.c
@@ -320,7 +320,7 @@ typedef struct {
320/* Fetches stats from tracker */ 320/* Fetches stats from tracker */
321size_t stats_top_txt(char *reply, int amount) { 321size_t stats_top_txt(char *reply, int amount) {
322 size_t j; 322 size_t j;
323 ot_record top100s[100], top100c[100]; 323 ot_record top100s[100], top100c[100], top100l[100];
324 char *r = reply, hex_out[42]; 324 char *r = reply, hex_out[42];
325 int idx, bucket; 325 int idx, bucket;
326 326
@@ -329,14 +329,16 @@ size_t stats_top_txt(char *reply, int amount) {
329 329
330 byte_zero(top100s, sizeof(top100s)); 330 byte_zero(top100s, sizeof(top100s));
331 byte_zero(top100c, sizeof(top100c)); 331 byte_zero(top100c, sizeof(top100c));
332 byte_zero(top100l, sizeof(top100l));
332 333
333 for (bucket = 0; bucket < OT_BUCKET_COUNT; ++bucket) { 334 for (bucket = 0; bucket < OT_BUCKET_COUNT; ++bucket) {
334 ot_vector *torrents_list = mutex_bucket_lock(bucket); 335 ot_vector *torrents_list = mutex_bucket_lock(bucket);
335 for (j = 0; j < torrents_list->size; ++j) { 336 for (j = 0; j < torrents_list->size; ++j) {
336 ot_torrent *torrent = (ot_torrent *)(torrents_list->data) + j; 337 ot_torrent *torrent = (ot_torrent *)(torrents_list->data) + j;
337 size_t peer_count = torrent->peer_list6->peer_count + torrent->peer_list4->peer_count; 338 size_t peer_count = torrent->peer_list6->peer_count + torrent->peer_list4->peer_count;
338 size_t seed_count = torrent->peer_list6->seed_count + torrent->peer_list4->seed_count; 339 size_t seed_count = torrent->peer_list6->seed_count + torrent->peer_list4->seed_count;
339 idx = amount - 1; 340 size_t leech_count = peer_count - seed_count;
341 idx = amount - 1;
340 while ((idx >= 0) && (peer_count > top100c[idx].val)) 342 while ((idx >= 0) && (peer_count > top100c[idx].val))
341 --idx; 343 --idx;
342 if (idx++ != amount - 1) { 344 if (idx++ != amount - 1) {
@@ -352,6 +354,14 @@ size_t stats_top_txt(char *reply, int amount) {
352 memcpy(&top100s[idx].hash, &torrent->hash, sizeof(ot_hash)); 354 memcpy(&top100s[idx].hash, &torrent->hash, sizeof(ot_hash));
353 top100s[idx].val = seed_count; 355 top100s[idx].val = seed_count;
354 } 356 }
357 idx = amount - 1;
358 while ((idx >= 0) && (leech_count > top100l[idx].val))
359 --idx;
360 if (idx++ != amount - 1) {
361 memmove(top100l + idx + 1, top100l + idx, (amount - 1 - idx) * sizeof(ot_record));
362 memcpy(&top100l[idx].hash, &torrent->hash, sizeof(ot_hash));
363 top100l[idx].val = leech_count;
364 }
355 } 365 }
356 mutex_bucket_unlock(bucket, 0); 366 mutex_bucket_unlock(bucket, 0);
357 if (!g_opentracker_running) 367 if (!g_opentracker_running)
@@ -366,6 +376,10 @@ size_t stats_top_txt(char *reply, int amount) {
366 for (idx = 0; idx < amount; ++idx) 376 for (idx = 0; idx < amount; ++idx)
367 if (top100s[idx].val) 377 if (top100s[idx].val)
368 r += sprintf(r, "\t%zd\t%s\n", top100s[idx].val, to_hex(hex_out, top100s[idx].hash)); 378 r += sprintf(r, "\t%zd\t%s\n", top100s[idx].val, to_hex(hex_out, top100s[idx].hash));
379 r += sprintf(r, "Top %d torrents by leechers:\n", amount);
380 for (idx = 0; idx < amount; ++idx)
381 if (top100l[idx].val)
382 r += sprintf(r, "\t%zd\t%s\n", top100l[idx].val, to_hex(hex_out, top100l[idx].hash));
369 383
370 return r - reply; 384 return r - reply;
371} 385}
@@ -476,9 +490,9 @@ static void stats_return_fulllog(int *iovec_entries, struct iovec **iovector, ch
476 return; 490 return;
477 re = r + 32 * OT_STATS_TMPSIZE; 491 re = r + 32 * OT_STATS_TMPSIZE;
478 } 492 }
479 r += sprintf(r, "%08ld: ", loglist->time); 493 r += sprintf(r, "%08ld: ", loglist->time);
480 r += fmt_ip6c(r, loglist->ip); 494 r += fmt_ip6c(r, loglist->ip);
481 *r++ = '\n'; 495 *r++ = '\n';
482 memcpy(r, loglist->data, loglist->size); 496 memcpy(r, loglist->data, loglist->size);
483 r += loglist->size; 497 r += loglist->size;
484 *r++ = '\n'; 498 *r++ = '\n';
@@ -518,10 +532,8 @@ static size_t stats_return_everything(char *reply) {
518 r += sprintf(r, " <completed>\n <count>%llu</count>\n </completed>\n", ot_overall_completed); 532 r += sprintf(r, " <completed>\n <count>%llu</count>\n </completed>\n", ot_overall_completed);
519 r += sprintf(r, " <connections>\n"); 533 r += sprintf(r, " <connections>\n");
520 r += sprintf(r, " <tcp>\n <accept>%llu</accept>\n <announce>%llu</announce>\n <scrape>%llu</scrape>\n </tcp>\n", 534 r += sprintf(r, " <tcp>\n <accept>%llu</accept>\n <announce>%llu</announce>\n <scrape>%llu</scrape>\n </tcp>\n",
521 ot_overall_tcp_connections, ot_overall_tcp_successfulannounces, ot_overall_tcp_successfulscrapes); 535 ot_overall_tcp_connections, ot_overall_tcp_successfulannounces, ot_overall_tcp_successfulscrapes);
522 r += sprintf( 536 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",
523 r,
524 " <udp>\n <overall>%llu</overall>\n <connect>%llu</connect>\n <announce>%llu</announce>\n <scrape>%llu</scrape>\n <missmatch>%llu</missmatch>\n </udp>\n",
525 ot_overall_udp_connections, ot_overall_udp_connects, ot_overall_udp_successfulannounces, ot_overall_udp_successfulscrapes, 537 ot_overall_udp_connections, ot_overall_udp_connects, ot_overall_udp_successfulannounces, ot_overall_udp_successfulscrapes,
526 ot_overall_udp_connectionidmissmatches); 538 ot_overall_udp_connectionidmissmatches);
527 r += sprintf(r, " <livesync>\n <count>%llu</count>\n </livesync>\n", ot_overall_sync_count); 539 r += sprintf(r, " <livesync>\n <count>%llu</count>\n </livesync>\n", ot_overall_sync_count);
@@ -541,14 +553,10 @@ static size_t stats_return_everything(char *reply) {
541 return r - reply; 553 return r - reply;
542} 554}
543 555
544extern const char *g_version_opentracker_c, *g_version_accesslist_c, *g_version_clean_c, *g_version_fullscrape_c, *g_version_http_c, *g_version_iovec_c,
545 *g_version_mutex_c, *g_version_stats_c, *g_version_udp_c, *g_version_vector_c, *g_version_scan_urlencoded_query_c, *g_version_trackerlogic_c,
546 *g_version_livesync_c, *g_version_rijndael_c;
547
548size_t stats_return_tracker_version(char *reply) { 556size_t stats_return_tracker_version(char *reply) {
549 return sprintf(reply, "%s%s%s%s%s%s%s%s%s%s%s%s%s%s", g_version_opentracker_c, g_version_accesslist_c, g_version_clean_c, g_version_fullscrape_c, 557#define QUOTE(name) #name
550 g_version_http_c, g_version_iovec_c, g_version_mutex_c, g_version_stats_c, g_version_udp_c, g_version_vector_c, 558#define SQUOTE(name) QUOTE(name)
551 g_version_scan_urlencoded_query_c, g_version_trackerlogic_c, g_version_livesync_c, g_version_rijndael_c); 559 return sprintf(reply, "https://erdgeist.org/gitweb/opentracker/commit/?id=" SQUOTE(GIT_VERSION) "\n");
552} 560}
553 561
554size_t return_stats_for_tracker(char *reply, int mode, int format) { 562size_t return_stats_for_tracker(char *reply, int mode, int format) {
@@ -775,5 +783,3 @@ void stats_init() {
775void stats_deinit() { 783void stats_deinit() {
776 pthread_cancel(thread_id); 784 pthread_cancel(thread_id);
777} 785}
778
779const char *g_version_stats_c = "$Source$: $Revision$\n";