cf58ad7efd400054bc034fdff4fe6fd06fe26ddb
[BackupPC.git] / lib / BackupPC / CGI / HostInfo.pm
1 #============================================================= -*-perl-*-
2 #
3 # BackupPC::CGI::HostInfo package
4 #
5 # DESCRIPTION
6 #
7 #   This module implements the HostInfo 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::HostInfo;
38
39 use strict;
40 use BackupPC::CGI::Lib qw(:all);
41
42 sub action
43 {
44     my $host = $1 if ( $In{host} =~ /(.*)/ );
45     my($statusStr, $startIncrStr);
46
47     $host =~ s/^\s+//;
48     $host =~ s/\s+$//;
49     return Action_GeneralInfo() if ( $host eq "" );
50     $host = lc($host)
51                 if ( !-d "$TopDir/pc/$host" && -d "$TopDir/pc/" . lc($host) );
52     if ( $host =~ /\.\./ || !-d "$TopDir/pc/$host" ) {
53         #
54         # try to lookup by user name
55         #
56         if ( !defined($Hosts->{$host}) ) {
57             foreach my $h ( keys(%$Hosts) ) {
58                 if ( $Hosts->{$h}{user} eq $host
59                         || lc($Hosts->{$h}{user}) eq lc($host) ) {
60                     $host = $h;
61                     last;
62                 }
63             }
64             CheckPermission();
65             ErrorExit(eval("qq{$Lang->{Unknown_host_or_user}}"))
66                                 if ( !defined($Hosts->{$host}) );
67         }
68         $In{host} = $host;
69     }
70     GetStatusInfo("host(${EscURI($host)})");
71     $bpc->ConfigRead($host);
72     %Conf = $bpc->Conf();
73     my $Privileged = CheckPermission($host);
74     if ( !$Privileged ) {
75         ErrorExit(eval("qq{$Lang->{Only_privileged_users_can_view_information_about}}"));
76     }
77     ReadUserEmailInfo();
78
79     if ( $Conf{XferMethod} eq "archive" ) {
80         my @Archives = $bpc->ArchiveInfoRead($host);
81         my ($ArchiveStr,$warnStr);
82
83         for ( my $i = 0 ; $i < @Archives ; $i++ ) {
84             my $startTime = timeStamp2($Archives[$i]{startTime});
85             my $dur       = $Archives[$i]{endTime} - $Archives[$i]{startTime};
86             $dur          = 1 if ( $dur <= 0 );
87             my $duration  = sprintf("%.1f", $dur / 60);
88             my $Archives_Result = $Lang->{failed};
89             if ($Archives[$i]{result} ne "failed") { $Archives_Result = $Lang->{success}; }
90             $ArchiveStr  .= <<EOF;
91 <tr><td align="center"><a href="$MyURL?action=archiveInfo&num=$Archives[$i]{num}&host=${EscURI($host)}">$Archives[$i]{num}</a> </td>
92     <td align="center"> $Archives_Result </td>
93     <td align="right"> $startTime </td>
94     <td align="right"> $duration </td>
95 </tr>
96 EOF
97         }
98         if ( $ArchiveStr ne "" ) {
99             $ArchiveStr = eval("qq{$Lang->{Archive_Summary}}");
100         }
101         if ( @Archives == 0 ) {
102             $warnStr = $Lang->{There_have_been_no_archives};
103         }
104         if ( $StatusHost{BgQueueOn} ) {
105             $statusStr .= eval("qq{$Lang->{Host_host_is_queued_on_the_background_queue_will_be_backed_up_soon}}");
106         }
107         if ( $StatusHost{UserQueueOn} ) {
108             $statusStr .= eval("qq{$Lang->{Host_host_is_queued_on_the_user_queue__will_be_backed_up_soon}}");
109         }
110         if ( $StatusHost{CmdQueueOn} ) {
111             $statusStr .= eval("qq{$Lang->{A_command_for_host_is_on_the_command_queue_will_run_soon}}");
112         }
113
114         my $content = eval("qq{$Lang->{Host__host_Archive_Summary2}}");
115         Header(eval("qq{$Lang->{Host__host_Archive_Summary}}"), $content, 1);
116         Trailer();
117         return;
118     }
119
120     #
121     # Normal, non-archive case
122     #
123     my @Backups = $bpc->BackupInfoRead($host);
124     my($str, $sizeStr, $compStr, $errStr, $warnStr);
125     for ( my $i = 0 ; $i < @Backups ; $i++ ) {
126         my $startTime = timeStamp2($Backups[$i]{startTime});
127         my $dur       = $Backups[$i]{endTime} - $Backups[$i]{startTime};
128         $dur          = 1 if ( $dur <= 0 );
129         my $duration  = sprintf("%.1f", $dur / 60);
130         my $MB        = sprintf("%.1f", $Backups[$i]{size} / (1024*1024));
131         my $MBperSec  = sprintf("%.2f", $Backups[$i]{size} / (1024*1024*$dur));
132         my $MBExist   = sprintf("%.1f", $Backups[$i]{sizeExist} / (1024*1024));
133         my $MBNew     = sprintf("%.1f", $Backups[$i]{sizeNew} / (1024*1024));
134         my($MBExistComp, $ExistComp, $MBNewComp, $NewComp);
135         if ( $Backups[$i]{sizeExist} && $Backups[$i]{sizeExistComp} ) {
136             $MBExistComp = sprintf("%.1f", $Backups[$i]{sizeExistComp}
137                                                 / (1024 * 1024));
138             $ExistComp = sprintf("%.1f%%", 100 *
139                   (1 - $Backups[$i]{sizeExistComp} / $Backups[$i]{sizeExist}));
140         }
141         if ( $Backups[$i]{sizeNew} && $Backups[$i]{sizeNewComp} ) {
142             $MBNewComp = sprintf("%.1f", $Backups[$i]{sizeNewComp}
143                                                 / (1024 * 1024));
144             $NewComp = sprintf("%.1f%%", 100 *
145                   (1 - $Backups[$i]{sizeNewComp} / $Backups[$i]{sizeNew}));
146         }
147         my $age = sprintf("%.1f", (time - $Backups[$i]{startTime}) / (24*3600));
148         my $browseURL = "$MyURL?action=browse&host=${EscURI($host)}&num=$Backups[$i]{num}";
149         my $filled = $Backups[$i]{noFill} ? $Lang->{No} : $Lang->{Yes};
150         $filled .= " ($Backups[$i]{fillFromNum}) "
151                             if ( $Backups[$i]{fillFromNum} ne "" );
152         my $ltype = $Lang->{"backupType_$Backups[$i]{type}"};
153         $str .= <<EOF;
154 <tr><td align="center" class="border"> <a href="$browseURL">$Backups[$i]{num}</a> </td>
155     <td align="center" class="border"> $ltype </td>
156     <td align="center" class="border"> $filled </td>
157     <td align="right" class="border">  $startTime </td>
158     <td align="right" class="border">  $duration </td>
159     <td align="right" class="border">  $age </td>
160     <td align="left" class="border">   <tt>$TopDir/pc/$host/$Backups[$i]{num}</tt> </td></tr>
161 EOF
162         $sizeStr .= <<EOF;
163 <tr><td align="center" class="border"> <a href="$browseURL">$Backups[$i]{num}</a> </td>
164     <td align="center" class="border"> $ltype </td>
165     <td align="right" class="border">  $Backups[$i]{nFiles} </td>
166     <td align="right" class="border">  $MB </td>
167     <td align="right" class="border">  $MBperSec </td>
168     <td align="right" class="border">  $Backups[$i]{nFilesExist} </td>
169     <td align="right" class="border">  $MBExist </td>
170     <td align="right" class="border">  $Backups[$i]{nFilesNew} </td>
171     <td align="right" class="border">  $MBNew </td>
172 </tr>
173 EOF
174         my $is_compress = $Backups[$i]{compress} || $Lang->{off};
175         if (! $ExistComp) { $ExistComp = "&nbsp;"; }
176         if (! $MBExistComp) { $MBExistComp = "&nbsp;"; }
177         $compStr .= <<EOF;
178 <tr><td align="center" class="border"> <a href="$browseURL">$Backups[$i]{num}</a> </td>
179     <td align="center" class="border"> $ltype </td>
180     <td align="center" class="border"> $is_compress </td>
181     <td align="right" class="border">  $MBExist </td>
182     <td align="right" class="border">  $MBExistComp </td>
183     <td align="right" class="border">  $ExistComp </td>
184     <td align="right" class="border">  $MBNew </td>
185     <td align="right" class="border">  $MBNewComp </td>
186     <td align="right" class="border">  $NewComp </td>
187 </tr>
188 EOF
189         $errStr .= <<EOF;
190 <tr><td align="center" class="border"> <a href="$browseURL">$Backups[$i]{num}</a> </td>
191     <td align="center" class="border"> $ltype </td>
192     <td align="center" class="border"> <a href="$MyURL?action=view&type=XferLOG&num=$Backups[$i]{num}&host=${EscURI($host)}">$Lang->{XferLOG}</a>,
193                       <a href="$MyURL?action=view&type=XferErr&num=$Backups[$i]{num}&host=${EscURI($host)}">$Lang->{Errors}</a> </td>
194     <td align="right" class="border">  $Backups[$i]{xferErrs} </td>
195     <td align="right" class="border">  $Backups[$i]{xferBadFile} </td>
196     <td align="right" class="border">  $Backups[$i]{xferBadShare} </td>
197     <td align="right" class="border">  $Backups[$i]{tarErrs} </td></tr>
198 EOF
199     }
200
201     my @Restores = $bpc->RestoreInfoRead($host);
202     my $restoreStr;
203
204     for ( my $i = 0 ; $i < @Restores ; $i++ ) {
205         my $startTime = timeStamp2($Restores[$i]{startTime});
206         my $dur       = $Restores[$i]{endTime} - $Restores[$i]{startTime};
207         $dur          = 1 if ( $dur <= 0 );
208         my $duration  = sprintf("%.1f", $dur / 60);
209         my $MB        = sprintf("%.1f", $Restores[$i]{size} / (1024*1024));
210         my $MBperSec  = sprintf("%.2f", $Restores[$i]{size} / (1024*1024*$dur));
211         my $Restores_Result = $Lang->{failed};
212         if ($Restores[$i]{result} ne "failed") { $Restores_Result = $Lang->{success}; }
213         $restoreStr  .= <<EOF;
214 <tr><td align="center" class="border"><a href="$MyURL?action=restoreInfo&num=$Restores[$i]{num}&host=${EscURI($host)}">$Restores[$i]{num}</a> </td>
215     <td align="center" class="border"> $Restores_Result </td>
216     <td align="right" class="border"> $startTime </td>
217     <td align="right" class="border"> $duration </td>
218     <td align="right" class="border"> $Restores[$i]{nFiles} </td>
219     <td align="right" class="border"> $MB </td>
220     <td align="right" class="border"> $Restores[$i]{tarCreateErrs} </td>
221     <td align="right" class="border"> $Restores[$i]{xferErrs} </td>
222 </tr>
223 EOF
224     }
225     if ( $restoreStr ne "" ) {
226         $restoreStr = eval("qq{$Lang->{Restore_Summary}}");
227     }
228     if ( @Backups == 0 ) {
229         $warnStr = $Lang->{This_PC_has_never_been_backed_up};
230     }
231     if ( defined($Hosts->{$host}) ) {
232         my $user = $Hosts->{$host}{user};
233         my @moreUsers = sort(keys(%{$Hosts->{$host}{moreUsers}}));
234         my $moreUserStr;
235         foreach my $u ( sort(keys(%{$Hosts->{$host}{moreUsers}})) ) {
236             $moreUserStr .= ", " if ( $moreUserStr ne "" );
237             $moreUserStr .= "${UserLink($u)}";
238         }
239         if ( $moreUserStr ne "" ) {
240             $moreUserStr = " ($Lang->{and} $moreUserStr).\n";
241         } else {
242             $moreUserStr = ".\n";
243         }
244         if ( $user ne "" ) {
245             $statusStr .= eval("qq{$Lang->{This_PC_is_used_by}$moreUserStr}");
246         }
247         if ( defined($UserEmailInfo{$user})
248                 && $UserEmailInfo{$user}{lastHost} eq $host ) {
249             my $mailTime = timeStamp2($UserEmailInfo{$user}{lastTime});
250             my $subj     = $UserEmailInfo{$user}{lastSubj};
251             $statusStr  .= eval("qq{$Lang->{Last_email_sent_to__was_at___subject}}");
252         }
253     }
254     if ( defined($Jobs{$host}) ) {
255         my $startTime = timeStamp2($Jobs{$host}{startTime});
256         (my $cmd = $Jobs{$host}{cmd}) =~ s/$BinDir\///g;
257         $statusStr .= eval("qq{$Lang->{The_command_cmd_is_currently_running_for_started}}");
258     }
259     if ( $StatusHost{BgQueueOn} ) {
260         $statusStr .= eval("qq{$Lang->{Host_host_is_queued_on_the_background_queue_will_be_backed_up_soon}}");
261     }
262     if ( $StatusHost{UserQueueOn} ) {
263         $statusStr .= eval("qq{$Lang->{Host_host_is_queued_on_the_user_queue__will_be_backed_up_soon}}");
264     }
265     if ( $StatusHost{CmdQueueOn} ) {
266         $statusStr .= eval("qq{$Lang->{A_command_for_host_is_on_the_command_queue_will_run_soon}}");
267     }
268     my $startTime = timeStamp2($StatusHost{endTime} == 0 ?
269                 $StatusHost{startTime} : $StatusHost{endTime});
270     my $reason = "";
271     if ( $StatusHost{reason} ne "" ) {
272         $reason = " ($Lang->{$StatusHost{reason}})";
273     }
274     $statusStr .= eval("qq{$Lang->{Last_status_is_state_StatusHost_state_reason_as_of_startTime}}");
275
276     if ( $StatusHost{state} ne "Status_backup_in_progress"
277             && $StatusHost{state} ne "Status_restore_in_progress"
278             && $StatusHost{error} ne "" ) {
279         $statusStr .= eval("qq{$Lang->{Last_error_is____EscHTML_StatusHost_error}}");
280     }
281     my $priorStr = "Pings";
282     if ( $StatusHost{deadCnt} > 0 ) {
283         $statusStr .= eval("qq{$Lang->{Pings_to_host_have_failed_StatusHost_deadCnt__consecutive_times}}");
284         $priorStr = $Lang->{Prior_to_that__pings};
285     }
286     if ( $StatusHost{aliveCnt} > 0 ) {
287         $statusStr .= eval("qq{$Lang->{priorStr_to_host_have_succeeded_StatusHostaliveCnt_consecutive_times}}");
288
289         if ( $StatusHost{aliveCnt} >= $Conf{BlackoutGoodCnt}
290                 && $Conf{BlackoutGoodCnt} >= 0 && $Conf{BlackoutHourBegin} >= 0
291                 && $Conf{BlackoutHourEnd} >= 0 ) {
292             my(@days) = qw(Sun Mon Tue Wed Thu Fri Sat);
293             my($days) = join(", ", @days[@{$Conf{BlackoutWeekDays}}]);
294             my($t0) = sprintf("%d:%02d", $Conf{BlackoutHourBegin},
295                             60 * ($Conf{BlackoutHourBegin}
296                                      - int($Conf{BlackoutHourBegin})));
297             my($t1) = sprintf("%d:%02d", $Conf{BlackoutHourEnd},
298                             60 * ($Conf{BlackoutHourEnd}
299                                      - int($Conf{BlackoutHourEnd})));
300             $statusStr .= eval("qq{$Lang->{Because__host_has_been_on_the_network_at_least__Conf_BlackoutGoodCnt_consecutive_times___}}");
301         }
302     }
303     if ( $StatusHost{backoffTime} > time ) {
304         my $hours = sprintf("%.1f", ($StatusHost{backoffTime} - time) / 3600);
305         $statusStr .= eval("qq{$Lang->{Backups_are_deferred_for_hours_hours_change_this_number}}");
306
307     }
308     if ( @Backups ) {
309         # only allow incremental if there are already some backups
310         $startIncrStr = <<EOF;
311 <input type="submit" value="\$Lang->{Start_Incr_Backup}" name="action">
312 EOF
313     }
314
315     $startIncrStr = eval ("qq{$startIncrStr}");
316     my $content = eval("qq{$Lang->{Host__host_Backup_Summary2}}");
317     Header(eval("qq{$Lang->{Host__host_Backup_Summary}}"), $content);
318     Trailer();
319 }
320
321 1;