Code

new functionality: periodical jobs
[gosa.git] / gosa-si / client / events / gosaTriggered.pm
2 =head1 NAME
4 gosaTriggered.pm
6 =head1 SYNOPSIS
8 use GOSA::GosaSupportDaemon;
10 use MIME::Base64
12 =head1 DESCRIPTION
14 This module contains all GOsa-SI-client processing instructions concerning actions controllable from GOsa.
16 =head1 VERSION
18 Version 1.0
20 =head1 AUTHOR
22 Andreas Rettenberger <rettenberger at gonicus dot de>
24 =head1 FUNCTIONS
26 =cut
29 package gosaTriggered;
30 use Exporter;
31 @ISA = qw(Exporter);
32 my @events = (
33     "get_events",
34     "usr_msg",
35     "trigger_action_localboot",
36     "trigger_action_halt",
37     "trigger_action_faireboot",
38     "trigger_action_reboot",
39     "trigger_action_reinstall",
40     "trigger_action_update",
41     "trigger_action_instant_update",
42     "trigger_goto_settings_reload",
43     );
44 @EXPORT = @events;
46 use strict;
47 use warnings;
48 use GOSA::GosaSupportDaemon;
49 use MIME::Base64;
50 use File::Temp qw/ tempfile/;
52 BEGIN {}
54 END {}
56 ###############################################################################
57 =over 
59 =item B<get_events ()>
61 =over
63 =item description 
65     Reports all provided functions.
67 =item parameter
69     None.
71 =item return 
73     \@events - ARRAYREF - array containing all functions 
75 =back
77 =back
79 =cut
80 ###############################################################################
81 sub get_events { return \@events; }
84 ###############################################################################
85 =over 
87 =item B<usr_msg ($$)>
89 =over
91 =item description 
93     Executes '/usr/bin/goto-notify' wich displays the message, subject und receiver at screen
95 =item parameter
97     $msg - STRING - complete GOsa-si message
98     $msg_hash - HASHREF - content of GOsa-si message in a hash
100 =item GOsa-si message xml content
101     
102     <to> - STRING - username message should be deliverd to
103     <subject> - STRING - subject of the message, base64 encoded
104     <message> - STRING - message itself, base64 encoded
106 =item return 
108     $out_msg - STRING - GOsa-si valid xml message, feedback that message was deliverd
110 =back
112 =back
114 =cut
115 ###############################################################################
116 sub usr_msg {
117     my ($msg, $msg_hash) = @_;
118     my $header = @{$msg_hash->{'header'}}[0];
119     my $source = @{$msg_hash->{'source'}}[0];
120     my $target = @{$msg_hash->{'target'}}[0];
122     my $to = @{$msg_hash->{'usr'}}[0];
123     my $subject = &decode_base64(@{$msg_hash->{'subject'}}[0]);
124     my $message = &decode_base64(@{$msg_hash->{'message'}}[0]);
126     my ($rand_fh, $rand_file) = tempfile( SUFFIX => '.goto_notify');
127     print $rand_fh "source:$source\ntarget:$target\nusr:$to\nsubject:".@{$msg_hash->{'subject'}}[0]."\nmessage:".@{$msg_hash->{'message'}}[0]."\n";
128     close $rand_fh;
129         
130         my ($host, $port) = split(':', $target);
131     my $feedback = system("/usr/bin/goto-notify user-message '$to' '$subject' '$message' '$rand_file' '$host' &" );
133     return
137 ###############################################################################
138 =over 
140 =item B<trigger_action_localboot ($$)>
142 =over
144 =item description 
146     Executes '/sbin/shutdown -r' if  no user is logged in otherwise write 
147     'trigger_action_localboot' to '/etc/gosa-si/event'
149 =item parameter
151     $msg - STRING - complete GOsa-si message
152     $msg_hash - HASHREF - content of GOsa-si message in a hash
154 =item GOsa-si message xml content
156     <timeout> - INTEGER - timeout to wait befor restart, default 0
158 =item return 
159     
160     Nothing.
162 =back
164 =back
166 =cut
167 ###############################################################################
168 sub trigger_action_localboot {
169     my ($msg, $msg_hash) = @_;
170     my $timeout;
172     if((not exists $msg_hash->{timeout} ) || (1 != @{$msg_hash->{timeout}} ) ) {
173         $timeout = -1;
174     } 
175     else {
176         $timeout = @{$msg_hash->{timeout}}[0];
177     }
179     # check logged in user
180     my $logged_in_user = 1;
181     if( $logged_in_user ) {
182         # TODO do something
183     }
184     else {
185         $timeout = 0;
186     }
187         
188     # execute function
189     if( $timeout == 0 ) {
190         print STDERR ("shutdown -r +$timeout\n");
191     }
192     elsif( $timeout > 0 ) {
193         print STDERR ("shutdown -r +$timeout\n");
194     }
195     elsif( $timeout < 0 ) {
196         print STDERR "The administrator has sent a signal to reboot this workstation. It will reboot after you've logged out.\n";
197         open(FILE, "> /etc/gosa-si/event");
198         print FILE "trigger_action_localboot\n";
199         close(FILE);
200     }
201     else {
202         # TODO do something, error handling, logging
203     }
205     return;
209 ###############################################################################
210 =over 
212 =item B<trigger_action_faireboot ($$)>
214 =over
216 =item description 
218     Executes '/usr/sbin/faireboot'.
220 =item parameter
222     $msg - STRING - complete GOsa-si message
223     $msg_hash - HASHREF - content of GOsa-si message in a hash
225 =item GOsa-si message xml content
227     None.
229 =item return 
230     
231     Nothing.
233 =back
235 =back
237 =cut
238 ###############################################################################
239 sub trigger_action_faireboot {
240     my ($msg, $msg_hash) = @_;
241         &main::daemon_log("DEBUG: run /usr/sbin/faireboot\n", 7); 
242     system("/usr/sbin/faireboot");
243     return;
247 ###############################################################################
248 =over 
250 =item B<trigger_action_reboot ($$)>
252 =over
254 =item description 
256     Executes '/usr/bin/goto-notify reboot' and '/sbin/shutdown -r'  if  no 
257     user is logged in otherwise write 'reboot' to '/etc/gosa-si/event'
259 =item parameter
261     $msg - STRING - complete GOsa-si message
262     $msg_hash - HASHREF - content of GOsa-si message in a hash
264 =item GOsa-si message xml content
266     <timeout> - INTEGER - timeout to wait befor reboot, default 0
268 =item return 
269     
270     Nothing.
272 =back
274 =back
276 =cut
277 ###############################################################################
278 sub trigger_action_reboot {
279     my ($msg, $msg_hash) = @_;
280     my $timeout;
282     if((not exists $msg_hash->{timeout} ) || (1 != @{$msg_hash->{timeout}} ) ) {
283         $timeout = 0;
284     } 
285     else {
286         $timeout = @{$msg_hash->{timeout}}[0];
287     }
289     # check logged in user
290     my @user_list = &get_logged_in_users;
291     if( @user_list >= 1 ) {
292         system( "/usr/bin/goto-notify reboot" );
293         open(FILE, "> /etc/gosa-si/event");
294         print FILE "reboot\n";
295         close(FILE);
296     }
297     else {
298         system( "/sbin/shutdown -r +$timeout &" );
299     }
301     return;
305 ###############################################################################
306 =over 
308 =item B<trigger_action_halt ($$)>
310 =over
312 =item description 
314     Executes '/usr/bin/goto-notify halt' and '/sbin/shutdown -h' if  no 
315     user is logged in otherwise write 'halt' to '/etc/gosa-si/event'
317 =item parameter
319     $msg - STRING - complete GOsa-si message
320     $msg_hash - HASHREF - content of GOsa-si message in a hash
322 =item GOsa-si message xml content
324     <timeout> - INTEGER - timeout to wait befor halt, default 0
326 =item return 
327     
328     Nothing.    
330 =back
332 =back
334 =cut
335 ###############################################################################
336 sub trigger_action_halt {
337     my ($msg, $msg_hash) = @_;
338     my $timeout;
340     if((not exists $msg_hash->{timeout} ) || (1 != @{$msg_hash->{timeout}} ) ) {
341         $timeout = 0;
342     } 
343     else {
344         $timeout = @{$msg_hash->{timeout}}[0];
345     }
347     # check logged in user
348     my @user_list = &get_logged_in_users;
349     if( @user_list >= 1 ) {
350         system( "/usr/bin/goto-notify halt" );
351         open(FILE, "> /etc/gosa-si/event");
352         print FILE "halt\n";
353         close(FILE);
354     }
355     else {
356         system( "/sbin/shutdown -h +$timeout &" );
357     }
359     return;
363 ###############################################################################
364 =over 
366 =item B<trigger_action_reinstall>
368 =over
370 =item description 
372     Executes '/usr/bin/goto-notify install' and '/sbin/shutdown -r now' if no 
373     user is logged in otherwise write 'install' to '/etc/gosa-si/event'
375 =item parameter
377     $msg - STRING - complete GOsa-si message
378     $msg_hash - HASHREF - content of GOsa-si message in a hash
380 =item GOsa-si message xml content
382     None.
384 =item return 
385     
386     Nothing.
388 =back
390 =back
392 =cut
393 ###############################################################################
394 sub trigger_action_reinstall {
395     my ($msg, $msg_hash) = @_;
397     # check logged in user
398     my @user_list = &get_logged_in_users;
399     if( @user_list >= 1 ) {
400         system( "/usr/bin/goto-notify install" );
401         open(FILE, "> /etc/gosa-si/event");
402         print FILE "install\n";
403         close(FILE);
404     }
405     else {
406         system( "/sbin/shutdown -r now &" );
407     }
409     return;
413 ###############################################################################
414 =over 
416 =item B<trigger_action_updae>
418 =over
420 =item description 
422     Executes 'DEBIAN_FRONTEND=noninteractive /usr/sbin/fai-softupdate &'
424 =item parameter
426     $msg - STRING - complete GOsa-si message
427     $msg_hash - HASHREF - content of GOsa-si message in a hash
429 =item GOsa-si message xml content
431     None.
433 =item return 
434     
435     Nothing
437 =back
439 =back
441 =cut
442 ###############################################################################
443 # Backward compatibility
444 sub trigger_action_update {
445     my ($msg, $msg_hash) = @_;
447     # Execute update
448     system( "DEBIAN_FRONTEND=noninteractive /usr/sbin/fai-softupdate &" );
450     return;
454 ###############################################################################
455 =over 
457 =item B<trigger_action_instant_update ($$)>
459 =over
461 =item description 
463     Executes 'DEBIAN_FRONTEND=noninteractive /usr/sbin/fai-softupdate &'
465 =item parameter
467     $msg - STRING - complete GOsa-si message
468     $msg_hash - HASHREF - content of GOsa-si message in a hash
470 =item GOsa-si message xml content
472     None.
474 =item return 
475     
476     Nothing.
478 =back
480 =back
482 =cut
483 ###############################################################################
484 # Backward compatibility
485 sub trigger_action_instant_update {
486     my ($msg, $msg_hash) = @_;
488     # Execute update
489     system( "DEBIAN_FRONTEND=noninteractive /usr/sbin/fai-softupdate &" );
491     return;
494 sub trigger_goto_settings_reload {
495     my ($msg, $msg_hash) = @_;
497     # Execute goto settings reload
498     my $cmd = "/etc/init.d/goto-agents";
499     my $pram = "start";
500     if (-f $cmd){
501         my $feedback = system("$cmd $pram") or &main::daemon_log("ERROR: $@");
502     } else {
503         &main::daemon_log("ERROR: cannot exec $cmd, file not found!");
504     }
506     return;
510 1;