628db7059c4660617a5800d46696f9cce0f9d4c7
[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-2007  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 3.1.0beta0, released 3 Sep 2007.
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 info");
49     my $Privileged = CheckPermission();
50
51     foreach my $host ( GetUserHosts(1) ) {
52         my($fullDur, $incrCnt, $incrAge, $fullSize, $fullRate, $reasonHilite,
53            $lastAge, $tempState, $tempReason);
54         my($shortErr);
55         my @Backups = $bpc->BackupInfoRead($host);
56         my $fullCnt = $incrCnt = 0;
57         my $fullAge = $incrAge = $lastAge = -1;
58
59         $bpc->ConfigRead($host);
60         %Conf = $bpc->Conf();
61
62         next if ( $Conf{XferMethod} eq "archive" );
63         next if ( !$Privileged && !CheckPermission($host) );
64
65         for ( my $i = 0 ; $i < @Backups ; $i++ ) {
66             if ( $Backups[$i]{type} eq "full" ) {
67                 $fullCnt++;
68                 if ( $fullAge < 0 || $Backups[$i]{startTime} > $fullAge ) {
69                     $fullAge  = $Backups[$i]{startTime};
70                     $fullSize = $Backups[$i]{size} / (1024 * 1024);
71                     $fullDur  = $Backups[$i]{endTime} - $Backups[$i]{startTime};
72                 }
73                 $fullSizeTot += $Backups[$i]{size} / (1024 * 1024);
74             } else {
75                 $incrCnt++;
76                 if ( $incrAge < 0 || $Backups[$i]{startTime} > $incrAge ) {
77                     $incrAge = $Backups[$i]{startTime};
78                 }
79                 $incrSizeTot += $Backups[$i]{size} / (1024 * 1024);
80             }
81         }
82         if ( $fullAge > $incrAge && $fullAge >= 0 )  {
83             $lastAge = $fullAge;
84         } else {
85             $lastAge = $incrAge;
86         }
87         if ( $lastAge < 0 ) {
88             $lastAge = "";
89         } else {
90             $lastAge = sprintf("%.1f", (time - $lastAge) / (24 * 3600));
91         }
92         if ( $fullAge < 0 ) {
93             $fullAge = "";
94             $fullRate = "";
95         } else {
96             $fullAge = sprintf("%.1f", (time - $fullAge) / (24 * 3600));
97             $fullRate = sprintf("%.2f",
98                                 $fullSize / ($fullDur <= 0 ? 1 : $fullDur));
99         }
100         if ( $incrAge < 0 ) {
101             $incrAge = "";
102         } else {
103             $incrAge = sprintf("%.1f", (time - $incrAge) / (24 * 3600));
104         }
105         $fullTot += $fullCnt;
106         $incrTot += $incrCnt;
107         $fullSize = sprintf("%.2f", $fullSize / 1000);
108         $incrAge = "&nbsp;" if ( $incrAge eq "" );
109         $reasonHilite = $Conf{CgiStatusHilightColor}{$Status{$host}{reason}}
110                       || $Conf{CgiStatusHilightColor}{$Status{$host}{state}};
111         if ( $Conf{BackupsDisable} == 1 ) {
112             if ( $Status{$host}{state} ne "Status_backup_in_progress"
113                     && $Status{$host}{state} ne "Status_restore_in_progress" ) {
114                 $reasonHilite = $Conf{CgiStatusHilightColor}{Disabled_OnlyManualBackups};
115                 $tempState = "Disabled_OnlyManualBackups";
116                 $tempReason = "";
117             } else {
118                 $tempState = $Status{$host}{state};
119                 $tempReason = $Status{$host}{reason};
120             }
121         } elsif ($Conf{BackupsDisable} == 2 ) {
122             $reasonHilite = $Conf{CgiStatusHilightColor}{Disabled_AllBackupsDisabled};
123             $tempState = "Disabled_AllBackupsDisabled";
124             $tempReason = "";
125         } else {
126             $tempState = $Status{$host}{state};
127             $tempReason = $Status{$host}{reason};
128         }
129         $reasonHilite = " bgcolor=\"$reasonHilite\"" if ( $reasonHilite ne "" );
130         if ( $tempState ne "Status_backup_in_progress"
131                 && $tempState ne "Status_restore_in_progress"
132                 && $Conf{BackupsDisable} == 0
133                 && $Status{$host}{error} ne "" ) {
134             ($shortErr = $Status{$host}{error}) =~ s/(.{48}).*/$1.../;
135             $shortErr = " ($shortErr)";
136         }
137
138         $str = <<EOF;
139 <tr$reasonHilite><td class="border"> ${HostLink($host)} </td>
140     <td align="center" class="border"> ${UserLink(defined($Hosts->{$host})
141                                     ? $Hosts->{$host}{user} : "")} </td>
142     <td align="center" class="border"> $fullCnt </td>
143     <td align="center" class="border"> $fullAge </td>
144     <td align="center" class="border"> $fullSize </td>
145     <td align="center" class="border"> $fullRate </td>
146     <td align="center" class="border"> $incrCnt </td>
147     <td align="center" class="border"> $incrAge </td>
148     <td align="center" class="border"> $lastAge </td> 
149     <td align="center" class="border"> $Lang->{$tempState} </td>
150     <td class="border"> $Lang->{$tempReason}$shortErr </td></tr>
151 EOF
152         if ( @Backups == 0 ) {
153             $hostCntNone++;
154             $strNone .= $str;
155         } else {
156             $hostCntGood++;
157             $strGood .= $str;
158         }
159     }
160     $fullSizeTot = sprintf("%.2f", $fullSizeTot / 1000);
161     $incrSizeTot = sprintf("%.2f", $incrSizeTot / 1000);
162     my $now      = timeStamp2(time);
163     my $DUlastTime   = timeStamp2($Info{DUlastValueTime});
164     my $DUmaxTime    = timeStamp2($Info{DUDailyMaxTime});
165
166     my $content = eval ("qq{$Lang->{BackupPC_Summary}}");
167     Header($Lang->{BackupPC__Server_Summary}, $content);
168     Trailer();
169 }
170
171 1;