#!/usr/bin/perl

$debug=1; 
$task_type = $ARGV[0];
$task_arg1 = $ARGV[1];

if ( $task_type ne "auto" && $task_type ne "task" ) { 
     print "Monitor: \"$task_type\" unknown option task \n";
     print "monitor [auto|task]\n";
     exit 1;
};

# where is config file for parameters .... (same as Webmin)
$config_directory="/etc/webmin";

do './base-lib.pl';
do './global-lib.pl';
do './lists-lib.pl';
do './ttasess-lib.pl';
do './print-lib.pl';
require './monitor-lib.pl';


$tool_name="monitor";
$tta_logfile=$ENV{'TTA_LOGFILE'};
$tta_logfile="/tmp/.webmin/stta_log";
$default_lang = "en";
$current_lang = "es";
$module_name = "stta";
$remote_user="admin";
$session_id="$$";
$hostname=`hostname`;
$hostname=~s/\n//g;
$ENV{'REMOTE_HOST'}=$hostname;
$action_id_count=0;
%ttaprops=();
@ttahosts=();
%sttaparams=();
$cginame= $0 =~ /([^\/]+)$/ ? $1 : '-';
$ldap_ok=true;
$ldapcache_ok=true;
$tta_ok=true;
 
&initialize();
&load_dataconf();
#$DebugFile="/var/webmin/webmin.log";
&start_debug();
%access = &get_module_acl();
%text = &load_language(); 
 
# globals to keep STTA info
  %ttaprops=();
  @ttahosts=();
  %sttaparams=();
  $ldap_ok=true;
  $ldapcache_ok=true;
  $tta_ok=true;
 
  &load_ttaprops();
  $mhost=$sttaparams{'tta'}{'masthost'};
  &scanSTTA_services("all"); 
  if ( $debug gt 1 ) { 
       &checkSTTA_services("all","warn");
       &out_params();
  }

$has_monitor=$config{has_monitor};

 
if ($has_monitor ne true ) { exit } ;

$mon_scripts=$config{monitor_scripts_path};
$act_list=$config{act_list};
$mon_scripts_title=$config{mon_title};
$run_exec_user=$config{run_exec_user};


#  Load actions list  ....
#  Is there anything to look at ????

    $now = time();
    @tm = localtime($now);
    $t_sec=$tm[0];
    $t_mins=$tm[1];
    $t_hours=$tm[2];
    $t_days=$tm[3];
    $t_months=($tm[4]+1);
    $t_yea=($tm[5]+1900);
    $t_weekdays=($tm[6]);
    $t_yead=$tm[7];
    &webmin_log($tool_name, "scan_actions",&set_now($now));
   
    # As $task_type is already defined ... only task_type will be included in hash ...
    %h_actions=&list_actions();
    foreach $item (keys (%h_actions)) { 
            next if ( $h_actions{$item}->{how} ne $task_type ); # only $task_type
            $monWhen=$h_actions{$item}->{when};
            next if ( &check_when() ne "true" );   # only if when is OK
            push (@all_actions, $h_actions{$item});
    }
    if ( $#all_actions < 0) { 
         $err_msg=$text{noacts_err};
         &webmin_log($tool_name, "load_acts",$err_msg);
         if ( $debug gt 0 ) { &endclose_debug(); }
         exit;
    }

    $ahora=&set_now();
    @l_ahora = split(/ /,$ahora, -1);       
    @l_date = split(/-/,$l_ahora[0], -1);       
    $f_date=$l_date[2].$l_date[1].$l_date[0];
    $f_time=$l_ahora[1];
    $f_time=~s/://g;   
    $rv = { 'fh' => time().$$ }; 

    $sort_on="when";
    @actions = sort {$a->{$sort_on} cmp $b->{$sort_on}} @all_actions; 


#  Loading TTA Sessions ...

$is_reload=false;
 
if ( $config{has_cronsess} ne "true" || $config{'countsessout_file'} eq "" ) {$is_reload=true;}


  $status_ret=ttasess_tables();
  if ( $status_ret eq -2 ) {
          # if ( $is_reload ne true && "$counter_time" ne "$ttasess_time" ) 
          $status_ret=ttasess_tables();
          if ( $status_ret ne 0 ) {
               $err_msg=$text{sessdiff_err};
               &webmin_log($tool_name, "load_ttasess",$err_msg);
          }
  }
  if ( $status_ret eq -1 || $#all_ttasess < 0) { 
         $err_msg=$text{nosess_err};
         &webmin_log($tool_name, "load_ttasess",$err_msg);
  } else {
         %h_countsess=&list_countsess(4);  # To get countsess by alias ... pos 4
  }

  %h_hosts=&list_hosts(5); # Loading hosts by Ip-Name

 foreach $action (@actions) {
        if ( DebugLog && $debug gt 0 ) {    
             print DebugLog "$tool_name (scanning): $monName ($monAlias) \n";
        }
        $monAlias=$action->{alias};
        $monName=$action->{name};
        $monObject=$action->{object};
        $monTarget=$action->{target};
        $monObject=$action->{object};
        $monCondition=$action->{condition};
        $monConditionValue=$action->{conditionValue};
        $monAction=$action->{action};
        $monScript=$action->{script};
        $monHow=$action->{how};
        $monWhen=$action->{when};

     $o_numsess=""; 
     $o_maxsess=""; 
     next if ( &check_target() ne "true" );
     next if ( &check_condition() ne "true" );
     print DebugLog "$tool_name: $monName ($monAlias) \n";
}

#  Processing actions ...
   # mon_actions_value=run-script,logout,cancel-old-resume 
#    @l_actions = split(/,/,$text{'mon_actions_value'}, -1);       
   # mon_target_value=user,ou,host,grphosts,app,grpapps,date,time 
#    @l_targets = split(/,/,$text{'mon_target_value'}, -1);       
   # mon_conditions_value=gt,lt,eq,ne,noactive,disconnected 
#    @l_conditions = split(/,/,$text{'mon_conditions_value'}, -1);       
   # mon_how_value=manual,auto,task 
#    @l_how = split(/,/,$text{'mon_how_value'}, -1);       
   # mon_when_value=start-tta,start-sys,stop-tta,stop-sys 
#    @l_when = split(/,/,$text{'mon_when_value'}, -1);       
#    foreach $item (@l_target) {
#                if ( $item eq $monTarget ) {
#                        $targetext='mon_'.$item.'_sess';
#                        $t_monTarget=$text{$targetext};
#                                last;
#                }
#    }
if ( $debug gt 0 ) { &endclose_debug(); }
exit;
