1 |
95b003ff
|
Origo
|
#!/usr/bin/perl
|
2 |
|
|
# save_service.cgi
|
3 |
|
|
# Save, create or delete a service
|
4 |
|
|
|
5 |
|
|
require './mon-lib.pl';
|
6 |
|
|
&ReadParse();
|
7 |
|
|
$conf = &get_mon_config();
|
8 |
|
|
$watch = $conf->[$in{'idx'}];
|
9 |
|
|
if ($in{'sidx'} ne '') {
|
10 |
|
|
$oldservice = $service = $watch->{'members'}->[$in{'sidx'}];
|
11 |
|
|
}
|
12 |
|
|
else {
|
13 |
|
|
$service = { 'name' => 'service',
|
14 |
|
|
'indent' => ' ',
|
15 |
|
|
'members' => [ ] };
|
16 |
|
|
}
|
17 |
|
|
&error_setup($text{'service_err'});
|
18 |
|
|
|
19 |
|
|
if ($in{'delete'}) {
|
20 |
|
|
# Delete this service from the watch
|
21 |
|
|
&save_directive($watch->{'members'}, $service, undef);
|
22 |
|
|
}
|
23 |
|
|
else {
|
24 |
|
|
# Validate and store service inputs
|
25 |
|
|
$in{'name'} =~ /^\S+$/ || &error($text{'service_ename'});
|
26 |
|
|
$service->{'values'} = [ $in{'name'} ];
|
27 |
|
|
$in{'interval'} =~ /^\d+$/ || &error($text{'service_einterval'});
|
28 |
|
|
|
29 |
|
|
# Save the description
|
30 |
|
|
if ($in{'desc'}) {
|
31 |
|
|
&set_directive($service->{'members'}, "description",
|
32 |
|
|
$in{'desc'});
|
33 |
|
|
}
|
34 |
|
|
else {
|
35 |
|
|
&set_directive($service->{'members'}, "description");
|
36 |
|
|
}
|
37 |
|
|
|
38 |
|
|
&set_directive($service->{'members'}, "interval",
|
39 |
|
|
$in{'interval'}.$in{'interval_u'});
|
40 |
|
|
if ($in{'monitor_def'}) {
|
41 |
|
|
&set_directive($service->{'members'}, "monitor",
|
42 |
|
|
$in{'monitor'}.' '.$in{'args'});
|
43 |
|
|
}
|
44 |
|
|
else {
|
45 |
|
|
$in{'other'} =~ /^\S+$/ || &error($text{'service_eother'});
|
46 |
|
|
&set_directive($service->{'members'}, "monitor",
|
47 |
|
|
$in{'other'}.' '.$in{'args'});
|
48 |
|
|
}
|
49 |
|
|
|
50 |
|
|
# Validate and store each period
|
51 |
|
|
for($i=0; defined($in{"idx_$i"}); $i++) {
|
52 |
|
|
# Save period time
|
53 |
|
|
next if ($in{"delete_$i"});
|
54 |
|
|
local $period;
|
55 |
|
|
if (!$in{'new'} && $in{"idx_$i"} ne '') {
|
56 |
|
|
$period = $service->{'members'}->[$in{"idx_$i"}];
|
57 |
|
|
}
|
58 |
|
|
else {
|
59 |
|
|
$period = { 'name' => 'period',
|
60 |
|
|
'members' => [ ] };
|
61 |
|
|
}
|
62 |
|
|
if ($in{"known_$i"} == 0) {
|
63 |
|
|
$in{"pstr_$i"} =~ /\S/ ||
|
64 |
|
|
&error($text{'service_epstr'});
|
65 |
|
|
$period->{'values'} = [ $in{"pstr_$i"} ];
|
66 |
|
|
}
|
67 |
|
|
elsif ($in{"known_$i"} == 2) {
|
68 |
|
|
$period->{'values'} = [ $in{"name_$i"}.":" ];
|
69 |
|
|
}
|
70 |
|
|
else {
|
71 |
|
|
local @pv;
|
72 |
|
|
if (!$in{"days_def_$i"}) {
|
73 |
|
|
push(@pv, "wd {".$in{"dfrom_$i"}."-".
|
74 |
|
|
$in{"dto_$i"}."}");
|
75 |
|
|
}
|
76 |
|
|
if (!$in{"hours_def_$i"}) {
|
77 |
|
|
$in{"hfrom_$i"} =~ /^(\d+)(am|pm|)$/ ||
|
78 |
|
|
&error(&text('service_ehour', $i+1));
|
79 |
|
|
$in{"hto_$i"} =~ /^(\d+)(am|pm|)$/ ||
|
80 |
|
|
&error(&text('service_ehour', $i+1));
|
81 |
|
|
push(@pv, "hr {".$in{"hfrom_$i"}."-".
|
82 |
|
|
$in{"hto_$i"}."}");
|
83 |
|
|
}
|
84 |
|
|
#@pv || &error(&text('service_eperiod', $i+1));
|
85 |
|
|
$period->{'values'} = \@pv;
|
86 |
|
|
}
|
87 |
|
|
|
88 |
|
|
# Save alerts
|
89 |
|
|
local (@alert, @upalert, @startupalert);
|
90 |
|
|
for($j=0; defined($in{"alert_${i}_${j}"}); $j++) {
|
91 |
|
|
next if (!$in{"alert_${i}_${j}"});
|
92 |
|
|
local @v = ( $in{"alert_${i}_${j}"},
|
93 |
|
|
$in{"aargs_${i}_${j}"} );
|
94 |
|
|
if ($in{"atype_${i}_${j}"} eq 'alert') {
|
95 |
|
|
push(@alert, { 'name' => 'alert',
|
96 |
|
|
'values' => \@v });
|
97 |
|
|
}
|
98 |
|
|
elsif ($in{"atype_${i}_${j}"} eq 'upalert') {
|
99 |
|
|
push(@upalert, { 'name' => 'upalert',
|
100 |
|
|
'values' => \@v });
|
101 |
|
|
}
|
102 |
|
|
else {
|
103 |
|
|
push(@startupalert, { 'name' => 'startupalert',
|
104 |
|
|
'values' => \@v });
|
105 |
|
|
}
|
106 |
|
|
}
|
107 |
|
|
&set_directive($period->{'members'}, "alert", @alert);
|
108 |
|
|
&set_directive($period->{'members'}, "upalert", @upalert);
|
109 |
|
|
&set_directive($period->{'members'}, "startupalert",
|
110 |
|
|
@startupalert);
|
111 |
|
|
|
112 |
|
|
# Save other period options
|
113 |
|
|
if ($in{"every_def_$i"}) {
|
114 |
|
|
&set_directive($period->{'members'}, "alertevery");
|
115 |
|
|
}
|
116 |
|
|
else {
|
117 |
|
|
$in{"every_$i"} =~ /^\d+$/ ||
|
118 |
|
|
&error($text{'service_eevery'});
|
119 |
|
|
&set_directive($period->{'members'}, "alertevery",
|
120 |
|
|
$in{"every_$i"}.$in{"every_${i}_u"});
|
121 |
|
|
}
|
122 |
|
|
|
123 |
|
|
if ($in{"after_def_$i"}) {
|
124 |
|
|
&set_directive($period->{'members'}, "alertafter");
|
125 |
|
|
}
|
126 |
|
|
else {
|
127 |
|
|
$in{"after_$i"} =~ /^\d+$/ ||
|
128 |
|
|
&error($text{'service_eafter'});
|
129 |
|
|
if ($in{"after_interval_$i"} =~ /^\d+$/) {
|
130 |
|
|
&set_directive($period->{'members'},
|
131 |
|
|
"alertafter", $in{"after_$i"}." ".
|
132 |
|
|
$in{"after_interval_$i"}.
|
133 |
|
|
$in{"after_interval_${i}_u"});
|
134 |
|
|
}
|
135 |
|
|
}
|
136 |
|
|
|
137 |
|
|
if ($in{"num_def_$i"}) {
|
138 |
|
|
&set_directive($period->{'members'}, "numalerts");
|
139 |
|
|
}
|
140 |
|
|
else {
|
141 |
|
|
$in{"num_$i"} =~ /^\d+$/ ||
|
142 |
|
|
&error($text{'service_enum'});
|
143 |
|
|
&set_directive($period->{'members'}, "numalerts",
|
144 |
|
|
$in{"num_$i"});
|
145 |
|
|
}
|
146 |
|
|
|
147 |
|
|
push(@period, $period);
|
148 |
|
|
}
|
149 |
|
|
&set_directive($service->{'members'}, "period", @period);
|
150 |
|
|
|
151 |
|
|
# Store the service in the config file
|
152 |
|
|
&save_directive($watch->{'members'}, $oldservice, $service);
|
153 |
|
|
}
|
154 |
|
|
&flush_file_lines();
|
155 |
|
|
&redirect("list_watches.cgi");
|
156 |
|
|
|
157 |
|
|
# set_directive(&config, name, value, value, ..)
|
158 |
|
|
sub set_directive
|
159 |
|
|
{
|
160 |
|
|
local @o = &find($_[1], $_[0]);
|
161 |
|
|
local @n = @_[2 .. @_-1];
|
162 |
|
|
local $i;
|
163 |
|
|
for($i=0; $i<@o || $i<@n; $i++) {
|
164 |
|
|
local $idx = &indexof($o[$i], @{$_[0]}) if ($o[$i]);
|
165 |
|
|
local $nv = ref($n[$i]) ? $n[$i] : { 'name' => $_[1],
|
166 |
|
|
'values' => [ $n[$i] ] }
|
167 |
|
|
if (defined($n[$i]));
|
168 |
|
|
if ($o[$i] && defined($n[$i])) {
|
169 |
|
|
$_[0]->[$idx] = $nv;
|
170 |
|
|
}
|
171 |
|
|
elsif ($o[$i]) {
|
172 |
|
|
splice(@{$_[0]}, $idx, 1);
|
173 |
|
|
}
|
174 |
|
|
else {
|
175 |
|
|
push(@{$_[0]}, $nv);
|
176 |
|
|
}
|
177 |
|
|
}
|
178 |
|
|
}
|