Added an already running check
[zfs-nexenta/.git] / zfs-log-parser
1 #!/usr/bin/perl
2
3 # Author: Alan J. Pippin
4 # Description: This script parses logfiles generated by the following zfs scripts:
5 #              zfs-replicate
6
7 $logfile=shift(@ARGV);
8 $startdate=shift(@ARGV);
9 $backup_pool="backups";
10
11 sub usage {
12     print "Usage: $0 <log file> <date>\n";
13     exit 0;
14 }
15 if(! -f "$logfile") { &usage; }
16
17 $kilo = 1024;
18 $mega = 1024 * 1024;
19 $giga = 1024 * 1024 * 1024;
20
21 %month2num = ( "Jan" => "01", "Feb" => "02", "Mar" => "03", "Apr" => "04", "May" => "05", "Jun" => "06",
22                "Jul" => "07", "Aug" => "08", "Sep" => "09", "Oct" => "10", "Nov" => "11", "Dec" => "12");
23
24 sub time_to_seconds {
25     my ($hour,$minute,$sec) = @_;
26     $seconds = ($hour * 60 * 60) + ($minute * 60) + ($sec);
27     return($seconds);
28 }
29
30 sub adjust_duration {
31     my ($duration) = @_;
32     if($duration > 3600) { $duration=int($duration/3600); $duration.="h"; }
33     elsif($duration > 60) { $duration=int($duration/60); $duration.="m"; }
34     else { $duration.="s"; }
35     return $duration;
36 }
37
38 sub adjust_data {
39     my ($data) = @_;
40     if(abs($data) > ($giga)) { $data = $data / $giga; $data = sprintf("%2.2fGb",$data); }
41     elsif(abs($data) > ($mega)) { $data = int($data / $mega); $data = "$data"."Mb"; }
42     elsif(abs($data) > ($kilo)) { $data = int($data / $kilo); $data = "$data"."Kb"; }
43     return $data;
44 }
45
46 sub parse_replicate_logfile {
47     $in_replicate=0;
48     $date="";
49     %totals=();
50     while(<FILE>) {
51         $line = $_;
52         if(($in_replicate == 0) && ("$startdate" ne "") && ($line !~ /$startdate/)) { next; }
53         if($line =~ /(\S+)\s+(\S+)\s+(\d+)\s+(\d+):(\d+):(\d+)\s+(\S+)\s+(\S+)/) {
54             $dayname=$1; $month=$2; $daynum=$3; $hour=$4; $minute=$5; $sec=$6; $year=$8;
55             if(($in_replicate == 0) && ($line =~ /replicate started/)) {
56                 $in_replicate = 1;
57                 $date="$dayname $month $daynum $hour:$minute:$sec $year";
58                 $totals{$date}{data} = 0;
59                 $totals{$date}{transfertime} = 0;
60                 $totals{$date}{duration} = time_to_seconds($hour,$minute,$sec);
61             }
62             elsif(($in_replicate == 1) && ($line=~ /replicate complete/)) {
63                 $in_replicate = 0;
64                 $totals{$date}{duration} = time_to_seconds($hour,$minute,$sec) - $totals{$date}{duration};
65             }
66         }   
67         if(($in_replicate == 1) && ($line =~ /received ([\d\.]+)(\w+)/)) {
68             $data = $1; $size = $2;
69             if($size =~ /Kb/i) { $data = $data * $kilo; }
70             if($size =~ /Mb/i) { $data = $data * $mega; }
71             if($size =~ /Gb/i) { $data = $data * $giga; }
72             chomp($line);
73             $totals{$date}{data} += $data;
74         }
75         if(($in_replicate == 1) && ($line =~ /in (\d+) seconds/)) {
76             $transfertime = $1;
77             $totals{$date}{transfertime} += $transfertime;
78         }
79     }
80     
81     foreach $date (keys %totals) {
82         $duration=adjust_duration($totals{$date}{duration});
83         $data=adjust_data($totals{$date}{data});
84         $transfertime=adjust_duration($totals{$date}{transfertime});
85         if($totals{$date}{transfertime} > 0) { 
86           $rate = adjust_data(int($totals{$date}{data}/$totals{$date}{transfertime}));
87         } else { 
88           $rate = 0;
89         }
90         print "$date: data=${data} transfertime=$transfertime rate=${rate}/sec duration=$duration\n";
91     }
92 }
93
94 sub parse_snapshot_totals_logfile {
95     %totals=();
96     $in_totals=0;
97     $maxlen=0;
98     $found_startdate=0;
99     $header="";
100     while(<FILE>) {
101         $line = $_;
102         if($line =~ /logfile turned over/) { next; }
103         if(($in_totals == 0) && ("$startdate" ne "") && ($line !~ /$startdate/) && ($found_startdate==0)) { next; }
104         if(($in_totals == 0) && ("$startdate" ne "") && ($line =~ /$startdate/) && ($found_startdate==0)) { $found_startdate=1; }
105         if(($in_totals == 0) && ($line =~ /(\S+)\s+(\S+)\s+(\d+)\s+(\d+):(\d+):(\d+)\s+(\S+)\s+(\S+)/)) {
106             $dayname=$1; $month=$2; $daynum=$3; $hour=$4; $minute=$5; $sec=$6; $year=$8;
107             $in_totals = 1;
108             $month = $month2num{$month};
109             if($daynum < 10) { $daynum = "0".$daynum; }
110             $date="$month-$daynum-$year";
111             if(!defined $founddates{$date}) { $header .= $date . " "; }
112             $founddates{$date} = 1;
113         }
114         elsif(($in_totals == 1) && ($line =~ /^\s+$/)) {
115             $in_totals = 0;
116         }
117         elsif(($in_totals == 1) && ($line =~ /(\S+)\s+([\d\.]*)(\w+)\s+(\d+)/)) {
118             $filesystem = $1; $data = $2; $size = $3; $num_snaps = $4;
119             if($filesystem =~ /Snapshots/ || $filesystem =~ /Total/) { next; }
120             if($filesystem =~ /^$backup_pool/) { next; }
121             if(length($filesystem) > $maxlen) { $maxlen = length($filesystem); }
122             if($size =~ /K/i) { $data = $data * $kilo; }
123             if($size =~ /M/i) { $data = $data * $mega; }
124             if($size =~ /G/i) { $data = $data * $giga; }
125             chomp($line);
126             $totals{$filesystem}{$date}{data} = $data;
127             if($totals{$filesystem}{start_data} == 0) { $totals{$filesystem}{start_data} = $data; }
128             $totals{$filesystem}{delta} = $data - $totals{$filesystem}{start_data};
129         }
130     }
131     $total_delta=0;
132     printf("%-${maxlen}s %s %s\n","ZFS Filesystem","$header","Net Change");
133     foreach $filesystem (sort keys %totals) {
134         $hashref = $totals{$filesystem};
135         $data="";
136         foreach $date (sort keys %$hashref) {
137             if($date !~ /(\d+)-(\d+)-(\d+)/) { next; }
138             $date_data=adjust_data($totals{$filesystem}{$date}{data});
139             if($date_data eq "") { $date_data = "0"; }
140             $data .= sprintf("%10s",$date_data). " ";
141             $date_totals{$date}{data} += $totals{$filesystem}{$date}{data};
142         }
143         $data_total=adjust_data($data_total);
144         $total_delta+=$totals{$filesystem}{delta};
145         $delta=adjust_data($totals{$filesystem}{delta});
146         printf("%-${maxlen}s %s %10s\n",$filesystem,$data,$delta);
147     }
148     $data="";
149     $data_len=length($data);
150     $total_delta=adjust_data($total_delta);
151     foreach $date (sort keys %date_totals) {
152         $date_data=adjust_data($date_totals{$date}{data});
153         $data .= sprintf("%10s",$date_data). " ";
154     }
155     printf("%-${maxlen}s %-${data_len}s %10s\n","Totals:",$data,$total_delta);
156 }
157
158 #########
159 # MAIN
160 #########
161 #print "-> Parsing $logfile\n";
162 if("$logfile" =~ /\.[bz2|gz]/) { 
163   open(FILE,"zcat $logfile|") || die "-E- Unable to open $logfile\n";
164 } else {
165   open(FILE,"$logfile") || die "-E- Unable to open $logfile\n";
166 }
167
168 if($logfile =~ /replicate/) { parse_replicate_logfile(); }
169 if($logfile =~ /snapshot-totals/) { parse_snapshot_totals_logfile(); }
170
171 close(FILE);