0b636108141245a99e7e75a80e1b3bafff38f910
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 ######################################################################
29 #&main::read_configfile($main::cfg_file, %cfg_defaults);
31 sub get_events {
32 return \@events
33 }
35 sub show_log_by_date {
36 my ($msg, $msg_hash, $session_id) = @_;
37 my $header = @{$msg_hash->{header}}[0];
38 my $target = @{$msg_hash->{target}}[0];
39 my $source = @{$msg_hash->{source}}[0];
40 my $date_l = $msg_hash->{date};
42 $header =~ s/gosa_//;
44 if (not -d $main::client_fai_log_dir) {
45 &main::daemon_log("$session_id ERROR: client fai log directory '$main::client_fai_log_dir' do not exist", 1);
46 return;
47 }
49 # build out_msg
50 my $out_hash = &create_xml_hash($header, $target, $source);
52 # read mac directory
53 opendir(DIR, $main::client_fai_log_dir);
54 my @avail_macs = readdir(DIR);
55 closedir(DIR);
56 foreach my $avail_mac (@avail_macs) {
57 # check mac address
58 if ($avail_mac eq ".." || $avail_mac eq ".") { next; }
60 # read install dates directory
61 my $mac_dir = File::Spec->catdir($main::client_fai_log_dir, $avail_mac);
62 opendir(DIR, $mac_dir);
63 my @avail_dates = readdir(DIR);
64 closedir(DIR);
65 foreach my $date ( @{$date_l} ) { # multiple date selection is allowed
66 foreach my $avail_date (@avail_dates) {
67 # check install date
68 if ($avail_date eq ".." || $avail_date eq ".") { next; }
69 if (not $avail_date =~ /$date/i) { next; }
71 # add content to out_msg
72 &add_content2xml_hash($out_hash, $avail_date, $avail_mac);
73 }
74 }
75 }
77 my $out_msg = &create_xml_string($out_hash);
78 return ($out_msg);
79 }
81 sub show_log_by_mac {
82 my ($msg, $msg_hash, $session_id) = @_;
83 my $header = @{$msg_hash->{header}}[0];
84 my $target = @{$msg_hash->{target}}[0];
85 my $source = @{$msg_hash->{source}}[0];
86 my $mac_l = $msg_hash->{mac};
88 $header =~ s/gosa_//;
90 if (not -d $main::client_fai_log_dir) {
91 &main::daemon_log("$session_id ERROR: client fai log directory '$main::client_fai_log_dir' do not exist", 1);
92 return;
93 }
95 # build out_msg
96 my $out_hash = &create_xml_hash($header, $target, $source);
98 # read mac directory
99 opendir(DIR, $main::client_fai_log_dir);
100 my @avail_macs = readdir(DIR);
101 closedir(DIR);
102 foreach my $mac (@{$mac_l}) { # multiple mac selection is allowed
103 foreach my $avail_mac ( @avail_macs ) {
104 # check mac address
105 if ($avail_mac eq ".." || $avail_mac eq ".") { next; }
106 if (not $avail_mac =~ /$mac/i) { next; }
108 # read install dates directory
109 my $act_log_dir = File::Spec->catdir($main::client_fai_log_dir, $avail_mac);
110 if (not -d $act_log_dir) { next; }
111 opendir(DIR, $act_log_dir);
112 my @avail_dates = readdir(DIR);
113 closedir(DIR);
114 $avail_mac =~ s/:/_/g; # make mac address XML::Simple valid
115 foreach my $avail_date (@avail_dates) {
116 # check install date
117 if ($avail_date eq ".." || $avail_date eq ".") { next; }
119 # add content to out_msg
120 &add_content2xml_hash($out_hash, "mac_$avail_mac", $avail_date);
121 }
122 }
123 }
125 my $out_msg = &create_xml_string($out_hash);
126 return ($out_msg);
127 }
130 sub show_log_by_date_and_mac {
131 my ($msg, $msg_hash, $session_id) = @_ ;
132 my $header = @{$msg_hash->{header}}[0];
133 my $target = @{$msg_hash->{target}}[0];
134 my $source = @{$msg_hash->{source}}[0];
135 my $date = @{$msg_hash->{date}}[0];
136 my $mac = @{$msg_hash->{mac}}[0];
137 $header =~ s/gosa_//;
139 if (not -d $main::client_fai_log_dir) {
140 &main::daemon_log("$session_id ERROR: client fai log directory '$main::client_fai_log_dir' do not exist", 1);
141 return;
142 }
144 # build out_msg
145 my $out_hash = &create_xml_hash($header, $target, $source);
147 # read mac directory
148 opendir(DIR, $main::client_fai_log_dir);
149 my @avail_macs = readdir(DIR);
150 closedir(DIR);
151 foreach my $avail_mac ( @avail_macs ) {
152 # check mac address
153 if ($avail_mac eq ".." || $avail_mac eq ".") { next; }
154 if (not $avail_mac =~ /$mac/i) { next; }
155 my $act_log_dir = File::Spec->catdir($main::client_fai_log_dir, $avail_mac);
157 # read install date directory
158 opendir(DIR, $act_log_dir);
159 my @install_dates = readdir(DIR);
160 closedir(DIR);
161 foreach my $avail_date (@install_dates) {
162 # check install date
163 if ($avail_date eq ".." || $avail_date eq ".") { next; }
164 if (not $avail_date =~ /$date/i) { next; }
166 # add content to out_msg
167 &add_content2xml_hash($out_hash, $avail_date, $avail_mac);
168 }
169 }
171 my $out_msg = &create_xml_string($out_hash);
172 return $out_msg;
173 }
176 sub show_log_files_by_date_and_mac {
177 my ($msg, $msg_hash, $session_id) = @_ ;
178 my $header = @{$msg_hash->{header}}[0];
179 my $target = @{$msg_hash->{target}}[0];
180 my $source = @{$msg_hash->{source}}[0];
181 my $date = @{$msg_hash->{date}}[0];
182 my $mac = @{$msg_hash->{mac}}[0];
183 $header =~ s/gosa_//;
185 my $act_log_dir = File::Spec->catdir($main::client_fai_log_dir, $mac, $date);
186 if (not -d $act_log_dir) {
187 &main::daemon_log("$session_id ERROR: client fai log directory '$act_log_dir' do not exist", 1);
188 return;
189 }
191 # build out_msg
192 my $out_hash = &create_xml_hash($header, $target, $source);
194 # read mac / install date directory
195 opendir(DIR, $act_log_dir);
196 my @log_files = readdir(DIR);
197 closedir(DIR);
199 foreach my $log_file (@log_files) {
200 if ($log_file eq ".." || $log_file eq ".") { next; }
202 # add content to out_msg
203 &add_content2xml_hash($out_hash, $header, $log_file);
204 }
206 my $out_msg = &create_xml_string($out_hash);
207 return $out_msg;
208 }
211 sub get_log_file_by_date_and_mac {
212 my ($msg, $msg_hash, $session_id) = @_ ;
213 my $header = @{$msg_hash->{header}}[0];
214 my $target = @{$msg_hash->{target}}[0];
215 my $source = @{$msg_hash->{source}}[0];
216 my $date = @{$msg_hash->{date}}[0];
217 my $mac = @{$msg_hash->{mac}}[0];
218 my $log_file = @{$msg_hash->{log_file}}[0];
219 $header =~ s/gosa_//;
221 # sanity check
222 my $act_log_file = File::Spec->catfile($main::client_fai_log_dir, $mac, $date, $log_file);
223 if (not -f $act_log_file) {
224 &main::daemon_log("$session_id ERROR: client fai log file '$act_log_file' do not exist or could not be read", 1);
225 return;
226 }
228 # read log file
229 my $log_content;
230 open(FILE, "<$act_log_file");
231 my @log_lines = <FILE>;
232 close(FILE);
234 # prepare content for xml sending
235 $log_content = join("", @log_lines);
236 $log_content = &encode_base64($log_content);
238 # build out_msg and send
239 my $out_hash = &create_xml_hash($header, $target, $source);
240 &add_content2xml_hash($out_hash, $log_file, $log_content);
241 my $out_msg = &create_xml_string($out_hash);
242 return $out_msg;
243 }
246 # sorting function for fai log directory names
247 # used by get_recent_log_by_mac
248 sub transform {
249 my $a = shift;
250 $a =~ /_(\d{8}?)_(\d{6}?)$/ || return 0;
251 return int("$1$2");
252 }
253 sub by_log_date {
254 &transform($a) <=> &transform($b);
255 }
256 sub get_recent_log_by_mac {
257 my ($msg, $msg_hash, $session_id) = @_ ;
258 my $header = @{$msg_hash->{header}}[0];
259 my $target = @{$msg_hash->{target}}[0];
260 my $source = @{$msg_hash->{source}}[0];
261 my $mac = @{$msg_hash->{mac}}[0];
262 $header =~ s/gosa_//;
264 # sanity check
265 if (not -d $main::client_fai_log_dir) {
266 my $error_string = "client fai log directory '$main::client_fai_log_dir' do not exist";
267 &main::daemon_log("$session_id ERROR: $error_string", 1);
268 my $out_hash = &create_xml_hash($header, $target, $source, $error_string);
269 my $out_msg = &create_xml_string($out_hash);
270 return $out_msg;
271 }
273 opendir (DIR, $main::client_fai_log_dir);
274 my @avail_macs = readdir(DIR);
275 closedir(DIR);
276 my $act_log_dir;
277 my $act_mac;
278 foreach my $avail_mac (@avail_macs) {
279 if ($avail_mac eq ".." || $avail_mac eq ".") { next; }
280 if (not $avail_mac =~ /$mac/i) { next; }
281 $act_log_dir = File::Spec->catdir($main::client_fai_log_dir, $avail_mac);
282 $act_mac = $avail_mac;
283 }
284 if (not defined $act_log_dir) {
285 my $error_string = "do not find mac '$mac' in directory '$main::client_fai_log_dir'";
286 &main::daemon_log("$session_id ERROR: $error_string", 1);
287 my $out_hash = &create_xml_hash($header, $target, $source, $error_string);
288 my $out_msg = &create_xml_string($out_hash);
289 return $out_msg;
290 }
292 # read mac directory
293 opendir(DIR, $act_log_dir);
294 my @avail_dates = readdir(DIR);
295 closedir(DIR);
297 # search for the latest log
298 my @sorted_dates = sort by_log_date @avail_dates;
299 my $latest_log = pop(@sorted_dates);
301 # build out_msg
302 my $out_hash = &create_xml_hash($header, $target, $source);
304 # read latest log directory
305 my $latest_log_dir = File::Spec->catdir($main::client_fai_log_dir, $act_mac, $latest_log);
306 opendir(DIR, $latest_log_dir);
307 my @log_files = readdir(DIR);
308 closedir(DIR);
310 # add all log_files to out_msg
311 foreach my $log_file (@log_files) {
312 if ($log_file eq ".." || $log_file eq ".") { next; }
313 &add_content2xml_hash($out_hash, $latest_log, $log_file);
314 }
316 my $out_msg = &create_xml_string($out_hash);
317 return $out_msg;
318 }
321 sub delete_log_by_date_and_mac {
322 my ($msg, $msg_hash, $session_id) = @_ ;
323 my $header = @{$msg_hash->{header}}[0];
324 my $target = @{$msg_hash->{target}}[0];
325 my $source = @{$msg_hash->{source}}[0];
326 my $date = @{$msg_hash->{date}}[0];
327 my $mac = @{$msg_hash->{mac}}[0];
328 $header =~ s/gosa_//;
330 # sanity check
331 if (not -d $main::client_fai_log_dir) {
332 &main::daemon_log("$session_id ERROR: client fai log directory '$main::client_fai_log_dir' do not exist", 1);
333 return;
334 }
335 if ((not defined $date) && (not defined $mac)) {
336 my $err_string = "deleting all log files from gosa-si-server by an empty delete message is not permitted";
337 &main::daemon_log("$session_id INFO: $err_string", 5);
338 my $out_hash = &create_xml_hash($header, $target, $source, $err_string);
339 my $out_msg = &create_xml_string($out_hash);
340 return $out_msg;
341 }
342 if (not defined $date) { $date = "."; } # set date to a regular expression matching to everything
343 if (not defined $mac) { $mac = "."; } # set mac to a regular expression matching to everything
345 # build out_msg
346 my $out_hash = &create_xml_hash($header, $target, $source);
348 # read mac directory
349 opendir(DIR, $main::client_fai_log_dir);
350 my @avail_macs = readdir(DIR);
351 closedir(DIR);
352 foreach my $avail_mac ( @avail_macs ) {
353 # check mac address
354 if ($avail_mac eq ".." || $avail_mac eq ".") { next; }
355 if (not $avail_mac =~ /$mac/i) { next; }
356 my $act_log_dir = File::Spec->catdir($main::client_fai_log_dir, $avail_mac);
358 # read install date directory
359 opendir(DIR, $act_log_dir);
360 my @install_dates = readdir(DIR);
361 closedir(DIR);
362 foreach my $avail_date (@install_dates) {
363 # check install date
364 if ($avail_date eq ".." || $avail_date eq ".") { next; }
365 if (not $avail_date =~ /$date/i) { next; }
367 # delete directory and reptorting
368 my $dir_to_delete = File::Spec->catdir($main::client_fai_log_dir, $avail_mac, $avail_date);
369 #my $error = rmdir($dir_to_delete);
370 my $error = 0;
371 if ($error == 1) {
372 &main::daemon_log("$session_id ERROR: log directory '$dir_to_delete' cannot be deleted: $!", 1);
373 } else {
374 &main::daemon_log("$session_id INFO: log directory '$dir_to_delete' deleted", 5);
375 &add_content2xml_hash($out_hash, $avail_date, $avail_mac);
376 }
377 }
378 }
380 my $out_msg = &create_xml_string($out_hash);
381 return $out_msg;
382 }
384 1;