* Various changes for 3.0.0beta1
[BackupPC.git] / lib / BackupPC / CGI / RSS.pm
1 #=============================================================
2 #
3 # BackupPC::CGI::RSS package
4 #
5 # DESCRIPTION
6 #
7 #   This module implements an RSS page for the CGI interface.
8 #
9 # AUTHOR
10 #   Rich Duzenbury (rduz at theduz dot com)
11 #
12 # COPYRIGHT
13 #   Copyright (C) 2005  Rich Duzenbury and 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.0.0beta1, released 30 Jul 2006.
32 #
33 # See http://backuppc.sourceforge.net.
34 #
35 #========================================================================
36
37 package BackupPC::CGI::RSS;
38
39 use strict;
40 use BackupPC::CGI::Lib qw(:all);
41 use XML::RSS;
42
43 sub action
44 {
45     my $base_url = 'http://' . $ENV{'SERVER_NAME'} . $ENV{SCRIPT_NAME};
46
47     my($fullTot, $fullSizeTot, $incrTot, $incrSizeTot, $str,
48        $strNone, $strGood, $hostCntGood, $hostCntNone);
49
50     binmode(STDOUT, ":utf8");
51
52     my $rss = new XML::RSS (version => '0.91',
53                             encoding => 'utf-8');
54
55     $rss->channel( title => eval("qq{$Lang->{RSS_Doc_Title}}"),
56                    link => $base_url,
57                    language => $Conf{Language},
58                    description => eval("qq{$Lang->{RSS_Doc_Description}}"),
59                );
60
61     $hostCntGood = $hostCntNone = 0;
62     GetStatusInfo("hosts");
63     my $Privileged = CheckPermission();
64
65     foreach my $host ( GetUserHosts(1) ) {
66         my($fullDur, $incrCnt, $incrAge, $fullSize, $fullRate, $reasonHilite);
67         my($shortErr);
68         my @Backups = $bpc->BackupInfoRead($host);
69         my $fullCnt = $incrCnt = 0;
70         my $fullAge = $incrAge = -1;
71
72         $bpc->ConfigRead($host);
73         %Conf = $bpc->Conf();
74
75         next if ( $Conf{XferMethod} eq "archive" );
76         next if ( !$Privileged && !CheckPermission($host) );
77
78         for ( my $i = 0 ; $i < @Backups ; $i++ ) {
79             if ( $Backups[$i]{type} eq "full" ) {
80                 $fullCnt++;
81                 if ( $fullAge < 0 || $Backups[$i]{startTime} > $fullAge ) {
82                     $fullAge  = $Backups[$i]{startTime};
83                     $fullSize = $Backups[$i]{size} / (1024 * 1024);
84                     $fullDur  = $Backups[$i]{endTime} - $Backups[$i]{startTime};
85                 }
86                 $fullSizeTot += $Backups[$i]{size} / (1024 * 1024);
87             } else {
88                 $incrCnt++;
89                 if ( $incrAge < 0 || $Backups[$i]{startTime} > $incrAge ) {
90                     $incrAge = $Backups[$i]{startTime};
91                 }
92                 $incrSizeTot += $Backups[$i]{size} / (1024 * 1024);
93             }
94         }
95         if ( $fullAge < 0 ) {
96             $fullAge = "";
97             $fullRate = "";
98         } else {
99             $fullAge = sprintf("%.1f", (time - $fullAge) / (24 * 3600));
100             $fullRate = sprintf("%.2f",
101                                 $fullSize / ($fullDur <= 0 ? 1 : $fullDur));
102         }
103         if ( $incrAge < 0 ) {
104             $incrAge = "";
105         } else {
106             $incrAge = sprintf("%.1f", (time - $incrAge) / (24 * 3600));
107         }
108         $fullTot += $fullCnt;
109         $incrTot += $incrCnt;
110         $fullSize = sprintf("%.2f", $fullSize / 1000);
111         $incrAge = "&nbsp;" if ( $incrAge eq "" );
112         $reasonHilite = $Conf{CgiStatusHilightColor}{$Status{$host}{reason}}
113                       || $Conf{CgiStatusHilightColor}{$Status{$host}{state}};
114         $reasonHilite = " bgcolor=\"$reasonHilite\"" if ( $reasonHilite ne "" );
115         if ( $Status{$host}{state} ne "Status_backup_in_progress"
116                 && $Status{$host}{state} ne "Status_restore_in_progress"
117                 && $Status{$host}{error} ne "" ) {
118             ($shortErr = $Status{$host}{error}) =~ s/(.{48}).*/$1.../;
119             $shortErr = " ($shortErr)";
120         }
121
122         my $host_state = $Lang->{$Status{$host}{state}};
123         my $host_last_attempt =  $Lang->{$Status{$host}{reason}} . $shortErr;
124
125         $str = eval("qq{$Lang->{RSS_Host_Summary}}");
126
127         $rss->add_item(title => $host . ', ' . 
128                                 $host_state . ', ' . 
129                                 $host_last_attempt,
130                        link => $base_url . '?host=' . $host,
131                        description => $str);
132     }
133
134     $fullSizeTot = sprintf("%.2f", $fullSizeTot / 1000);
135     $incrSizeTot = sprintf("%.2f", $incrSizeTot / 1000);
136     my $now      = timeStamp2(time);
137
138     print 'Content-type: text/xml', "\n\n",
139           $rss->as_string;
140
141 }
142
143 1;