2 * Copyright (c) 2009, 2010 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 94085, USA, or: http://www.zmanda.com
25 Amanda::Config - access to Amanda configuration parameters
29 use Amanda::Config qw( :init :getconf );
31 my $config_name = shift @ARGV;
32 config_init($CONFIG_INIT_EXPLICIT_NAME, $config_name);
33 apply_config_overrides($config_overrides);
34 my ($cfgerr_level, @cfgerr_errors) = config_errors();
35 if ($cfgerr_level >= $CFGERR_WARNINGS) {
36 config_print_errors();
37 if ($cfgerr_level >= $CFGERR_ERRORS) {
38 die("errors processing config file");
42 print "tape device is ", getconf($CNF_TAPEDEV), "\n";
44 This API closely parallels the C API.
48 The Amanda configuration is treated as a global state for the
49 application. It is not possible to load two configurations
52 All initialization-related symbols can be imported with the tag
55 =head2 LOADING CONFIGURATION
57 The Amanda configuration is loaded with the aptly named
58 C<config_init($flags, $name)>. Because of the great variety in
59 invocation method among Amanda applications, this function has a number
60 of flags that affect its behavior. These flags can be OR'd together.
64 =item If C<CONFIG_INIT_EXPLICIT_NAME> is given, then the C<$name> parameter can
65 contain the name of a configuration to load. Note that if the parameter is
66 C<".">, this is equivalent to C<CONFIG_INIT_USE_CWD>.
68 =item If C<CONFIG_INIT_USE_CWD> is given, and if the current directory
69 contains C<amanda.conf>, then that file is loaded.
71 =item If C<CONFIG_INIT_CLIENT> is given, then a client configuration
74 =item If C<CONFIG_INIT_OVERLAY> is given, then any existing
75 configuration is not reset.
79 See C<conffile.h> for more detailed information on these flags and
82 C<config_uninit()> reverses the effects of C<config_init>. It is
85 Once the configuration is loaded, the configuration name
86 (e.g., "DailySet1"), directory (C</etc/amanda/DailySet1>),
87 and filename (C</etc/amanda/DailySet1/amanda.conf>) are
88 available from C<get_config_name()>, C<get_config_dir()>, and
89 C<get_config_filename()>, respectively.
93 This module collects configuration errors and warnings in a list, and also
94 tracks the overall error level with an enumeration: C<$CFGERR_OK>,
95 C<$CFGERR_WARNINGS>, and C<$CFGERR_ERRORS>. C<config_init> and
96 C<apply_config_overrides> both return the current level. The level and the
97 list of error messages are available from C<config_errors>:
99 my ($cfgerr_level, @errors) = Amanda::Config::config_errors();
101 As a convenience, C<config_print_errors> will print all error messages to
102 stderr. The error state can be cleared with C<config_clear_errors>.
104 =head2 CONFIG OVERWRITES
106 Most Amanda applications accept the command-line option C<-o>
107 to "overwrite" configuration values in C<amanda.conf>. In Perl
108 applications, these options should be parsed with L<Getopt::Long|Getopt::Long>, with
109 the action being a call to C<add_config_override_opt>. For example:
111 my $config_overrides = new_config_overrides($#ARGV+1);
114 'o=s' => sub { add_config_override_opt($config_overrides, $_[1]); },
116 my $cfg_ok = config_init($CONFIG_INIT_EXPLICIT_NAME | $CONFIG_INIT_USE_CWD, $config_name);
117 apply_config_overrides($config_overrides);
119 C<new_config_overrides($size_estimate)> creates a new
120 overwrites object, using the given size as an estimate of
121 the number of items it will contain (C<$#ARGC/2> is a good
122 estimate). Individual configuration options are then added via
123 C<add_config_override($co, $key, $value)> (which takes a key/value
124 pair) or C<add_config_override_opt($co, $optarg)>, which parses a
125 string following C<-o> on the command line.
127 Once the overwrites are gathered, they are applied with
128 C<apply_config_overrides($co)>, which applies the overwrites to the
129 active configuration. No further operations can be performed on the
130 overwrites object after C<apply_config_overrides> has been called.
132 The utility function C<get_config_options()> returns a list of
133 command-line arguments to represent any overwrites that were used
134 to generate the current configuration. (TODO: this function isn't
137 =head1 PARAMETER ACCESS
139 Amanda configurations consist of "global" parameters and several
140 sets of "subsections" -- one set for dumptypes, one for tapetypes,
143 All of the global parameters are represented by a constant beginning with
144 C<$CNF_>, e.g., C<$CNF_LABELSTR>. The function C<getconf($cnf)> returns the
145 value of parameter C<$cnf>, in whatever format is appropriate for the parameter
146 (see DATA FORMATS, below). C<getconf_seen($cnf)> returns a true value if
147 C<$cnf> was seen in the configuration file. If it was not seen, then it will
148 have its default value. C<getconf_linenum($cnf)> returns the line number in
149 the configuration file where it is set, 0 if it is not in a configuration file,
150 or -2 if it is set in a command line argument.
152 Some parameters have enumerated types. The values for those
153 enumerations are available from this module with the same name as
154 in C<conffile.h>. For example, C<$CNF_TAPERALGO> will yield a value
155 from the enumeration C<taperalgo_t>, the constants for which all
156 begin with C<$ALGO_>. See C<conffile.h> for the details.
158 Each subsection type C<TYP> has the following functions:
162 =item C<lookup_TYP($subsec_name)>
164 which returns an opaque object
165 (C<$ss>) representing the subsection, or C<undef> if no subsection
166 with that name exists;
168 =item C<TYP_name($ss)>
170 returning the name of the subsection;
172 =item C<TYP_getconf($ss, $cnf)>
174 which fetches a parameter value from C<$ss>; and
176 =item C<TYP_seen($ss, $cnf)>
178 which returns a true value if <$cnf> was seen in the subsection.
188 with constants beginning with C<$TAPETYPE_>
192 with constants beginning with C<$DUMPTYPE_>
196 with constants beginning with C<$INTER_>
200 with constants beginning with C<$HOLDING_>
204 with constants beginning with C<$APPLICATION_>
208 with constants beginning with C<$PP_SCRIPT_>
212 with constants beginning with C<$DEVICE_CONFIG_>.
216 with constants beginning with C<$CHANGER_CONFIG_>.
220 See C<conffile.h> for the names of the constants themselves.
224 Each configuration parameter has a "conftype", as assigned in
225 C<common-src/conffile.c>. The translation of most of these types into Perl
226 values is straightforward:
228 CONFTYPE_INT Math::BigInt
229 CONFTYPE_INT64 Math::BigInt
230 CONFTYPE_REAL floating-point value
232 CONFTYPE_IDENT string
233 CONFTYPE_TIME Math::BigInt (epoch value)
234 CONFTYPE_SIZE Math::BigInt
235 CONFTYPE_BOOLEAN Math::BigInt
236 CONFTYPE_COMPRESS Math::BigInt
237 CONFTYPE_ENCRYPT Math::BigInt
238 CONFTYPE_HOLDING Math::BigInt
239 CONFTYPE_ESTIMATELIST [ Math::BigInt, .. ]
240 CONFTYPE_STRATEGY Math::BigInt
241 CONFTYPE_TAPERALGO Math::BigInt
242 CONFTYPE_PRIORITY Math::BigInt
243 CONFTYPE_RATE float, float
244 CONFTYPE_INTRANGE Math::BigInt, Math::BigInt
245 CONFTYPE_APPLICATION string
246 CONFTYPE_EXECUTE_ON string
247 CONFTYPE_EXECUTE_WHERE Math::BigInt
248 CONFTYPE_SEND_AMREPORT_ON Math::BigInt
249 CONFTYPE_IDENTLIST [ string, .. ]
250 CONFTYPE_PART_CACHE_TYPE Math::BigInt
251 CONFTYPE_RECOVERY_LIMIT [ string, .. ] (undef if not specified;
252 undef in the list means same-host)
254 Note that C<CONFTYPE_INTRANGE> and C<CONFTYPE_RATE> each return two values, not
257 Include and exclude lists with type C<CONFTYPE_EXINCLUDE> return a hash giving
258 all listed filenames (in the C<list> key), include/exclude files (C<files>),
259 and a boolean indicating that the list is optional (C<optional>):
261 { list => [ str, .. ], file => [ str, .. ], optional => Math::BigInt }
263 Properties are represented as a hash of hashes. The keys are the property
264 names, converted to ASCII lowercase. Each property has a C<values> array
265 giving all values specified for this property, as well as booleans C<priority>
266 and C<append> that are true if the corresponding keyword was supplied.
268 { prop1 => { values => [ str, .. ] priority => int, append => int },
271 Note that integer types of all sizes become C<Math::BigInt> objects rather than
272 Perl integers, as is the habit throughout Amanda.
274 The C<CNF_AUTOLABEL> value is a hash with the following keys
276 template label template, or undef
284 Parameter values are available by name from C<getconf_byname($name)> and
285 C<getconf_byname_strs($name, $str_needs_quotes)>. These functions implement
286 the C<TYP:NAME:PARAM> syntax advertised by C<amgetconf> to access values in
287 subsections. The first function returns a Perl value (see DATA FORMATS,
288 above), while the second returns a list of strings suitable for use in
289 C<amanda.conf>, including quotes around strings if C<$str_needs_quotes> is
292 C<getconf_list($typ)> returns a list of the names of all subsections of the
293 given type. C<%subsection_names> is a hash whose keys are allowed subsection
296 =head2 DERIVED VALUES
298 The C<$CNF_DISPLAYUNIT> implies a certain divisor to convert from
299 kilobytes to the desired unit. This divisor is available from
300 C<getconf_unit_divisor()>. Note carefully that it is a I<divisor>
301 for a value in I<kilobytes>!
303 Finally, various subsections of Amanda enable verbose debugging via
304 configuration parameters. The status of each parameter is available
305 a similarly-named variable, e.g., C<$debug_auth>.
307 All parameter access functions and constants can be imported with
312 These functions defy categorization.
314 The function C<config_dir_relative> will interpret a path relative to
315 the current configuration directory. Absolute paths are passed through
316 unchanged, while relative paths are converted to absolute paths.
318 C<dump_configuration()> dumps the current configuration, in a format
319 suitable for re-evaluation for this module, to standard output.
320 This function may be revised to return a string.
322 Several parts of Amanda need to convert unit modifier value like
323 "gbytes" to a multiplier. The function C<find_multiplier($str)>
324 returns the unit multiplier for such a string. For example, "mbytes"
325 is converted to 1048576 (1024*1024).
327 C<string_to_boolean()> takes a string and returns 0 if it matches any of
328 Amanda's names for false, or 1 if matches a name for true. If it can't be
329 interpreted, C<undef> is returned.
331 C<amandaify_property_name()> converts a string into Amanda's property style:
332 all lower-case and with "-" replacing "_".
336 This section lists all of the configuration parameter constants defined in this
337 module. All of these constants are available with the C<:getconf> export tag.