#!/usr/local/bin/perl
# services.cgi
#
###
require './mon-lib.pl';
&ReadParse();
&header($text{'services_title'}, "","service");
print"<hr>";
#
local @groups=();
local @watchgroups=();
local %members=();
local %services=();
for (local $i=0;$i<$in{'count'};$i++){
	if($in{"groups_$i"}){
		local $groupname=$in{"groups_$i"};
		push(@groups,$groupname);
		push(@watchgroups,$groupname) if($in{"watchgroup_$i"}=~/Yes/);
		$members{$groupname}=$in{"members_$i"};
		$services{$groupname}=(join ' ',split(/\0/,$in{"services_$i"}),$in{"other_services_$i"});
#print"hostgroup $groupname $members{$groupname}<br>";
#print"SERV= $services{$groupname}<br>";
	}
}
#
## error messeges if insufficient data 
if($#watchgroups<0){
	&error($text{'err_nowatch'});
}else{
	foreach (@watchgroups){
		&error("$text{'err_nomemb'}: $_") if(!($members{$_}));
		&error("$text{'err_noserv'}: $_") if(!($services{$_}));
	}
}
#
###form generation
print"<form action=save_config.cgi><center>\n";
local @aaa=%members;
local @monitors=split(/\s+/,(local $a=`ls $config{'mondir'}`));
local @alerts=split(/\s+/,(local $b=`ls $config{'alertdir'}`));
	foreach $g(@watchgroups){
print <<EOF;
<table border width=90%><tr $tb align=left><th colspan=2><table><tr><td>$text{'watch'}:</td><td><font color=red size=+2>$g</font></td></tr><tr><td>$text{'members'}:</td><td><font color=red size=+2>$members{$g}</font></td></tr></table></th></tr>
<tr bgcolor=wheat><th>$text{'head_service'}</th><th>$text{'head_param'}<br>( Parameters in <font color=red> RED</font> color are Mandatory )</th></tr>
EOF
local @tmp=split(/\s+/,$services{$g});
	foreach $ser(@tmp){
	local $def_descargs="\'$ser\' service for watchgroup \'$g\'";
print <<EOF;
<tr bgcolor=#cccccc><td><b>$ser</b></td>
<td><table><tr>
<td><input type=checkbox name=def_$g.$ser value=\"$text{'def_desc'}\" checked>&nbsp;$text{'def_desc'}<input type=text name=value_$g.$ser.$text{'def_desc'} size=14 value=\"$def_descargs\"></td>
<td><input type=checkbox name=def_$g.$ser value=\"$text{'def_mon'}\" checked>&nbsp;<font color=red>$text{'def_mon'}</font><select name=value_$g.$ser.$text{'def_mon'} size=1 single>
EOF
	foreach $mon(@monitors){
		if(!($mon=~/monitor$/)){
			next;
		}elsif($mon=~/^$ser\.monitor/){
			printf"<option value=\"$mon\" selected>$mon\n";
		}else{
			printf"<option value=\"$mon\">$mon\n";
		}
	}
print <<EOF;
</select></td>
<td><input type=checkbox name=def_$g.$ser value=\"$text{'def_alert'}\" checked>&nbsp;<font color=red>$text{'def_alert'}</font><select name=value_$g.$ser.$text{'def_alert'} size=1 single>
EOF
        foreach $aler(@alerts){
		if(!($aler=~/alert$/)){
                        next;
		}elsif($aler=~/^mail\.alert/){
               		printf"<option value=\"$aler\" selected>$aler\n";
               	}else{	
			printf"<option value=\"$aler\">$aler\n";
		}
        }
local $def_alertargs="root\@".`uname -n`;
print <<EOF;
</select><input type=text name=alertval_$g.$ser size=14 value=\"$def_alertargs\"></td>
</tr></table>

<table><tr>
<td><input type=checkbox name=def_$g.$ser value=\"$text{'def_dep'}\">&nbsp;$text{'def_dep'}</td><td><input type=text name=value_$g.$ser.$text{'def_dep'} size=12></td>
<td><input type=checkbox name=def_$g.$ser value=\"$text{'def_traptime'}\">&nbsp;$text{'def_traptime'}</td><td><input type=text name=value_$g.$ser.$text{'def_traptime'} size=2></td>
<td><input type=checkbox name=def_$g.$ser value=\"$text{'def_inter'}\" checked>&nbsp;<font color=red>$text{'def_inter'}</font></td><td><input type=text name=value_$g.$ser.$text{'def_inter'} size=2 value=\"5m\"></td>
<td><input type=checkbox name=def_$g.$ser value=\"$text{'def_exclper'}\">&nbsp;$text{'def_exclper'}</td><td><input type=text name=value_$g.$ser.$text{'def_exclper'} size=2></td>
<td><input type=checkbox name=def_$g.$ser value=\"$text{'def_alertaft'}\">&nbsp;$text{'def_alertaft'}</td><td><input type=text name=value_$g.$ser.$text{'def_alertaft'} size=2></td>
<td><input type=checkbox name=def_$g.$ser value=\"$text{'def_alertstr'}\">&nbsp;$text{'def_alertstr'}<input type=text name=value_$g.$ser.$text{'def_alertstr'} size=2></td>
</tr>

<tr>
<td><input type=checkbox name=def_$g.$ser value=\"$text{'def_per'}\" checked>&nbsp;<font color=red>$text{'def_per'}</font></td><td><input type=text name=value_$g.$ser.$text{'def_per'} size=12 value=\"$text{'def_perargs'}\"></td>
<td><input type=checkbox name=def_$g.$ser value=\"$text{'def_trapdur'}\">&nbsp;$text{'def_trapdur'}</td><td><input type=text name=value_$g.$ser.$text{'def_trapdur'} size=2></td>
<td><input type=checkbox name=def_$g.$ser value=\"$text{'def_rand'}\">&nbsp;$text{'def_rand'}</td><td><input type=text name=value_$g.$ser.$text{'def_rand'} size=2></td>
<td><input type=checkbox name=def_$g.$ser value=\"$text{'def_exclhost'}\">&nbsp;$text{'def_exclhost'}</td><td><input type=text name=value_$g.$ser.$text{'def_exclhost'} size=2></td>
<td><input type=checkbox name=def_$g.$ser value=\"$text{'def_alertevr'}\" checked>&nbsp;<font color=red>$text{'def_alertevr'}</font></td><td><input type=text name=value_$g.$ser.$text{'def_alertevr'} size=2 value=\"2m\"></td>
<td><input type=checkbox name=def_$g.$ser value=\"$text{'def_allow'}\" checked>&nbsp;$text{'def_allow'}</td>
</tr></table></td>
</tr>
EOF
	}###end services
print"</table><br>\n";
	}###end watchgroups
print <<EOF;
<input type=submit value=\"$text{'save_apply'}\">
<input type=hidden name=hostgroups value=\"@groups\">
<input type=hidden name=watchgroups value=\"@watchgroups\">
EOF
foreach $key(keys %members){
	$aaa=(join ' ',split(/\s+/,$members{$key}));
	print "<input type=hidden name=members_$key value=\"$aaa\">\n";
	print "<input type=hidden name=services_$key value=\"$services{$key}\">\n";
}
print"</center></form><hr>\n";
&footer("./",$text{'index_title'});
