50e730b380c32da02625331b6c81b4de6c0b7579
[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 3 Jul 2003.
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() ) { ## give access to users hosts only
52     ## foreach my $host ( sort(keys(%Status)) ) {
53         my($fullDur, $incrCnt, $incrAge, $fullSize, $fullRate, $reasonHilite);
54         my($shortErr);
55         my @Backups = $bpc->BackupInfoRead($host);
56         my $fullCnt = $incrCnt = 0;
57         my $fullAge = $incrAge = -1;
58
59         if ( defined(my $error = $bpc->ConfigRead($host)) ) {
60             print("dump failed: Can't read PC's config file: $error\n");
61             exit(1);
62         }
63         %Conf = $bpc->Conf();
64
65         if ($Conf{XferMethod} eq "archive" ) {
66             next;
67         }
68
69         for ( my $i = 0 ; $i < @Backups ; $i++ ) {
70             if ( $Backups[$i]{type} eq "full" ) {
71                 $fullCnt++;
72                 if ( $fullAge < 0 || $Backups[$i]{startTime} > $fullAge ) {
73                     $fullAge  = $Backups[$i]{startTime};
74                     $fullSize = $Backups[$i]{size} / (1024 * 1024);
75                     $fullDur  = $Backups[$i]{endTime} - $Backups[$i]{startTime};
76                 }
77                 $fullSizeTot += $Backups[$i]{size} / (1024 * 1024);
78             } else {
79                 $incrCnt++;
80                 if ( $incrAge < 0 || $Backups[$i]{startTime} > $incrAge ) {
81                     $incrAge = $Backups[$i]{startTime};
82                 }
83                 $incrSizeTot += $Backups[$i]{size} / (1024 * 1024);
84             }
85         }
86         if ( $fullAge < 0 ) {
87             $fullAge = "";
88             $fullRate = "";
89         } else {
90             $fullAge = sprintf("%.1f", (time - $fullAge) / (24 * 3600));
91             $fullRate = sprintf("%.2f",
92                                 $fullSize / ($fullDur <= 0 ? 1 : $fullDur));
93         }
94         if ( $incrAge < 0 ) {
95             $incrAge = "";
96         } else {
97             $incrAge = sprintf("%.1f", (time - $incrAge) / (24 * 3600));
98         }
99         $fullTot += $fullCnt;
100         $incrTot += $incrCnt;
101         $fullSize = sprintf("%.2f", $fullSize / 1000);
102         $incrAge = "&nbsp;" if ( $incrAge eq "" );
103         $reasonHilite = $Conf{CgiStatusHilightColor}{$Status{$host}{reason}}
104                       || $Conf{CgiStatusHilightColor}{$Status{$host}{state}};
105         $reasonHilite = " bgcolor=\"$reasonHilite\"" if ( $reasonHilite ne "" );
106         if ( $Status{$host}{state} ne "Status_backup_in_progress"
107                 && $Status{$host}{state} ne "Status_restore_in_progress"
108                 && $Status{$host}{error} ne "" ) {
109             ($shortErr = $Status{$host}{error}) =~ s/(.{48}).*/$1.../;
110             $shortErr = " ($shortErr)";
111         }
112
113         $str = <<EOF;
114 <tr$reasonHilite><td class="border"> ${HostLink($host)} </td>
115     <td align="center" class="border"> ${UserLink(defined($Hosts->{$host})
116                                     ? $Hosts->{$host}{user} : "")} </td>
117     <td align="center" class="border"> $fullCnt </td>
118     <td align="center" class="border"> $fullAge </td>
119     <td align="center" class="border"> $fullSize </td>
120     <td align="center" class="border"> $fullRate </td>
121     <td align="center" class="border"> $incrCnt </td>
122     <td align="center" class="border"> $incrAge </td>
123     <td align="center" class="border"> $Lang->{$Status{$host}{state}} </td>
124     <td class="border"> $Lang->{$Status{$host}{reason}}$shortErr </td></tr>
125 EOF
126         if ( @Backups == 0 ) {
127             $hostCntNone++;
128             $strNone .= $str;
129         } else {
130             $hostCntGood++;
131             $strGood .= $str;
132         }
133     }
134     $fullSizeTot = sprintf("%.2f", $fullSizeTot / 1000);
135     $incrSizeTot = sprintf("%.2f", $incrSizeTot / 1000);
136     my $now      = timeStamp2(time);
137
138     my $content = eval ("qq{$Lang->{BackupPC_Summary}}");
139     Header($Lang->{BackupPC__Server_Summary}, $content);
140     Trailer();
141 }
142
143 1;