/
logstats.cc
2582 lines (2298 loc) · 83.1 KB
/
logstats.cc
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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
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
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/** @file
A brief file description
@section license License
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (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.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#include "libts.h"
#undef std // FIXME: remove dependency on the STL
#include "ink_config.h"
#include "ink_file.h"
#include "I_Layout.h"
#include "I_Version.h"
// Includes and namespaces etc.
#include "LogStandalone.cc"
#include "LogObject.h"
#include "hdrs/HTTP.h"
#include <math.h>
#include <sys/utsname.h>
#if defined(solaris)
#include <sys/types.h>
#include <unistd.h>
#endif
#include <iostream>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <string>
#include <algorithm>
#include <vector>
#include <list>
#include <functional>
#include <fcntl.h>
#ifndef _XOPEN_SOURCE
#define _XOPEN_SOURCE 600
#endif
using namespace std;
// Constants, please update the VERSION number when you make a new build!!!
#define PROGRAM_NAME "traffic_logstats"
const int MAX_LOGBUFFER_SIZE = 65536;
const int DEFAULT_LINE_LEN = 78;
const double LOG10_1024 = 3.0102999566398116;
const int MAX_ORIG_STRING = 4096;
// Optimizations for "strcmp()", treat some fixed length (3 or 4 bytes) strings
// as integers.
const int GET_AS_INT = 5522759;
const int PUT_AS_INT = 5526864;
const int HEAD_AS_INT = 1145128264;
const int POST_AS_INT = 1414745936;
const int TEXT_AS_INT = 1954047348;
const int JPEG_AS_INT = 1734701162;
const int JPG_AS_INT = 6778986;
const int GIF_AS_INT = 6711655;
const int PNG_AS_INT = 6778480;
const int BMP_AS_INT = 7368034;
const int CSS_AS_INT = 7566179;
const int XML_AS_INT = 7105912;
const int HTML_AS_INT = 1819112552;
const int ZIP_AS_INT = 7367034;
const int JAVA_AS_INT = 1635148138; // For "javascript"
const int X_JA_AS_INT = 1634348408; // For "x-javascript"
const int RSSp_AS_INT = 728986482; // For "RSS+"
const int PLAI_AS_INT = 1767992432; // For "plain"
const int IMAG_AS_INT = 1734438249; // For "image"
const int HTTP_AS_INT = 1886680168; // For "http" followed by "s://" or "://"
// Store our "state" (position in log file etc.)
struct LastState
{
off_t offset;
ino_t st_ino;
};
static LastState last_state;
// Store the collected counters and stats, per Origin Server, URL or total
struct StatsCounter
{
int64_t count;
int64_t bytes;
};
struct ElapsedStats
{
int min;
int max;
float avg;
float stddev;
};
struct OriginStats
{
const char *server;
StatsCounter total;
struct
{
struct
{
ElapsedStats hit;
ElapsedStats ims;
ElapsedStats refresh;
ElapsedStats other;
ElapsedStats total;
} hits;
struct
{
ElapsedStats miss;
ElapsedStats ims;
ElapsedStats refresh;
ElapsedStats other;
ElapsedStats total;
} misses;
} elapsed;
struct
{
struct
{
StatsCounter hit;
StatsCounter ims;
StatsCounter refresh;
StatsCounter other;
StatsCounter total;
} hits;
struct
{
StatsCounter miss;
StatsCounter ims;
StatsCounter refresh;
StatsCounter other;
StatsCounter total;
} misses;
struct
{
StatsCounter client_abort;
StatsCounter connect_fail;
StatsCounter invalid_req;
StatsCounter unknown;
StatsCounter other;
StatsCounter total;
} errors;
StatsCounter other;
} results;
struct
{
StatsCounter c_000; // Bad
StatsCounter c_100;
StatsCounter c_200;
StatsCounter c_201;
StatsCounter c_202;
StatsCounter c_203;
StatsCounter c_204;
StatsCounter c_205;
StatsCounter c_206;
StatsCounter c_2xx;
StatsCounter c_300;
StatsCounter c_301;
StatsCounter c_302;
StatsCounter c_303;
StatsCounter c_304;
StatsCounter c_305;
StatsCounter c_307;
StatsCounter c_3xx;
StatsCounter c_400;
StatsCounter c_401;
StatsCounter c_402;
StatsCounter c_403;
StatsCounter c_404;
StatsCounter c_405;
StatsCounter c_406;
StatsCounter c_407;
StatsCounter c_408;
StatsCounter c_409;
StatsCounter c_410;
StatsCounter c_411;
StatsCounter c_412;
StatsCounter c_413;
StatsCounter c_414;
StatsCounter c_415;
StatsCounter c_416;
StatsCounter c_417;
StatsCounter c_4xx;
StatsCounter c_500;
StatsCounter c_501;
StatsCounter c_502;
StatsCounter c_503;
StatsCounter c_504;
StatsCounter c_505;
StatsCounter c_5xx;
} codes;
struct
{
StatsCounter direct;
StatsCounter none;
StatsCounter sibling;
StatsCounter parent;
StatsCounter empty;
StatsCounter invalid;
StatsCounter other;
} hierarchies;
struct
{
StatsCounter http;
StatsCounter https;
StatsCounter none;
StatsCounter other;
} schemes;
struct
{
StatsCounter options;
StatsCounter get;
StatsCounter head;
StatsCounter post;
StatsCounter put;
StatsCounter del;
StatsCounter trace;
StatsCounter connect;
StatsCounter purge;
StatsCounter none;
StatsCounter other;
} methods;
struct
{
struct
{
StatsCounter plain;
StatsCounter xml;
StatsCounter html;
StatsCounter css;
StatsCounter javascript;
StatsCounter other;
StatsCounter total;
} text;
struct
{
StatsCounter jpeg;
StatsCounter gif;
StatsCounter png;
StatsCounter bmp;
StatsCounter other;
StatsCounter total;
} image;
struct
{
StatsCounter shockwave_flash;
StatsCounter quicktime;
StatsCounter javascript;
StatsCounter zip;
StatsCounter other;
StatsCounter rss_xml;
StatsCounter rss_atom;
StatsCounter rss_other;
StatsCounter total;
} application;
struct
{
StatsCounter wav;
StatsCounter mpeg;
StatsCounter other;
StatsCounter total;
} audio;
StatsCounter none;
StatsCounter other;
} content;
};
struct UrlStats
{
bool operator < (const UrlStats& rhs) const { return req.count > rhs.req.count; } // Reverse order
const char *url;
StatsCounter req;
ElapsedStats time;
int64_t c_000;
int64_t c_2xx;
int64_t c_3xx;
int64_t c_4xx;
int64_t c_5xx;
int64_t hits;
int64_t misses;
int64_t errors;
};
///////////////////////////////////////////////////////////////////////////////
// Equal operator for char* (for the hash_map)
struct eqstr
{
inline bool operator() (const char *s1, const char *s2) const
{
return 0 == strcmp(s1, s2);
}
};
struct hash_fnv32 {
inline uint32_t operator()(const char* s) const
{
uint32_t hval = (uint32_t)0x811c9dc5; /* FNV1_32_INIT */
if (s) {
while (*s) {
hval ^= (uint32_t)*s++;
hval *= (uint32_t)0x01000193; /* FNV_32_PRIME */
}
}
return hval;
}
};
typedef std::list<UrlStats> LruStack;
#if HAVE_CXX_11 && HAVE_UNORDERED_MAP && HAVE_UNORDERED_SET
#include <unordered_map>
#include <unordered_set>
typedef std::unordered_map<const char *, OriginStats *, hash_fnv32, eqstr> OriginStorage;
typedef std::unordered_set<const char *, hash_fnv32, eqstr> OriginSet;
typedef std::unordered_map<const char *, LruStack::iterator, hash_fnv32, eqstr> LruHash;
// Resize a hash-based container.
template <class T, class N>
void
rehash(T& container, N size) {
container.rehash(size);
}
#elif HAVE_GNU_CXX_HASH_MAP
#define _BACKWARD_BACKWARD_WARNING_H // needed for gcc 4.3
#include <ext/hash_map>
#include <ext/hash_set>
typedef __gnu_cxx::hash_map<const char *, OriginStats *, hash_fnv32, eqstr> OriginStorage;
typedef __gnu_cxx::hash_set<const char *, hash_fnv32, eqstr> OriginSet;
typedef __gnu_cxx::hash_map<const char *, LruStack::iterator, hash_fnv32, eqstr> LruHash;
// Resize a hash-based container.
template <class T, class N>
void
rehash(T& container, N size) {
container.resize(size);
}
#undef _BACKWARD_BACKWARD_WARNING_H
#else
#error no supported hash container
#endif
// LRU class for the URL data
void update_elapsed(ElapsedStats &stat, const int elapsed, const StatsCounter &counter);
class UrlLru
{
public:
UrlLru(int size=1000000, int show_urls=0)
: _size(size)
{
_show_urls = size > 0 ? (show_urls >= size ? size-1 : show_urls) : show_urls;
_init();
_reset(false);
_cur = _stack.begin();
}
void
resize(int size=0)
{
if (0 != size)
_size = size;
_init();
_reset(true);
_cur = _stack.begin();
}
void
dump(int as_object=0)
{
int show = _stack.size();
if (_show_urls > 0 && _show_urls < show)
show = _show_urls;
_stack.sort();
for (LruStack::iterator u=_stack.begin(); NULL != u->url && --show >= 0; ++u)
_dump_url(u, as_object);
if (as_object)
std::cout << " \"_timestamp\" : \"" << static_cast<int>(ink_time_wall_seconds()) << "\"" << std::endl;
else
std::cout << " { \"_timestamp\" : \"" << static_cast<int>(ink_time_wall_seconds()) << "\" }" << std::endl;
}
void
add_stat(const char* url, int64_t bytes, int time, int result, int http_code, int as_object=0)
{
LruHash::iterator h = _hash.find(url);
if (h != _hash.end()) {
LruStack::iterator &l = h->second;
++(l->req.count);
l->req.bytes += bytes;
if ((http_code >= 600) || (http_code < 200))
++(l->c_000);
else if (http_code >= 500)
++(l->c_5xx);
else if (http_code >= 400)
++(l->c_4xx);
else if (http_code >= 300)
++(l->c_3xx);
else // http_code >= 200
++(l->c_2xx);
switch (result) {
case SQUID_LOG_TCP_HIT:
case SQUID_LOG_TCP_IMS_HIT:
case SQUID_LOG_TCP_REFRESH_HIT:
case SQUID_LOG_TCP_DISK_HIT:
case SQUID_LOG_TCP_MEM_HIT:
case SQUID_LOG_TCP_REF_FAIL_HIT:
case SQUID_LOG_UDP_HIT:
case SQUID_LOG_UDP_WEAK_HIT:
case SQUID_LOG_UDP_HIT_OBJ:
++(l->hits);
break;
case SQUID_LOG_TCP_MISS:
case SQUID_LOG_TCP_IMS_MISS:
case SQUID_LOG_TCP_REFRESH_MISS:
case SQUID_LOG_TCP_EXPIRED_MISS:
case SQUID_LOG_TCP_WEBFETCH_MISS:
case SQUID_LOG_UDP_MISS:
++(l->misses);
break;
case SQUID_LOG_ERR_CLIENT_ABORT:
case SQUID_LOG_ERR_CONNECT_FAIL:
case SQUID_LOG_ERR_INVALID_REQ:
case SQUID_LOG_ERR_UNKNOWN:
case SQUID_LOG_ERR_READ_TIMEOUT:
++(l->errors);
break;
}
update_elapsed(l->time, time, l->req);
// Move this entry to the top of the stack (hence, LRU)
if (_size > 0)
_stack.splice(_stack.begin(), _stack, l);
} else { // "new" URL
const char *u = ats_strdup(url); // We own it.
LruStack::iterator l = _stack.end();
if (_size > 0) {
if (_cur == l) { // LRU is full, take the last one
--l;
h = _hash.find(l->url);
if (h != _hash.end())
_hash.erase(h);
if (0 == _show_urls)
_dump_url(l, as_object);
} else {
l = _cur++;
}
ats_free(const_cast<char*>(l->url)); // We no longer own this string.
} else {
l = _stack.insert(l, UrlStats()); // This seems faster than having a static "template" ...
}
// Setup this URL stat
l->url = u;
l->req.bytes = bytes;
l->req.count = 1;
if ((http_code >= 600) || (http_code < 200))
l->c_000 = 1;
else if (http_code >= 500)
l->c_5xx = 1;
else if (http_code >= 400)
l->c_4xx = 1;
else if (http_code >= 300)
l->c_3xx = 1;
else // http_code >= 200
l->c_2xx = 1;
switch (result) {
case SQUID_LOG_TCP_HIT:
case SQUID_LOG_TCP_IMS_HIT:
case SQUID_LOG_TCP_REFRESH_HIT:
case SQUID_LOG_TCP_DISK_HIT:
case SQUID_LOG_TCP_MEM_HIT:
case SQUID_LOG_TCP_REF_FAIL_HIT:
case SQUID_LOG_UDP_HIT:
case SQUID_LOG_UDP_WEAK_HIT:
case SQUID_LOG_UDP_HIT_OBJ:
l->hits = 1;
break;
case SQUID_LOG_TCP_MISS:
case SQUID_LOG_TCP_IMS_MISS:
case SQUID_LOG_TCP_REFRESH_MISS:
case SQUID_LOG_TCP_EXPIRED_MISS:
case SQUID_LOG_TCP_WEBFETCH_MISS:
case SQUID_LOG_UDP_MISS:
l->misses = 1;
break;
case SQUID_LOG_ERR_CLIENT_ABORT:
case SQUID_LOG_ERR_CONNECT_FAIL:
case SQUID_LOG_ERR_INVALID_REQ:
case SQUID_LOG_ERR_UNKNOWN:
case SQUID_LOG_ERR_READ_TIMEOUT:
l->errors = 1;
break;
}
l->time.min = -1;
l->time.max = -1;
update_elapsed(l->time, time, l->req);
_hash[u] = l;
// We running a real LRU or not?
if (_size > 0)
_stack.splice(_stack.begin(), _stack, l); // Move this to the top of the stack
}
}
private:
void
_init()
{
if (_size > 0) {
_stack.resize(_size);
rehash(_hash, _size);
}
}
void
_reset(bool free=false)
{
for (LruStack::iterator l=_stack.begin(); l != _stack.end(); ++l) {
if (free && l->url)
ats_free(const_cast<char*>(l->url));
memset(&(*l), 0, sizeof(UrlStats));
}
}
void
_dump_url(LruStack::iterator &u, int as_object)
{
if (as_object)
std::cout << " \"" << u->url << "\" : { ";
else
std::cout << " { \"" << u->url << "\" : { ";
// Requests
std::cout << "\"req\" : { \"total\" : \"" << u->req.count <<
"\", \"hits\" : \"" << u->hits <<
"\", \"misses\" : \"" << u->misses <<
"\", \"errors\" : \"" << u->errors <<
"\", \"000\" : \"" << u->c_000 <<
"\", \"2xx\" : \"" << u->c_2xx <<
"\", \"3xx\" : \"" << u->c_3xx <<
"\", \"4xx\" : \"" << u->c_4xx <<
"\", \"5xx\" : \"" << u->c_5xx << "\" }, ";
std:: cout << "\"bytes\" : \"" << u->req.bytes << "\", ";
// Service times
std::cout << "\"svc_t\" : { \"min\" : \"" << u->time.min <<
"\", \"max\" : \"" << u->time.max <<
"\", \"avg\" : \"" << std::setiosflags(ios::fixed) << std::setprecision(2) << u->time.avg <<
"\", \"dev\" : \"" << std::setiosflags(ios::fixed) << std::setprecision(2) << u->time.stddev;
if (as_object)
std::cout << "\" } }," << std::endl;
else
std::cout << "\" } } }," << std::endl;
}
LruHash _hash;
LruStack _stack;
int _size, _show_urls;
LruStack::iterator _cur;
};
///////////////////////////////////////////////////////////////////////////////
// Globals, holding the accumulated stats (ok, I'm lazy ...)
static OriginStats totals;
static OriginStorage origins;
static OriginSet *origin_set;
static UrlLru *urls;
static int parse_errors;
// Command line arguments (parsing)
struct CommandLineArgs
{
char log_file[1024];
char origin_file[1024];
char origin_list[MAX_ORIG_STRING];
int max_origins;
char state_tag[1024];
int64_t min_hits;
int max_age;
int line_len;
int incremental; // Do an incremental run
int tail; // Tail the log file
int summary; // Summary only
int json; // JSON output
int cgi; // CGI output (typically with json)
int urls; // Produce JSON output of URL stats, arg is LRU size
int show_urls; // Max URLs to show
int as_object; // Show the URL stats as a single JSON object (not array)
int version;
int help;
CommandLineArgs()
: max_origins(0), min_hits(0), max_age(0), line_len(DEFAULT_LINE_LEN), incremental(0),
tail(0), summary(0), json(0), cgi(0), urls(0), show_urls(0), as_object(0), version(0), help(0)
{
log_file[0] = '\0';
origin_file[0] = '\0';
origin_list[0] = '\0';
state_tag[0] = '\0';
}
void parse_arguments(char** argv);
};
static CommandLineArgs cl;
static ArgumentDescription argument_descriptions[] = {
{"help", 'h', "Give this help", "T", &cl.help, NULL, NULL},
{"log_file", 'f', "Specific logfile to parse", "S1023", cl.log_file, NULL, NULL},
{"origin_list", 'o', "Only show stats for listed Origins", "S4095", cl.origin_list, NULL, NULL},
{"origin_file", 'O', "File listing Origins to show", "S1023", cl.origin_file, NULL, NULL},
{"max_orgins", 'M', "Max number of Origins to show", "I", &cl.max_origins, NULL, NULL},
{"urls", 'u', "Produce JSON stats for URLs, argument is LRU size", "I", &cl.urls, NULL, NULL},
{"show_urls", 'U', "Only show max this number of URLs", "I", &cl.show_urls, NULL, NULL},
{"as_object", 'A', "Produce URL stats as a JSON object instead of array", "T", &cl.as_object, NULL, NULL},
{"incremental", 'i', "Incremental log parsing", "T", &cl.incremental, NULL, NULL},
{"statetag", 'S', "Name of the state file to use", "S1023", cl.state_tag, NULL, NULL},
{"tail", 't', "Parse the last <sec> seconds of log", "I", &cl.tail, NULL, NULL},
{"summary", 's', "Only produce the summary", "T", &cl.summary, NULL, NULL},
{"json", 'j', "Produce JSON formatted output", "T", &cl.json, NULL, NULL},
{"cgi", 'c', "Produce HTTP headers suitable as a CGI", "T", &cl.cgi, NULL, NULL},
{"min_hits", 'm', "Minimum total hits for an Origin", "L", &cl.min_hits, NULL, NULL},
{"max_age", 'a', "Max age for log entries to be considered", "I", &cl.max_age, NULL, NULL},
{"line_len", 'l', "Output line length", "I", &cl.line_len, NULL, NULL},
{"debug_tags", 'T', "Colon-Separated Debug Tags", "S1023", &error_tags, NULL, NULL},
{"version", 'V', "Print Version Id", "T", &cl.version, NULL, NULL},
};
static const char *USAGE_LINE =
"Usage: " PROGRAM_NAME " [-f logfile] [-o origin[,...]] [-O originfile] [-m minhits] [-inshv]";
void
CommandLineArgs::parse_arguments(char** argv)
{
// process command-line arguments
process_args(argument_descriptions, countof(argument_descriptions), argv, USAGE_LINE);
// Process as "CGI" ?
if (strstr(argv[0], ".cgi") || cgi) {
char *query;
json = 1;
cgi = 1;
if (NULL != (query = getenv("QUERY_STRING"))) {
char buffer[MAX_ORIG_STRING];
char *tok, *sep_ptr, *val;
ink_strlcpy(buffer, query, sizeof(buffer));
unescapifyStr(buffer);
for (tok = strtok_r(buffer, "&", &sep_ptr); tok != NULL;) {
val = strchr(tok, '=');
if (val)
*(val++) = '\0';
if (0 == strncmp(tok, "origin_list", 11)) {
ink_strlcpy(origin_list, val, sizeof(origin_list));
} else if (0 == strncmp(tok, "state_tag", 9)) {
ink_strlcpy(state_tag, val, sizeof(state_tag));
} else if (0 == strncmp(tok, "max_origins", 11)) {
max_origins = strtol(val, NULL, 10);
} else if (0 == strncmp(tok, "urls", 4)) {
urls = strtol(val, NULL, 10);
} else if (0 == strncmp(tok, "show_urls", 9)) {
show_urls = strtol(val, NULL, 10);
} else if (0 == strncmp(tok, "as_object", 9)) {
as_object = strtol(val, NULL, 10);
} else if (0 == strncmp(tok, "min_hits", 8)) {
min_hits = strtol(val, NULL, 10);
} else if (0 == strncmp(tok, "incremental", 11)) {
incremental = strtol(val, NULL, 10);
} else {
// Unknown query arg.
}
tok = strtok_r(NULL, "&", &sep_ptr);
}
}
}
// check for the version number request
if (version) {
std::cerr << appVersionInfo.FullVersionInfoStr << std::endl;
_exit(0);
}
// check for help request
if (help) {
usage(argument_descriptions, countof(argument_descriptions), USAGE_LINE);
_exit(0);
}
}
// Enum for return code levels.
enum ExitLevel
{
EXIT_OK = 0,
EXIT_WARNING = 1,
EXIT_CRITICAL = 2,
EXIT_UNKNOWN = 3
};
struct ExitStatus
{
ExitLevel level;
char notice[1024];
ExitStatus()
: level(EXIT_OK)
{
memset(notice, 0, sizeof(notice));
}
void set(ExitLevel l, const char* n=NULL) {
if (l > level)
level = l;
if (n)
ink_strlcat(notice, n, sizeof(notice));
}
void append(const char *n) {
ink_strlcat(notice, n, sizeof(notice));
}
void append(const std::string s) {
ink_strlcat(notice, s.c_str(), sizeof(notice));
}
};
// Enum for parsing a log line
enum ParseStates
{
P_STATE_ELAPSED,
P_STATE_IP,
P_STATE_RESULT,
P_STATE_CODE,
P_STATE_SIZE,
P_STATE_METHOD,
P_STATE_URL,
P_STATE_RFC931,
P_STATE_HIERARCHY,
P_STATE_PEER,
P_STATE_TYPE,
P_STATE_END
};
// Enum for HTTP methods
enum HTTPMethod
{
METHOD_OPTIONS,
METHOD_GET,
METHOD_HEAD,
METHOD_POST,
METHOD_PUT,
METHOD_DELETE,
METHOD_TRACE,
METHOD_CONNECT,
METHOD_PURGE,
METHOD_NONE,
METHOD_OTHER
};
// Enum for URL schemes
enum URLScheme
{
SCHEME_HTTP,
SCHEME_HTTPS,
SCHEME_NONE,
SCHEME_OTHER
};
///////////////////////////////////////////////////////////////////////////////
// Initialize the elapsed field
inline void
init_elapsed(OriginStats *stats)
{
stats->elapsed.hits.hit.min = -1;
stats->elapsed.hits.ims.min = -1;
stats->elapsed.hits.refresh.min = -1;
stats->elapsed.hits.other.min = -1;
stats->elapsed.hits.total.min = -1;
stats->elapsed.misses.miss.min = -1;
stats->elapsed.misses.ims.min = -1;
stats->elapsed.misses.refresh.min = -1;
stats->elapsed.misses.other.min = -1;
stats->elapsed.misses.total.min = -1;
}
// Update the counters for one StatsCounter
inline void
update_counter(StatsCounter& counter, int size)
{
counter.count++;
counter.bytes += size;
}
inline void
update_elapsed(ElapsedStats &stat, const int elapsed, const StatsCounter &counter)
{
int newcount, oldcount;
float oldavg, newavg, sum_of_squares;
// Skip all the "0" values.
if (0 == elapsed)
return;
if (-1 == stat.min)
stat.min = elapsed;
else if (stat.min > elapsed)
stat.min = elapsed;
if (stat.max < elapsed)
stat.max = elapsed;
// update_counter should have been called on counter.count before calling
// update_elapsed.
newcount = counter.count;
// New count should never be zero, else there was a programming error.
ink_release_assert(newcount);
oldcount = counter.count - 1;
oldavg = stat.avg;
newavg = (oldavg * oldcount + elapsed) / newcount;
// Now find the new standard deviation from the old one
if (oldcount != 0)
sum_of_squares = (stat.stddev * stat.stddev * oldcount);
else
sum_of_squares = 0;
//Find the old sum of squares.
sum_of_squares = sum_of_squares + 2 * oldavg * oldcount * (oldavg - newavg)
+ oldcount * (newavg * newavg - oldavg * oldavg);
//Now, find the new sum of squares.
sum_of_squares = sum_of_squares + (elapsed - newavg) * (elapsed - newavg);
stat.stddev = sqrt(sum_of_squares / newcount);
stat.avg = newavg;
}
///////////////////////////////////////////////////////////////////////////////
// Update the "result" and "elapsed" stats for a particular record
inline void
update_results_elapsed(OriginStats * stat, int result, int elapsed, int size)
{
switch (result) {
case SQUID_LOG_TCP_HIT:
update_counter(stat->results.hits.hit, size);
update_counter(stat->results.hits.total, size);
update_elapsed(stat->elapsed.hits.hit, elapsed, stat->results.hits.hit);
update_elapsed(stat->elapsed.hits.total, elapsed, stat->results.hits.total);
break;
case SQUID_LOG_TCP_MISS:
update_counter(stat->results.misses.miss, size);
update_counter(stat->results.misses.total, size);
update_elapsed(stat->elapsed.misses.miss, elapsed, stat->results.misses.miss);
update_elapsed(stat->elapsed.misses.total, elapsed, stat->results.misses.total);
break;
case SQUID_LOG_TCP_IMS_HIT:
update_counter(stat->results.hits.ims, size);
update_counter(stat->results.hits.total, size);
update_elapsed(stat->elapsed.hits.ims, elapsed, stat->results.hits.ims);
update_elapsed(stat->elapsed.hits.total, elapsed, stat->results.hits.total);
break;
case SQUID_LOG_TCP_IMS_MISS:
update_counter(stat->results.misses.ims, size);
update_counter(stat->results.misses.total, size);
update_elapsed(stat->elapsed.misses.ims, elapsed, stat->results.misses.ims);
update_elapsed(stat->elapsed.misses.total, elapsed, stat->results.misses.total);
break;
case SQUID_LOG_TCP_REFRESH_HIT:
update_counter(stat->results.hits.refresh, size);
update_counter(stat->results.hits.total, size);
update_elapsed(stat->elapsed.hits.refresh, elapsed, stat->results.hits.refresh);
update_elapsed(stat->elapsed.hits.total, elapsed, stat->results.hits.total);
break;
case SQUID_LOG_TCP_REFRESH_MISS:
update_counter(stat->results.misses.refresh, size);
update_counter(stat->results.misses.total, size);
update_elapsed(stat->elapsed.misses.refresh, elapsed, stat->results.misses.refresh);
update_elapsed(stat->elapsed.misses.total, elapsed, stat->results.misses.total);
break;
case SQUID_LOG_ERR_CLIENT_ABORT:
update_counter(stat->results.errors.client_abort, size);
update_counter(stat->results.errors.total, size);
break;
case SQUID_LOG_ERR_CONNECT_FAIL:
update_counter(stat->results.errors.connect_fail, size);
update_counter(stat->results.errors.total, size);
break;
case SQUID_LOG_ERR_INVALID_REQ:
update_counter(stat->results.errors.invalid_req, size);
update_counter(stat->results.errors.total, size);
break;
case SQUID_LOG_ERR_UNKNOWN:
update_counter(stat->results.errors.unknown, size);
update_counter(stat->results.errors.total, size);
break;
case SQUID_LOG_TCP_DISK_HIT:
case SQUID_LOG_TCP_MEM_HIT:
case SQUID_LOG_TCP_REF_FAIL_HIT:
case SQUID_LOG_UDP_HIT:
case SQUID_LOG_UDP_WEAK_HIT:
case SQUID_LOG_UDP_HIT_OBJ:
update_counter(stat->results.hits.other, size);
update_counter(stat->results.hits.total, size);
update_elapsed(stat->elapsed.hits.other, elapsed, stat->results.hits.other);
update_elapsed(stat->elapsed.hits.total, elapsed, stat->results.hits.total);
break;
case SQUID_LOG_TCP_EXPIRED_MISS:
case SQUID_LOG_TCP_WEBFETCH_MISS:
case SQUID_LOG_UDP_MISS:
update_counter(stat->results.misses.other, size);
update_counter(stat->results.misses.total, size);
update_elapsed(stat->elapsed.misses.other, elapsed, stat->results.misses.other);
update_elapsed(stat->elapsed.misses.total, elapsed, stat->results.misses.total);
break;
default:
if ((result >= SQUID_LOG_ERR_READ_TIMEOUT) && (result <= SQUID_LOG_ERR_UNKNOWN)) {
update_counter(stat->results.errors.other, size);
update_counter(stat->results.errors.total, size);
} else
update_counter(stat->results.other, size);
break;
}
}
///////////////////////////////////////////////////////////////////////////////
// Update the "codes" stats for a particular record
inline void
update_codes(OriginStats * stat, int code, int size)
{
switch (code) {
case 100:
update_counter(stat->codes.c_100, size);
break;
// 200's
case 200:
update_counter(stat->codes.c_200, size);
break;
case 201:
update_counter(stat->codes.c_201, size);
break;
case 202: