* Split BackupPC_Admin into a set of modules, one for each major action.
[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     if ( !$Privileged ) {
52         ErrorExit($Lang->{Only_privileged_users_can_view_PC_summaries} );
53     }
54     foreach my $host ( sort(keys(%Status)) ) {
55         my($fullDur, $incrCnt, $incrAge, $fullSize, $fullRate, $reasonHilite);
56         my($shortErr);
57         my @Backups = $bpc->BackupInfoRead($host);
58         my $fullCnt = $incrCnt = 0;
59         my $fullAge = $incrAge = -1;
60         for ( my $i = 0 ; $i < @Backups ; $i++ ) {
61             if ( $Backups[$i]{type} eq "full" ) {
62                 $fullCnt++;
63                 if ( $fullAge < 0 || $Backups[$i]{startTime} > $fullAge ) {
64                     $fullAge  = $Backups[$i]{startTime};
65                     $fullSize = $Backups[$i]{size} / (1024 * 1024);
66                     $fullDur  = $Backups[$i]{endTime} - $Backups[$i]{startTime};
67                 }
68                 $fullSizeTot += $Backups[$i]{size} / (1024 * 1024);
69             } else {
70                 $incrCnt++;
71                 if ( $incrAge < 0 || $Backups[$i]{startTime} > $incrAge ) {
72                     $incrAge = $Backups[$i]{startTime};
73                 }
74                 $incrSizeTot += $Backups[$i]{size} / (1024 * 1024);
75             }
76         }
77         if ( $fullAge < 0 ) {
78             $fullAge = "";
79             $fullRate = "";
80         } else {
81             $fullAge = sprintf("%.1f", (time - $fullAge) / (24 * 3600));
82             $fullRate = sprintf("%.2f",
83                                 $fullSize / ($fullDur <= 0 ? 1 : $fullDur));
84         }
85         if ( $incrAge < 0 ) {
86             $incrAge = "";
87         } else {
88             $incrAge = sprintf("%.1f", (time - $incrAge) / (24 * 3600));
89         }
90         $fullTot += $fullCnt;
91         $incrTot += $incrCnt;
92         $fullSize = sprintf("%.2f", $fullSize / 1000);
93         $incrAge = "&nbsp;" if ( $incrAge eq "" );
94         $reasonHilite = $Conf{CgiStatusHilightColor}{$Status{$host}{reason}}
95                       || $Conf{CgiStatusHilightColor}{$Status{$host}{state}};
96         $reasonHilite = " bgcolor=\"$reasonHilite\"" if ( $reasonHilite ne "" );
97         if ( $Status{$host}{state} ne "Status_backup_in_progress"
98                 && $Status{$host}{state} ne "Status_restore_in_progress"
99                 && $Status{$host}{error} ne "" ) {
100             ($shortErr = $Status{$host}{error}) =~ s/(.{48}).*/$1.../;
101             $shortErr = " ($shortErr)";
102         }
103
104         $str = <<EOF;
105 <tr$reasonHilite><td> ${HostLink($host)} </td>
106     <td align="center"> ${UserLink(defined($Hosts->{$host})
107                                     ? $Hosts->{$host}{user} : "")} </td>
108     <td align="center"> $fullCnt </td>
109     <td align="center"> $fullAge </td>
110     <td align="center"> $fullSize </td>
111     <td align="center"> $fullRate </td>
112     <td align="center"> $incrCnt </td>
113     <td align="center"> $incrAge </td>
114     <td align="center"> $Lang->{$Status{$host}{state}} </td>
115     <td> $Lang->{$Status{$host}{reason}}$shortErr </td></tr>
116 EOF
117         if ( @Backups == 0 ) {
118             $hostCntNone++;
119             $strNone .= $str;
120         } else {
121             $hostCntGood++;
122             $strGood .= $str;
123         }
124     }
125     $fullSizeTot = sprintf("%.2f", $fullSizeTot / 1000);
126     $incrSizeTot = sprintf("%.2f", $incrSizeTot / 1000);
127     my $now      = timeStamp2(time);
128
129     Header($Lang->{BackupPC__Server_Summary});
130     print eval ("qq{$Lang->{BackupPC_Summary}}");
131
132     Trailer();
133 }
134
135 1;