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