Amanda-Users

Re: Problems with new Amanda release (2.5.0p2): amcheck not reporting tape errors via e-mail

2006-07-11 23:40:56
Subject: Re: Problems with new Amanda release (2.5.0p2): amcheck not reporting tape errors via e-mail
From: Olivier Nicole <on AT cs.ait.ac DOT th>
To: donald.ritchey AT exeloncorp DOT com
Date: Wed, 12 Jul 2006 10:34:34 +0700 (ICT)
> I recently upgraded the first of our Amanda installations to 2.5.0p2
> ] and found an unwelcome surprise: amcheck no longer reports tape
> problems via e-mail from an amcheck run out of the Amanda user's
> crontab file.

I understood that was one of the changes in the new version.

Since I started using Amanda I wrote a script that is in charge of
verifying what tape is being used.

OK that script has evolved a bit and it will now restart (option -r)
itself every so and so if we still forget to change the tape. It will
also check that the tape drive is in use (by amanda that did not
finished working, or by other guy), etc.

I include the script below. I run it every day at 11:00 from crontab,
under amanda's user.

(Note that script is used to do tape rotation, put one set of tapes in
use and old set in nouse mode, it has also a number of path hard coded
in it)

Olivier

#!/usr/bin/perl
# change tape set at the end of one set
# the last tape number and number of sets is defined bellow
# check that the correct tape is mounted, if not request for it
# use -r for doing a reccursive check (every 30 or 60 minutes) until 23:00

$lasttape=5;  # the maximum number of the tape in a set (in range 00 to 05)
$lastset=3;    # the maximum number of the set (in range 1 to 3)

if ($#ARGV!=0 && $#ARGV!=1) {
    print "Usage: amrotate [-r] <configdir>\n";
    exit;
}

$rotate=0;
$config=$ARGV[0];
if ($config eq "-r" && $#ARGV!=1) {
    print "Usage: amrotate [-r] <configdir>\n";
    exit;
}
if ($config eq "-r") {
    $rotate=1;
    $config=$ARGV[1];
} 
elsif ($#ARGV==1) {
    if ($ARGV[1] eq "-r") {
        $rotate=1;
    }
    else {
        print "Usage: amrotate [-r] <configdir>\n";
        exit;
    }
}

if (! -d "/usr/local/etc/amanda/$config") {
    print "No such configuration directory $config.\n";
    exit;
}

@n=getpwnam "amanda";
if ($< != $n[2]) {
    print "Must be user \"amanda\" to run this script.\n";
    exit;
}

# Do nothing is amanda is still running
open IN, "/bin/ps auwx |/usr/bin/egrep \"^amanda\"|egrep 
\"amverify|amdump|amflush|amtoc|taper|dumper|amrestore|amrecover\"|grep -v 
grep|";
$l=<IN>;
close IN;
if ($l) {
    # amanda is still running, do nothing, but warn the user
    $message="Amanda IS STILL RUNNING, 

DO NOT change the tape, but check again later
^^^^^^^^^^^^^^^^^^^^^^";
    $subject="Amanda check tape: AMANDA BUSY";
} else {
    # amanda is finished
    open IN, "/usr/local/etc/amanda/tapelist";
    while (<IN>) {
        chop;
        $line[++$#line]=$_;
    }
    close INN;
    @sort=sort @line;
    
    $last=$sort[$#sort];
    $last=~/CSIM-set-(\d)-(\d+)\s(.*)$/;
    
    if (($2 == $lasttape) && ($3!~/no-reuse/)) {
        $thisset=$1;
        $nextset=$1+1;
        $nextset=1 if $nextset > $lastset;
        for ($i=0; $i<=$lasttape; $i++) {
            $noreuse.=sprintf " CSIM-set-%d-%02d", $thisset, $i;
            $reuse.=sprintf " CSIM-set-%d-%02d", $nextset, $i;
        }
        system "/usr/local/sbin/amadmin $config no-reuse$noreuse;
/usr/local/sbin/amadmin $config reuse$reuse;
";
    }
    
    $nexttape=`/usr/local/sbin/amadmin $config tape`;
    chop $nexttape;
    $nexttape=~s/^.*onto tape\s+//;
    $nexttape=~s/\s+or.*$//;
    
    $in=`/usr/bin/mt rewind 2>&1`;
    $busy=1 if $in=~/Device busy/i;
    $notape=1 if $in=~/Device not configured/i;
    if ($busy || $notape) {
        if ($busy) {
            $message="Tape IS BUSY, 

DO NOT change the tape, but check again later
^^^^^^^^^^^^^^^^^^^^^^";
            $subject="Amanda check tape: TAPE BUSY";
        }
        if ($notape) {
            $message="There is no tape in the drive.

Please mount the tape $nexttape.";
            $subject="Amanda check tape: DRIVE EMPTY";
        }
    }
    else {
        
        open IN, "/usr/local/sbin/amcheck -t $config |";
        
        while (<IN>) {
            $expect=1 if /expecting tape/;
        }
        close IN;
        
        if ($expect==1) {
            # eject the tape if a bad one in use
            system "/usr/bin/mt offline";
            $message.="Please mount the tape $nexttape.";
            $subject="Amanda check tape: MOUNT $nexttape";
        }
    }
}

# find the amanda operators (from amanda.conf)
open IN, "/usr/local/etc/amanda/$config/amanda.conf";
while (<IN>) {
    chop;
    $mailto=$_ if /mailto/;
}
close IN;

$mailto=~/\"(.*)\"/;
$mailto=$1;

if (defined $message) {
    $time=time();
    $time=int($time/1800)*1800;
    @t=localtime($time);
    if ($t[2]>14) {
        $interval=1800; # 30 minutes
    } else {
        $interval=3600; # one hour
    }
    @t=localtime($time+$interval);
    $time=sprintf "%02d:%02d", $t[2], $t[1];
    $mtime=sprintf "%02d%02d", $t[2], $t[1];

    open OUT, "|/usr/sbin/sendmail $mailto";
    print OUT "To: $mailto
Subject: $subject

$message\n";
    if ($rotate && $mtime <2300) {
        print OUT "This procedure will run again at $time.\n";
        system "echo /usr/local/sbin/amrotate -r $config |/usr/bin/at $time 
>/dev/null 2>&1";
    }
    close OUT;
}