the logging wasn't showing the full prefix for filenames
[rsync/rsync.git] / support / rsyncstats
CommitLineData
2c51d5de
AT
1#! /usr/bin/perl
2# ---------------------------------------------------------------------------
3#
4# USAGE: rsyncstats <options>
5#
6# OPTIONS:
7# -f <filename> Use <filename> for the log file
8# -h include report on hourly traffic
9# -d include report on domain traffic
10# -t report on total traffic by section
11# -D <domain> report only on traffic from <domain>
12# -l <depth> Depth of path detail for sections
13# -s <section> Section to report on, For example: -s /pub will report
14# only on paths under /pub
15#
16# This script parses the default logfile format produced by rsync when running
17# as a daemon with transfer logging enabled. It is derived from the xferstats
18# script that comes with wuftpd
19#
20# Andrew Tridgell, October 1998
21# rsync-bugs@samba.anu.edu.au
22#
23# ---------------------------------------------------------------------------
24
25# edit the next line to customize for your default log file
26$usage_file = "/var/adm/rsyncd.log";
27
28# Edit the following lines for default report settings.
29# Entries defined here will be over-ridden by the command line.
30
31$opt_h = 1;
32$opt_d = 0;
33$opt_t = 1;
34$opt_l = 2;
35
36require 'getopts.pl';
37&Getopts('f:rahdD:l:s:');
38
39if ($opt_r) { $real = 1;}
40if ($opt_a) { $anon = 1;}
41if ($real == 0 && $anon == 0) { $anon = 1; }
42if ($opt_f) {$usage_file = $opt_f;}
43
44open (LOG,$usage_file) || die "Error opening usage log file: $usage_file\n";
45
46if ($opt_D) {print "Transfer Totals include the '$opt_D' domain only.\n";
47 print "All other domains are filtered out for this report.\n\n";}
48
49if ($opt_s) {print "Transfer Totals include the '$opt_s' section only.\n";
50 print "All other sections are filtered out for this report.\n\n";}
51
52line: while (<LOG>) {
53
54 @line = split;
55
56 $day = $line[0];
57 $time = $line[1];
58 $pid = $line[2];
59 $op = $line[3];
60 $host = $line[4];
61 $ip = $line[5];
62 $module = $line[6];
63 $user = $line[7];
64 $file = $line[8];
65 $bytes = $line[9];
66
67 next if ($#line != 9);
68
69 next if ($op != "send" && $op != "recv");
70
71 $daytime = $day;
72 $hour = substr($time,0,2);
73
74 $file = $module . "/" . $file;
75
76 @path = split(/\//, $file);
77
78 $pathkey = "";
79 for ($i=0; $i <= $#path && $i <= $opt_l;$i++) {
80 $pathkey = $pathkey . "/" . $path[$i];
81 }
82
83 next if (substr($pathkey,0,length("$opt_s")) ne "$opt_s");
84
85 $host =~ tr/A-Z/a-z/;
86
87 @address = split(/\./, $host);
88
89 $domain = $address[$#address];
90 if ( int($address[0]) > 0 || $#address < 2 )
91 { $domain = "unresolved"; }
92
93 if ($opt_D) {
94 next unless (substr($domain,0,length("$opt_D")) eq "$opt_D");
95 }
96
97
98# printf ("c=%d day=%s bytes=%d file=%s path=%s\n",
99# $#line, $daytime, $bytes, $file, $pathkey);
100
101 $xferfiles++; # total files sent
102 $xfertfiles++; # total files sent
103 $xferfiles{$daytime}++; # files per day
104 $groupfiles{$pathkey}++; # per-group accesses
105 $domainfiles{$domain}++;
106
107 $xferbytes{$daytime} += $bytes; # bytes per day
108 $domainbytes{$domain} += $bytes; # xmit bytes to domain
109 $xferbytes += $bytes; # total bytes sent
110 $groupbytes{$pathkey} += $bytes; # per-group bytes sent
111
112 $xfertfiles{$hour}++; # files per hour
113 $xfertbytes{$hour} += $bytes; # bytes per hour
114 $xfertbytes += $bytes; # total bytes sent
115}
116close LOG;
117
118@syslist = keys(systemfiles);
119@dates = sort datecompare keys(xferbytes);
120
121if ($xferfiles == 0) {die "There was no data to process.\n";}
122
123
124print "TOTALS FOR SUMMARY PERIOD ", $dates[0], " TO ", $dates[$#dates], "\n\n";
125printf ("Files Transmitted During Summary Period %12.0f\n", $xferfiles);
126printf ("Bytes Transmitted During Summary Period %12.0f\n", $xferbytes);
127printf ("Systems Using Archives %12.0f\n\n", $#syslist+1);
128
129printf ("Average Files Transmitted Daily %12.0f\n",
130 $xferfiles / ($#dates + 1));
131printf ("Average Bytes Transmitted Daily %12.0f\n",
132 $xferbytes / ($#dates + 1));
133
134format top1 =
135
136Daily Transmission Statistics
137
138 Number Of Number of Percent Of Percent Of
139 Date Files Sent MB Sent Files Sent Bytes Sent
140--------------- ---------- ----------- ---------- ----------
141.
142
143format line1 =
144@<<<<<<<<<<<<<< @>>>>>>>>> @>>>>>>>>>> @>>>>>>> @>>>>>>>
145$date, $nfiles, $nbytes/(1024*1024), $pctfiles, $pctbytes
146.
147
148$^ = top1;
149$~ = line1;
150
151foreach $date ( sort datecompare keys(xferbytes) ) {
152
153 $nfiles = $xferfiles{$date};
154 $nbytes = $xferbytes{$date};
155 $pctfiles = sprintf("%8.2f", 100*$xferfiles{$date} / $xferfiles);
156 $pctbytes = sprintf("%8.2f", 100*$xferbytes{$date} / $xferbytes);
157 write;
158}
159
160if ($opt_t) {
161format top2 =
162
163Total Transfers from each Archive Section (By bytes)
164
165 - Percent -
166 Archive Section NFiles MB Files Bytes
167------------------------------------- ------- ----------- ----- -------
168.
169
170format line2 =
171@<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< @>>>>>> @>>>>>>>>>> @>>>> @>>>>
172$section, $files, $bytes/(1024*1024), $pctfiles, $pctbytes
173.
174
175$| = 1;
176$- = 0;
177$^ = top2;
178$~ = line2;
179
180foreach $section ( sort bytecompare keys(groupfiles) ) {
181
182 $files = $groupfiles{$section};
183 $bytes = $groupbytes{$section};
184 $pctbytes = sprintf("%8.2f", 100 * $groupbytes{$section} / $xferbytes);
185 $pctfiles = sprintf("%8.2f", 100 * $groupfiles{$section} / $xferfiles);
186 write;
187
188}
189
190if ( $xferfiles < 1 ) { $xferfiles = 1; }
191if ( $xferbytes < 1 ) { $xferbytes = 1; }
192}
193
194if ($opt_d) {
195format top3 =
196
197Total Transfer Amount By Domain
198
199 Number Of Number of Percent Of Percent Of
200Domain Name Files Sent MB Sent Files Sent Bytes Sent
201----------- ---------- ------------ ---------- ----------
202.
203
204format line3 =
205@<<<<<<<<<< @>>>>>>>>> @>>>>>>>>>>> @>>>>>>> @>>>>>>>
206$domain, $files, $bytes/(1024*1024), $pctfiles, $pctbytes
207.
208
209$- = 0;
210$^ = top3;
211$~ = line3;
212
213foreach $domain ( sort domnamcompare keys(domainfiles) ) {
214
215 if ( $domainsecs{$domain} < 1 ) { $domainsecs{$domain} = 1; }
216
217 $files = $domainfiles{$domain};
218 $bytes = $domainbytes{$domain};
219 $pctfiles = sprintf("%8.2f", 100 * $domainfiles{$domain} / $xferfiles);
220 $pctbytes = sprintf("%8.2f", 100 * $domainbytes{$domain} / $xferbytes);
221 write;
222
223}
224
225}
226
227if ($opt_h) {
228
229format top8 =
230
231Hourly Transmission Statistics
232
233 Number Of Number of Percent Of Percent Of
234 Time Files Sent MB Sent Files Sent Bytes Sent
235--------------- ---------- ----------- ---------- ----------
236.
237
238format line8 =
239@<<<<<<<<<<<<<< @>>>>>>>>> @>>>>>>>>>> @>>>>>>> @>>>>>>>
240$hour, $nfiles, $nbytes/(1024*1024), $pctfiles, $pctbytes
241.
242
243
244$| = 1;
245$- = 0;
246$^ = top8;
247$~ = line8;
248
249foreach $hour ( sort keys(xfertbytes) ) {
250
251 $nfiles = $xfertfiles{$hour};
252 $nbytes = $xfertbytes{$hour};
253 $pctfiles = sprintf("%8.2f", 100*$xfertfiles{$hour} / $xferfiles);
254 $pctbytes = sprintf("%8.2f", 100*$xfertbytes{$hour} / $xferbytes);
255 write;
256}
257}
258exit(0);
259
260sub datecompare {
c7c05641 261 $a gt $b;
2c51d5de
AT
262}
263
264sub domnamcompare {
265
266 $sdiff = length($a) - length($b);
267 ($sdiff < 0) ? -1 : ($sdiff > 0) ? 1 : ($a lt $b) ? -1 : ($a gt $b) ? 1 : 0;
268
269}
270
271sub bytecompare {
272
273 $bdiff = $groupbytes{$b} - $groupbytes{$a};
274 ($bdiff < 0) ? -1 : ($bdiff > 0) ? 1 : ($a lt $b) ? -1 : ($a gt $b) ? 1 : 0;
275
276}
277
278sub faccompare {
279
280 $fdiff = $fac{$b} - $fac{$a};
281 ($fdiff < 0) ? -1 : ($fdiff > 0) ? 1 : ($a lt $b) ? -1 : ($a gt $b) ? 1 : 0;
282
283}
284