#!/usr/bin/perl
# STTA Rev. 1.3 Copyright (c) 2001-2004 Jesús Pérez Lorenzo  --- license GNU GPL
# stta: (@#) 1.31051043346- [2004_01_29_102036]


$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 = "en";
$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();
&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};
$acts_list=$config{acts_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();
    $nlins=0;
    foreach $item (keys (%h_actions)) { 
            next if ( $h_actions{$item}->{how} ne $task_type ); # only $task_type
            $monWhen=$h_actions{$item}->{when};
            if ( $monWhen =~ /$task_arg1/ ) {
                 $nlins++;      
            } else {
                 next if ( &check_when() eq false );   # only if when is set is OK
                 $nlins++;      
            }
            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

$type_cmd=$tool_name;
foreach $action (@all_actions) {
     $monAlias=$action->{alias};
     $monName=$action->{name};
     $monObject=$action->{object};
     $monTarget=$action->{target};
     $monCondition=$action->{condition};
     $monConditionValue=$action->{conditionValue};
     $monAction=$action->{action};
     $monScript=$action->{script};
     $monHow=$action->{how};
     $monWhen=$action->{when};
     if ( DebugLog && $debug gt 0 ) {    
          print DebugLog "$tool_name (scanning): $monName ($monAlias) \n";
          print DebugLog "$tool_name ($monName): -- $monTarget -- $monCondition --$monConditionValue => $monAction: $monScript\n";
     }
     $o_numsess=""; 
     $o_maxsess=""; 
     next if ( &check_target() ne true ) ;
     if ( DebugLog && $debug gt 0 ) {    
          print DebugLog "$tool_name ($monAlias)  -Sess:-- $o_numsess --- MaxSess:  $o_maxsess\n";
          print DebugLog "$tool_name ($monAlias)  -$f_date -- $f_time\n";
     }
     next if ( &check_condition() ne true );
     print DebugLog "$tool_name (TO RUN): $monName ($monAlias) \n";
     $str="$config{'monitor_scripts_path'}/$monScript";
     &run_cmd($str);
}
if ( $debug gt 0 ) { &endclose_debug(); }
exit;
