Imported Upstream version 2.6.1p2
[debian/amanda] / server-src / amdevcheck.pl
1 #! @PERL@
2 # Copyright (c) 2005-2008 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 Mathlida 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_name) = @_;
33
34     if ( !$device_name ) {
35         die("No device name specified.\n");
36     }
37
38     my $result;
39
40     my $device = Amanda::Device->new($device_name);
41     if ( !$device ) {
42         die("Error creating $device_name");
43     }
44
45     if ($device->status() == $DEVICE_STATUS_SUCCESS) {
46         $result = $device->read_label();
47     } else {
48         $result = $device->status();
49     }
50
51     print_result( $result, $device->error() );
52     return $result;
53 }
54
55 # print the results, one flag per line
56 sub print_result {
57     my ($flags, $errmsg) = @_;
58
59     if ($flags != $DEVICE_STATUS_SUCCESS) {
60         print "MESSAGE $errmsg\n";
61     }
62     print join( "\n", DeviceStatusFlags_to_strings($flags) ), "\n";
63 }
64
65 sub usage {
66     print <<EOF;
67 Usage: amdevcheck <config> [ <device name> ]
68 EOF
69     exit(1);
70 }
71
72 ## Application initialization
73
74 Amanda::Util::setup_application("amdevcheck", "server", $CONTEXT_SCRIPTUTIL);
75
76 my $config_overwrites = new_config_overwrites($#ARGV+1);
77
78 Getopt::Long::Configure(qw(bundling));
79 GetOptions(
80     'help|usage|?' => \&usage,
81     'o=s' => sub { add_config_overwrite_opt($config_overwrites, $_[1]); },
82 ) or usage();
83
84 usage() if ( @ARGV < 1 || @ARGV > 2 );
85 my $config_name = $ARGV[0];
86
87 config_init($CONFIG_INIT_EXPLICIT_NAME, $config_name);
88 apply_config_overwrites($config_overwrites);
89 my ($cfgerr_level, @cfgerr_errors) = config_errors();
90 if ($cfgerr_level >= $CFGERR_WARNINGS) {
91     config_print_errors();
92     if ($cfgerr_level >= $CFGERR_ERRORS) {
93         die("errors processing config file");
94     }
95 }
96
97 Amanda::Util::finish_setup($RUNNING_AS_DUMPUSER);
98
99 ## Check the device
100
101 my $device_name;
102 if ( $#ARGV == 1 ) {
103     $device_name = $ARGV[1];
104 } else {
105     $device_name = getconf($CNF_TAPEDEV);
106 }
107
108 try_read_label($device_name);
109 exit 0;