1 |
95b003ff
|
Origo
|
# mon-lib.pl
|
2 |
|
|
# Common functions for mon
|
3 |
|
|
|
4 |
|
|
BEGIN { push(@INC, ".."); };
|
5 |
|
|
use WebminCore;
|
6 |
|
|
&init_config();
|
7 |
|
|
|
8 |
|
|
$mon_config_file = "$config{'cfbasedir'}/mon.cf";
|
9 |
|
|
$under = { '' => [ 'authtype', 'hostgroup', 'watch', 'use', 'period',
|
10 |
|
|
'serverbind', 'trapbind' ],
|
11 |
|
|
'watch' => [ 'service' ],
|
12 |
|
|
'service' => [ 'description', 'interval', 'monitor', 'period',
|
13 |
|
|
'depend', 'allow_empty_group', 'traptimeout',
|
14 |
|
|
'trapduration', 'randskew', 'dep_behavior',
|
15 |
|
|
'dep_behaviour', 'exclude_hosts', 'exclude_period',
|
16 |
|
|
'failure_interval', 'redistribute' ],
|
17 |
|
|
'period' => [ 'alert', 'upalert', 'alertevery', 'alertafter',
|
18 |
|
|
'numalerts', 'comp_alerts', 'startupalert',
|
19 |
|
|
'upalertafter', 'no_comp_alerts' ]
|
20 |
|
|
};
|
21 |
|
|
|
22 |
|
|
# get_mon_config()
|
23 |
|
|
# Parses the mon config file into a heirachical structure
|
24 |
|
|
sub get_mon_config
|
25 |
|
|
{
|
26 |
|
|
if (@get_mon_config_cache) {
|
27 |
|
|
return \@get_mon_config_cache;
|
28 |
|
|
}
|
29 |
|
|
local @rv;
|
30 |
|
|
local $cv = \@rv;
|
31 |
|
|
local ($last_indent, $parent);
|
32 |
|
|
local $lnum = 0;
|
33 |
|
|
open(CONF, $mon_config_file);
|
34 |
|
|
while(<CONF>) {
|
35 |
|
|
local $slnum = $lnum;
|
36 |
|
|
s/\s+$//;
|
37 |
|
|
s/#.*$//g;
|
38 |
|
|
while(s/\\$//) {
|
39 |
|
|
local $nl = <CONF>;
|
40 |
|
|
$nl =~ s/\s+$//;
|
41 |
|
|
$nl =~ s/^\s+//;
|
42 |
|
|
$_ .= $nl;
|
43 |
|
|
$lnum++;
|
44 |
|
|
}
|
45 |
|
|
if (/^(\S+)\s*=\s*(.*)$/) {
|
46 |
|
|
# Global directive
|
47 |
|
|
local $str = { 'name' => $1,
|
48 |
|
|
'values' => [ split(/\s+/, $2) ],
|
49 |
|
|
'value' => $2,
|
50 |
|
|
'global' => 1,
|
51 |
|
|
'index' => scalar(@rv),
|
52 |
|
|
'line' => $slnum,
|
53 |
|
|
'eline' => $lnum };
|
54 |
|
|
push(@rv, $str);
|
55 |
|
|
}
|
56 |
|
|
elsif (/^(\s*)(\S+)\s*(.*)$/) {
|
57 |
|
|
# Normal directive, possibly in a heirachy
|
58 |
|
|
local $str = { 'name' => $2,
|
59 |
|
|
'values' => [ split(/\s+/, $3) ],
|
60 |
|
|
'value' => $3,
|
61 |
|
|
'members' => [ { 'dummy' => 1,
|
62 |
|
|
'line' => $lnum,
|
63 |
|
|
'eline' => $lnum } ],
|
64 |
|
|
'indent' => $1,
|
65 |
|
|
'line' => $slnum,
|
66 |
|
|
'eline' => $lnum };
|
67 |
|
|
|
68 |
|
|
# Check if under the previous directive
|
69 |
|
|
local $found;
|
70 |
|
|
if (@$cv > 0) {
|
71 |
|
|
local $ld = $cv->[@$cv-1];
|
72 |
|
|
local $pu = $under->{$ld->{'name'}};
|
73 |
|
|
foreach $u (@$pu) {
|
74 |
|
|
$found++ if ($u eq $str->{'name'});
|
75 |
|
|
}
|
76 |
|
|
if ($found) {
|
77 |
|
|
# It is .. so just update the parent
|
78 |
|
|
$parent = $ld;
|
79 |
|
|
$cv = $ld ? $ld->{'members'} : \@rv;
|
80 |
|
|
}
|
81 |
|
|
}
|
82 |
|
|
|
83 |
|
|
if (!$found) {
|
84 |
|
|
# Check if under a parent
|
85 |
|
|
local $pp = $parent;
|
86 |
|
|
while(1) {
|
87 |
|
|
local $pu = $under->{$pp ? $pp->{'name'} : ""};
|
88 |
|
|
foreach $u (@$pu) {
|
89 |
|
|
$found++ if ($u eq $str->{'name'});
|
90 |
|
|
}
|
91 |
|
|
if ($found) {
|
92 |
|
|
# Under some ancestor .. make that
|
93 |
|
|
# the current parent
|
94 |
|
|
$parent = $pp;
|
95 |
|
|
$cv = $pp ? $pp->{'members'} : \@rv;
|
96 |
|
|
last;
|
97 |
|
|
}
|
98 |
|
|
else {
|
99 |
|
|
last if (!$pp);
|
100 |
|
|
$pp = $pp->{'parent'};
|
101 |
|
|
}
|
102 |
|
|
}
|
103 |
|
|
|
104 |
|
|
if (!$found) {
|
105 |
|
|
# Check if a hostname under a previous hostgroup
|
106 |
|
|
if (@$cv &&
|
107 |
|
|
$cv->[$#cv]->{'name'} eq 'hostgroup') {
|
108 |
|
|
push(@{$cv->[$#cv]->{'values'}},
|
109 |
|
|
$str->{'name'});
|
110 |
|
|
$cv->[$#cv]->{'eline'} = $lnum;
|
111 |
|
|
goto nextline;
|
112 |
|
|
}
|
113 |
|
|
}
|
114 |
|
|
&error("Unknown directive $str->{'name'}")
|
115 |
|
|
if (!$found);
|
116 |
|
|
}
|
117 |
|
|
|
118 |
|
|
$str->{'index'} = scalar(@$cv);
|
119 |
|
|
$str->{'parent'} = $parent;
|
120 |
|
|
push(@$cv, $str);
|
121 |
|
|
|
122 |
|
|
# Set parent end lines
|
123 |
|
|
local $pp = $parent;
|
124 |
|
|
do {
|
125 |
|
|
$pp->{'eline'} = $lnum;
|
126 |
|
|
$pp = $pp->{'parent'};
|
127 |
|
|
} while($pp);
|
128 |
|
|
}
|
129 |
|
|
|
130 |
|
|
nextline:
|
131 |
|
|
$lnum++;
|
132 |
|
|
}
|
133 |
|
|
close(CONF);
|
134 |
|
|
@get_mon_config_cache = @rv;
|
135 |
|
|
return \@get_mon_config_cache;
|
136 |
|
|
}
|
137 |
|
|
|
138 |
|
|
# find_value(name, &config)
|
139 |
|
|
sub find_value
|
140 |
|
|
{
|
141 |
|
|
foreach $c (@{$_[1]}) {
|
142 |
|
|
if ($c->{'name'} eq $_[0]) {
|
143 |
|
|
return wantarray ? @{$c->{'values'}} : $c->{'values'}->[0];
|
144 |
|
|
}
|
145 |
|
|
}
|
146 |
|
|
return wantarray ? ( ) : undef;
|
147 |
|
|
}
|
148 |
|
|
|
149 |
|
|
# find(name, &config)
|
150 |
|
|
sub find
|
151 |
|
|
{
|
152 |
|
|
local @rv;
|
153 |
|
|
foreach $c (@{$_[1]}) {
|
154 |
|
|
if ($c->{'name'} eq $_[0]) {
|
155 |
|
|
push(@rv, $c);
|
156 |
|
|
}
|
157 |
|
|
}
|
158 |
|
|
return wantarray ? @rv : $rv[0];
|
159 |
|
|
}
|
160 |
|
|
|
161 |
|
|
# save_directive(&config, [&old|undef], [&new|undef])
|
162 |
|
|
sub save_directive
|
163 |
|
|
{
|
164 |
|
|
local $lref = &read_file_lines($mon_config_file);
|
165 |
|
|
local @same = &find($_[2]->{'name'}, $_[0]) if ($_[2]);
|
166 |
|
|
local $idx = &indexof($_[1], @{$_[0]}) if ($_[1]);
|
167 |
|
|
local $olen = $_[1]->{'eline'} - $_[1]->{'line'} + 1 if ($_[1]);
|
168 |
|
|
local $conf = &get_mon_config();
|
169 |
|
|
local @dirs = &directive_lines($_[2], $_[2]->{'indent'}) if ($_[2]);
|
170 |
|
|
if ($_[1]) {
|
171 |
|
|
# Replace the old directive
|
172 |
|
|
splice(@$lref, $_[1]->{'line'}, $_[1]->{'eline'} - $_[1]->{'line'} + 1,
|
173 |
|
|
@dirs);
|
174 |
|
|
$_[0]->[$idx] = $_[2];
|
175 |
|
|
&renumber($conf, $_[1]->{'line'}, @dirs - $olen);
|
176 |
|
|
$_[2]->{'line'} = $_[1]->{'line'};
|
177 |
|
|
$_[2]->{'eline'} = $_[2]->{'line'} + @dirs - 1;
|
178 |
|
|
}
|
179 |
|
|
elsif (!$_[2]) {
|
180 |
|
|
# Remove the old directive
|
181 |
|
|
splice(@$lref, $_[1]->{'line'}, $_[1]->{'eline'} - $_[1]->{'line'} + 1);
|
182 |
|
|
splice(@{$_[0]}, $idx, 1);
|
183 |
|
|
&renumber($conf, $_[1]->{'line'}, -$olen);
|
184 |
|
|
}
|
185 |
|
|
elsif (@same) {
|
186 |
|
|
# Add after last directive of same type
|
187 |
|
|
splice(@$lref, $same[@same-1]->{'eline'}+1, 0, @dirs);
|
188 |
|
|
splice(@{$_[0]}, $idx+1, 0, $_[2]);
|
189 |
|
|
&renumber($conf, $same[@same-1]->{'eline'}+1, scalar(@dirs));
|
190 |
|
|
$_[2]->{'line'} = $same[@same-1]->{'eline'}+1;
|
191 |
|
|
$_[2]->{'eline'} = $_[2]->{'line'} + @dirs - 1;
|
192 |
|
|
}
|
193 |
|
|
else {
|
194 |
|
|
# Add after last directive in config
|
195 |
|
|
local $ld = $_[0]->[@{$_[0]}-1];
|
196 |
|
|
splice(@$lref, $ld->{'eline'} + 1, 0, @dirs);
|
197 |
|
|
push(@{$_[0]}, $_[2]);
|
198 |
|
|
&renumber($conf, $ld->{'eline'} + 1, scalar(@dirs));
|
199 |
|
|
$_[2]->{'line'} = $ld->{'eline'} + 1;
|
200 |
|
|
$_[2]->{'eline'} = $ld->{'eline'} + @dirs - 1;
|
201 |
|
|
}
|
202 |
|
|
}
|
203 |
|
|
|
204 |
|
|
# renumber(&config, position, offset)
|
205 |
|
|
sub renumber
|
206 |
|
|
{
|
207 |
|
|
foreach $c (@{$_[0]}) {
|
208 |
|
|
$c->{'line'} += $_[2] if ($c->{'line'} >= $_[1]);
|
209 |
|
|
$c->{'eline'} += $_[2] if ($c->{'eline'} >= $_[1]);
|
210 |
|
|
&renumber($c->{'members'}, $_[1], $_[2]);
|
211 |
|
|
}
|
212 |
|
|
}
|
213 |
|
|
|
214 |
|
|
# directive_lines(&directive, [indent])
|
215 |
|
|
sub directive_lines
|
216 |
|
|
{
|
217 |
|
|
local @rv;
|
218 |
|
|
@rv = ( $_[1].join(" ", $_[0]->{'name'}, $_[0]->{'global'} ? ( "=" ) : ( ),
|
219 |
|
|
@{$_[0]->{'values'}}) );
|
220 |
|
|
foreach $m (@{$_[0]->{'members'}}) {
|
221 |
|
|
push(@rv, &directive_lines($m, "$_[1] ")) if (!$m->{'dummy'});
|
222 |
|
|
}
|
223 |
|
|
return @rv;
|
224 |
|
|
}
|
225 |
|
|
|
226 |
|
|
# list_monitors()
|
227 |
|
|
# Returns a list of all monitors
|
228 |
|
|
sub list_monitors
|
229 |
|
|
{
|
230 |
|
|
local $conf = &get_mon_config();
|
231 |
|
|
local $mondir = &find_value("mondir", $conf);
|
232 |
|
|
local @rv;
|
233 |
|
|
foreach my $dir (split(/:/, $mondir)) {
|
234 |
|
|
opendir(DIR, $dir);
|
235 |
|
|
foreach my $f (readdir(DIR)) {
|
236 |
|
|
push(@rv, $f) if ($f =~ /\.monitor$/);
|
237 |
|
|
}
|
238 |
|
|
closedir(DIR);
|
239 |
|
|
}
|
240 |
|
|
return @rv;
|
241 |
|
|
}
|
242 |
|
|
|
243 |
|
|
# list_alerts()
|
244 |
|
|
# Returns a list of all alerts
|
245 |
|
|
sub list_alerts
|
246 |
|
|
{
|
247 |
|
|
local $conf = &get_mon_config();
|
248 |
|
|
local $mondir = &find_value("alertdir", $conf);
|
249 |
|
|
local @rv;
|
250 |
|
|
foreach my $dir (split(/:/, $mondir)) {
|
251 |
|
|
opendir(DIR, $dir);
|
252 |
|
|
foreach $f (readdir(DIR)) {
|
253 |
|
|
push(@rv, $f) if ($f =~ /\.alert$/ || $f =~ /^alert\./);
|
254 |
|
|
}
|
255 |
|
|
closedir(DIR);
|
256 |
|
|
}
|
257 |
|
|
return @rv;
|
258 |
|
|
}
|
259 |
|
|
|
260 |
|
|
# mon_users_file()
|
261 |
|
|
# Returns the file in which MON users are stored
|
262 |
|
|
sub mon_users_file
|
263 |
|
|
{
|
264 |
|
|
local $conf = &get_mon_config();
|
265 |
|
|
local $uf = &find_value("userfile", $conf);
|
266 |
|
|
$uf = "monusers.cf" if (!$uf);
|
267 |
|
|
if ($uf =~ /^\//) {
|
268 |
|
|
return $uf;
|
269 |
|
|
}
|
270 |
|
|
else {
|
271 |
|
|
local $bd = &find_value("cfbasedir", $conf);
|
272 |
|
|
$bd = $config{'cfbasedir'} if (!$bd);
|
273 |
|
|
return "$bd/$uf";
|
274 |
|
|
}
|
275 |
|
|
}
|
276 |
|
|
|
277 |
|
|
# list_users()
|
278 |
|
|
sub list_users
|
279 |
|
|
{
|
280 |
|
|
local(@rv, $lnum = 0);
|
281 |
|
|
open(USERS, &mon_users_file());
|
282 |
|
|
while(<USERS>) {
|
283 |
|
|
s/\r|\n//g;
|
284 |
|
|
s/#.*//;
|
285 |
|
|
if (/^([^:]+):(\S+)/) {
|
286 |
|
|
push(@rv, { 'user' => $1, 'pass' => $2, 'line' => $lnum });
|
287 |
|
|
}
|
288 |
|
|
$lnum++;
|
289 |
|
|
}
|
290 |
|
|
close(USERS);
|
291 |
|
|
return @rv;
|
292 |
|
|
}
|
293 |
|
|
|
294 |
|
|
# create_user(&user)
|
295 |
|
|
sub create_user
|
296 |
|
|
{
|
297 |
|
|
local $lref = &read_file_lines(&mon_users_file());
|
298 |
|
|
push(@$lref, $_[0]->{'user'}.":".$_[0]->{'pass'});
|
299 |
|
|
&flush_file_lines(&mon_users_file());
|
300 |
|
|
}
|
301 |
|
|
|
302 |
|
|
# modify_user(&user)
|
303 |
|
|
sub modify_user
|
304 |
|
|
{
|
305 |
|
|
local $lref = &read_file_lines(&mon_users_file());
|
306 |
|
|
$lref->[$_[0]->{'line'}] = $_[0]->{'user'}.":".$_[0]->{'pass'};
|
307 |
|
|
&flush_file_lines(&mon_users_file());
|
308 |
|
|
}
|
309 |
|
|
|
310 |
|
|
# delete_user(&user)
|
311 |
|
|
sub delete_user
|
312 |
|
|
{
|
313 |
|
|
local $lref = &read_file_lines(&mon_users_file());
|
314 |
|
|
splice(@$lref, $_[0]->{'line'}, 1);
|
315 |
|
|
&flush_file_lines(&mon_users_file());
|
316 |
|
|
}
|
317 |
|
|
|
318 |
|
|
# mon_auth_file()
|
319 |
|
|
sub mon_auth_file
|
320 |
|
|
{
|
321 |
|
|
local $conf = &get_mon_config();
|
322 |
|
|
local $af = &find_value("authfile", $conf);
|
323 |
|
|
$af = "auth.cf" if (!$af);
|
324 |
|
|
if ($af =~ /^\//) {
|
325 |
|
|
return $af;
|
326 |
|
|
}
|
327 |
|
|
else {
|
328 |
|
|
local $bd = &find_value("cfbasedir", $conf);
|
329 |
|
|
return "$bd/$af";
|
330 |
|
|
}
|
331 |
|
|
}
|
332 |
|
|
|
333 |
|
|
sub list_auth_types
|
334 |
|
|
{
|
335 |
|
|
return ('list', 'servertime', 'reset', 'loadstate', 'savestate',
|
336 |
|
|
'term', 'stop', 'start', 'set', 'get', 'dump', 'disable',
|
337 |
|
|
'enable', 'test', 'ack', 'reload', 'clear');
|
338 |
|
|
}
|
339 |
|
|
|
340 |
|
|
# day_input(name, value)
|
341 |
|
|
sub day_input
|
342 |
|
|
{
|
343 |
|
|
local @days = ( 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun' );
|
344 |
|
|
local $rv = "<select name=$_[0]>\n";
|
345 |
|
|
foreach $d (@days) {
|
346 |
|
|
$rv .= sprintf "<option %s>%s\n",
|
347 |
|
|
lc($d) eq lc($_[1]) ? "selected" : "", $d;
|
348 |
|
|
}
|
349 |
|
|
$rv .= "</select>\n";
|
350 |
|
|
return $rv;
|
351 |
|
|
}
|
352 |
|
|
|
353 |
|
|
# interval_input(name, value)
|
354 |
|
|
sub interval_input
|
355 |
|
|
{
|
356 |
|
|
local ($int, $units, $rv);
|
357 |
|
|
if ($_[1] =~ /^([\d\.]+)(\S)$/) {
|
358 |
|
|
$int = $1; $units = $2;
|
359 |
|
|
}
|
360 |
|
|
$rv = "<input name=$_[0] size=6 value='$int'>\n";
|
361 |
|
|
$rv .= "<select name=$_[0]_u>\n";
|
362 |
|
|
foreach $u ('s', 'm', 'h', 'd') {
|
363 |
|
|
$rv .= sprintf "<option value=%s %s>%s\n",
|
364 |
|
|
$u, $units eq $u ? "selected" : "", $text{"service_units_$u"};
|
365 |
|
|
}
|
366 |
|
|
$rv .= "</select>\n";
|
367 |
|
|
return $rv;
|
368 |
|
|
}
|
369 |
|
|
|
370 |
|
|
# restart_mon()
|
371 |
|
|
# Re-start the MON process, returning undef on success or an error message
|
372 |
|
|
# on failure
|
373 |
|
|
sub restart_mon
|
374 |
|
|
{
|
375 |
|
|
if ($config{'restart_cmd'}) {
|
376 |
|
|
local $out =
|
377 |
|
|
&backquote_logged("$config{'restart_cmd'} 2>&1 </dev/null");
|
378 |
|
|
return "<tt>$out</tt>" if ($?);
|
379 |
|
|
}
|
380 |
|
|
else {
|
381 |
|
|
local $pid = &check_pid_file($config{'pid_file'});
|
382 |
|
|
if ($pid) {
|
383 |
|
|
kill('HUP', $pid);
|
384 |
|
|
return undef;
|
385 |
|
|
}
|
386 |
|
|
else {
|
387 |
|
|
return $text{'restart_epid'};
|
388 |
|
|
}
|
389 |
|
|
}
|
390 |
|
|
}
|
391 |
|
|
|
392 |
|
|
1;
|