2 # Copyright (c) 2009 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@';
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 );
29 # try to open the device and read its label, returning the device_read_label
30 # result (one or more of ReadLabelStatusFlags)
35 $result = $device->read_label();
36 if ($device->status() != $DEVICE_STATUS_SUCCESS ) {
37 $result = $device->status();
42 sub list_device_property {
43 my ( $device, $plist ) = @_;
48 if ($device->status() == $DEVICE_STATUS_SUCCESS ) {
49 my @list = $device->property_list();
50 foreach my $line (@list) {
51 push(@proplist, $line->{'name'} );
54 $result = $device->status();
55 print_result($result, $device->error() );
59 @proplist = split(/,/, $plist );
62 foreach my $prop (sort @proplist ) {
63 my $value = $device->property_get(lc($prop) );
64 print uc($prop) . "=$value\n" if (defined($value) );
69 # print the results, one flag per line
71 my ($flags, $errmsg) = @_;
73 if ($flags != $DEVICE_STATUS_SUCCESS) {
74 print "MESSAGE $errmsg\n";
76 print join( "\n", DeviceStatusFlags_to_strings($flags) ), "\n";
81 Usage: amdevcheck <config> [ <device name> ] [ --properties {prop1,prop2,prop3} ]
86 ## Application initialization
88 Amanda::Util::setup_application("amdevcheck", "server", $CONTEXT_SCRIPTUTIL);
90 my $config_overrides = new_config_overrides($#ARGV+1);
95 Getopt::Long::Configure(qw(bundling));
97 'help|usage|?' => \&usage,
98 'o=s' => sub { add_config_override_opt($config_overrides, $_[1]); },
99 'properties:s' => \$getproplist,
100 'label' => \$print_label
103 usage() if ( @ARGV < 1 || @ARGV > 3 );
104 my $config_name = $ARGV[0];
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");
116 Amanda::Util::finish_setup($RUNNING_AS_DUMPUSER);
120 if (defined $getproplist && defined $print_label) {
121 die("Can't set both --label and --properties");
124 $device_name = $ARGV[1];
126 $device_name = getconf($CNF_TAPEDEV);
129 my $device = Amanda::Device->new($device_name);
131 die("Error creating $device_name");
134 $result = $device->status();
135 if ($result == $DEVICE_STATUS_SUCCESS) {
136 $device->configure(1);
137 if (defined $getproplist) {
138 list_device_property($device,$getproplist);
141 $result = try_read_label($device);
142 if (defined $print_label) {
143 if ($result == $DEVICE_STATUS_SUCCESS) {
144 print $device->volume_label(), "\n";
150 print_result($result, $device->error());
154 if (!defined $getproplist && !defined $print_label) {
155 print_result($result, $device->error());
162 Amanda::Util::finish_application();