714b9768e948bdf38681b6d9ecc730a42d9c1d24
1 package logHandling;
2 use Exporter;
3 @ISA = qw(Exporter);
4 my @events = (
5 "get_events",
6 "show_log_by_mac",
7 "show_log_by_date",
8 "show_log_by_date_and_mac",
9 "show_log_files_by_date_and_mac",
10 "get_log_file_by_date_and_mac",
11 "get_recent_log_by_mac",
12 "delete_log_by_date_and_mac",
13 );
14 @EXPORT = @events;
16 use strict;
17 use warnings;
18 use GOSA::GosaSupportDaemon;
19 use Data::Dumper;
20 use File::Spec;
21 use MIME::Base64;
23 BEGIN {}
25 END {}
27 ### Start ######################################################################
30 #=== FUNCTION ================================================================
31 # NAME: get_events
32 # PARAMETERS: none
33 # RETURNS: reference of exported events
34 # DESCRIPTION: tells the caller which functions are available
35 #===============================================================================
36 sub get_events {
37 return \@events
38 }
41 #=== FUNCTION ================================================================
42 # NAME: show_log_by_date
43 # DESCRIPTION: reporting installed hosts matching to regex of date
44 # PARAMETERS: [$msg] original incoming message
45 # [$msg_hash] incoming message transformed to hash concerning XML::Simple
46 # [$session_id] POE session id
47 # RETURNS: gosa-si valid answer string
48 #===============================================================================
49 sub show_log_by_date {
50 my ($msg, $msg_hash, $session_id) = @_;
51 my $header = @{$msg_hash->{header}}[0];
52 my $target = @{$msg_hash->{target}}[0];
53 my $source = @{$msg_hash->{source}}[0];
54 my $date_l = $msg_hash->{date};
55 my $out_msg;
56 $header =~ s/gosa_//;
58 if (not -d $main::client_fai_log_dir) {
59 my $error_string = "client fai log directory '$main::client_fai_log_dir' do not exist";
60 &main::daemon_log("$session_id ERROR: $error_string", 1);
61 return &create_xml_string(&create_xml_hash($header, $target, $source, $error_string));
62 }
64 # build out_msg
65 my $out_hash = &create_xml_hash($header, $target, $source);
67 # read mac directory
68 opendir(DIR, $main::client_fai_log_dir);
69 my @avail_macs = readdir(DIR);
70 closedir(DIR);
71 foreach my $avail_mac (@avail_macs) {
72 # check mac address
73 if ($avail_mac eq ".." || $avail_mac eq ".") { next; }
75 # read install dates directory
76 my $mac_dir = File::Spec->catdir($main::client_fai_log_dir, $avail_mac);
77 opendir(DIR, $mac_dir);
78 my @avail_dates = readdir(DIR);
79 closedir(DIR);
80 foreach my $date ( @{$date_l} ) { # multiple date selection is allowed
81 foreach my $avail_date (@avail_dates) {
82 # check install date
83 if ($avail_date eq ".." || $avail_date eq ".") { next; }
84 if (not $avail_date =~ /$date/i) { next; }
86 # add content to out_msg
87 &add_content2xml_hash($out_hash, $avail_date, $avail_mac);
88 }
89 }
90 }
92 $out_msg = &create_xml_string($out_hash);
93 my $forward_to_gosa = @{$msg_hash->{'forward_to_gosa'}}[0];
94 if (defined $forward_to_gosa) {
95 $out_msg =~s/<\/xml>/<forward_to_gosa>$forward_to_gosa<\/forward_to_gosa><\/xml>/;
96 }
98 return ($out_msg);
99 }
102 #=== FUNCTION ================================================================
103 # NAME: show_log_by_mac
104 # DESCRIPTION: reporting installation dates matching to regex of mac address
105 # PARAMETERS: [$msg] original incoming message
106 # [$msg_hash] incoming message transformed to hash concerning XML::Simple
107 # [$session_id] POE session id
108 # RETURNS: gosa-si valid answer string
109 #===============================================================================
110 sub show_log_by_mac {
111 my ($msg, $msg_hash, $session_id) = @_;
112 my $header = @{$msg_hash->{header}}[0];
113 my $target = @{$msg_hash->{target}}[0];
114 my $source = @{$msg_hash->{source}}[0];
115 my $mac_l = $msg_hash->{mac};
117 $header =~ s/gosa_//;
119 if (not -d $main::client_fai_log_dir) {
120 my $error_string = "client fai log directory '$main::client_fai_log_dir' do not exist";
121 &main::daemon_log("$session_id ERROR: $error_string", 1);
122 return &create_xml_string(&create_xml_hash($header, $target, $source, $error_string));
123 }
125 # build out_msg
126 my $out_hash = &create_xml_hash($header, $target, $source);
128 # read mac directory
129 opendir(DIR, $main::client_fai_log_dir);
130 my @avail_macs = readdir(DIR);
131 closedir(DIR);
132 foreach my $mac (@{$mac_l}) { # multiple mac selection is allowed
133 foreach my $avail_mac ( @avail_macs ) {
134 # check mac address
135 if ($avail_mac eq ".." || $avail_mac eq ".") { next; }
136 if (not $avail_mac =~ /$mac/i) { next; }
138 # read install dates directory
139 my $act_log_dir = File::Spec->catdir($main::client_fai_log_dir, $avail_mac);
140 if (not -d $act_log_dir) { next; }
141 opendir(DIR, $act_log_dir);
142 my @avail_dates = readdir(DIR);
143 closedir(DIR);
144 $avail_mac =~ s/:/_/g; # make mac address XML::Simple valid
145 foreach my $avail_date (@avail_dates) {
146 # check install date
147 if ($avail_date eq ".." || $avail_date eq ".") { next; }
149 # add content to out_msg
150 &add_content2xml_hash($out_hash, "mac_$avail_mac", $avail_date);
151 }
152 }
153 }
155 my $out_msg = &create_xml_string($out_hash);
156 my $forward_to_gosa = @{$msg_hash->{'forward_to_gosa'}}[0];
157 if (defined $forward_to_gosa) {
158 $out_msg =~s/<\/xml>/<forward_to_gosa>$forward_to_gosa<\/forward_to_gosa><\/xml>/;
159 }
161 return ($out_msg);
162 }
165 #=== FUNCTION ================================================================
166 # NAME: show_log_by_date_and_mac
167 # DESCRIPTION: reporting host and installation dates matching to regex of date and regex of mac address
168 # PARAMETERS: [$msg] original incoming message
169 # [$msg_hash] incoming message transformed to hash concerning XML::Simple
170 # [$session_id] POE session id
171 # RETURNS: gosa-si valid answer string
172 #===============================================================================
173 sub show_log_by_date_and_mac {
174 my ($msg, $msg_hash, $session_id) = @_ ;
175 my $header = @{$msg_hash->{header}}[0];
176 my $target = @{$msg_hash->{target}}[0];
177 my $source = @{$msg_hash->{source}}[0];
178 my $date = @{$msg_hash->{date}}[0];
179 my $mac = @{$msg_hash->{mac}}[0];
180 $header =~ s/gosa_//;
182 if (not -d $main::client_fai_log_dir) {
183 my $error_string = "client fai log directory '$main::client_fai_log_dir' do not exist";
184 &main::daemon_log("$session_id ERROR: $error_string", 1);
185 return &create_xml_string(&create_xml_hash($header, $target, $source, $error_string));
186 }
188 # build out_msg
189 my $out_hash = &create_xml_hash($header, $target, $source);
191 # read mac directory
192 opendir(DIR, $main::client_fai_log_dir);
193 my @avail_macs = readdir(DIR);
194 closedir(DIR);
195 foreach my $avail_mac ( @avail_macs ) {
196 # check mac address
197 if ($avail_mac eq ".." || $avail_mac eq ".") { next; }
198 if (not $avail_mac =~ /$mac/i) { next; }
199 my $act_log_dir = File::Spec->catdir($main::client_fai_log_dir, $avail_mac);
201 # read install date directory
202 opendir(DIR, $act_log_dir);
203 my @install_dates = readdir(DIR);
204 closedir(DIR);
205 foreach my $avail_date (@install_dates) {
206 # check install date
207 if ($avail_date eq ".." || $avail_date eq ".") { next; }
208 if (not $avail_date =~ /$date/i) { next; }
210 # add content to out_msg
211 &add_content2xml_hash($out_hash, $avail_date, $avail_mac);
212 }
213 }
215 my $out_msg = &create_xml_string($out_hash);
216 my $forward_to_gosa = @{$msg_hash->{'forward_to_gosa'}}[0];
217 if (defined $forward_to_gosa) {
218 $out_msg =~s/<\/xml>/<forward_to_gosa>$forward_to_gosa<\/forward_to_gosa><\/xml>/;
219 }
221 return $out_msg;
222 }
225 #=== FUNCTION ================================================================
226 # NAME: show_log_files_by_date_and_mac
227 # DESCRIPTION: reporting installation log files matching exatly to date and mac address
228 # PARAMETERS: [$msg] original incoming message
229 # [$msg_hash] incoming message transformed to hash concerning XML::Simple
230 # [$session_id] POE session id
231 # RETURNS: gosa-si valid answer string
232 #===============================================================================
233 sub show_log_files_by_date_and_mac {
234 my ($msg, $msg_hash, $session_id) = @_ ;
235 my $header = @{$msg_hash->{header}}[0];
236 my $target = @{$msg_hash->{target}}[0];
237 my $source = @{$msg_hash->{source}}[0];
238 my $date = @{$msg_hash->{date}}[0];
239 my $mac = @{$msg_hash->{mac}}[0];
240 $header =~ s/gosa_//;
242 my $act_log_dir = File::Spec->catdir($main::client_fai_log_dir, $mac, $date);
243 if (not -d $act_log_dir) {
244 my $error_string = "client fai log directory '$act_log_dir' do not exist";
245 &main::daemon_log("$session_id ERROR: $error_string", 1);
246 return &create_xml_string(&create_xml_hash($header, $target, $source, $error_string));
247 }
249 # build out_msg
250 my $out_hash = &create_xml_hash($header, $target, $source);
252 # read mac / install date directory
253 opendir(DIR, $act_log_dir);
254 my @log_files = readdir(DIR);
255 closedir(DIR);
257 foreach my $log_file (@log_files) {
258 if ($log_file eq ".." || $log_file eq ".") { next; }
260 # add content to out_msg
261 &add_content2xml_hash($out_hash, $header, $log_file);
262 }
264 my $out_msg = &create_xml_string($out_hash);
265 my $forward_to_gosa = @{$msg_hash->{'forward_to_gosa'}}[0];
266 if (defined $forward_to_gosa) {
267 $out_msg =~s/<\/xml>/<forward_to_gosa>$forward_to_gosa<\/forward_to_gosa><\/xml>/;
268 }
270 return $out_msg;
271 }
274 #=== FUNCTION ================================================================
275 # NAME: get_log_file_by_date_and_mac
276 # DESCRIPTION: returning the given log file, base64 coded, matching exactly to date and mac address
277 # PARAMETERS: [$msg] original incoming message
278 # [$msg_hash] incoming message transformed to hash concerning XML::Simple
279 # [$session_id] POE session id
280 # RETURNS: gosa-si valid answer string
281 #===============================================================================
282 sub get_log_file_by_date_and_mac {
283 my ($msg, $msg_hash, $session_id) = @_ ;
284 my $header = @{$msg_hash->{header}}[0];
285 my $target = @{$msg_hash->{target}}[0];
286 my $source = @{$msg_hash->{source}}[0];
287 my $date = @{$msg_hash->{date}}[0];
288 my $mac = @{$msg_hash->{mac}}[0];
289 my $log_file = @{$msg_hash->{log_file}}[0];
290 $header =~ s/gosa_//;
292 # sanity check
293 my $act_log_file = File::Spec->catfile($main::client_fai_log_dir, $mac, $date, $log_file);
294 if (not -f $act_log_file) {
295 my $error_string = "client fai log file '$act_log_file' do not exist or could not be read";
296 &main::daemon_log("$session_id ERROR: $error_string", 1);
297 return &create_xml_string(&create_xml_hash($header, $target, $source, $error_string));
298 }
300 # read log file
301 my $log_content;
302 open(FILE, "<$act_log_file");
303 my @log_lines = <FILE>;
304 close(FILE);
306 # prepare content for xml sending
307 $log_content = join("", @log_lines);
308 $log_content = &encode_base64($log_content);
310 # build out_msg and send
311 my $out_hash = &create_xml_hash($header, $target, $source);
312 &add_content2xml_hash($out_hash, $log_file, $log_content);
313 my $out_msg = &create_xml_string($out_hash);
314 my $forward_to_gosa = @{$msg_hash->{'forward_to_gosa'}}[0];
315 if (defined $forward_to_gosa) {
316 $out_msg =~s/<\/xml>/<forward_to_gosa>$forward_to_gosa<\/forward_to_gosa><\/xml>/;
317 }
319 return $out_msg;
320 }
323 # sorting function for fai log directory names
324 # used by get_recent_log_by_mac
325 sub transform {
326 my $a = shift;
327 $a =~ /_(\d{8}?)_(\d{6}?)$/ || return 0;
328 return int("$1$2");
329 }
330 sub by_log_date {
331 &transform($a) <=> &transform($b);
332 }
333 #=== FUNCTION ================================================================
334 # NAME: get_recent_log_by_mac
335 # DESCRIPTION: reporting the latest installation date matching to regex of mac address
336 # PARAMETERS: [$msg] original incoming message
337 # [$msg_hash] incoming message transformed to hash concerning XML::Simple
338 # [$session_id] POE session id
339 # RETURNS: gosa-si valid answer string
340 #===============================================================================
341 sub get_recent_log_by_mac {
342 my ($msg, $msg_hash, $session_id) = @_ ;
343 my $header = @{$msg_hash->{header}}[0];
344 my $target = @{$msg_hash->{target}}[0];
345 my $source = @{$msg_hash->{source}}[0];
346 my $mac = @{$msg_hash->{mac}}[0];
347 $header =~ s/gosa_//;
349 # sanity check
350 if (not -d $main::client_fai_log_dir) {
351 my $error_string = "client fai log directory '$main::client_fai_log_dir' do not exist";
352 &main::daemon_log("$session_id ERROR: $error_string", 1);
353 return &create_xml_string(&create_xml_hash($header, $target, $source, $error_string));
354 }
356 opendir (DIR, $main::client_fai_log_dir);
357 my @avail_macs = readdir(DIR);
358 closedir(DIR);
359 my $act_log_dir;
360 my $act_mac;
361 foreach my $avail_mac (@avail_macs) {
362 if ($avail_mac eq ".." || $avail_mac eq ".") { next; }
363 if (not $avail_mac =~ /$mac/i) { next; }
364 $act_log_dir = File::Spec->catdir($main::client_fai_log_dir, $avail_mac);
365 $act_mac = $avail_mac;
366 }
367 if (not defined $act_log_dir) {
368 my $error_string = "do not find mac '$mac' in directory '$main::client_fai_log_dir'";
369 &main::daemon_log("$session_id ERROR: $error_string", 1);
370 return &create_xml_string(&create_xml_hash($header, $target, $source, $error_string));
371 }
373 # read mac directory
374 opendir(DIR, $act_log_dir);
375 my @avail_dates = readdir(DIR);
376 closedir(DIR);
378 # search for the latest log
379 my @sorted_dates = sort by_log_date @avail_dates;
380 my $latest_log = pop(@sorted_dates);
382 # build out_msg
383 my $out_hash = &create_xml_hash($header, $target, $source);
385 # read latest log directory
386 my $latest_log_dir = File::Spec->catdir($main::client_fai_log_dir, $act_mac, $latest_log);
387 opendir(DIR, $latest_log_dir);
388 my @log_files = readdir(DIR);
389 closedir(DIR);
391 # add all log_files to out_msg
392 foreach my $log_file (@log_files) {
393 if ($log_file eq ".." || $log_file eq ".") { next; }
394 &add_content2xml_hash($out_hash, $latest_log, $log_file);
395 }
397 my $out_msg = &create_xml_string($out_hash);
398 my $forward_to_gosa = @{$msg_hash->{'forward_to_gosa'}}[0];
399 if (defined $forward_to_gosa) {
400 $out_msg =~s/<\/xml>/<forward_to_gosa>$forward_to_gosa<\/forward_to_gosa><\/xml>/;
401 }
403 return $out_msg;
404 }
407 #=== FUNCTION ================================================================
408 # NAME: delete_log_by_date_and_mac
409 # DESCRIPTION: delete installation date directory matching to regex of date and regex of mac address
410 # missing date or mac is substitutet with regex '.'; if both is missing, deleting is rejected
411 # PARAMETERS: [$msg] original incoming message
412 # [$msg_hash] incoming message transformed to hash concerning XML::Simple
413 # [$session_id] POE session id
414 # RETURNS: gosa-si valid answer string
415 #===============================================================================
416 sub delete_log_by_date_and_mac {
417 my ($msg, $msg_hash, $session_id) = @_ ;
418 my $header = @{$msg_hash->{header}}[0];
419 my $target = @{$msg_hash->{target}}[0];
420 my $source = @{$msg_hash->{source}}[0];
421 my $date = @{$msg_hash->{date}}[0];
422 my $mac = @{$msg_hash->{mac}}[0];
423 $header =~ s/gosa_//;
425 # sanity check
426 if (not -d $main::client_fai_log_dir) {
427 my $error_string = "client fai log directory '$main::client_fai_log_dir' do not exist";
428 &main::daemon_log("$session_id ERROR: $session_id", 1);
429 return &create_xml_string(&create_xml_hash($header, $target, $source, $error_string));
430 }
431 if ((not defined $date) && (not defined $mac)) {
432 my $error_string = "deleting all log files from gosa-si-server by an empty delete message is not permitted";
433 &main::daemon_log("$session_id INFO: $error_string", 5);
434 return &create_xml_string(&create_xml_hash($header, $target, $source, $error_string));
435 }
436 if (not defined $date) { $date = "."; } # set date to a regular expression matching to everything
437 if (not defined $mac) { $mac = "."; } # set mac to a regular expression matching to everything
439 # build out_msg
440 my $out_hash = &create_xml_hash($header, $target, $source);
442 # read mac directory
443 opendir(DIR, $main::client_fai_log_dir);
444 my @avail_macs = readdir(DIR);
445 closedir(DIR);
446 foreach my $avail_mac ( @avail_macs ) {
447 # check mac address
448 if ($avail_mac eq ".." || $avail_mac eq ".") { next; }
449 if (not $avail_mac =~ /$mac/i) { next; }
450 my $act_log_dir = File::Spec->catdir($main::client_fai_log_dir, $avail_mac);
452 # read install date directory
453 opendir(DIR, $act_log_dir);
454 my @install_dates = readdir(DIR);
455 closedir(DIR);
456 foreach my $avail_date (@install_dates) {
457 # check install date
458 if ($avail_date eq ".." || $avail_date eq ".") { next; }
459 if (not $avail_date =~ /$date/i) { next; }
461 # delete directory and reptorting
462 my $dir_to_delete = File::Spec->catdir($main::client_fai_log_dir, $avail_mac, $avail_date);
463 #my $error = rmdir($dir_to_delete);
464 my $error = 0;
465 if ($error == 1) {
466 &main::daemon_log("$session_id ERROR: log directory '$dir_to_delete' cannot be deleted: $!", 1);
467 } else {
468 &main::daemon_log("$session_id INFO: log directory '$dir_to_delete' deleted", 5);
469 &add_content2xml_hash($out_hash, $avail_date, $avail_mac);
470 }
471 }
472 }
474 my $out_msg = &create_xml_string($out_hash);
475 my $forward_to_gosa = @{$msg_hash->{'forward_to_gosa'}}[0];
476 if (defined $forward_to_gosa) {
477 $out_msg =~s/<\/xml>/<forward_to_gosa>$forward_to_gosa<\/forward_to_gosa><\/xml>/;
478 }
480 return $out_msg;
481 }
483 1;