Code

* new plugin load_reporter.pm
[gosa.git] / gosa-si / server / events / siTriggered.pm
1 package siTriggered;
2 use Exporter;
3 @ISA = qw(Exporter);
4 my @events = (
5     "got_ping",
6     "detected_hardware",
7     "trigger_wake",
8     "reload_ldap_config",
9         "get_terminal_server",
10     );
11 @EXPORT = @events;
13 use strict;
14 use warnings;
15 use Data::Dumper;
16 use GOSA::GosaSupportDaemon;
17 use Socket;
21 BEGIN {}
23 END {}
25 ### Start ######################################################################
27 my $ldap_uri;
28 my $ldap_base;
29 my $ldap_admin_dn;
30 my $ldap_admin_password;
31 my $mesg;
33 my %cfg_defaults = (
34     "server" => {
35         "ldap-uri" => [\$ldap_uri, ""],
36         "ldap-base" => [\$ldap_base, ""],
37         "ldap-admin-dn" => [\$ldap_admin_dn, ""],
38         "ldap-admin-password" => [\$ldap_admin_password, ""],
39     },
40 );
41 &GOSA::GosaSupportDaemon::read_configfile($main::cfg_file, %cfg_defaults);
44 sub get_terminal_server
45 {
46         my ($msg, $msg_hash, $session_id) = @_ ;
47         my $source = @{$msg_hash->{source}}[0];
48         my @out_msg_l;
50         # Send get_load message to all si-clients at terminal server specified in LDAP
51         my $ldap_handle = &main::get_ldap_handle();
52         if (defined $ldap_handle) 
53         {
54                 my $ldap_mesg = $ldap_handle->search(
55                                 base => $ldap_base,
56                                 scope => 'sub',
57                                 attrs => ['macAddress'],
58                                 filter => "objectClass=goTerminalServer",
59                                 );
60                 if ($ldap_mesg->count) 
61                 {       
62                         # Parse all LDAP results to a sql compliant where statement
63                         my @entries = $ldap_mesg->entries;
64                         @entries = map ($_->get_value("macAddress"), @entries);
65                         @entries = map ("macaddress LIKE '$_'", @entries);
67                         my ($hit, $hash, $db_res, $out_msg);
68                         # Check known clients if a terminal server is active
69                         $db_res = $main::known_clients_db->select_dbentry("SELECT * FROM $main::known_clients_tn WHERE ".join(" AND ", @entries));
70                         while (($hit, $hash) = each %$db_res) 
71                         {
72                                 $out_msg = &create_xml_string(&create_xml_hash('get_load', $source, $hash->{macaddress}));
73                                 push(@out_msg_l, $out_msg);
74                         }
75                         # Check foreign_clients if a terminal server is active
76                         $db_res = $main::foreign_clients_db->select_dbentry("SELECT * FROM $main::foreign_clients_tn WHERE ".join(" AND ", @entries));
77                         while (($hit, $hash) = each %$db_res) 
78                         {
79                                 $out_msg = &create_xml_string(&create_xml_hash('get_load', $source, $hash->{macaddress}));
80                                 push(@out_msg_l, $out_msg);
81                         }
83 ### JUST FOR DEBUGGING # CAN BE DELETED AT ANY TIME ###########################
84                         my $db_res = $main::foreign_clients_db->select_dbentry("SELECT * FROM $main::foreign_clients_tn WHERE macaddress LIKE '00:01:6c:9d:b9:fa'");
85                         while (($hit, $hash) = each %$db_res) 
86                         {
87                                 $out_msg = &create_xml_string(&create_xml_hash('get_load', $source, $hash->{macaddress}));
88                                 push(@out_msg_l, $out_msg);
89                         }
90 ### JUST FOR DEBUGGING # CAN BE DELETED AT ANY TIME ###########################
91                 }
92                 # Translating errors ?
93                 if ($ldap_mesg->code) 
94                 {
95                         &main::daemon_log("0 ERROR: Cannot fetch terminal server from LDAP: \n\tbase='$ldap_base'\n\tscope='sub'\n\tattrs='['macAddress']'\n\tfilter='objectClass=goTerminalServer'", 1);
96                 }
97         }
98         &main::release_ldap_handle($ldap_handle);
100     return @out_msg_l;
104 sub get_events {
105     return \@events;
108 sub reload_ldap_config {
109     my ($msg, $msg_hash, $session_id) = @_;
110     my $header = @{$msg_hash->{header}}[0];
111     my $target = @{$msg_hash->{$header}}[0];
113     my $out_msg = &ClientPackages::new_ldap_config($target, $session_id);
114     my @out_msg_l = ( $out_msg );
115     return @out_msg_l;
119 sub got_ping {
120     my ($msg, $msg_hash, $session_id) = @_;
122     my $source = @{$msg_hash->{source}}[0];
123     my $target = @{$msg_hash->{target}}[0];
124     my $header = @{$msg_hash->{header}}[0];
125     my $act_time = &get_time;
126     my @out_msg_l;
127     my $out_msg;
129     $session_id = @{$msg_hash->{'session_id'}}[0];
131     # check known_clients_db
132     my $sql_statement = "SELECT * FROM known_clients WHERE hostname='$source'";
133     my $query_res = $main::known_clients_db->select_dbentry( $sql_statement );
134     if( 1 == keys %{$query_res} ) {
135          my $sql_statement= "UPDATE known_clients ".
136             "SET status='$header', timestamp='$act_time' ".
137             "WHERE hostname='$source'";
138          my $res = $main::known_clients_db->update_dbentry( $sql_statement );
139     } 
140     
141     # check known_server_db
142     $sql_statement = "SELECT * FROM known_server WHERE hostname='$source'";
143     $query_res = $main::known_server_db->select_dbentry( $sql_statement );
144     if( 1 == keys %{$query_res} ) {
145          my $sql_statement= "UPDATE known_server ".
146             "SET status='$header', timestamp='$act_time' ".
147             "WHERE hostname='$source'";
148          my $res = $main::known_server_db->update_dbentry( $sql_statement );
149     } 
151     # create out_msg
152     my $out_hash = &create_xml_hash($header, $source, "GOSA");
153     &add_content2xml_hash($out_hash, "session_id", $session_id);
154     $out_msg = &create_xml_string($out_hash);
155     my $forward_to_gosa = @{$msg_hash->{'forward_to_gosa'}}[0];
156     if (defined $forward_to_gosa) {
157         $out_msg =~s/<\/xml>/<forward_to_gosa>$forward_to_gosa<\/forward_to_gosa><\/xml>/;
158     }
159     push(@out_msg_l, $out_msg);
160     
161     return @out_msg_l;
165 sub detected_hardware {
166         my ($msg, $msg_hash, $session_id) = @_;
167         my $address = $msg_hash->{source}[0];
168         my $header = $msg_hash->{header}[0];
169         my $gotoHardwareChecksum= $msg_hash->{detected_hardware}[0]->{gotoHardwareChecksum};
171         my $sql_statement= "SELECT * FROM known_clients WHERE hostname='$address'";
172         my $res = $main::known_clients_db->select_dbentry( $sql_statement );
174         # check hit
175         my $hit_counter = keys %{$res};
176         if( not $hit_counter == 1 ) {
177                 &main::daemon_log("$session_id ERROR: more or no hit found in known_clients_db by query by '$address'", 1);
178                 return;
179         }
181         my $macaddress = $res->{1}->{macaddress};
182         my $hostkey = $res->{1}->{hostkey};
184         if (not defined $macaddress) {
185                 &main::daemon_log("$session_id ERROR: no mac address found for client $address", 1);
186                 return;
187         }
189         # Perform search
190         my $ldap_handle = &main::get_ldap_handle();
191         $mesg = $ldap_handle->search(
192                 base   => $ldap_base,
193                 scope  => 'sub',
194                 filter => "(&(objectClass=GOhard)(|(macAddress=$macaddress)(dhcpHWaddress=ethernet $macaddress)))"
195         );
197         # We need to create a base entry first (if not done from ArpHandler)
198         if($mesg->count == 0) {
199                 &main::daemon_log("INFO: Need to create a new LDAP Entry for client $address", 4);
200                 my $ipaddress= $1 if $address =~ /^([0-9\.]*?):.*$/;
201                 my $dnsname;
202                 #FIXME: like in ClientPackages!
203                 #if ( defined($heap->{force-hostname}->{$macaddress}) ){
204                 #       $dnsname= $heap->{force-hostname}->{$macaddress};
205                 #       &main::daemon_log("INFO: Using forced hostname $dnsname for client $address", 4);
206                 if (-e "/var/tmp/$macaddress" ){
207                         open(TFILE, "< /var/tmp/$macaddress");
208                         $dnsname= <TFILE>;
209                         close(TFILE);
210                 } else {
211                         $dnsname= gethostbyaddr(inet_aton($ipaddress), AF_INET) || $ipaddress;
212                 }
214                 my $cn = (($dnsname =~ /^(\d){1,3}\.(\d){1,3}\.(\d){1,3}\.(\d){1,3}/) ? $dnsname : sprintf "%s", $dnsname =~ /([^\.]+)\.?/);
215                 my $dn = "cn=$cn,ou=incoming,$ldap_base";
216                 &main::daemon_log("INFO: Creating entry for $dn",5);
217                 my $entry= Net::LDAP::Entry->new( $dn );
218                 $entry->dn($dn);
219                 $entry->add("objectClass" => "GOhard");
220                 $entry->add("cn" => $cn);
221                 $entry->add("macAddress" => $macaddress);
222                 $entry->add("gotomode" => "locked");
223                 $entry->add("gotoSysStatus" => "new-system");
224                 $entry->add("ipHostNumber" => $ipaddress);
225                 if(defined($main::gosa_unit_tag) && length($main::gosa_unit_tag) > 0) {
226                         $entry->add("objectClass" => "gosaAdministrativeUnitTag");
227                         $entry->add("gosaUnitTag" => $main::gosa_unit_tag);
228                 }
229                 my $res=$entry->update($ldap_handle);
230                 if(defined($res->{'errorMessage'}) &&
231                         length($res->{'errorMessage'}) >0) {
232                         &main::daemon_log("ERROR: can not add entries to LDAP: ".$res->{'errorMessage'}, 1);
233                         &main::release_ldap_handle($ldap_handle);
234                         return;
235                 } else {
236                         # Fill $mesg again
237                         $mesg = $ldap_handle->search(
238                                 base   => $ldap_base,
239                                 scope  => 'sub',
240                                 filter => "(&(objectClass=GOhard)(|(macAddress=$macaddress)(dhcpHWaddress=ethernet $macaddress)))"
241                         );
242                 }
243         }
245         if($mesg->count == 1) {
246                 my $entry= $mesg->entry(0);
247                 $entry->changetype("modify");
248                 foreach my $attribute (
249                         "gotoSndModule", "ghNetNic", "gotoXResolution", "ghSoundAdapter", "ghCpuType", "gotoXkbModel", 
250                         "ghGfxAdapter", "gotoXMousePort", "ghMemSize", "gotoXMouseType", "ghUsbSupport", "gotoXHsync", 
251                         "gotoXDriver", "gotoXVsync", "gotoXMonitor", "gotoHardwareChecksum") {
252                         if(defined($msg_hash->{detected_hardware}[0]->{$attribute}) &&
253                                 length($msg_hash->{detected_hardware}[0]->{$attribute}) >0 ) {
254                                 if(defined($entry->get_value($attribute))) {
255                                         $entry->delete($attribute => []);
256                                 }
257                                 &main::daemon_log("INFO: Adding attribute $attribute with value ".$msg_hash->{detected_hardware}[0]->{$attribute},5);
258                                 $entry->add($attribute => $msg_hash->{detected_hardware}[0]->{$attribute});     
259                         }
260                 }
261                 foreach my $attribute (
262                         "gotoModules", "ghScsiDev", "ghIdeDev") {
263                         if(defined($msg_hash->{detected_hardware}[0]->{$attribute}) &&
264                                 length($msg_hash->{detected_hardware}[0]->{$attribute}) >0 ) {
265                                 if(defined($entry->get_value($attribute))) {
266                                         $entry->delete($attribute => []);
267                                 }
268                                 foreach my $array_entry (keys %{{map { $_ => 1 } sort(@{$msg_hash->{detected_hardware}[0]->{$attribute}}) }}) {
269                                         $entry->add($attribute => $array_entry);
270                                 }
271                         }
272                 }
274                 my $res=$entry->update($ldap_handle);
275                 if(defined($res->{'errorMessage'}) &&
276                         length($res->{'errorMessage'}) >0) {
277                         &main::daemon_log("ERROR: can not add entries to LDAP: ".$res->{'errorMessage'}, 1);
278                 } else {
279                         &main::daemon_log("INFO: Added Hardware configuration to LDAP", 5);
280                 }
281         }
283         # if there is a job in job queue for this host and this macaddress, delete it, cause its no longer used
284         my $del_sql = "DELETE FROM $main::job_queue_tn WHERE (macaddress LIKE '$macaddress' AND headertag='$header')";
285         my $del_res = $main::job_db->exec_statement($del_sql);
286   &main::release_ldap_handle($ldap_handle);
288         return ;
291 1;