Code

Removed usePrototype flag, its activated always now.
[gosa.git] / gosa-si / server / events / siTriggered.pm
index d4ec0c668911a0871c2b426a466a6d8c4426af3d..f190e15fee6f580d6f3f12b3cf0d184a36562bcb 100644 (file)
@@ -1,19 +1,24 @@
 package siTriggered;
+
+use strict;
+use warnings;
+
 use Exporter;
-@ISA = qw(Exporter);
+use Data::Dumper;
+use GOSA::GosaSupportDaemon;
+use Socket;
+
+our @ISA = qw(Exporter);
+
 my @events = (
     "got_ping",
     "detected_hardware",
     "trigger_wake",
     "reload_ldap_config",
+    "get_terminal_server",
     );
-@EXPORT = @events;
-
-use strict;
-use warnings;
-use GOSA::GosaSupportDaemon;
-use Socket;
-
+    
+our @EXPORT = @events;
 
 BEGIN {}
 
@@ -38,32 +43,90 @@ my %cfg_defaults = (
 &GOSA::GosaSupportDaemon::read_configfile($main::cfg_file, %cfg_defaults);
 
 
-sub get_events {
-    return \@events;
-}
+sub get_terminal_server
+{
+       my ($msg, $msg_hash, $session_id) = @_ ;
+       my $source = @{$msg_hash->{source}}[0];
+       my @out_msg_l;
+
+       # Send get_load message to all si-clients at terminal server specified in LDAP
+       my $ldap_handle = &main::get_ldap_handle();
+       if (defined $ldap_handle) 
+       {
+               my $ldap_mesg = $ldap_handle->search(
+                               base => $ldap_base,
+                               scope => 'sub',
+                               attrs => ['macAddress', 'cn', 'ipHostNumber'],
+                               filter => "objectClass=goTerminalServer",
+                               );
+               if ($ldap_mesg->count) 
+               {       
+                       # Parse all LDAP results to a sql compliant where statement
+                       my @entries = $ldap_mesg->entries;
+                       @entries = map ($_->get_value("macAddress"), @entries);
+                       @entries = map ("macaddress LIKE '$_'", @entries);
+
+                       my ($hit, $hash, $db_res, $out_msg);
+                       # Check known clients if a terminal server is active
+                       $db_res = $main::known_clients_db->select_dbentry("SELECT * FROM $main::known_clients_tn WHERE ".join(" AND ", @entries));
+                       while (($hit, $hash) = each %$db_res) 
+                       {
+                               $out_msg = &create_xml_string(&create_xml_hash('get_load', $source, $hash->{macaddress}));
+                               push(@out_msg_l, $out_msg);
+                       }
+                       # Check foreign_clients if a terminal server is active
+                       $db_res = $main::foreign_clients_db->select_dbentry("SELECT * FROM $main::foreign_clients_tn WHERE ".join(" AND ", @entries));
+                       while (($hit, $hash) = each %$db_res) 
+                       {
+                               $out_msg = &create_xml_string(&create_xml_hash('get_load', $source, $hash->{macaddress}));
+                               push(@out_msg_l, $out_msg);
+                       }
 
+### JUST FOR DEBUGGING # CAN BE DELETED AT ANY TIME ###########################
+#                      my $db_res = $main::known_clients_db->select_dbentry("SELECT * FROM $main::known_clients_tn WHERE macaddress LIKE '00:01:6c:9d:b9:fa'");
+#                      while (($hit, $hash) = each %$db_res) 
+#                      {
+#                              $out_msg = &create_xml_string(&create_xml_hash('get_load', $source, $hash->{macaddress}));
+#                              push(@out_msg_l, $out_msg);
+#                      }
+### JUST FOR DEBUGGING # CAN BE DELETED AT ANY TIME ###########################
+
+                       # Found terminal server but no running clients on them
+                       if (@out_msg_l == 0) 
+                       {
+                               &main::daemon_log("$session_id ERROR: Found no running clients (known_clients_db, foreign_clients_db) on the following determined terminal server", 1);
+                               my @entries = $ldap_mesg->entries;
+                               foreach my $ts (@entries) 
+                               {
+                                       my $ip = (defined $ts->get_value("ipHostNumber")) ? "   ip='".$ts->get_value("ipHostNumber")."'" : "" ;
+                                       my $cn = (defined $ts->get_value("cn")) ? "   cn='".$ts->get_value("cn")."'" : "" ;
+                                       my $mac = (defined $ts->get_value("macAddress")) ? "   macAddress='".$ts->get_value("macAddress")."'" : "" ;
+                                       &main::daemon_log("$session_id ERROR: ".$cn.$mac.$ip , 1);
+                               }
+                       }
 
-#sub read_configfile {
-#    my ($cfg_file, %cfg_defaults) = @_;
-#    my $cfg;
-#
-#    if( defined( $cfg_file) && ( (-s $cfg_file) > 0 )) {
-#        if( -r $cfg_file ) {
-#            $cfg = Config::IniFiles->new( -file => $cfg_file );
-#        } else {
-#            &main::daemon_log("ERROR: siTriggered.pm couldn't read config file!", 1);
-#        }
-#    } else {
-#        $cfg = Config::IniFiles->new() ;
-#    }
-#    foreach my $section (keys %cfg_defaults) {
-#        foreach my $param (keys %{$cfg_defaults{ $section }}) {
-#            my $pinfo = $cfg_defaults{ $section }{ $param };
-#            ${@$pinfo[0]} = $cfg->val( $section, $param, @$pinfo[1] );
-#        }
-#    }
-#}
+               }
+               # No terminal server found in LDAP
+               if ($ldap_mesg->count == 0) 
+               {
+                       &main::daemon_log("$session_id ERROR: No terminal server found in LDAP: \n\tbase='$ldap_base'\n\tscope='sub'\n\tattrs='['macAddress']'\n\tfilter='objectClass=goTerminalServer'", 1);
+               }
 
+               # Translating errors ?
+               if ($ldap_mesg->code) 
+               {
+                       &main::daemon_log("$session_id ERROR: Cannot fetch terminal server from LDAP: \n\tbase='$ldap_base'\n\tscope='sub'\n\tattrs='['macAddress']'\n\tfilter='objectClass=goTerminalServer'", 1);
+               }
+       }
+       &main::release_ldap_handle($ldap_handle);
+
+    return @out_msg_l;
+}
+
+
+sub get_events {
+    return \@events;
+}
 
 sub reload_ldap_config {
     my ($msg, $msg_hash, $session_id) = @_;
@@ -123,7 +186,7 @@ sub got_ping {
 
 
 sub detected_hardware {
-       my ($msg, $msg_hash, $session_id, $ldap_handle) = @_;
+       my ($msg, $msg_hash, $session_id) = @_;
        my $address = $msg_hash->{source}[0];
        my $header = $msg_hash->{header}[0];
        my $gotoHardwareChecksum= $msg_hash->{detected_hardware}[0]->{gotoHardwareChecksum};
@@ -147,6 +210,7 @@ sub detected_hardware {
        }
 
        # Perform search
+       my $ldap_handle = &main::get_ldap_handle();
        $mesg = $ldap_handle->search(
                base   => $ldap_base,
                scope  => 'sub',
@@ -163,9 +227,9 @@ sub detected_hardware {
                #       $dnsname= $heap->{force-hostname}->{$macaddress};
                #       &main::daemon_log("INFO: Using forced hostname $dnsname for client $address", 4);
                if (-e "/var/tmp/$macaddress" ){
-                       open(TFILE, "< /var/tmp/$macaddress");
-                       $dnsname= <TFILE>;
-                       close(TFILE);
+                       open(my $TFILE, "<", "/var/tmp/$macaddress");
+                       $dnsname= <$TFILE>;
+                       close($TFILE);
                } else {
                        $dnsname= gethostbyaddr(inet_aton($ipaddress), AF_INET) || $ipaddress;
                }
@@ -189,6 +253,7 @@ sub detected_hardware {
                if(defined($res->{'errorMessage'}) &&
                        length($res->{'errorMessage'}) >0) {
                        &main::daemon_log("ERROR: can not add entries to LDAP: ".$res->{'errorMessage'}, 1);
+                       &main::release_ldap_handle($ldap_handle);
                        return;
                } else {
                        # Fill $mesg again
@@ -239,8 +304,9 @@ sub detected_hardware {
        }
 
        # if there is a job in job queue for this host and this macaddress, delete it, cause its no longer used
-       my $del_sql = "DELETE FROM $main::job_queue_tn WHERE (macaddress='$macaddress' AND headertag='$header')";
+       my $del_sql = "DELETE FROM $main::job_queue_tn WHERE (macaddress LIKE '$macaddress' AND headertag='$header')";
        my $del_res = $main::job_db->exec_statement($del_sql);
+  &main::release_ldap_handle($ldap_handle);
 
        return ;
 }