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