# Library 
# STTA Rev. 1.3 Copyright (c) 2001-2004 Jesús Pérez Lorenzo  --- license GNU GPL
# stta: (@#) 1.31051043346- [2004_01_29_102036]
# monitor-lib  

# Local base Lib ...
 
sub get_acttitles
{
	local ($scr, $item,$nItems);
        local (@l_target, @l_conditions, @l_conditions_tit);
        local (@l_actions, @l_actions_tit);
        local (@l_how, @l_how_tit);
        local (@l_when, @l_when_tit);
        local (@l_target);
        local (%h_list);

	@l_target = split(/,/,$text{'mon_target_value'}, -1);
	foreach $item (@l_target) {
     		if ( $item eq $monTarget ) {
        		$targetext='mon_'.$item.'_sess';
        		$t_monTarget=$text{$targetext};
        			last;
     		}
	}
	@l_conditions = split(/,/,$text{'mon_conditions_value'}, -1);
	@l_conditions_tit = split(/,/,$text{'mon_conditions_desc'}, -1);
        $nItems=0;
	foreach $item (@l_conditions) {
     		if ( $item eq $monCondition ) {
        		$t_monCondition=$l_conditions_tit[$nItems];
        		last;
     		}
                $nItems++;
	}
	@l_actions = split(/,/,$text{'mon_actions_value'}, -1);
	@l_actions_tit = split(/,/,$text{'mon_actions_desc'}, -1);
        $nItems=0;
	foreach $item (@l_actions) {
     		if ( $item eq $monAction ) {
        		$t_monAction=$l_actions_tit[$nItems];
        		last;
     		}
                $nItems++;
	}
        $t_monScript="-";
        %h_list=&load_scriptlist();
     	if ( $h_list{$monScript}->{'name'} eq $monScript ) {
        		$t_monScript="<A class=\"menu1\" href=\"file_mod.cgi?fName=".$h_list{$monScript}->{'name'}."&perm=all&wkdir=$config{monitor_scripts_path}&fPerm=0700\">".$h_list{$monScript}->{'name'}."</A> (".$h_list{$monScript}->{'desc'}.")";
     	}
	@l_how = split(/,/,$text{'mon_how_value'}, -1);
	@l_how_tit = split(/,/,$text{'mon_how_desc'}, -1);
        $nItems=0;
	foreach $item (@l_how) {
     		if ( $item eq $monHow ) {
          		$t_monHow=$l_how_tit[$nItems];
          		last;
     		}
                $nItems++;
	}
        if ( $monHow eq "auto" && $monWhen ne "" ) {
        	@l_when = split(/,/,$text{'mon_when_value'}, -1);
        	@l_when_tit = split(/,/,$text{'mon_when_desc'}, -1);
                $nItems=0;
        	foreach $item (@l_when) {
     			if ( $item eq $monWhen ) {
          			$t_monWhen=$l_when_tit[$nItems];
          			last;
     			}
		}
                $nItems++;
        } elsif ( $monHow eq "task" ) {   
		@l_when = split(/ /, $monWhen, -1);
                $t_monWhen="$text{'edit_mins'}: $l_when[0]".
                           " $text{'edit_hours'}: $l_when[1]".
                           " $text{'edit_days'}: $l_when[2]".
                           " $text{'edit_months'}: $l_when[3]".
                           " $text{'edit_weekdays'}: $l_when[4]";

        } elsif ( $monHow eq "manual" ) {   
         	$t_monWhen=" - ";
        }
        $t_monObject=$monObject;
        if ( $monObject ne "" ) {
             if ( $monTarget eq "user" ) {
                  $dn = "$monObject,".$config{ldap_base};
                  $ldap_server=1;
                  &connect ();
                  $cur_entry=&sel_ldap_obj($dn,"cn=*");
                  if ( !defined($cur_entry) || $cur_entry->dn ne $dn) {
                          $t_monObject=$monObject;
                          &additional_log($dn, undef, $err);
                  } else {
                          %entryObj=&get_ldap_obj($dn,$cur_entry);
                          $dn_ou=$monObject;
                          $dn_ou=~s/cn=$entryObj{cn},//;            
                          $organizationname="";
                          %h_list=&list_ous(2);
                          if ( $h_list{$dn_ou}->{'obj'} eq $dn_ou ) {
                               $organizationname=$h_list{$dn_ou}->{name};
                          }
                          $t_monObject=$entryObj{cn}."  ".$entryiObj{sn}." ".$organizationname;
                  }
                  &webmin_log("monitor:read User OU", "browse", "ldap", "$dn");
             } elsif  ( $monTarget eq "ou" ) {
                  %h_list=&list_ous();
     	          if ( $h_list{$monObject}->{'alias'} eq $monObject ) {
                       $t_monObject=$h_list{$monObject}->{'name'};
     		  }
             } elsif  ( $monTarget eq "host" ) {
                  %h_list=&list_hosts();
     	          if ( $h_list{$monObject}->{'alias'} eq $monObject ) {
                       $t_monObject=$h_list{$monObject}->{'name'};
     		  }
             } elsif  ( $monTarget eq "grphosts" ) {
                  %h_list=&list_grphosts();
     	          if ( $h_list{$monObject}->{'alias'} eq $monObject ) {
                       $t_monObject=$h_list{$monObject}->{'name'};
     		  }
             } elsif  ( $monTarget eq "app" ) {
                  %h_list=&list_apps();
     	          if ( $h_list{$monObject}->{'alias'} eq $monObject ) {
                       $t_monObject=$h_list{$monObject}->{'name'};
     		  }
             } elsif  ( $monTarget eq "grpapps" ) {
                  %h_list=&list_grpapps();
     	          if ( $h_list{$monObject}->{'alias'} eq $monObject ) {
                       $t_monObject=$h_list{$monObject}->{'name'};
     		  }
             }
        }
}

sub check_when 
{
     local ($isnow);
     local (@job, @is_now);
     local (@l_when);
     local (@mins = (0..59));
     local (@hours = (0..23));
     local (@days = (1..31));
     local (@months = map { $text{"month_$_"}."=".$_ } (1 .. 12));
     local (@weekdays = map { $text{"day_$_"}."=".$_ } (0 .. 6));
     local ($min,$max,$w,$v,$j,$jj,$disp,$code,$t_val);
     
     @l_when=split(/ /, $monWhen, -1);
          $job = { 'mins' =>     $l_when[0],
                   'hours' =>    $l_when[1],
                   'days' =>     $l_when[2],
                   'months' =>   $l_when[3],
                   'weekdays' => $l_when[4] };

          $is_now = { 'mins' =>  false,
                   'hours' =>    false,
                   'days' =>     false,
                   'months' =>   false,
                   'weekdays' => false };
     if ( DebugLog && $debug gt 1 ) {
          print DebugLog "now: ".&set_now($now)."\n";
          print DebugLog "--------mins----hours----days----months---------weekdays\n";
          print DebugLog "now :\t$t_mins\t $t_hours\t $t_days\t $t_months\t\t $t_weekdays\n";
          print DebugLog "job :\t$job->{mins}\t $job->{hours}\t $job->{days}\t $job->{months}\t\t $job->{weekdays}\n";
     }
     $isnow=false;
     foreach $arr ("mins", "hours", "days", "months", "weekdays") {
	     # Find out which ones are being used
	     local %inuse;
	     $min = ($arr =~ /days|months/ ? 1 : 0);
	     $max = $min+scalar(@$arr)-1;
	     foreach $w (split(/,/ , $job->{$arr})) {
		     if ($w eq "*") {
                             $is_now->{$arr}=true;
                             last;
			     for($j=$min; $j<=$max; $j++) { $inuse{$j}++; }
		     }
		     elsif ($w =~ /^\*\/(\d+)$/) {
			     # only every Nth
			     for($j=$min; $j<=$max; $j+=$1) { $inuse{$j}++; }
		     }
		     elsif ($w =~ /^(\d+)-(\d+)\/(\d+)$/) {
			     # only every Nth of some range
			     for($j=$1; $j<=$2; $j+=$3) { $inuse{int($j)}++; }
		     }
		     elsif ($w =~ /^(\d+)-(\d+)$/) {
			     # all of some range
			     for($j=$1; $j<=$2; $j++) { $inuse{int($j)}++; }
		     }
		     else {
			     # One value
			     $inuse{int($w)}++;
		     }
	     }
	     if ($job->{$arr} eq "*") { undef(%inuse); }
             for($j=0; $j<@$arr; $j+=12) {
                     $jj = $j+11;
                     if ($jj >= @$arr) { $jj = @$arr - 1; }
                     @sec = @$arr[$j .. $jj];
                     foreach $v (@sec) {
                             if ($v =~ /^(.*)=(.*)$/) { $disp = $1; $code = $2; }
                             else { $disp = $code = $v; }
                             if  ( $inuse{$code} eq 1 ) {
                                eval ("\$t_val=\$t_$arr;");
                                if ( $t_val == $code ) { 
                                     $is_now->{$arr}=true;
                                     last;
                                }
                             }
                     }
             }
             $isnow=$is_now->{$arr};
             if ( $isnow ne true ) { 
                  if ( DebugLog  && $debug gt 1 ) {
                        print DebugLog "$arr: $t_val  not in \"$job->{$arr}\" \n"; 
                  }
                  last
             };
      }
      return $isnow;
}


# -----------------------------------------------------------------
sub print_results
{
   print DebugLog "----------- ENVIRONMENT VALUES ----------------\n";
   print DebugLog "----------- SCANED PARAMS --------------------\n";
}
sub endclose_debug
{
   print DebugLog "----------------------------------------------\n";
   close (DebugLog);
}

sub check_condition {
  if ( $monCondition eq "lt" ) {
     if ( $o_numsess lt $monConditionValue ) { return true };
  } elsif ( $monCondition eq "gt" ) {
     if ( $o_numsess gt $monConditionValue ) { return true };
  } elsif ( $monCondition eq "eq" ) {
     if ( $o_numsess eq $monConditionValue ) { return true };
  } elsif ( $monCondition eq "ne" ) {
     if ( $o_numsess ne $monConditionValue ) { return true };
  } elsif ( $monCondition eq "noactive" ) {
     # checking alive host or session
     local $hosts_status=&check_host($monAlias);
     local $respo=$text{responding_on};
     if  ( "$host_status" =~ /$respo/ ) { return true };
  } elsif ( $monCondition eq "disconnected" ) {
     # checking if is not running
  }
  return false;
}

sub check_target {
  if ( "$monTarget" eq "date" ) {  
         $o_numsess=$f_date;
         return true;
  } elsif ( "$monTarget" eq "time" ) {  
         $o_numsess=$f_time;
         return true;
  } elsif ( $monCondition eq "noactive" ) {
         return true;
  } else {
         if ( $all_ttasess < 0) {  return false; }
         if ( $h_countsess{$monTarget}->{alias} ne $monTarget ) {  return false; }
         $o_numsess=$h_countsess{$monTarget}->{numsess};
         $o_maxsess=$h_countsess{$monTarget}->{maxsess};
         return true;
  }
}
1;  # Return true
