1package Munin::Common::Config;
2
3use warnings;
4use strict;
5
6use Carp;
7use English qw(-no_match_vars);
8
9# Functions here are unable to log as they don't know if they're used
10# by the node or the master which use divergent logging facilities.
11# In fact, the list in %legal is only used by the master.
12
13my %legal = map { $_ => 1 } qw(
14	address
15	always_send
16	category_order
17	cdef
18	cdef_name
19	cgitmpdir
20	cgiurl
21	cgiurl_graph
22	colour
23	command
24	compare
25	contact
26	contacts
27	create_args
28	critical
29	dbdir
30	domain_order
31	draw
32	dropdownlimit
33	extinfo
34	fetch_data
35	filename
36	fork
37	graph
38	graph
39	graphable
40	graph_args
41	graph_args_after
42	graph_category
43	graph_data_size
44	graph_future
45	graph_height
46	graph_info
47	graph_noscale
48	graph_order
49	graph_period
50	graph_printf
51	graph_scale
52	graph_sources
53	graph_strategy
54	graph_sums
55	graph_title
56	graph_total
57	graph_vlabel
58	graph_vtitle
59	graph_width
60	group_order
61	host_name
62	htaccess
63	htmldir
64	html_rename
65	html_strategy
66	includedir
67	info
68	label
69	line
70	local_address
71	logdir
72	max
73	max_cgi_graph_jobs
74	max_graph_jobs
75	max_html_jobs
76	max_messages
77	max_processes
78	max_size_x
79	max_size_y
80	min
81	munin_cgi_graph_jobs
82	nagios
83	ncsa
84	ncsa_config
85	ncsa_server
86	negative
87	node_order
88	notify_alias
89	nsca
90	nsca_config
91	nsca_server
92	num_messages
93	num_unknowns
94	ok
95	onlynullcdef
96	palette
97	pipe
98	pipe_command
99	port
100	predict
101	process
102	realname
103	realservname
104	rrdcached_socket
105	rundir
106	service_order
107	skipdraw
108	ssh_command
109	ssh_options
110	stack
111	state
112	staticdir
113	sum
114	text
115	timeout
116	timeout_fetch_one_node
117	timeout_fetch_all_nodes
118	tls
119	tls_ca_certificate
120	tls_certificate
121	tls_match
122	tls_pem
123	tls_private_key
124	tls_verify_certificate
125	tls_verify_depth
126	tmpldir
127	trend
128	type
129	unknown
130	unknown_limit
131	update
132	update_rate
133	use_default_name
134	use_node_name
135	use_default_node
136	version
137	warn
138	warning
139	worker_start_delay
140);
141
142my %bools = map { $_ => 1} qw(yes no true false on off 1 0);
143
144sub cl_is_keyword {
145    # Class-less version of is_keyword for legacy code.
146    my ($word) = @_;
147
148    return defined $legal{$word};
149}
150
151
152sub is_keyword {
153    my ($self, $word) = @_;
154
155    return defined $legal{$word};
156}
157
158
159sub parse_config_from_file {
160    my ($self, $config_file) = @_;
161
162    $config_file ||= $self->{config_file};
163
164    open my $file, '<', $config_file
165        or croak "ERROR: Cannot open '$config_file': $OS_ERROR";
166
167    # Note, parse_config is provided by node or master specific config class
168    eval {
169        $self->parse_config($file);
170    };
171    if ($EVAL_ERROR) {
172        croak "ERROR: Failed to parse config file '$config_file': $EVAL_ERROR";
173    }
174
175    close $file
176        or croak "Cannot close '$config_file': $OS_ERROR";
177}
178
179
180sub _trim {
181    # Trim leading and trailing whitespace.
182    my $class = shift;
183
184    chomp $_[0];
185    $_[0] =~ s/^\s+//;
186    $_[0] =~ s/\s+$//;
187
188    return;
189}
190
191
192# allows # characters to get through as long as they're escaped
193# with a backslash
194sub _strip_comment {
195    my $class = shift;
196
197    $_[0] =~ s/(?<!\\)#.*//;
198    $_[0] =~ s/\\#/#/g;
199
200    return;
201}
202
203
204sub _looks_like_a_bool {
205    my ($class, $str) = @_;
206
207    return $bools{lc $str};
208}
209
210
211sub _parse_bool {
212    my ($class, $str) = @_;
213
214    croak "Parse exception: '$str' is not a boolean."
215        unless $class->_looks_like_a_bool($str);
216
217    return $str =~ m{\A no|false|off|0 \z}xi ? 0 : 1;
218}
219
220
2211;
222
223
224__END__
225
226=head1 NAME
227
228Munin::Common::Config - Abstract base class for common config code.
229
230=head1 SYNOPSIS
231
232Don't use it directly. See L<Munin::Master::Config> and L<Munin::Node::Config>.
233
234=head1 METHODS
235
236=over
237
238=item B<parse_config_from_file>
239
240 $config->parse_config_from_file($file_name);
241
242Parses the configuration in $file_name.
243
244=back
245