forked from jimsynz/Culvert
-
Notifications
You must be signed in to change notification settings - Fork 1
/
netstats.pike
executable file
·332 lines (302 loc) · 8.97 KB
/
netstats.pike
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
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
90
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
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
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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
#!/usr/local/bin/pike -Mlib/
/* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is IP.v4 Public Module.
*
* The Initial Developer of the Original Code is
* James Harton, <james@mashd.cc>.
* Portions created by the Initial Developer are Copyright (C) 2005-2009
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
*
* ***** END LICENSE BLOCK ***** */
#ifdef DISABLE_THREADS
#undef ENABLE_THREADS
constant threads = 0;
#else
#define ENABLE_THREADS
constant threads = 10;
#endif
constant snaplen = 68;
constant options =
({
({ "interface", Getopt.HAS_ARG, ({ "-i", "--interface" }) }),
({ "logfile", Getopt.HAS_ARG, ({ "-l", "--log-file" }) }),
({ "filter", Getopt.HAS_ARG, ({ "-f", "--filter" }) }),
({ "snaplen", Getopt.HAS_ARG, ({ "-s", "--snaplen" }) }),
({ "help", Getopt.NO_ARG, ({ "-h", "--help" }) }),
({ "threads", Getopt.HAS_ARG, ({ "-t", "--threads" }) }),
});
object five_minute_avg;
object one_hour_avg;
object one_day_avg;
object one_month_avg;
int main(int argc, array argv) {
mapping args = (mapping)Getopt.find_all_options(argv, options, 0);
if (args->help) {
werror("No help for you. Sorry.\n\n");
return 1;
}
if (!args->interface) {
werror("You must specify an interface to listen on.\n\n");
return 1;
}
if (args->threads)
args->threads = (int)args->threads;
if (args->snaplen)
args->snaplen = (int)args->snaplen;
#ifdef ENABLE_THREADS
if ((!args->threads) || (args->threads == 0))
args->threads = threads;
#endif
object queue = Thread.Fifo(args->threads);
object fe = IP.Flow.Engine();
#ifdef ENABLE_THREADS
write("Culvert %d.%d-%s started with %d worker threads.\n\n", Culvert.MAJOR, Culvert.MINOR, Culvert.BRANCH, args->threads);
#else
write("Culvert %d.%d-%s started.\n\n", Culvert.MAJOR, Culvert.MINOR, Culvert.BRANCH);
#endif
five_minute_avg = SampleJar(5 * 60);
one_hour_avg = SampleJar(60 * 60);
one_day_avg = SampleJar(60 * 60 * 24);
one_month_avg = SampleJar(60 * 60 * 24 * 31);
fe->set_expired_flow_cb(time_tcp_session);
Thread.thread_create(setup_time_report, args->logfile);
object cap = Public.Network.Pcap.Pcap();
cap->set_capture_length(args->snaplen||snaplen);
cap->set_capture_timeout(10);
mixed err = catch(cap->open_live(args->interface));
if (err) {
werror("Unable to open packet capture interface:\n");
array tmp = (err[0] / ":")[1..];
werror("%{%s%}\n", tmp);
return 1;
}
if (args->filter)
cap->set_filter(args->filter);
else
// We're only interested in TCP session setup.
cap->set_filter("tcp");
cap->set_promisc(1);
if (args->threads) {
for (int i=0; i < args->threads; i++) {
Thread.thread_create(dequeue, queue);
}
Thread.thread_create(dispatch, cap, queue, fe);
}
else {
werror("Can't run with no threads, for now.\n\n");
return 1;
dispatch(cap,queue,fe);
}
return -1;
}
void dispatch(object cap, object queue, object fe) {
while(1) {
//for (int i; i < 10; i++) {
mixed c = cap->next();
if (mappingp(c)) {
queue->write(({ desplat, fe, c }));
}
}
}
void dequeue(object queue) {
while(1) {
mixed c = queue->read();
if (sizeof(c) > 1)
c[0](@c[1..]);
else
c[0]();
}
}
void precache_dns(object flow, object queue) {
#ifdef ENABLE_THREADS
queue->write(({ IP.Flow.DNSCache->lookup_ip,(string)flow->src }));
queue->write(({ IP.Flow.DNSCache->lookup_ip,(string)flow->dst }));
#else
catch(IP.Flow.DNSCache->lookup_ip((string)flow->src));
catch(IP.Flow.DNSCache->lookup_ip((string)flow->dst));
#endif
}
void log(object flow, object queue, object logfile, int dns) {
#ifdef ENABLE_THREADS
queue->write(({ lambda() { catch(logfile->write("%s\n", flow->english(dns, 1))); } }));
// queue->write(({ lambda() { time_tcp_session(flow); }}));
time_tcp_session(flow);
#else
catch(logfile->write("%s\n", flow->english(dns, 1)));
time_tcp_session(flow);
#endif
}
void desplat(object fe, mixed ... args) {
object frame = Ethernet.Packet(args[0]->data);
if (frame->type->name() == "IPv4") {
object packet;
mixed err = catch(packet = IP.v4.Packet(frame->data, 1));
if (err)
write("%O\n", err);
else {
if (packet->protocol->name() == "IPv6") {
object sixtofour;
mixed err = catch(sixtofour = IP.v6.Packet(packet->data, 1));
if (err)
write("%O\n", err);
catch(fe->packet(sixtofour));
}
else
catch(fe->packet(packet));
}
}
else if (frame->type->name() == "IPv6") {
object packet;
mixed err = catch(packet = IP.v6.Packet(frame->data, 1));
if (err)
write("%O\n", err);
else
catch(fe->packet(packet));
}
}
void time_tcp_session(object flow) {
if (sizeof(flow->conversation) > 1) {
// If we started capturing in the middle of a flow then just ignore it.
if (flow->conversation[0]->packet->flags["SYN"]) {
float first = flow->conversation[0]->time;
float second;
for (int i=1; i < sizeof(flow->conversation); i++) {
if (flow->conversation[i]->packet->flags["ACK"]) {
second = flow->conversation[i]->time;
break;
}
}
if (second > first) {
// * 100 to get ms instead of s.
float setup_time = (second - first) * 100;
one_month_avg->add(setup_time);
one_day_avg->add(setup_time);
one_hour_avg->add(setup_time);
five_minute_avg->add(setup_time);
}
// else just discard this session, it ain't much
// use to us.
}
}
}
void setup_time_report(void|string logfile) {
if (stringp(logfile)) {
while (1) {
object log = Stdio.File(logfile, "cwa");
log->write("%s: 5m (%f ms, %f ms, %f ms, %d), 1h (%f ms, %f ms, %f ms, %d), 1d (%f ms, %f ms, %f ms, %d), 1M (%f ms, %f ms, %f ms, %d)\n",
Calendar.now()->format_time(),
five_minute_avg->min(), five_minute_avg->max(), five_minute_avg->avg(), five_minute_avg->count(),
one_hour_avg->min(), one_hour_avg->max(), one_hour_avg->avg(), one_hour_avg->count(),
one_day_avg->min(), one_day_avg->max(), one_day_avg->avg(), one_day_avg->count(),
one_month_avg->min(), one_month_avg->max(), one_month_avg->avg(), one_month_avg->count()
);
sleep(30);
}
}
else {
write("[time]: [sample length]([min] ms, [max] ms, [avg] ms, [count])\n");
while (1) {
write("%s: 5m (%f ms, %f ms, %f ms, %d), 1h (%f ms, %f ms, %f ms, %d), 1d (%f ms, %f ms, %f ms, %d), 1M (%f ms, %f ms, %f ms, %d)\n",
Calendar.now()->format_time(),
five_minute_avg->min(), five_minute_avg->max(), five_minute_avg->avg(), five_minute_avg->count(),
one_hour_avg->min(), one_hour_avg->max(), one_hour_avg->avg(), one_hour_avg->count(),
one_day_avg->min(), one_day_avg->max(), one_day_avg->avg(), one_day_avg->count(),
one_month_avg->min(), one_month_avg->max(), one_month_avg->avg(), one_month_avg->count()
);
sleep(2);
}
}
}
class SampleJar {
int age_limit;
array samples;
int start_time;
object lock = Thread.Mutex();
void create(int limit) {
// Not sure about passing Calendar.Hour, etc, so let's just work
// in integer seconds. Easier.
object key = lock->lock();
age_limit = limit;
samples = ({});
start_time = time();
}
int add(float sample) {
// Add a sample to the sample pool.
object key = lock->lock();
samples += ({ ([ "time" : (start_time + time(start_time)), "sample" : sample ]) });
destruct(key);
return sizeof(samples);
}
int clean() {
// Removes too old samples from the jar.
// We can safely assume that older samples
// are at the beginning of the array.
int culled;
if (sizeof(samples) > 0) {
mapping sample = samples[0];
while (sample->time < (start_time + time(start_time) - age_limit)) {
// remove.
culled++;
object key = lock->lock();
samples = samples[1..];
destruct(key);
// Escape hatch for accidentally empty sample jars.
if (sizeof(samples) > 0)
sample = samples[0];
else
sample = ([ "time" : 0 ]);
}
}
return culled;
}
float min() {
clean();
if (sizeof(samples)) {
array x = samples->sample;
return sort(x)[0];
}
else {
return 0.0;
}
}
float max() {
clean();
if (sizeof(samples)) {
array x = samples->sample;
return reverse(sort(x))[0];
}
else {
return 0.0;
}
}
float avg() {
clean();
if (sizeof(samples)) {
array x = samples->sample;
return `+(@x) / sizeof(x);
}
else {
return 0.0;
}
}
int count() {
return sizeof(samples);
}
string _sprintf() {
return sprintf("SampleJar(/* %d seconds */)", age_limit);
}
}