| 1 |
#! @PERL@ |
| 2 |
# Copyright (c) 2009-2012 Zmanda, Inc. All Rights Reserved. |
| 3 |
# Copyright (c) 2013-2016 Carbonite, Inc. All Rights Reserved. |
| 4 |
# |
| 5 |
# This program is free software; you can redistribute it and/or |
| 6 |
# modify it under the terms of the GNU General Public License |
| 7 |
# as published by the Free Software Foundation; either version 2 |
| 8 |
# of the License, or (at your option) any later version. |
| 9 |
# |
| 10 |
# This program is distributed in the hope that it will be useful, but |
| 11 |
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY |
| 12 |
# or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 13 |
# for more details. |
| 14 |
# |
| 15 |
# You should have received a copy of the GNU General Public License along |
| 16 |
# with this program; if not, write to the Free Software Foundation, Inc., |
| 17 |
# 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 18 |
# |
| 19 |
# Contact information: Carbonite Inc., 756 N Pastoria Ave |
| 20 |
# Sunnyvale, CA 94086, USA, or: http://www.zmanda.com |
| 21 |
|
| 22 |
use lib '@amperldir@'; |
| 23 |
use Getopt::Long; |
| 24 |
|
| 25 |
use strict; |
| 26 |
use warnings; |
| 27 |
use Amanda::Device qw( :constants ); |
| 28 |
use Amanda::Config qw( :getconf :init ); |
| 29 |
use Amanda::Debug qw( :logging ); |
| 30 |
use Amanda::Util qw( :constants ); |
| 31 |
use Amanda::Storage; |
| 32 |
use Amanda::Changer; |
| 33 |
|
| 34 |
# try to open the device and read its label, returning the device_read_label |
| 35 |
# result (one or more of ReadLabelStatusFlags) |
| 36 |
sub try_read_label { |
| 37 |
my ($device) = @_; |
| 38 |
|
| 39 |
my $result; |
| 40 |
$result = $device->read_label(); |
| 41 |
if ($device->status() != $DEVICE_STATUS_SUCCESS ) { |
| 42 |
$result = $device->status(); |
| 43 |
} |
| 44 |
return $result; |
| 45 |
} |
| 46 |
|
| 47 |
sub list_device_property { |
| 48 |
my ( $device, $plist ) = @_; |
| 49 |
my @proplist; |
| 50 |
|
| 51 |
my $result; |
| 52 |
if (!$plist ) { |
| 53 |
if ($device->status() == $DEVICE_STATUS_SUCCESS ) { |
| 54 |
my @list = $device->property_list(); |
| 55 |
foreach my $line (@list) { |
| 56 |
push(@proplist, $line->{'name'} ); |
| 57 |
} |
| 58 |
} else { |
| 59 |
$result = $device->status(); |
| 60 |
print_result($result, $device->error() ); |
| 61 |
return $result; |
| 62 |
} |
| 63 |
} else { |
| 64 |
@proplist = split(/,/, $plist ); |
| 65 |
} |
| 66 |
|
| 67 |
foreach my $prop (sort @proplist ) { |
| 68 |
my $value = $device->property_get(lc($prop) ); |
| 69 |
print uc($prop) . "=$value\n" if (defined($value) ); |
| 70 |
} |
| 71 |
return; |
| 72 |
} |
| 73 |
|
| 74 |
# print the results, one flag per line |
| 75 |
sub print_result { |
| 76 |
my ($flags, $errmsg) = @_; |
| 77 |
|
| 78 |
if ($flags != $DEVICE_STATUS_SUCCESS) { |
| 79 |
print "MESSAGE $errmsg\n"; |
| 80 |
} |
| 81 |
print join( "\n", DeviceStatusFlags_to_strings($flags) ), "\n"; |
| 82 |
} |
| 83 |
|
| 84 |
sub usage { |
| 85 |
print <<EOF; |
| 86 |
Usage: amdevcheck [--label] [--properties {prop1,prop2,prop3}] [-o configoption]* <config> [<device name>] |
| 87 |
EOF |
| 88 |
exit(1); |
| 89 |
} |
| 90 |
|
| 91 |
## Application initialization |
| 92 |
|
| 93 |
Amanda::Util::setup_application("amdevcheck", "server", $CONTEXT_SCRIPTUTIL, "amanda", "amanda"); |
| 94 |
|
| 95 |
my $config_overrides = new_config_overrides($#ARGV+1); |
| 96 |
my $getproplist; |
| 97 |
my $device_name; |
| 98 |
my $print_label; |
| 99 |
|
| 100 |
debug("Arguments: " . join(' ', @ARGV)); |
| 101 |
Getopt::Long::Configure(qw(bundling)); |
| 102 |
GetOptions( |
| 103 |
'version' => \&Amanda::Util::version_opt, |
| 104 |
'help|usage|?' => \&usage, |
| 105 |
'o=s' => sub { add_config_override_opt($config_overrides, $_[1]); }, |
| 106 |
'properties:s' => \$getproplist, |
| 107 |
'label' => \$print_label |
| 108 |
) or usage(); |
| 109 |
|
| 110 |
usage() if ( @ARGV < 1 || @ARGV > 3 ); |
| 111 |
my $config_name = $ARGV[0]; |
| 112 |
|
| 113 |
set_config_overrides($config_overrides); |
| 114 |
config_init_with_global($CONFIG_INIT_EXPLICIT_NAME, $config_name); |
| 115 |
my ($cfgerr_level, @cfgerr_errors) = config_errors(); |
| 116 |
if ($cfgerr_level >= $CFGERR_WARNINGS) { |
| 117 |
config_print_errors(); |
| 118 |
if ($cfgerr_level >= $CFGERR_ERRORS) { |
| 119 |
die("errors processing config file"); |
| 120 |
} |
| 121 |
} |
| 122 |
|
| 123 |
Amanda::Util::finish_setup($RUNNING_AS_DUMPUSER); |
| 124 |
|
| 125 |
my $result; |
| 126 |
|
| 127 |
if (defined $getproplist && defined $print_label) { |
| 128 |
die("Can't set both --label and --properties"); |
| 129 |
} |
| 130 |
|
| 131 |
my $chg; |
| 132 |
my $res; |
| 133 |
my $device; |
| 134 |
if ( $#ARGV == 1 ) { |
| 135 |
$device_name = $ARGV[1]; |
| 136 |
$device = Amanda::Device->new($device_name); |
| 137 |
} elsif (getconf_seen($CNF_TAPEDEV)) { |
| 138 |
$device_name = getconf($CNF_TAPEDEV); |
| 139 |
$device = Amanda::Device->new($device_name); |
| 140 |
} else { |
| 141 |
my ($storage, $err) = Amanda::Storage->new(); |
| 142 |
die "$err" if !$storage; |
| 143 |
$chg = $storage->{'chg'}; |
| 144 |
$chg->load(relative_slot => "current", |
| 145 |
res_cb => sub { |
| 146 |
(my $err, $res) = @_; |
| 147 |
$device = $res->{'device'}; |
| 148 |
$device_name = $device->device_name(); |
| 149 |
Amanda::MainLoop::quit(); |
| 150 |
}); |
| 151 |
Amanda::MainLoop::run(); |
| 152 |
} |
| 153 |
|
| 154 |
if ( !$device ) { |
| 155 |
die("Error creating $device_name"); |
| 156 |
} |
| 157 |
|
| 158 |
$result = $device->status(); |
| 159 |
my $exit_status = 0; |
| 160 |
if ($result == $DEVICE_STATUS_SUCCESS || |
| 161 |
$result == $DEVICE_STATUS_VOLUME_UNLABELED) { |
| 162 |
$device->configure(1); |
| 163 |
if (defined $getproplist) { |
| 164 |
list_device_property($device,$getproplist); |
| 165 |
} else { |
| 166 |
$result = try_read_label($device); |
| 167 |
if (defined $print_label) { |
| 168 |
if ($result == $DEVICE_STATUS_SUCCESS) { |
| 169 |
print $device->volume_label(), "\n"; |
| 170 |
} else { |
| 171 |
$exit_status = 1; |
| 172 |
} |
| 173 |
} else { |
| 174 |
print_result($result, $device->error()); |
| 175 |
} |
| 176 |
} |
| 177 |
} else { |
| 178 |
if (!defined $getproplist && !defined $print_label) { |
| 179 |
print_result($result, $device->error()); |
| 180 |
} else { |
| 181 |
$exit_status = 1; |
| 182 |
} |
| 183 |
} |
| 184 |
|
| 185 |
if ($res) { |
| 186 |
$res->release(finished_cb => sub { Amanda::MainLoop::quit() }); |
| 187 |
Amanda::MainLoop::run(); |
| 188 |
} |
| 189 |
$chg->quit() if $chg; |
| 190 |
|
| 191 |
Amanda::Util::finish_application(); |
| 192 |
exit $exit_status; |