#!/usr/bin/perl -w # bbClientStatus.pl # # This script lists all accounts and their most recent file addition # The following options are supported: # # Synopsis: # bbClientStatus.pl [-m] [-e addr1,addr2...] [-c client1,client2...] # # Human readable. This makes it easy to see if a particular # client is having problems. Output looks as follows: # # Account ID Most Recent backup Time since last backup # 00000001 2004-12-06 21:10:59 1d 06:08:43 # 00000002 Currently backing up N/A # 00000100 2004-01-02 04:30:32 350d 05:32:30 # # # # -m: Machine readable. The output of the script will be 2 columns # (tab-separated) as follows: # \t # # Example: # 00000001 602 # 00000100 3 # 000FF340 86300 # DEADBEEF 3073 # # # -e : # Email the result to the email address given. multiple addresses # are separated by commas. # # -c : # Only perform the program for the client IDs given. Multiple clients # are separated by commas. # use strict; use File::Find; use Mail::Send; use BoxBackup::Config::Accounts; use BoxBackup::Config::DiskSets; use Getopt::Long; my $bMachineReadable; my $emails; my $clientString; GetOptions( "m" => \$bMachineReadable, "e=s" => \$emails, "c=s" => \$clientString ); # @emails = split(/,/,join(',', @emails)); # @clients = split(/,/, join(',', @clients)); my @clients; my %clientH; if(defined($clientString)) { @clients = split(/,/, $clientString); foreach my $client (@clients) { my $clientF = sprintf("%lx", hex($client)); $clientH{$clientF} = $client; } } # foreach my $email (@emails) # { # print $email, "\n"; # } # foreach my $client (@clients) # { # print $client, "\n"; # } my $disksets = BoxBackup::Config::DiskSets->new(); my $accounts = BoxBackup::Config::Accounts->new(); my $now = time; my @timeAry = localtime($now); my $nowF = sprintf("%4d-%02d-%02d %02d:%02d:%02d" ,$timeAry[5] + 1900, $timeAry[4] + 1, $timeAry[3], $timeAry[2], $timeAry[1], $timeAry[0] ); my $output = "Box backup Statistics as of $nowF\nAccount ID Most Recent backup Time since last backup\n" if (!($bMachineReadable)); my $subject = "Box backup Statistics as of $nowF"; my @IDs = $accounts->getAccountIDs(); my %newest; my $mostRecentTime; # The time stamp for the most recent file my $mostRecentName; # the name of the most recent file foreach my $ID (@IDs) { # If there are '-c' client IDs on the cmd line, only show those next if (@clients && !(defined($clientH{$ID}))); $mostRecentTime = 0; $mostRecentName = ""; my $diskName = "disc" . $accounts->getDisk($ID); my $accountIDLong = sprintf("%08x", hex($ID)); # my $backupDir = $disksets->{$diskName}{"Dir0"} . "/backup/" . $accountIDLong; my $backupDir = $disksets->getParamVal($diskName, "Dir0") . "/backup/" . $accountIDLong; # print $ID, ": ", $backupDir, "\n"; find(\&wanted, $backupDir); my $diffSecs = $now - $mostRecentTime; if ($bMachineReadable) { $output .= "$accountIDLong\t$diffSecs\n"; } else { if ($diffSecs < 5) { if ($mostRecentName =~ /rfwX$/) { $output .= sprintf("%s Currently backing up N/A\n",$accountIDLong); } else { $output .= sprintf("%s Backup just completed N/A\n",$accountIDLong); } } else { my @modTime = localtime($mostRecentTime); my $mostRecentTimeF = sprintf("%4d-%02d-%02d %02d:%02d:%02d" , $modTime[5] + 1900, $modTime[4] + 1, $modTime[3], $modTime[2], $modTime[1], $modTime[0] ); my $days = int($diffSecs / 86400); # How many days since last backup? my $hours = int(($diffSecs % 86400) / 3600); # and hours my $minutes = int(($diffSecs % 3600) / 60); # Minutes my $seconds = int($diffSecs % 60); # Seconds $output .= sprintf ("%s %s %4dd %02d:%02d:%02d\n", $accountIDLong, $mostRecentTimeF, $days, $hours, $minutes, $seconds ); } } # print "Account $accountIDLong: Most Recent File: $mostRecentName; Time Stamp: $mostRecentTime; Secs Difference: ", $now - $mostRecentTime, "\n"; } if ($emails) { SendMail($subject,$output,$emails); } else { print $output; } sub wanted { return unless -e $_; return if -d $_; return if ($_ eq "info.rfw" || $_ eq "write.lock"); my @sa = stat($_); my $time = $sa[9]; if($time > $mostRecentTime) { $mostRecentTime = $time; $mostRecentName = $_; } } sub SendMail { my $subject = shift; my $body = shift; my $eddrs = shift;; return if(!$eddrs); my $message = new Mail::Send; # print $eddrs, "\n"; $message->to($eddrs); $message->subject($subject); my $fh = $message->open; print $fh $body; $fh->close; }