#!/usr/local/bin/perl
# hostgroups.cgi
#
###
require './mon-lib.pl';
&header($text{'hostgroups_title'}, "","hostgroup");
print"<hr>";
#
## Read mon.cf file to array
if(-f "$config{'cfbasedir'}/mon.cf"){
local @conf_array=&write_file_to_array("$config{'cfbasedir'}/mon.cf");
@conf_groups=();
%conf_memb=();
%conf_service=();
$count=0;
	foreach $count(0..$#conf_array){
		if(!($conf_array[$count]=~/^#/)&&($conf_array[$count]=~/^hostgroup/i)){
		#print"READ:$conf_array[$count]<br>";
			local @tmp=split(/\s+/,$conf_array[$count]);
			push(@conf_groups,$tmp[1]);
			$conf_memb{$tmp[1]}=(join "\n",@tmp[2..$#tmp]);
		#print"GRP:$tmp[1]<br>";
		#print"MEMB=$conf_memb{$tmp[1]}<br>";
		}elsif(!($conf_array[$count]=~/^#/i) && ($conf_array[$count]=~/^watch|^\s+watch/i)){
			$aa=`echo $conf_array[$count]|cut -d " " -f2`;chomp($aa);
			$conf_service{$aa}="";
		#print"WATCH:$aa<br>";
		}elsif(!($conf_array[$count]=~/^#/i) && ($conf_array[$count]=~/^service|^\s+service/i)){
			local @servline=split(/\s+/,$conf_array[$count]);
			$conf_service{$aa}=(join " ", $conf_service{$aa}, $servline[$#servline]);
		} 
	}
}
#
### form
print <<EOF;
<form action=services.cgi><center>
<table border><tr $tb><th height=35 colspan=4>$text{'head_hostgroups'}</th></tr>
<tr bgcolor=wheat><th>$text{'head_groups'}</th><th>$text{'head_members'}<br>$text{'line'}</th><th>$text{'head_watch'}</th><th>$text{'head_services'}</th></tr>
EOF
@services=('http','ftp','telnet','ping','freespace','nntp','dns','reboot','pop','smtp');
for ($i=0; $i<=($#conf_groups+4); $i++){
	print"<tr bgcolor=#cccccc><td><input type=text name=groups_$i size=15 value=\"$conf_groups[$i]\"></td><td><textarea name=members_$i cols=10 rows=3>$conf_memb{$conf_groups[$i]}</textarea></td>\n";
	print"<td align=center><input type=radio name=watchgroup_$i value=Yes checked>&nbsp;Yes<br><input type=radio name=watchgroup_$i value=No >&nbsp;No</td>\n";
	print"<td><table><tr>\n";
	for ($j=0;$j<10;$j++){
		if($conf_service{$conf_groups[$i]}=~/$services[$j]/){
			print"<td><input type=checkbox name=services_$i value=\"$services[$j]\" checked>&nbsp;$services[$j]</td>\n";
		}else{
			print"<td><input type=checkbox name=services_$i value=\"$services[$j]\">&nbsp;$services[$j]</td>\n";
		}
		print"</tr><tr>\n" if($j==4);
	}
	print"</tr></table>$text{'other'}<input type=text name=other_services_$i size=15></td></tr>";
}
print <<EOF;
</table>
<input type=hidden name=count value=$i>
<br><input type=submit value=\"$text{'next'}\"><br>
</center></form>
EOF
print"<hr>";
&footer("",$text{'index_title'});
