78392643d378bc6d555307cd3c2a8b56121ed19f
[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 [--label] [--properties {prop1,prop2,prop3}] [-o configoption]* <config> [<device name>]
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 debug("Arguments: " . join(' ', @ARGV));
97 Getopt::Long::Configure(qw(bundling));
98 GetOptions(
99     'version' => \&Amanda::Util::version_opt,
100     'help|usage|?' => \&usage,
101     'o=s' => sub { add_config_override_opt($config_overrides, $_[1]); },
102     'properties:s' => \$getproplist,
103     'label' => \$print_label
104 ) or usage();
105
106 usage() if ( @ARGV < 1 || @ARGV > 3 );
107 my $config_name = $ARGV[0];
108
109 set_config_overrides($config_overrides);
110 config_init($CONFIG_INIT_EXPLICIT_NAME, $config_name);
111 my ($cfgerr_level, @cfgerr_errors) = config_errors();
112 if ($cfgerr_level >= $CFGERR_WARNINGS) {
113     config_print_errors();
114     if ($cfgerr_level >= $CFGERR_ERRORS) {
115         die("errors processing config file");
116     }
117 }
118
119 Amanda::Util::finish_setup($RUNNING_AS_DUMPUSER);
120
121 my $result;
122
123 if (defined $getproplist && defined $print_label) {
124     die("Can't set both --label and --properties");
125 }
126 if ( $#ARGV == 1 ) {
127     $device_name = $ARGV[1];
128 } else {
129     $device_name = getconf($CNF_TAPEDEV);
130 }
131
132 my $device = Amanda::Device->new($device_name);
133 if ( !$device ) {
134     die("Error creating $device_name");
135 }
136
137 $result = $device->status();
138 if ($result == $DEVICE_STATUS_SUCCESS) {
139     $device->configure(1);
140     if (defined $getproplist) {
141         list_device_property($device,$getproplist);
142         exit 0;
143     }
144     $result = try_read_label($device);
145     if (defined $print_label) {
146         if ($result == $DEVICE_STATUS_SUCCESS) {
147             print $device->volume_label(), "\n";
148             exit 0;
149         } else {
150             exit 1;
151         }
152     } else {
153         print_result($result, $device->error());
154         exit 0;
155     }
156 } else {
157     if (!defined $getproplist && !defined $print_label) {
158         print_result($result, $device->error());
159         exit 0;
160     } else {
161         exit 1;
162     }
163 }
164
165 Amanda::Util::finish_application();
166 exit 0;