Debian Codegraph @

contrib/xferstats.holger-preiss

raw ◀ ▶
1 Debian patch touch this file
1 #!/usr/bin/env perl
2 # ---------------------------------------------------------------------------
3 #
4 # USAGE: xferstats <options>
5 #
6 # OPTIONS:
7 # -f <filename> Use <filename> for the log file
8 # -r include real users
9 # -a include anonymous users
10 # -h include report on hourly traffic
11 # -d include report on domain traffic
12 # -t report on total traffic by section
13 # -i report incoming traffic only (uploads)
14 # -o report outgoing traffic only (download)
15 # -e report deleted files only (just for complete)
16 # -D <domain> report only on traffic from <domain>
17 # this option leads to problems with the local
18 # domain: e.g. test.com is counted under test
19 # and not recognized under com, -D com will give
20 # you only statistics about com excluding
21 # test.com! use -A com for correct results.
22 # -A <address> report only on traffic from addresses ends matching
23 # <address> e.g. -A test.domain.com will report
24 # only on addresses ending with test.domain.com
25 # -l <depth> Depth of path detail for sections
26 # -s <section> Section to report on, For example: -s /pub will report
27 # only on paths under /pub
28 # -u <user> Report traffic for specified user.
29 #
30 # ---------------------------------------------------------------------------
31 # 04.09.2013: minor change by Andretta Paolo and TJ Saunders
32 # - enabled -d (not working)
33 # - corrected -i & -o (not working)
34 # - added -e
35 #
36 # ---------------------------------------------------------------------------
37 # 05.09.01: minor change by Jamie Fifield ([email protected]):
38 # - added option u which singles out the data for a single user
39 #
40 # ---------------------------------------------------------------------------
41 # 30.09.98: changes by Jan Menzel ([email protected]):
42 # -documented problems with option -D
43 # -fixed same problems with spaces in the filenames (lines which are not
44 # exactly 16 elements long are skipped in earlier versions)
45 # -added option A which compares the addresses end with a given pattern
46 # -added option i and o which reports either incoming or outgoing traffic only
47
48 @mydom = split(/\./, `dnsdomainname`);
49 $mydom2 = pop(@mydom); chop($mydom2);
50 $mydom1 = pop(@mydom);
51
52 # If you want to specify $mydom1 and $mydom2 manually you should edit the
53 # next two lines to customize for your domain. This will allow your domain
54 # to be separated in the domain listing.
55 # $mydom1 = "debian";
56 # $mydom2 = "org";
57
58 # edit the next line to customize for your default log file
•59 $usage_file = "/var/log/proftpd/xferlog";
60
61 # Edit the following lines for default report settings.
62 # Entries defined here will be over-ridden by the command line.
63
64 $opt_a = 0;
65 $opt_h = 0;
66 $opt_d = 0;
67 $opt_t = 1;
68 $opt_l = 3;
69 $opt_r = 0;
70
71 use Getopt::Std;
72 getopts('f:rahdD:l:s:A:iotu:e');
73
74 if ($opt_r) { $real = 1;}
75 if ($opt_a) { $anon = 1;}
76 if ($real == 0 && $anon == 0) { $anon = 1; }
77 if ($opt_f) {$usage_file = $opt_f;}
78 if ($opt_i) {$opt_i = 1;}
79 if ($opt_o) {$opt_o = 1;}
80 if ($opt_e) {$opt_e = 1;}
81
82 open (LOG,$usage_file) || die "Error opening usage log file: $usage_file\n";
83
84 if ($opt_o) {
85 print "Transfer totals include outgoing traffic only.\n\n";
86 }
87
88 if ($opt_i) {
89 print "Transfer totals include incoming traffic only.\n\n";
90 }
91
92 if ($opt_e) {
93 print "Transfer totals include deleted files only.\n\n";
94 }
95
96 if ($opt_D) {
97 $opt_D =~ tr/A-Z/a-z/;
98 print "Transfer totals include the '$opt_D' domain only.\n";
99 print "All other domains are filtered out for this report.\n\n";
100 }
101
102 if ($opt_A) {
103 $opt_A =~ tr/A-Z/a-z/;
104 print "Transfer totals include the addresses ending in '$opt_A' only.\n";
105 print "All other addresses are filtered out for this report.\n\n";
106 }
107
108 if ($opt_s) {
109 print "Transfer totals include the '$opt_s' section only.\n";
110 print "All other sections are filtered out for this report.\n\n";
111 }
112
113 if ($opt_u) {
114 print "Transfer totals are for user '$opt_u' only.\n";
115 print "All other users are filtered out for this report.\n\n";
116 }
117
118 line: while (<LOG>) {
119
120 @line = split;
121 # is the first entry week day abbreviation?
122 next if (length("$line[0]") != 3);
123
124 # check whether there is a valid 'username'
125 if ($line[$#line-7] eq "a" or $line[$#line-7] eq "b") {
126 # yes, there i
127 # offset points to the first element just behind the filename
128 $offset = $#line - 7;
129 } elsif ($line[$#line-6] eq "a" or $line[$#line-6] eq "b") {
130 $offset = $#line - 6
131 } elsif ($line[$#line-5] =~ /^(a|g|r)$/) {
132 $offset = $#line - 5;
133 } else {
134 next;
135 }
136 next if (!$opt_u && !$anon && $line[$offset+3] eq "a");
137 next if (!$opt_u && !$real && $line[$offset+3] eq "r");
138 next if ($opt_i && $line[$offset-1] ne "i");
139 next if ($opt_o && $line[$offset-1] ne "o");
140 next if ($opt_e && $line[$offset-1] ne "d");
141 next if ($opt_u && $line[$offset+4] ne $opt_u);
142
143 $daytime = substr($_, 0, 10) . substr($_, 19, 5);
144 $time = substr($_,11,2);
145
146 if ($line[8] eq "\.") { $line[8] = "/unreadable/filename";}
147 next if (substr($line[8],0,length("$opt_s")) ne "$opt_s");
148 $line[8] = substr($line[8],length("$opt_s"));
149 @path = split(/\//, $line[8]);
150
151 #
152 # Why was the original xferstats dropping leading 1 character path
153 # segments???
154 #
155 # while (length($path[1]) <= 1) {
156 # shift @path;
157 # next line if ($#path == -1);
158 # }
159
160 # Things in the top-level directory are assumed to be informational files
161
162 if ($#path == 1)
163 { $pathkey = "Index/Informational Files"; }
164 else {
165 $pathkey = "";
166 for ($i=1; $i <= $#path-1 && $i <= $opt_l;$i++) {
167 $pathkey = $pathkey . "/" . $path[$i];
168 }
169 }
170
171 $line[6] =~ tr/A-Z/a-z/;
172
173 @address = split(/\./, $line[6]);
174
175 $domain = $address[$#address];
176 if ($domain eq "$mydom2" && $address[$#address-1] eq "$mydom1")
177 { $domain = $mydom1 . "." . $mydom2; }
178 if ( int($address[0]) > 0 || $#address < 2 )
179 { $domain = "unresolved"; }
180
181 $count = 1;
182 if ($opt_D and substr($domain,0,length("$opt_D")) ne "$opt_D" ) {
183 $count = 0;
184 }
185 if ($opt_A and substr($line[6],length("$line[6]")-length("$opt_A")) ne "$opt_A" ) {
186 $count = 0;
187 }
188
189
190 if ($count) {
191
192 $systemfiles{$line[6]}++; # Systems Accessing the Arc
193 $xferfiles++; # total files sent
194 $xfertfiles++; # total files sent
195 $xferfiles{$daytime}++; # files per day
196 $groupfiles{$pathkey}++; # per-group accesses
197 $domainfiles{$domain}++;
198
199 $xfersecs{$daytime} += $line[5]; # xmit seconds per day
200 $domainsecs{$domain} += $line[5]; # xmit seconds for domain
201 $xferbytes{$daytime} += $line[7]; # bytes per day
202 $domainbytes{$domain} += $line[7]; # xmit bytes to domain
203 $xferbytes += $line[7]; # total bytes sent
204 $groupbytes{$pathkey} += $line[7]; # per-group bytes sent
205
206 $xfertfiles{$time}++; # files per hour
207 $xfertsecs{$time} += $line[5]; # xmit seconds per hour
208 $xfertbytes{$time} += $line[7]; # bytes per hour
209 $xfertbytes += $line[7]; # total bytes sent
210 }
211 }
212 close LOG;
213
214 @syslist = keys(%systemfiles);
215 @dates = sort datecompare keys(%xferbytes);
216
217 if ($xferfiles == 0) {die "There was no data to process.\n";}
218
219 print "TOTALS FOR SUMMARY PERIOD ", $dates[0], " TO ", $dates[$#dates], "\n\n";
220 printf ("Files Transmitted During Summary Period %12.0f\n", $xferfiles);
221 printf ("Bytes Transmitted During Summary Period %12.0f\n", $xferbytes);
222 printf ("Systems Using Archives %12.0f\n\n", $#syslist+1);
223 printf ("Average Files Transmitted Daily %12.0f\n", $xferfiles / ($#dates + 1));
224 printf ("Average Bytes Transmitted Daily %12.0f\n", $xferbytes / ($#dates + 1));
225
226 format top1 =
227
228 Daily Transmission Statistics
229
230 Number Of Number of Average Percent Of Percent Of
231 Date Files Sent Bytes Sent Xmit Rate Files Sent Bytes Sent
232 --------------- ---------- ----------- ---------- ---------- ----------
233 .
234
235 format line1 =
236 @<<<<<<<<<<<<<< @>>>>>>>>> @>>>>>>>>>> @>>>>>>>>> @>>>>>>> @>>>>>>>
237 $date, $nfiles, $nbytes, $avgrate, $pctfiles, $pctbytes
238 .
239
240 $^ = top1;
241 $~ = line1;
242
243 # sort daily traffic by bytes sent
244 foreach $date (sort datecompare keys(%xferbytes)) {
245
246 $nfiles = $xferfiles{$date};
247 $nbytes = $xferbytes{$date};
248 if ($xfersecs{$date}) {
249 $avgrate = sprintf("%5.1f KB/s", $xferbytes{$date}/$xfersecs{$date}/1024);
250 } else {
251 $avgrate = sprintf("undefined");
252 }
253 $pctfiles = sprintf("%8.2f", 100*$xferfiles{$date}/$xferfiles);
254 $pctbytes = sprintf("%8.2f", 100*$xferbytes{$date}/$xferbytes);
255 write;
256 }
257
258 if ($opt_t) {
259 format top2 =
260
261 Total Transfers from each Archive Section (By bytes)
262
263 ---- Percent Of ----
264 Archive Section Files Sent Bytes Sent Files Sent Bytes Sent
265 ------------------------- ---------- ------------- ---------- ----------
266 .
267
268 format line2 =
269 @<<<<<<<<<<<<<<<<<<<<<<<< @>>>>>>>>> @>>>>>>>>>>>> @>>>>>>> @>>>>>>>
270 $section, $files, $bytes, $pctfiles, $pctbytes
271 .
272
273 $| = 1;
274 $- = 0;
275 $^ = top2;
276 $~ = line2;
277
278 # sort total transfer for each archive by # files transferred
279 foreach $section (sort bytecompare keys(%groupfiles)) {
280
281 $files = $groupfiles{$section};
282 $bytes = $groupbytes{$section};
283 $pctbytes = sprintf("%8.2f", 100 * $groupbytes{$section} / $xferbytes);
284 $pctfiles = sprintf("%8.2f", 100 * $groupfiles{$section} / $xferfiles);
285 write;
286
287 }
288
289 }
290
291 if ($opt_d) {
292 format top3 =
293
294 Total Transfer Amount By Domain
295
296 Number Of Number of Average Percent Of Percent Of
297 Domain Name Files Sent Bytes Sent Xmit Rate Files Sent Bytes Sent
298 ----------- ---------- ------------ ---------- ---------- ----------
299 .
300
301 format line3 =
302 @<<<<<<<<<< @>>>>>>>>> @>>>>>>>>>>> @>>>>>>>>> @>>>>>>> @>>>>>>>
303 $domain, $files, $bytes, $avgrate, $pctfiles, $pctbytes
304 .
305
306 $- = 0;
307 $^ = top3;
308 $~ = line3;
309
310 # sort amount per domain by files
311 foreach $domain (sort domnamcompare keys(%domainfiles)) {
312
313 $files = $domainfiles{$domain};
314 $bytes = $domainbytes{$domain};
315 if ($domainsecs{$domain}) {
316 $avgrate = sprintf("%5.1f KB/s", $domainbytes{$domain}/$domainsecs{$domain}/1024);
317 } else {
318 $avgrate = sprintf("undefined");
319 }
320 $pctfiles = sprintf("%8.2f", 100 * $domainfiles{$domain} / $xferfiles);
321 $pctbytes = sprintf("%8.2f", 100 * $domainbytes{$domain} / $xferbytes);
322 write;
323
324 }
325
326 print "\n";
327
328 }
329
330 if ($opt_h) {
331
332 format top8 =
333
334 Hourly Transmission Statistics
335
336 Number Of Number of Average Percent Of Percent Of
337 Time Files Sent Bytes Sent Xmit Rate Files Sent Bytes Sent
338 --------------- ---------- ----------- ---------- ---------- ----------
339 .
340
341 format line8 =
342 @<<<<<<<<<<<<<< @>>>>>>>>> @>>>>>>>>>> @>>>>>>>>> @>>>>>>> @>>>>>>>
343 $time, $nfiles, $nbytes, $avgrate, $pctfiles, $pctbytes
344 .
345
346
347 $| = 1;
348 $- = 0;
349 $^ = top8;
350 $~ = line8;
351
352 # sort hourly transmission by sent bytes
353 foreach $time (sort keys(%xfertbytes)) {
354
355 $nfiles = $xfertfiles{$time};
356 $nbytes = $xfertbytes{$time};
357 if ($xfertsecs{$time}) {
358 $avgrate = sprintf("%5.1f KB/s", $xfertbytes{$time}/$xfertsecs{$time}/1024);
359 } else {
360 $avgrate = sprintf("undefined");
361 }
362 $pctfiles = sprintf("%8.2f", 100*$xfertfiles{$time} / $xferfiles);
363 $pctbytes = sprintf("%8.2f", 100*$xfertbytes{$time} / $xferbytes);
364 write;
365 }
366 }
367 exit(0);
368
369 sub datecompare {
370
371 $date1 = substr($a, 11, 4) * 4800;
372 $date2 = substr($b, 11, 4) * 4800;
373 $date1 += index("JanFebMarAprMayJunJulAugSepOctNovDec",substr($a, 4, 3)) / 3 * 400;
374 $date2 += index("JanFebMarAprMayJunJulAugSepOctNovDec",substr($b, 4, 3)) / 3 * 400;
375 $date1 += substr($a, 8, 2);
376 $date2 += substr($b, 8, 2);
377 $date1 - $date2;
378
379 }
380
381 sub domnamcompare {
382
383 $sdiff = length($a) - length($b);
384 ($sdiff < 0) ? -1 : ($sdiff > 0) ? 1 : ($a lt $b) ? -1 : ($a gt $b) ? 1 : 0;
385
386 }
387
388 sub bytecompare {
389
390 $bdiff = $groupbytes{$b} - $groupbytes{$a};
391 ($bdiff < 0) ? -1 : ($bdiff > 0) ? 1 : ($a lt $b) ? -1 : ($a gt $b) ? 1 : 0;
392
393 }
394
395 sub faccompare {
396
397 $fdiff = $fac{$b} - $fac{$a};
398 ($fdiff < 0) ? -1 : ($fdiff > 0) ? 1 : ($a lt $b) ? -1 : ($a gt $b) ? 1 : 0;
399
400 }
401