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