This is Joe Smith's rrsync (restricted rsync) perl script.
[rsync.git] / support / rsyncstats
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.org
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
36 require 'getopts.pl';
37 &Getopts('f:rahdD:l:s:');
38
39 if ($opt_r) { $real = 1;}
40 if ($opt_a) { $anon = 1;}
41 if ($real == 0 && $anon == 0) { $anon = 1; }
42 if ($opt_f) {$usage_file = $opt_f;}
43
44 open (LOG,$usage_file) || die "Error opening usage log file: $usage_file\n";
45
46 if ($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
49 if ($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
52 line: 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    $file =~ s|//|/|mg;
77
78    @path = split(/\//, $file);
79
80    $pathkey = "";
81    for ($i=0; $i <= $#path && $i <= $opt_l;$i++) {
82         $pathkey = $pathkey . "/" . $path[$i];
83    }
84
85    next if (substr($pathkey,0,length("$opt_s")) ne "$opt_s");
86
87    $host =~ tr/A-Z/a-z/;
88
89    @address = split(/\./, $host);
90
91    $domain = $address[$#address];
92    if ( int($address[0]) > 0 || $#address < 2 )
93       { $domain = "unresolved"; }
94
95    if ($opt_D) {
96        next unless (substr($domain,0,length("$opt_D")) eq "$opt_D");
97    }
98
99
100 #   printf ("c=%d day=%s bytes=%d file=%s path=%s\n", 
101 #          $#line, $daytime, $bytes, $file, $pathkey);
102
103    $xferfiles++;                                # total files sent
104    $xfertfiles++;                               # total files sent
105    $xferfiles{$daytime}++;                      # files per day
106    $groupfiles{$pathkey}++;                     # per-group accesses
107    $domainfiles{$domain}++;
108
109    $xferbytes{$daytime}   += $bytes;          # bytes per day
110    $domainbytes{$domain}  += $bytes;            # xmit bytes to domain
111    $xferbytes             += $bytes;          # total bytes sent
112    $groupbytes{$pathkey}  += $bytes;          # per-group bytes sent
113
114    $xfertfiles{$hour}++;                        # files per hour
115    $xfertbytes{$hour}     += $bytes;          # bytes per hour
116    $xfertbytes            += $bytes;          # total bytes sent
117 }
118 close LOG;
119
120 @syslist = keys(systemfiles);
121 @dates = sort datecompare keys(xferbytes);
122
123 if ($xferfiles == 0) {die "There was no data to process.\n";}
124
125
126 print "TOTALS FOR SUMMARY PERIOD ", $dates[0], " TO ", $dates[$#dates], "\n\n";
127 printf ("Files Transmitted During Summary Period  %12.0f\n", $xferfiles);
128 printf ("Bytes Transmitted During Summary Period  %12.0f\n", $xferbytes); 
129 printf ("Systems Using Archives                   %12.0f\n\n", $#syslist+1);
130
131 printf ("Average Files Transmitted Daily          %12.0f\n",
132    $xferfiles / ($#dates + 1));
133 printf ("Average Bytes Transmitted Daily          %12.0f\n",
134    $xferbytes / ($#dates + 1));
135
136 format top1 =
137
138 Daily Transmission Statistics
139
140                  Number Of    Number of   Percent Of  Percent Of
141      Date        Files Sent   MB  Sent    Files Sent  Bytes Sent
142 ---------------  ----------  -----------  ----------  ----------
143 .
144
145 format line1 =
146 @<<<<<<<<<<<<<<  @>>>>>>>>>  @>>>>>>>>>>  @>>>>>>>    @>>>>>>>  
147 $date,           $nfiles,    $nbytes/(1024*1024), $pctfiles,  $pctbytes
148 .
149
150 $^ = top1;
151 $~ = line1;
152
153 foreach $date ( sort datecompare keys(xferbytes) ) {
154
155    $nfiles   = $xferfiles{$date};
156    $nbytes   = $xferbytes{$date};
157    $pctfiles = sprintf("%8.2f", 100*$xferfiles{$date} / $xferfiles);
158    $pctbytes = sprintf("%8.2f", 100*$xferbytes{$date} / $xferbytes);
159    write;
160 }
161
162 if ($opt_t) {
163 format top2 =
164
165 Total Transfers from each Archive Section (By bytes)
166
167                                                            - Percent -
168      Archive Section                   NFiles     MB      Files   Bytes
169 ------------------------------------- ------- ----------- ----- -------
170 .
171
172 format line2 =
173 @<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< @>>>>>> @>>>>>>>>>> @>>>>   @>>>>
174 $section,                 $files,    $bytes/(1024*1024),     $pctfiles, $pctbytes
175 .
176
177 $| = 1;
178 $- = 0;
179 $^ = top2;
180 $~ = line2;
181
182 foreach $section ( sort bytecompare keys(groupfiles) ) {
183
184    $files = $groupfiles{$section};
185    $bytes = $groupbytes{$section};
186    $pctbytes = sprintf("%8.2f", 100 * $groupbytes{$section} / $xferbytes);
187    $pctfiles = sprintf("%8.2f", 100 * $groupfiles{$section} / $xferfiles);
188    write;
189
190 }
191
192 if ( $xferfiles < 1 ) { $xferfiles = 1; }
193 if ( $xferbytes < 1 ) { $xferbytes = 1; }
194 }
195
196 if ($opt_d) {
197 format top3 =
198
199 Total Transfer Amount By Domain
200
201              Number Of    Number of    Percent Of  Percent Of
202 Domain Name  Files Sent    MB Sent     Files Sent  Bytes Sent
203 -----------  ----------  ------------  ----------  ----------
204 .
205
206 format line3 =
207 @<<<<<<<<<<  @>>>>>>>>>  @>>>>>>>>>>>  @>>>>>>>    @>>>>>>>  
208 $domain,     $files,     $bytes/(1024*1024), $pctfiles,  $pctbytes
209 .
210
211 $- = 0;
212 $^ = top3;
213 $~ = line3;
214
215 foreach $domain ( sort domnamcompare keys(domainfiles) ) {
216
217    if ( $domainsecs{$domain} < 1 ) { $domainsecs{$domain} = 1; }
218
219    $files = $domainfiles{$domain};
220    $bytes = $domainbytes{$domain};
221    $pctfiles = sprintf("%8.2f", 100 * $domainfiles{$domain} / $xferfiles);
222    $pctbytes = sprintf("%8.2f", 100 * $domainbytes{$domain} / $xferbytes);
223    write;
224
225 }
226
227 }
228
229 if ($opt_h) {
230
231 format top8 =
232
233 Hourly Transmission Statistics
234
235                  Number Of    Number of   Percent Of  Percent Of
236      Time        Files Sent    MB  Sent   Files Sent  Bytes Sent
237 ---------------  ----------  -----------  ----------  ----------
238 .
239
240 format line8 =
241 @<<<<<<<<<<<<<<  @>>>>>>>>>  @>>>>>>>>>>  @>>>>>>>    @>>>>>>>  
242 $hour,           $nfiles,    $nbytes/(1024*1024), $pctfiles,  $pctbytes
243 .
244
245
246 $| = 1;
247 $- = 0;
248 $^ = top8;
249 $~ = line8;
250
251 foreach $hour ( sort keys(xfertbytes) ) {
252
253    $nfiles   = $xfertfiles{$hour};
254    $nbytes   = $xfertbytes{$hour};
255    $pctfiles = sprintf("%8.2f", 100*$xfertfiles{$hour} / $xferfiles);
256    $pctbytes = sprintf("%8.2f", 100*$xfertbytes{$hour} / $xferbytes);
257    write;
258 }
259 }
260 exit(0);
261
262 sub datecompare {
263     $a gt $b;
264 }
265
266 sub domnamcompare {
267
268    $sdiff = length($a) - length($b);
269    ($sdiff < 0) ? -1 : ($sdiff > 0) ? 1 : ($a lt $b) ? -1 : ($a gt $b) ? 1 : 0;
270
271 }
272
273 sub bytecompare {
274
275    $bdiff = $groupbytes{$b} - $groupbytes{$a};
276    ($bdiff < 0) ? -1 : ($bdiff > 0) ? 1 : ($a lt $b) ? -1 : ($a gt $b) ? 1 : 0;
277
278 }
279
280 sub faccompare {
281
282    $fdiff = $fac{$b} - $fac{$a};
283    ($fdiff < 0) ? -1 : ($fdiff > 0) ? 1 : ($a lt $b) ? -1 : ($a gt $b) ? 1 : 0;
284
285 }
286