Imported Upstream version 3.2.0
[debian/amanda] / device-src / amdevcheck.pl
1 #! @PERL@
2 # Copyright (c) 2009, 2010 Zmanda, Inc.  All Rights Reserved.
3 #
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.
7 #
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
11 # for more details.
12 #
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
16 #
17 # Contact information: Zmanda Inc., 465 S. Mathilda Ave., Suite 300
18 # Sunnyvale, CA 94086, USA, or: http://www.zmanda.com
19
20 use lib '@amperldir@';
21 use Getopt::Long;
22
23 use strict;
24 use warnings;
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 );
29
30 # try to open the device and read its label, returning the device_read_label
31 # result (one or more of ReadLabelStatusFlags)
32 sub try_read_label {
33     my ($device) = @_;
34
35     my $result;
36     $result = $device->read_label();
37     if ($device->status() != $DEVICE_STATUS_SUCCESS ) {
38         $result = $device->status();
39     }
40     return $result;
41 }
42
43 sub list_device_property {
44     my ( $device, $plist ) = @_;
45     my @proplist;
46
47     my $result;
48     if (!$plist ) {
49         if ($device->status() == $DEVICE_STATUS_SUCCESS ) {
50             my @list = $device->property_list();
51             foreach my $line (@list) {
52                 push(@proplist, $line->{'name'} );
53             }
54         } else {
55             $result = $device->status();
56             print_result($result, $device->error() );
57             return $result;
58         }
59     } else {
60         @proplist = split(/,/, $plist );
61     }
62
63     foreach my $prop (sort @proplist ) {
64         my $value = $device->property_get(lc($prop) );
65         print uc($prop) . "=$value\n" if (defined($value) );
66     }
67     return;
68 }
69
70 # print the results, one flag per line
71 sub print_result {
72     my ($flags, $errmsg) = @_;
73
74     if ($flags != $DEVICE_STATUS_SUCCESS) {
75         print "MESSAGE $errmsg\n";
76     }
77     print join( "\n", DeviceStatusFlags_to_strings($flags) ), "\n";
78 }
79
80 sub usage {
81     print <<EOF;
82 Usage: amdevcheck <config> [ <device name> ] [ --properties {prop1,prop2,prop3} ]
83 EOF
84     exit(1);
85 }
86
87 ## Application initialization
88
89 Amanda::Util::setup_application("amdevcheck", "server", $CONTEXT_SCRIPTUTIL);
90
91 my $config_overrides = new_config_overrides($#ARGV+1);
92 my $getproplist;
93 my $device_name;
94 my $print_label;
95
96 Getopt::Long::Configure(qw(bundling));
97 GetOptions(
98     'help|usage|?' => \&usage,
99     'o=s' => sub { add_config_override_opt($config_overrides, $_[1]); },
100     'properties:s' => \$getproplist,
101     'label' => \$print_label
102 ) or usage();
103
104 usage() if ( @ARGV < 1 || @ARGV > 3 );
105 my $config_name = $ARGV[0];
106
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");
114     }
115 }
116
117 Amanda::Util::finish_setup($RUNNING_AS_DUMPUSER);
118
119 my $result;
120
121 if (defined $getproplist && defined $print_label) {
122     die("Can't set both --label and --properties");
123 }
124 if ( $#ARGV == 1 ) {
125     $device_name = $ARGV[1];
126 } else {
127     $device_name = getconf($CNF_TAPEDEV);
128 }
129
130 my $device = Amanda::Device->new($device_name);
131 if ( !$device ) {
132     die("Error creating $device_name");
133 }
134
135 $result = $device->status();
136 if ($result == $DEVICE_STATUS_SUCCESS) {
137     $device->configure(1);
138     if (defined $getproplist) {
139         list_device_property($device,$getproplist);
140         exit 0;
141     }
142     $result = try_read_label($device);
143     if (defined $print_label) {
144         if ($result == $DEVICE_STATUS_SUCCESS) {
145             print $device->volume_label(), "\n";
146             exit 0;
147         } else {
148             exit 1;
149         }
150     } else {
151         print_result($result, $device->error());
152         exit 0;
153     }
154 } else {
155     if (!defined $getproplist && !defined $print_label) {
156         print_result($result, $device->error());
157         exit 0;
158     } else {
159         exit 1;
160     }
161 }
162
163 Amanda::Util::finish_application();
164 exit 0;