c06a0b2f9d80ac16cf5328487f8512cda030dd02
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 return ($out_msg);
94 }
97 #=== FUNCTION ================================================================
98 # NAME: show_log_by_mac
99 # DESCRIPTION: reporting installation dates matching to regex of mac address
100 # PARAMETERS: [$msg] original incoming message
101 # [$msg_hash] incoming message transformed to hash concerning XML::Simple
102 # [$session_id] POE session id
103 # RETURNS: gosa-si valid answer string
104 #===============================================================================
105 sub show_log_by_mac {
106 my ($msg, $msg_hash, $session_id) = @_;
107 my $header = @{$msg_hash->{header}}[0];
108 my $target = @{$msg_hash->{target}}[0];
109 my $source = @{$msg_hash->{source}}[0];
110 my $mac_l = $msg_hash->{mac};
112 $header =~ s/gosa_//;
114 if (not -d $main::client_fai_log_dir) {
115 my $error_string = "client fai log directory '$main::client_fai_log_dir' do not exist";
116 &main::daemon_log("$session_id ERROR: $error_string", 1);
117 return &create_xml_string(&create_xml_hash($header, $target, $source, $error_string));
118 }
120 # build out_msg
121 my $out_hash = &create_xml_hash($header, $target, $source);
123 # read mac directory
124 opendir(DIR, $main::client_fai_log_dir);
125 my @avail_macs = readdir(DIR);
126 closedir(DIR);
127 foreach my $mac (@{$mac_l}) { # multiple mac selection is allowed
128 foreach my $avail_mac ( @avail_macs ) {
129 # check mac address
130 if ($avail_mac eq ".." || $avail_mac eq ".") { next; }
131 if (not $avail_mac =~ /$mac/i) { next; }
133 # read install dates directory
134 my $act_log_dir = File::Spec->catdir($main::client_fai_log_dir, $avail_mac);
135 if (not -d $act_log_dir) { next; }
136 opendir(DIR, $act_log_dir);
137 my @avail_dates = readdir(DIR);
138 closedir(DIR);
139 $avail_mac =~ s/:/_/g; # make mac address XML::Simple valid
140 foreach my $avail_date (@avail_dates) {
141 # check install date
142 if ($avail_date eq ".." || $avail_date eq ".") { next; }
144 # add content to out_msg
145 &add_content2xml_hash($out_hash, "mac_$avail_mac", $avail_date);
146 }
147 }
148 }
150 my $out_msg = &create_xml_string($out_hash);
151 return ($out_msg);
152 }
155 #=== FUNCTION ================================================================
156 # NAME: show_log_by_date_and_mac
157 # DESCRIPTION: reporting host and installation dates matching to regex of date and regex of mac address
158 # PARAMETERS: [$msg] original incoming message
159 # [$msg_hash] incoming message transformed to hash concerning XML::Simple
160 # [$session_id] POE session id
161 # RETURNS: gosa-si valid answer string
162 #===============================================================================
163 sub show_log_by_date_and_mac {
164 my ($msg, $msg_hash, $session_id) = @_ ;
165 my $header = @{$msg_hash->{header}}[0];
166 my $target = @{$msg_hash->{target}}[0];
167 my $source = @{$msg_hash->{source}}[0];
168 my $date = @{$msg_hash->{date}}[0];
169 my $mac = @{$msg_hash->{mac}}[0];
170 $header =~ s/gosa_//;
172 if (not -d $main::client_fai_log_dir) {
173 my $error_string = "client fai log directory '$main::client_fai_log_dir' do not exist";
174 &main::daemon_log("$session_id ERROR: $error_string", 1);
175 return &create_xml_string(&create_xml_hash($header, $target, $source, $error_string));
176 }
178 # build out_msg
179 my $out_hash = &create_xml_hash($header, $target, $source);
181 # read mac directory
182 opendir(DIR, $main::client_fai_log_dir);
183 my @avail_macs = readdir(DIR);
184 closedir(DIR);
185 foreach my $avail_mac ( @avail_macs ) {
186 # check mac address
187 if ($avail_mac eq ".." || $avail_mac eq ".") { next; }
188 if (not $avail_mac =~ /$mac/i) { next; }
189 my $act_log_dir = File::Spec->catdir($main::client_fai_log_dir, $avail_mac);
191 # read install date directory
192 opendir(DIR, $act_log_dir);
193 my @install_dates = readdir(DIR);
194 closedir(DIR);
195 foreach my $avail_date (@install_dates) {
196 # check install date
197 if ($avail_date eq ".." || $avail_date eq ".") { next; }
198 if (not $avail_date =~ /$date/i) { next; }
200 # add content to out_msg
201 &add_content2xml_hash($out_hash, $avail_date, $avail_mac);
202 }
203 }
205 my $out_msg = &create_xml_string($out_hash);
206 return $out_msg;
207 }
210 #=== FUNCTION ================================================================
211 # NAME: show_log_files_by_date_and_mac
212 # DESCRIPTION: reporting installation log files matching exatly to date and mac address
213 # PARAMETERS: [$msg] original incoming message
214 # [$msg_hash] incoming message transformed to hash concerning XML::Simple
215 # [$session_id] POE session id
216 # RETURNS: gosa-si valid answer string
217 #===============================================================================
218 sub show_log_files_by_date_and_mac {
219 my ($msg, $msg_hash, $session_id) = @_ ;
220 my $header = @{$msg_hash->{header}}[0];
221 my $target = @{$msg_hash->{target}}[0];
222 my $source = @{$msg_hash->{source}}[0];
223 my $date = @{$msg_hash->{date}}[0];
224 my $mac = @{$msg_hash->{mac}}[0];
225 $header =~ s/gosa_//;
227 my $act_log_dir = File::Spec->catdir($main::client_fai_log_dir, $mac, $date);
228 if (not -d $act_log_dir) {
229 my $error_string = "client fai log directory '$act_log_dir' do not exist";
230 &main::daemon_log("$session_id ERROR: $error_string", 1);
231 return &create_xml_string(&create_xml_hash($header, $target, $source, $error_string));
232 }
234 # build out_msg
235 my $out_hash = &create_xml_hash($header, $target, $source);
237 # read mac / install date directory
238 opendir(DIR, $act_log_dir);
239 my @log_files = readdir(DIR);
240 closedir(DIR);
242 foreach my $log_file (@log_files) {
243 if ($log_file eq ".." || $log_file eq ".") { next; }
245 # add content to out_msg
246 &add_content2xml_hash($out_hash, $header, $log_file);
247 }
249 my $out_msg = &create_xml_string($out_hash);
250 return $out_msg;
251 }
254 #=== FUNCTION ================================================================
255 # NAME: get_log_file_by_date_and_mac
256 # DESCRIPTION: returning the given log file, base64 coded, matching exactly to date and mac address
257 # PARAMETERS: [$msg] original incoming message
258 # [$msg_hash] incoming message transformed to hash concerning XML::Simple
259 # [$session_id] POE session id
260 # RETURNS: gosa-si valid answer string
261 #===============================================================================
262 sub get_log_file_by_date_and_mac {
263 my ($msg, $msg_hash, $session_id) = @_ ;
264 my $header = @{$msg_hash->{header}}[0];
265 my $target = @{$msg_hash->{target}}[0];
266 my $source = @{$msg_hash->{source}}[0];
267 my $date = @{$msg_hash->{date}}[0];
268 my $mac = @{$msg_hash->{mac}}[0];
269 my $log_file = @{$msg_hash->{log_file}}[0];
270 $header =~ s/gosa_//;
272 # sanity check
273 my $act_log_file = File::Spec->catfile($main::client_fai_log_dir, $mac, $date, $log_file);
274 if (not -f $act_log_file) {
275 my $error_string = "client fai log file '$act_log_file' do not exist or could not be read";
276 &main::daemon_log("$session_id ERROR: $error_string", 1);
277 return &create_xml_string(&create_xml_hash($header, $target, $source, $error_string));
278 }
280 # read log file
281 my $log_content;
282 open(FILE, "<$act_log_file");
283 my @log_lines = <FILE>;
284 close(FILE);
286 # prepare content for xml sending
287 $log_content = join("", @log_lines);
288 $log_content = &encode_base64($log_content);
290 # build out_msg and send
291 my $out_hash = &create_xml_hash($header, $target, $source);
292 &add_content2xml_hash($out_hash, $log_file, $log_content);
293 my $out_msg = &create_xml_string($out_hash);
294 return $out_msg;
295 }
298 # sorting function for fai log directory names
299 # used by get_recent_log_by_mac
300 sub transform {
301 my $a = shift;
302 $a =~ /_(\d{8}?)_(\d{6}?)$/ || return 0;
303 return int("$1$2");
304 }
305 sub by_log_date {
306 &transform($a) <=> &transform($b);
307 }
308 #=== FUNCTION ================================================================
309 # NAME: get_recent_log_by_mac
310 # DESCRIPTION: reporting the latest installation date matching to regex of mac address
311 # PARAMETERS: [$msg] original incoming message
312 # [$msg_hash] incoming message transformed to hash concerning XML::Simple
313 # [$session_id] POE session id
314 # RETURNS: gosa-si valid answer string
315 #===============================================================================
316 sub get_recent_log_by_mac {
317 my ($msg, $msg_hash, $session_id) = @_ ;
318 my $header = @{$msg_hash->{header}}[0];
319 my $target = @{$msg_hash->{target}}[0];
320 my $source = @{$msg_hash->{source}}[0];
321 my $mac = @{$msg_hash->{mac}}[0];
322 $header =~ s/gosa_//;
324 # sanity check
325 if (not -d $main::client_fai_log_dir) {
326 my $error_string = "client fai log directory '$main::client_fai_log_dir' do not exist";
327 &main::daemon_log("$session_id ERROR: $error_string", 1);
328 return &create_xml_string(&create_xml_hash($header, $target, $source, $error_string));
329 }
331 opendir (DIR, $main::client_fai_log_dir);
332 my @avail_macs = readdir(DIR);
333 closedir(DIR);
334 my $act_log_dir;
335 my $act_mac;
336 foreach my $avail_mac (@avail_macs) {
337 if ($avail_mac eq ".." || $avail_mac eq ".") { next; }
338 if (not $avail_mac =~ /$mac/i) { next; }
339 $act_log_dir = File::Spec->catdir($main::client_fai_log_dir, $avail_mac);
340 $act_mac = $avail_mac;
341 }
342 if (not defined $act_log_dir) {
343 my $error_string = "do not find mac '$mac' in directory '$main::client_fai_log_dir'";
344 &main::daemon_log("$session_id ERROR: $error_string", 1);
345 return &create_xml_string(&create_xml_hash($header, $target, $source, $error_string));
346 }
348 # read mac directory
349 opendir(DIR, $act_log_dir);
350 my @avail_dates = readdir(DIR);
351 closedir(DIR);
353 # search for the latest log
354 my @sorted_dates = sort by_log_date @avail_dates;
355 my $latest_log = pop(@sorted_dates);
357 # build out_msg
358 my $out_hash = &create_xml_hash($header, $target, $source);
360 # read latest log directory
361 my $latest_log_dir = File::Spec->catdir($main::client_fai_log_dir, $act_mac, $latest_log);
362 opendir(DIR, $latest_log_dir);
363 my @log_files = readdir(DIR);
364 closedir(DIR);
366 # add all log_files to out_msg
367 foreach my $log_file (@log_files) {
368 if ($log_file eq ".." || $log_file eq ".") { next; }
369 &add_content2xml_hash($out_hash, $latest_log, $log_file);
370 }
372 my $out_msg = &create_xml_string($out_hash);
373 return $out_msg;
374 }
377 #=== FUNCTION ================================================================
378 # NAME: delete_log_by_date_and_mac
379 # DESCRIPTION: delete installation date directory matching to regex of date and regex of mac address
380 # missing date or mac is substitutet with regex '.'; if both is missing, deleting is rejected
381 # PARAMETERS: [$msg] original incoming message
382 # [$msg_hash] incoming message transformed to hash concerning XML::Simple
383 # [$session_id] POE session id
384 # RETURNS: gosa-si valid answer string
385 #===============================================================================
386 sub delete_log_by_date_and_mac {
387 my ($msg, $msg_hash, $session_id) = @_ ;
388 my $header = @{$msg_hash->{header}}[0];
389 my $target = @{$msg_hash->{target}}[0];
390 my $source = @{$msg_hash->{source}}[0];
391 my $date = @{$msg_hash->{date}}[0];
392 my $mac = @{$msg_hash->{mac}}[0];
393 $header =~ s/gosa_//;
395 # sanity check
396 if (not -d $main::client_fai_log_dir) {
397 my $error_string = "client fai log directory '$main::client_fai_log_dir' do not exist";
398 &main::daemon_log("$session_id ERROR: $session_id", 1);
399 return &create_xml_string(&create_xml_hash($header, $target, $source, $error_string));
400 }
401 if ((not defined $date) && (not defined $mac)) {
402 my $error_string = "deleting all log files from gosa-si-server by an empty delete message is not permitted";
403 &main::daemon_log("$session_id INFO: $error_string", 5);
404 return &create_xml_string(&create_xml_hash($header, $target, $source, $error_string));
405 }
406 if (not defined $date) { $date = "."; } # set date to a regular expression matching to everything
407 if (not defined $mac) { $mac = "."; } # set mac to a regular expression matching to everything
409 # build out_msg
410 my $out_hash = &create_xml_hash($header, $target, $source);
412 # read mac directory
413 opendir(DIR, $main::client_fai_log_dir);
414 my @avail_macs = readdir(DIR);
415 closedir(DIR);
416 foreach my $avail_mac ( @avail_macs ) {
417 # check mac address
418 if ($avail_mac eq ".." || $avail_mac eq ".") { next; }
419 if (not $avail_mac =~ /$mac/i) { next; }
420 my $act_log_dir = File::Spec->catdir($main::client_fai_log_dir, $avail_mac);
422 # read install date directory
423 opendir(DIR, $act_log_dir);
424 my @install_dates = readdir(DIR);
425 closedir(DIR);
426 foreach my $avail_date (@install_dates) {
427 # check install date
428 if ($avail_date eq ".." || $avail_date eq ".") { next; }
429 if (not $avail_date =~ /$date/i) { next; }
431 # delete directory and reptorting
432 my $dir_to_delete = File::Spec->catdir($main::client_fai_log_dir, $avail_mac, $avail_date);
433 #my $error = rmdir($dir_to_delete);
434 my $error = 0;
435 if ($error == 1) {
436 &main::daemon_log("$session_id ERROR: log directory '$dir_to_delete' cannot be deleted: $!", 1);
437 } else {
438 &main::daemon_log("$session_id INFO: log directory '$dir_to_delete' deleted", 5);
439 &add_content2xml_hash($out_hash, $avail_date, $avail_mac);
440 }
441 }
442 }
444 my $out_msg = &create_xml_string($out_hash);
445 return $out_msg;
446 }
448 1;