#!/usr/local/bin/perl # edit_mon.cgi # Display a form for editing or creating a monitor require './status-lib.pl'; $access{'edit'} || &error($text{'mon_ecannot'}); &foreign_require("servers", "servers-lib.pl"); &ReadParse(); @handlers = &list_handlers(); if ($in{'type'}) { # Create a new monitor $in{'type'} =~ /^[a-zA-Z0-9\_\-\.\:]+$/ || &error($text{'mon_etype'}); $type = $in{'type'}; $title = $text{'mon_create'}; if ($in{'clone'}) { # Clone of existing $serv = &get_service($in{'clone'}); } else { # Totally new $serv = { 'notify' => 'email pager snmp sms', 'fails' => 1, 'nosched' => 0, 'remote' => '*' }; } } else { # Editing an existing monitor $serv = &get_service($in{'id'}); $type = $serv->{'type'}; $title = $text{'mon_edit'}; } ($han) = grep { $_->[0] eq $type } @handlers; if ($in{'type'} && !$in{'clone'}) { $serv->{'desc'} = $han->[1]; } &ui_print_header($han->[1], $title, ""); print &ui_form_start("save_mon.cgi", "post"); print &ui_hidden("type", $in{'type'}),"\n"; print &ui_hidden("id", $in{'id'}),"\n"; @tds = ( "width=30%" ); print &ui_table_start($text{'mon_header'}, "width=100%", 2, \@tds); # Check for clone modules of the monitor type if (-d "../$type") { local @st = stat("../$type"); opendir(DIR, ".."); foreach $m (readdir(DIR)) { local @lst = stat("../$m"); if (-l "../$m" && $st[1] == $lst[1]) { # found a clone push(@clones, $m); } } } # Show input for description print &ui_table_row($text{'mon_desc'}, &ui_textbox("desc", $serv->{'desc'}, 50), undef, \@tds); # Show current status if (!$in{'type'}) { @stats = &service_status($serv, 1); $stable = "
| ". ($stat->{'remote'} eq "*" ? $text{'mon_local'} : $stat->{'remote'}). " | \n"; $stable .= ": | \n"; } $stable .= "". ($stat->{'desc'} && $stat->{'up'} == 0 ? "$stat->{'desc'}" : $stat->{'desc'} ? $stat->{'desc'} : &status_to_string($stat->{'up'})). " | \n"; $stable .= "
\n";
print &ui_table_start($text{'mon_header2'}, "width=100%", 2, \@tds);
# Show commands to run on up/down
print &ui_table_row($text{'mon_ondown'},
&ui_textbox("ondown", $serv->{'ondown'}, 60),
undef, \@tds);
print &ui_table_row($text{'mon_onup'},
&ui_textbox("onup", $serv->{'onup'}, 60),
undef, \@tds);
print &ui_table_row($text{'mon_ontimeout'},
&ui_textbox("ontimeout", $serv->{'ontimeout'}, 60),
undef, \@tds);
print &ui_table_row(" ", "$text{'mon_oninfo'}",
undef, \@tds);
# Radio button for where to run commands
print &ui_table_row($text{'mon_runon'},
&ui_radio("runon", $serv->{'runon'} ? 1 : 0,
[ [ 0, $text{'mon_runon0'} ],
[ 1, $text{'mon_runon1'} ] ]),
undef, \@tds);
print &ui_table_end();
# Display inputs for this monitor type
if ($type =~ /^(\S+)::(\S+)$/) {
# From another module
($mod, $mtype) = ($1, $2);
&foreign_require($mod, "status_monitor.pl");
&foreign_call($mod, "load_theme_library");
if (&foreign_defined($mod, "status_monitor_dialog")) {
print &ui_table_start($text{'mon_header3'}, "width=100%", 4,
\@tds);
print &foreign_call($mod, "status_monitor_dialog", $mtype, $serv);
print &ui_table_end();
}
}
else {
# From this module
do "./${type}-monitor.pl";
$func = "show_${type}_dialog";
if (defined(&$func)) {
print &ui_table_start($text{'mon_header3'}, "width=100%", 4,
\@tds);
&$func($serv);
print &ui_table_end();
}
}
# Show history, in a hidden section
if (!$in{'type'}) {
@history = &list_history($serv,
$in{'all'} ? undef : $config{'history_show'});
}
if (@history) {
print &ui_hidden_table_start($text{'mon_header4'}, "width=100%", 2,
"history", defined($in{'all'}) || defined($in{'changes'}));
# Build links to switch to changes-only mode or show all history
@links = ( );
if ($in{'changes'}) {
push(@links, "$text{'mon_changes0'}");
}
else {
push(@links, "$text{'mon_changes1'}");
}
if (!$in{'all'}) {
push(@links, "$text{'mon_all'}");
}
if ($in{'changes'}) {
@history = grep { $_->{'old'} ne $_->{'new'} } @history;
}
# Check if any history events have a value
$anyvalue = 0;
foreach $h (@history) {
foreach my $hv (split(/\//, $h->{'value'})) {
my ($vhost, $v) = split(/=/, $hv, 2);
if ($v ne '') {
$anyvalue++;
last;
}
}
}
# Show history table
$links = &ui_links_row(\@links);
$table = &ui_columns_start([
$text{'mon_hwhen'},
$text{'mon_hold'},
$text{'mon_hnew'},
$anyvalue ? ( $text{'mon_hvalue'} ) : ( ) ]);
foreach $h (reverse(@history)) {
my @cols = ( &make_date($h->{'time'}) );
foreach my $s ($h->{'old'}, $h->{'new'}) {
my @ups;
my @statuses = split(/\s+/, $s);
foreach my $rs (@statuses) {
my ($host, $up) = split(/=/, $rs, 2);
$img = "";
if ($host ne "*") {
$img = $host.$img;
}
elsif (@statuses > 1) {
$img = &get_display_hostname().$img;
}
push(@ups, $img);
}
push(@cols, join(" ", @ups));
}
if ($anyvalue) {
my @vlist;
my @values = split(/\//, $h->{'value'});
my @nice_values = split(/\//, $h->{'nice_value'});
for(my $i=0; $i<@values; $i++) {
my ($vhost, $v) = split(/=/, $values[$i], 2);
my (undef, $nv) = split(/=/, $nice_values[$i], 2);
push(@vlist, $nv || $v);
}
push(@cols, join(" ", @vlist));
}
$table .= &ui_columns_row(\@cols);
}
$table .= &ui_columns_end();
if (@history) {
print &ui_table_row(undef, $links.$table, 2);
}
else {
print &ui_table_row(undef, $links.
&text('mon_nochanges', $config{'history_show'}), 2);
}
print &ui_hidden_table_end();
}
# Show create/delete buttons
if ($in{'type'}) {
print &ui_form_end([ [ "create", $text{'create'} ] ]);
}
else {
print &ui_form_end([ [ "save", $text{'save'} ],
[ "newclone", $text{'mon_clone2'} ],
[ "delete", $text{'delete'} ] ]);
}
&ui_print_footer("", $text{'index_return'});