Lots of changes:
[BackupPC.git] / lib / BackupPC / CGI / Summary.pm
1 #============================================================= -*-perl-*-
2 #
3 # BackupPC::CGI::Summary package
4 #
5 # DESCRIPTION
6 #
7 #   This module implements the Summary action for the CGI interface.
8 #
9 # AUTHOR
10 #   Craig Barratt  <cbarratt@users.sourceforge.net>
11 #
12 # COPYRIGHT
13 #   Copyright (C) 2003  Craig Barratt
14 #
15 #   This program is free software; you can redistribute it and/or modify
16 #   it under the terms of the GNU General Public License as published by
17 #   the Free Software Foundation; either version 2 of the License, or
18 #   (at your option) any later version.
19 #
20 #   This program is distributed in the hope that it will be useful,
21 #   but WITHOUT ANY WARRANTY; without even the implied warranty of
22 #   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23 #   GNU General Public License for more details.
24 #
25 #   You should have received a copy of the GNU General Public License
26 #   along with this program; if not, write to the Free Software
27 #   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
28 #
29 #========================================================================
30 #
31 # Version 2.1.0_CVS, released 8 Feb 2004.
32 #
33 # See http://backuppc.sourceforge.net.
34 #
35 #========================================================================
36
37 package BackupPC::CGI::Summary;
38
39 use strict;
40 use BackupPC::CGI::Lib qw(:all);
41
42 sub action
43 {
44     my($fullTot, $fullSizeTot, $incrTot, $incrSizeTot, $str,
45        $strNone, $strGood, $hostCntGood, $hostCntNone);
46
47     $hostCntGood = $hostCntNone = 0;
48     GetStatusInfo("hosts");
49     my $Privileged = CheckPermission();
50
51     foreach my $host ( GetUserHosts(undef, 1) ) {
52         my($fullDur, $incrCnt, $incrAge, $fullSize, $fullRate, $reasonHilite);
53         my($shortErr);
54         my @Backups = $bpc->BackupInfoRead($host);
55         my $fullCnt = $incrCnt = 0;
56         my $fullAge = $incrAge = -1;
57
58         if ( defined(my $error = $bpc->ConfigRead($host)) ) {
59             print("dump failed: Can't read PC's config file: $error\n");
60             exit(1);
61         }
62         %Conf = $bpc->Conf();
63
64         if ($Conf{XferMethod} eq "archive" ) {
65             next;
66         }
67
68         for ( my $i = 0 ; $i < @Backups ; $i++ ) {
69             if ( $Backups[$i]{type} eq "full" ) {
70                 $fullCnt++;
71                 if ( $fullAge < 0 || $Backups[$i]{startTime} > $fullAge ) {
72                     $fullAge  = $Backups[$i]{startTime};
73                     $fullSize = $Backups[$i]{size} / (1024 * 1024);
74                     $fullDur  = $Backups[$i]{endTime} - $Backups[$i]{startTime};
75                 }
76                 $fullSizeTot += $Backups[$i]{size} / (1024 * 1024);
77             } else {
78                 $incrCnt++;
79                 if ( $incrAge < 0 || $Backups[$i]{startTime} > $incrAge ) {
80                     $incrAge = $Backups[$i]{startTime};
81                 }
82                 $incrSizeTot += $Backups[$i]{size} / (1024 * 1024);
83             }
84         }
85         if ( $fullAge < 0 ) {
86             $fullAge = "";
87             $fullRate = "";
88         } else {
89             $fullAge = sprintf("%.1f", (time - $fullAge) / (24 * 3600));
90             $fullRate = sprintf("%.2f",
91                                 $fullSize / ($fullDur <= 0 ? 1 : $fullDur));
92         }
93         if ( $incrAge < 0 ) {
94             $incrAge = "";
95         } else {
96             $incrAge = sprintf("%.1f", (time - $incrAge) / (24 * 3600));
97         }
98         $fullTot += $fullCnt;
99         $incrTot += $incrCnt;
100         $fullSize = sprintf("%.2f", $fullSize / 1000);
101         $incrAge = "&nbsp;" if ( $incrAge eq "" );
102         $reasonHilite = $Conf{CgiStatusHilightColor}{$Status{$host}{reason}}
103                       || $Conf{CgiStatusHilightColor}{$Status{$host}{state}};
104         $reasonHilite = " bgcolor=\"$reasonHilite\"" if ( $reasonHilite ne "" );
105         if ( $Status{$host}{state} ne "Status_backup_in_progress"
106                 && $Status{$host}{state} ne "Status_restore_in_progress"
107                 && $Status{$host}{error} ne "" ) {
108             ($shortErr = $Status{$host}{error}) =~ s/(.{48}).*/$1.../;
109             $shortErr = " ($shortErr)";
110         }
111
112         $str = <<EOF;
113 <tr$reasonHilite><td class="border"> ${HostLink($host)} </td>
114     <td align="center" class="border"> ${UserLink(defined($Hosts->{$host})
115                                     ? $Hosts->{$host}{user} : "")} </td>
116     <td align="center" class="border"> $fullCnt </td>
117     <td align="center" class="border"> $fullAge </td>
118     <td align="center" class="border"> $fullSize </td>
119     <td align="center" class="border"> $fullRate </td>
120     <td align="center" class="border"> $incrCnt </td>
121     <td align="center" class="border"> $incrAge </td>
122     <td align="center" class="border"> $Lang->{$Status{$host}{state}} </td>
123     <td class="border"> $Lang->{$Status{$host}{reason}}$shortErr </td></tr>
124 EOF
125         if ( @Backups == 0 ) {
126             $hostCntNone++;
127             $strNone .= $str;
128         } else {
129             $hostCntGood++;
130             $strGood .= $str;
131         }
132     }
133     $fullSizeTot = sprintf("%.2f", $fullSizeTot / 1000);
134     $incrSizeTot = sprintf("%.2f", $incrSizeTot / 1000);
135     my $now      = timeStamp2(time);
136
137     my $content = eval ("qq{$Lang->{BackupPC_Summary}}");
138     Header($Lang->{BackupPC__Server_Summary}, $content);
139     Trailer();
140 }
141
142 1;