2 # Copyright (c) 2009-2012 Zmanda, Inc. All Rights Reserved.
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.
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
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
18 # Contact information: Zmanda Inc., 465 S. Mathilda Ave., Suite 300
19 # Sunnyvale, CA 94086, USA, or: http://www.zmanda.com
21 use lib '@amperldir@';
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 );
31 # try to open the device and read its label, returning the device_read_label
32 # result (one or more of ReadLabelStatusFlags)
37 $result = $device->read_label();
38 if ($device->status() != $DEVICE_STATUS_SUCCESS ) {
39 $result = $device->status();
44 sub list_device_property {
45 my ( $device, $plist ) = @_;
50 if ($device->status() == $DEVICE_STATUS_SUCCESS ) {
51 my @list = $device->property_list();
52 foreach my $line (@list) {
53 push(@proplist, $line->{'name'} );
56 $result = $device->status();
57 print_result($result, $device->error() );
61 @proplist = split(/,/, $plist );
64 foreach my $prop (sort @proplist ) {
65 my $value = $device->property_get(lc($prop) );
66 print uc($prop) . "=$value\n" if (defined($value) );
71 # print the results, one flag per line
73 my ($flags, $errmsg) = @_;
75 if ($flags != $DEVICE_STATUS_SUCCESS) {
76 print "MESSAGE $errmsg\n";
78 print join( "\n", DeviceStatusFlags_to_strings($flags) ), "\n";
83 Usage: amdevcheck [--label] [--properties {prop1,prop2,prop3}] [-o configoption]* <config> [<device name>]
88 ## Application initialization
90 Amanda::Util::setup_application("amdevcheck", "server", $CONTEXT_SCRIPTUTIL);
92 my $config_overrides = new_config_overrides($#ARGV+1);
97 debug("Arguments: " . join(' ', @ARGV));
98 Getopt::Long::Configure(qw(bundling));
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
107 usage() if ( @ARGV < 1 || @ARGV > 3 );
108 my $config_name = $ARGV[0];
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");
120 Amanda::Util::finish_setup($RUNNING_AS_DUMPUSER);
124 if (defined $getproplist && defined $print_label) {
125 die("Can't set both --label and --properties");
128 $device_name = $ARGV[1];
130 $device_name = getconf($CNF_TAPEDEV);
133 my $device = Amanda::Device->new($device_name);
135 die("Error creating $device_name");
138 $result = $device->status();
139 if ($result == $DEVICE_STATUS_SUCCESS) {
140 $device->configure(1);
141 if (defined $getproplist) {
142 list_device_property($device,$getproplist);
145 $result = try_read_label($device);
146 if (defined $print_label) {
147 if ($result == $DEVICE_STATUS_SUCCESS) {
148 print $device->volume_label(), "\n";
154 print_result($result, $device->error());
158 if (!defined $getproplist && !defined $print_label) {
159 print_result($result, $device->error());
166 Amanda::Util::finish_application();