2 # Copyright (c) 2009, 2010 Zmanda, Inc. All Rights Reserved.
4 # This program is free software; you can redistribute it and/or modify it
5 # under the terms of the GNU General Public License version 2 as published
6 # by the Free Software Foundation.
8 # This program is distributed in the hope that it will be useful, but
9 # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
10 # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 # You should have received a copy of the GNU General Public License along
14 # with this program; if not, write to the Free Software Foundation, Inc.,
15 # 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 # Contact information: Zmanda Inc., 465 S. Mathilda Ave., Suite 300
18 # Sunnyvale, CA 94086, USA, or: http://www.zmanda.com
20 use lib '@amperldir@';
25 use Amanda::Device qw( :constants );
26 use Amanda::Config qw( :getconf :init );
27 use Amanda::Debug qw( :logging );
28 use Amanda::Util qw( :constants );
30 # try to open the device and read its label, returning the device_read_label
31 # result (one or more of ReadLabelStatusFlags)
36 $result = $device->read_label();
37 if ($device->status() != $DEVICE_STATUS_SUCCESS ) {
38 $result = $device->status();
43 sub list_device_property {
44 my ( $device, $plist ) = @_;
49 if ($device->status() == $DEVICE_STATUS_SUCCESS ) {
50 my @list = $device->property_list();
51 foreach my $line (@list) {
52 push(@proplist, $line->{'name'} );
55 $result = $device->status();
56 print_result($result, $device->error() );
60 @proplist = split(/,/, $plist );
63 foreach my $prop (sort @proplist ) {
64 my $value = $device->property_get(lc($prop) );
65 print uc($prop) . "=$value\n" if (defined($value) );
70 # print the results, one flag per line
72 my ($flags, $errmsg) = @_;
74 if ($flags != $DEVICE_STATUS_SUCCESS) {
75 print "MESSAGE $errmsg\n";
77 print join( "\n", DeviceStatusFlags_to_strings($flags) ), "\n";
82 Usage: amdevcheck <config> [ <device name> ] [ --properties {prop1,prop2,prop3} ]
87 ## Application initialization
89 Amanda::Util::setup_application("amdevcheck", "server", $CONTEXT_SCRIPTUTIL);
91 my $config_overrides = new_config_overrides($#ARGV+1);
96 Getopt::Long::Configure(qw(bundling));
98 'help|usage|?' => \&usage,
99 'o=s' => sub { add_config_override_opt($config_overrides, $_[1]); },
100 'properties:s' => \$getproplist,
101 'label' => \$print_label
104 usage() if ( @ARGV < 1 || @ARGV > 3 );
105 my $config_name = $ARGV[0];
107 set_config_overrides($config_overrides);
108 config_init($CONFIG_INIT_EXPLICIT_NAME, $config_name);
109 my ($cfgerr_level, @cfgerr_errors) = config_errors();
110 if ($cfgerr_level >= $CFGERR_WARNINGS) {
111 config_print_errors();
112 if ($cfgerr_level >= $CFGERR_ERRORS) {
113 die("errors processing config file");
117 Amanda::Util::finish_setup($RUNNING_AS_DUMPUSER);
121 if (defined $getproplist && defined $print_label) {
122 die("Can't set both --label and --properties");
125 $device_name = $ARGV[1];
127 $device_name = getconf($CNF_TAPEDEV);
130 my $device = Amanda::Device->new($device_name);
132 die("Error creating $device_name");
135 $result = $device->status();
136 if ($result == $DEVICE_STATUS_SUCCESS) {
137 $device->configure(1);
138 if (defined $getproplist) {
139 list_device_property($device,$getproplist);
142 $result = try_read_label($device);
143 if (defined $print_label) {
144 if ($result == $DEVICE_STATUS_SUCCESS) {
145 print $device->volume_label(), "\n";
151 print_result($result, $device->error());
155 if (!defined $getproplist && !defined $print_label) {
156 print_result($result, $device->error());
163 Amanda::Util::finish_application();