Added an already running check
[zfs-nexenta/.git] / zfs-snapshot-totals
1 #!/usr/bin/perl
2
3 @snapshots=`zfs list -t snapshot`;
4
5 $kilo = 1024;
6 $mega = 1024 * 1024;
7 $giga = 1024 * 1024 * 1024;
8 $maxlen = 0;
9
10 sub adjust_size
11 {
12   my ($size) = @_;
13   if($size > ($giga)) { $size = $size / $giga; $size = sprintf("%2.2fG",$size); }
14   elsif($size > ($mega)) { $size = int($size / $mega); $size = "$size"."M"; }
15   elsif($size > ($kilo)) { $size = int($size / $kilo); $size = "$size"."K"; }
16   return $size;
17 }
18
19 foreach $snapshot (@snapshots)
20 {
21   chomp($snapshot);
22   if($snapshot =~ /(\S+)\@(\S+)\s+(\S+)\s+/) {
23     $filesystem = $1;
24     $size = $3;
25     if(length($filesystem) > $maxlen) { $maxlen = length($filesystem); }
26     if($size =~ /k/i) { $size = $size * $kilo; }
27     if($size =~ /m/i) { $size = $size * $mega; }
28     if($size =~ /g/i) { $size = $size * $giga; }
29     $totals{$filesystem}{size} += $size;
30     $totals{$filesystem}{snapshots}++;
31   }
32 }
33
34 $maxlen=$maxlen+2;
35 printf "%-${maxlen}s %-15s %-10s\n","ZFS Filesystem","Snapshots Size","Num Snapshots";
36 printf "%-${maxlen}s %-15s %-10s\n","--","--","--";
37 foreach $key (sort keys %totals)
38 {
39   $size = $totals{$key}{size};
40   $total_size += $size;
41   $total_snapshots += $totals{$key}{snapshots};
42   $size = &adjust_size($size);
43   printf "%-${maxlen}s %-15s %-10s\n", $key, $size, $totals{$key}{snapshots};
44 }
45
46 $total_size = &adjust_size($total_size);
47 printf "%-${maxlen}s %-15s %-10s\n","--","--","--";
48 printf "%-${maxlen}s %-15s %-10s\n","Total Snapshots",$total_size,$total_snapshots;
49