~ubuntu-branches/ubuntu/lucid/logwatch/lucid-201002122348

« back to all changes in this revision

Viewing changes to scripts/logwatch.pl

  • Committer: Bazaar Package Importer
  • Author(s): Chuck Short
  • Date: 2008-07-05 11:49:54 UTC
  • mfrom: (1.1.7 upstream)
  • Revision ID: james.westby@ubuntu.com-20080705114954-9naedpe6wrk3k7xt
Tags: 7.3.6.cvs20080702-1ubuntu1
* Merge from debian unstable, remaining changes:
  - Use postfix rather than exim4.
  - Update maintainers according to spec.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
#!/usr/bin/perl -w
2
2
use strict;
3
3
##########################################################################
4
 
# $Id: logwatch.pl,v 1.192 2007/05/20 00:24:20 kirk Exp $
 
4
# $Id: logwatch.pl,v 1.211 2008/05/09 01:05:29 mike Exp $
5
5
##########################################################################
6
6
# Most current version can always be found at:
7
 
# ftp://ftp.logwatch.org/pub/redhat/RPMS
 
7
# ftp://ftp.logwatch.org/pub/linux (tarball)
 
8
# ftp://ftp.logwatch.org/pub/redhat/RPMS (RPMs)
8
9
 
9
10
########################################################
10
11
# Specify version and build-date:
11
 
my $Version = '7.3.6';
12
 
my $VDate = '05/19/07';
 
12
my $Version = '7.3.6+cvs20080702-debian';
 
13
my $VDate = '07/02/08';
13
14
 
14
15
#######################################################
15
 
# This was written and is maintained by:
 
16
# Logwatch was written and is maintained by:
16
17
#    Kirk Bauer <kirk@kaybee.org>
17
18
#
 
19
# Unless otherwise specified, Logwatch and all bundled filter scripts
 
20
# are Copyright (c) Kirk Bauer and covered under the included MIT/X
 
21
# Consortium license.
 
22
#
18
23
# Please send all comments, suggestions, bug reports,
19
24
#    etc, to logwatch@logwatch.org.
20
25
#
21
26
########################################################
22
27
 
 
28
############################################################################
 
29
# ENV SETTINGS:
23
30
# About the locale:  some functions use locale information.  In particular,
24
31
# Logwatch makes use of strftime, which makes use of LC_TIME variable.  Other
25
32
# functions may also use locale information.
47
54
 
48
55
#############################################################################
49
56
 
 
57
#############################################################################
 
58
# SET LIBS, GLOBALS, and DEFAULTS
50
59
use Getopt::Long;
51
60
use POSIX qw(uname);
52
61
use File::Temp qw/ tempdir /;
61
70
 
62
71
# Default config here...
63
72
$Config{'detail'} = 0;
64
 
$Config{'mailto'} = "root";
 
73
# if MAILTO is set in the environment, grab it, as it may be used by cron
 
74
# or anacron
 
75
if ($ENV{'MAILTO'}) {
 
76
   $Config{'mailto'} = $ENV{'MAILTO'};
 
77
} else {
 
78
   $Config{'mailto'} = "root";
 
79
}
65
80
$Config{'mailfrom'} = "Logwatch";
66
 
$Config{'save'} = "";
67
 
$Config{'print'} = 1;
 
81
$Config{'subject'} = "";
 
82
$Config{'filename'} = "";
68
83
$Config{'range'} = "yesterday";
69
84
$Config{'debug'} = 0;
70
85
$Config{'archives'} = 1;
71
86
$Config{'tmpdir'} = "/var/cache/logwatch";
72
 
$Config{'splithosts'} = 0;
73
 
$Config{'multiemail'} = 0;
74
87
$Config{'numeric'} = 0;
75
88
$Config{'pathtocat'} = "cat";
76
89
$Config{'pathtozcat'} = "zcat";
77
90
$Config{'pathtobzcat'} = "bzcat";
78
 
$Config{'output'} = "unformatted";
79
 
$Config{'encode'} = 0;
 
91
$Config{'output'} = "stdout"; #8.0
 
92
$Config{'format'} = "text"; #8.0
 
93
$Config{'encode'} = "none"; #8.0
 
94
$Config{'hostformat'} = "none"; #8.0
80
95
$Config{'html_wrap'} = 80;
81
96
 
82
97
if (-e "$ConfigDir/conf/html/header.html") {
105
120
my ($OSname, $hostname, $release, $version, $machine) = POSIX::uname();
106
121
$Config{'hostname'} = "$hostname";
107
122
 
108
 
#############################################################################
109
 
 
110
 
sub Usage () {
111
 
   # Show usage for this program
112
 
   print "\nUsage: $0 [--detail <level>] [--logfile <name>]\n" . 
113
 
      "   [--print] [--mailto <addr>] [--archives] [--range <range>] [--debug <level>]\n" .
114
 
      "   [--save <filename>] [--help] [--version] [--service <name>]\n" .
115
 
      "   [--numeric] [--output <output_type>]\n" .
116
 
      "   [--splithosts] [--multiemail]\n\n";
117
 
   print "--detail <level>: Report Detail Level - High, Med, Low or any #.\n";
118
 
   print "--logfile <name>: *Name of a logfile definition to report on.\n";
119
 
   print "--logdir <name>: Name of default directory where logs are stored.\n";
120
 
   print "--service <name>: *Name of a service definition to report on.\n";
121
 
   print "--print: Display report to stdout.\n";
122
 
   print "--mailto <addr>: Mail report to <addr>.\n";
123
 
   print "--archives: Use archived log files too.\n";
124
 
   print "--save <filename>: Save to <filename>.\n";
125
 
   print "--range <range>: Date range: Yesterday, Today, All, Help\n";
126
 
   print "                             where help will describe additional options\n";
127
 
   print "--numeric: Display addresses numerically rather than symbolically and numerically\n";
128
 
   print "           (saves  a  nameserver address-to-name lookup).\n";
129
 
   print "--debug <level>: Debug Level - High, Med, Low or any #.\n";
130
 
# hostname needs to be cleaned up and explained
131
 
#   print "--hostname <host>: 
132
 
   print "--splithosts: Create a report for each host in syslog.\n";
133
 
   print "--multiemail: Send each host report in a separate email.  Ignored if \n";
134
 
   print "              not using --splithosts.\n";
135
 
   print "--output <output type>: Report Format - mail, html or unformatted#.\n";
136
 
   print "--encode: Use base64 encoding on output mail.\n";
137
 
   print "--version: Displays current version.\n";
138
 
   print "--help: This message.\n";
139
 
   print "* = Switch can be specified multiple times...\n\n";
140
 
   exit (99);
141
 
}
142
 
 
143
123
my %wordsToInts = (yes  => 1,  no     => 0,
144
124
                   true => 1,  false  => 0,
145
125
                   on   => 1,  off    => 0,
147
127
                   med  => 5,  medium => 5,
148
128
                   low  => 0);
149
129
 
150
 
sub getInt {
151
 
   my $word = shift;
152
 
   my $tmpWord = lc $word;
153
 
   $tmpWord =~ s/\W//g;
154
 
   return $wordsToInts{$tmpWord} if (defined $wordsToInts{$tmpWord});
155
 
   unless ($word =~ s/^"(.*)"$/$1/) {
156
 
      return lc $word;
157
 
   }
158
 
   return $word;
159
 
}
160
 
              
161
 
sub CleanVars {
162
 
   foreach (keys %Config) {
163
 
      $Config{$_} = getInt($Config{$_});
164
 
   }
165
 
}
166
 
 
167
 
sub PrintStdArray (@) {
168
 
   my @ThisArray = @_;
169
 
   my $i;
170
 
   for ($i=0;$i<=$#ThisArray;$i++) {
171
 
      print "[" . $i . "] = " . $ThisArray[$i] . "\n";
172
 
   }
173
 
}
174
 
 
175
 
sub PrintConfig () {
176
 
   # for debugging, print out config...
177
 
   foreach (keys %Config) {
178
 
      print $_ . ' -> ' . $Config{$_} . "\n";
179
 
   }
180
 
   print "Service List:\n";
181
 
   PrintStdArray @ServiceList;
182
 
   print "\n";
183
 
   print "LogFile List:\n";
184
 
   PrintStdArray @LogFileList;
185
 
   print "\n\n";
186
 
}
187
 
 
188
 
# for debugging...
189
 
sub PrintServiceData () {
190
 
   my ($ThisKey1,$ThisKey2,$i);
191
 
   foreach $ThisKey1 (keys %ServiceData) {
192
 
      print "\nService Name: " . $ThisKey1 . "\n";
193
 
      foreach $ThisKey2 (keys %{$ServiceData{$ThisKey1}}) {
194
 
         next unless ($ThisKey2 =~ /^\d+-/);
195
 
         print "   $ThisKey2 = $ServiceData{$ThisKey1}{$ThisKey2}\n";
196
 
      }
197
 
      for ($i=0;$i<=$#{$ServiceData{$ThisKey1}{'logfiles'}};$i++) {
198
 
         print "   Logfile = " . $ServiceData{$ThisKey1}{'logfiles'}[$i] . "\n";
199
 
      }
200
 
   }
201
 
}
202
 
 
203
 
# for debugging...
204
 
sub PrintLogFileData () {
205
 
   my ($ThisKey1,$ThisKey2,$i);
206
 
   foreach $ThisKey1 (keys %LogFileData) {
207
 
      print "\nLogfile Name: " . $ThisKey1 . "\n";
208
 
      foreach $ThisKey2 (keys %{$LogFileData{$ThisKey1}}) {
209
 
         next unless ($ThisKey2 =~ /^\d+-/);
210
 
         print "   $ThisKey2 = $LogFileData{$ThisKey1}{$ThisKey2}\n";
211
 
      }
212
 
      for ($i=0;$i<=$#{$LogFileData{$ThisKey1}{'logfiles'}};$i++) {
213
 
         print "   Logfile = " . $LogFileData{$ThisKey1}{'logfiles'}[$i] . "\n";
214
 
      }
215
 
      for ($i=0;$i<=$#{$LogFileData{$ThisKey1}{'archives'}};$i++) {
216
 
         print "   Archive = " . $LogFileData{$ThisKey1}{'archives'}[$i] . "\n";
217
 
      }
218
 
   }
219
 
}
220
 
 
221
 
sub ReadConfigFile {
222
 
   my $FileName = $_[0];
223
 
   my $Prefix = $_[1];
224
 
 
225
 
   if ( ! -f $FileName ) {
226
 
      return(0);
227
 
   }
228
 
 
229
 
   if ($Config{'debug'} > 5) {
230
 
      print "ReadConfigFile: Opening " . $FileName . "\n";
231
 
   }
232
 
   open (READCONFFILE, $FileName) or die "Cannot open file $FileName: $!\n";
233
 
   my $line;
234
 
   while ($line = <READCONFFILE>) {
235
 
      if ($Config{'debug'} > 9) {
236
 
         print "ReadConfigFile: Read Line: " . $line;
237
 
      }
238
 
      $line =~ s/\#.*\\\s*$/\\/;
239
 
      $line =~ s/\#.*$//;
240
 
      next if ($line =~ /^\s*$/);
241
 
 
242
 
      if ($Prefix) {
243
 
         next if ($line !~ m/\Q$Prefix:\E/);
244
 
         $line =~ s/\Q$Prefix:\E//;
245
 
      }
246
 
 
247
 
      if ($line =~ s/\\\s*$//) {
248
 
          $line .= <READCONFFILE>;
249
 
          redo unless eof(READCONFFILE);
250
 
      }
251
 
 
252
 
      my ($name, $value) = split /=/, $line, 2;
253
 
      $name =~ s/^\s+//; $name =~ s/\s+$//;
254
 
      if ($value) { $value =~ s/^\s+//; $value =~ s/\s+$//; }
255
 
      else { $value = ''; }
256
 
 
257
 
      push @ReadConfigNames, lc $name;
258
 
      push @ReadConfigValues, getInt $value;
259
 
      if ($Config{'debug'} > 7) {
260
 
         print "ReadConfigFile: Name=" . $name . ", Value=" . $value . "\n";
261
 
      }
262
 
   }
263
 
   close READCONFFILE;
264
 
}
265
 
 
266
 
#############################################################################
 
130
#############################################################################
 
131
 
 
132
#############################################################################
 
133
#Load CONFIG, READ OPTIONS, make adjustments
267
134
 
268
135
# Load main config file...
269
136
if ($Config{'debug'} > 8) {
270
137
   print "\nDefault Config:\n";
271
 
   PrintConfig();
 
138
   &PrintConfig();
272
139
}
273
140
 
274
 
CleanVars();
 
141
&CleanVars();
275
142
 
276
143
# For each of the configuration sets (logwatch.conf here, and
277
144
# logfiles,and services later), we do the following:
283
150
@ReadConfigNames = ();
284
151
@ReadConfigValues = ();
285
152
 
286
 
ReadConfigFile ("$BaseDir/default.conf/logwatch.conf", "");
287
 
ReadConfigFile ("$BaseDir/dist.conf/logwatch.conf", "");
288
 
ReadConfigFile ("$ConfigDir/conf/logwatch.conf", "");
289
 
ReadConfigFile ("$ConfigDir/conf/override.conf", "logwatch");
 
153
&ReadConfigFile ("$BaseDir/default.conf/logwatch.conf", "");
 
154
&ReadConfigFile ("$BaseDir/dist.conf/logwatch.conf", "");
 
155
&ReadConfigFile ("$ConfigDir/conf/logwatch.conf", "");
 
156
&ReadConfigFile ("$ConfigDir/conf/override.conf", "logwatch");
290
157
 
291
158
 
292
159
for (my $i = 0; $i <= $#ReadConfigNames; $i++) {
307
174
   }
308
175
}
309
176
 
310
 
CleanVars();
 
177
&CleanVars();
311
178
 
312
179
if ($Config{'debug'} > 8) {
313
180
   print "\nConfig After Config File:\n";
314
 
   PrintConfig();
 
181
   &PrintConfig();
315
182
}
316
183
 
317
184
# Options time...
320
187
my @TempServiceList = ();
321
188
my $Help = 0;
322
189
my $ShowVersion = 0;
323
 
my $tmp_mailto;
 
190
my ($tmp_mailto, $tmp_savefile);
324
191
 
325
 
GetOptions ( "d|detail=s"   => \$Config{'detail'},
 
192
&GetOptions ("d|detail=s"   => \$Config{'detail'},
326
193
             "l|logfile=s@" => \@TempLogFileList,
327
194
             "logdir=s"     => \$Config{'logdir'},
328
195
             "s|service=s@" => \@TempServiceList,
329
 
             "p|print"      => \$Config{'print'},
330
 
             "m|mailto:s"   => \$tmp_mailto,
331
 
             "save=s"       => \$Config{'save'},
 
196
             "m|mailto=s"   => \$tmp_mailto,
 
197
             "filename=s"   => \$tmp_savefile,
332
198
             "a|archives"   => \$Config{'archives'},
333
199
             "debug=s"      => \$Config{'debug'},
334
200
             "r|range=s"    => \$Config{'range'},
335
201
             "n|numeric"    => \$Config{'numeric'},
336
202
             "h|help"       => \$Help,
 
203
             "u|usage"      => \$Help,
337
204
             "v|version"    => \$ShowVersion,
338
205
             "hostname=s"   => \$Config{'hostname'},
339
 
             "splithosts"   => \$Config{'splithosts'},
340
 
             "multiemail"   => \$Config{'multiemail'},
341
206
             "o|output=s"   => \$Config{'output'},
342
 
             "encode"       => \$Config{'encode'},
343
 
             "html_wrap=s"  => \$Config{'html_wrap'}
344
 
           ) or Usage();
345
 
 
346
 
$Help and Usage();
 
207
             "f|format=s"   => \$Config{'format'},
 
208
             "e|encode=s"   => \$Config{'encode'},
 
209
             "hostformat=s" => \$Config{'hostformat'},
 
210
             "hostlimit=s"  => \$Config{'hostlimit'},
 
211
             "html_wrap=s"  => \$Config{'html_wrap'},
 
212
             "subject=s"    => \$Config{'subject'}
 
213
           ) or &Usage();
 
214
 
 
215
$Help and &Usage();
 
216
 
 
217
#Catch option exceptions and extra logic here -mgt
347
218
 
348
219
if ($Config{'range'} =~ /help/i) {
349
 
    RangeHelpDM();
 
220
    &RangeHelpDM();
350
221
    exit(0);
351
222
}
352
223
 
355
226
   exit 0;
356
227
}
357
228
 
358
 
if (defined $tmp_mailto) {
359
 
   if ($tmp_mailto ne "") {
360
 
      $Config{'mailto'} = $tmp_mailto;
361
 
   }
362
 
   $Config{'print'} = 0;
363
 
}
364
 
 
365
 
CleanVars();
366
 
 
367
 
 
368
 
my $outtype_mail=0;
369
 
my $outtype_html=0;
370
 
my $outtype_unformatted=0;
 
229
if ($tmp_mailto) {
 
230
   $Config{'mailto'} = $tmp_mailto;
 
231
   $Config{'output'} = "mail"; #8.0 
 
232
}
 
233
 
 
234
if ($tmp_savefile) {
 
235
   $Config{'filename'} = $tmp_savefile;
 
236
   $Config{'output'} = "file"; #8.0 
 
237
}
 
238
 
 
239
if ($Config{'hostformat'} eq "splitmail") {
 
240
   $Config{'output'} = "mail"; #8.0
 
241
   #split hosts 1 long output stream
 
242
   #split hosts multiple output streams -mgt
 
243
}
 
244
 
 
245
&CleanVars();
 
246
 
 
247
#Init Output vars -mgt
371
248
my $index_par=0;
372
249
my @format = (250);
373
250
my %out_body;
374
251
 
375
 
if ( $Config{'output'} eq 'mail' ) {
376
 
            $outtype_mail = 1;
377
 
        } elsif ( ($Config{'output'} eq 'html') || ($Config{'output'} eq 'html-embed') ) {
378
 
      #kept html-embed for backwarks compatibility -mgt
379
 
            $outtype_html = 1;
380
 
        } elsif ( $Config{'output'} eq 'unformatted' ) {
381
 
            $outtype_unformatted = 1;
382
 
        } else {
383
 
            print STDERR "$0: unknown output-format: $Config{'output'}\n\n";
384
 
        }
385
 
 
386
 
 
387
252
my @reports = ();
388
253
 
389
254
my $out_head ='';
393
258
 
394
259
#Eval wrapper for MIME::Base64. Perl 5.6.1 does not include it.
395
260
#So Solaris 9 will break with out this. -mgt
396
 
if ( $Config{'encode'} == 1 ) {
 
261
#8.0 Catch encode types here
 
262
if ( $Config{'encode'} eq "base64" ) {
397
263
   eval "require MIME::Base64";
398
264
   if ($@) {
399
265
      print STDERR "No MIME::Base64 installed can not use --encode\n";
402
268
   }
403
269
}
404
270
 
405
 
if (($Config{'save'} ne "") && (-e "$Config{'save'}") ) {
406
 
#Reset save file now
407
 
   unlink "$Config{'save'}";
 
271
#Reset save file now if we are going ot use it and it exists -mgt
 
272
if (($Config{'filename'} ne "") && (-e "$Config{'filename'}") ) {
 
273
   unlink "$Config{'filename'}";
408
274
}
409
275
 
410
 
if ($Config{'mailto'} eq "") {
411
 
   $Config{'print'} = 1;
 
276
#Check fallback to stdout if output is mail and no mailto exists -mgt
 
277
if ( ($Config{'output'} eq "mail") && ($Config{'mailto'} eq "") ) {
 
278
   $Config{'output'} = "stdout";
412
279
}
413
280
 
414
281
if ($Config{'debug'} > 8) {
415
282
   print "\nCommand Line Parameters:\n   Log File List:\n";
416
 
   PrintStdArray @TempLogFileList;
 
283
   &PrintStdArray(@TempLogFileList);
417
284
   print "\n   Service List:\n";
418
 
   PrintStdArray @TempServiceList;
 
285
   &PrintStdArray(@TempServiceList);
419
286
   print "\nConfig After Command Line Parsing:\n";
420
 
   PrintConfig();
 
287
   &PrintConfig();
421
288
}
422
289
 
423
290
if ($#TempLogFileList > -1) {
441
308
 
442
309
if ($Config{'debug'} > 5) {
443
310
   print "\nConfig After Everything:\n";
444
 
   PrintConfig();
 
311
   &PrintConfig();
445
312
}
446
313
 
447
314
#############################################################################
476
343
   @ReadConfigValues = ();
477
344
   @Separators = ();
478
345
   push (@Separators, scalar(@ReadConfigNames));
479
 
   ReadConfigFile("$BaseDir/default.conf/services/$f", "");
480
 
   push (@Separators, scalar(@ReadConfigNames));
481
 
   ReadConfigFile("$BaseDir/dist.conf/services/$f", "");
482
 
   push (@Separators, scalar(@ReadConfigNames));
483
 
   ReadConfigFile("$ConfigDir/conf/services/$f","");
484
 
   push (@Separators, scalar(@ReadConfigNames));
485
 
   ReadConfigFile("$ConfigDir/conf/override.conf", "services/$ThisService");
 
346
   &ReadConfigFile("$BaseDir/default.conf/services/$f", "");
 
347
   push (@Separators, scalar(@ReadConfigNames));
 
348
   &ReadConfigFile("$BaseDir/dist.conf/services/$f", "");
 
349
   push (@Separators, scalar(@ReadConfigNames));
 
350
   &ReadConfigFile("$ConfigDir/conf/services/$f","");
 
351
   push (@Separators, scalar(@ReadConfigNames));
 
352
   &ReadConfigFile("$ConfigDir/conf/override.conf", "services/$ThisService");
486
353
 
487
354
   @CmdList = ();
488
355
   @CmdArgList = ();
542
409
         @ReadConfigValues = ();
543
410
         @Separators = ();
544
411
         push (@Separators, scalar(@ReadConfigNames));
545
 
         ReadConfigFile("$BaseDir/default.conf/logfiles/$ThisFile", "");
546
 
         push (@Separators, scalar(@ReadConfigNames));
547
 
         ReadConfigFile("$BaseDir/dist.conf/logfiles/$ThisFile", "");
548
 
         push (@Separators, scalar(@ReadConfigNames));
549
 
         ReadConfigFile("$ConfigDir/conf/logfiles/$ThisFile", "");
550
 
         push (@Separators, scalar(@ReadConfigNames));
551
 
         ReadConfigFile("$ConfigDir/conf/override.conf", "logfiles/$ThisLogFile");
 
412
         &ReadConfigFile("$BaseDir/default.conf/logfiles/$ThisFile", "");
 
413
         push (@Separators, scalar(@ReadConfigNames));
 
414
         &ReadConfigFile("$BaseDir/dist.conf/logfiles/$ThisFile", "");
 
415
         push (@Separators, scalar(@ReadConfigNames));
 
416
         &ReadConfigFile("$ConfigDir/conf/logfiles/$ThisFile", "");
 
417
         push (@Separators, scalar(@ReadConfigNames));
 
418
         &ReadConfigFile("$ConfigDir/conf/override.conf", "logfiles/$ThisLogFile");
552
419
 
553
420
         @CmdList = ();
554
421
         @CmdArgList = ();
569
436
                     foreach my $dir ("$Config{'logdir'}/", "/var/adm/", "/var/log/", "") {
570
437
                        # We glob to obtain filenames.  We reverse in case
571
438
                        # we use the decimal suffix (.0, .1, etc.) in filenames
572
 
                        @TempLogFileList = reverse(glob($dir . $ReadConfigValues[$i]));
 
439
                        #@TempLogFileList = reverse(glob($dir . $ReadConfigValues[$i]));
 
440
                        @TempLogFileList = sort{
 
441
                           ($b =~ /(\d+)$/) <=> ($a =~ /(\d+)$/) || uc($a) cmp  uc($b)
 
442
                        }(glob($dir . $ReadConfigValues[$i]));
573
443
                        # And we check for existence once again, since glob
574
444
                        # may return the search pattern if no files found.
575
445
                        last if (@TempLogFileList && (-e $TempLogFileList[0]));
576
446
                     }
577
447
                  } else {
578
 
                     @TempLogFileList = reverse(glob($ReadConfigValues[$i]));
 
448
                     #@TempLogFileList = reverse(glob($ReadConfigValues[$i]));
 
449
                     @TempLogFileList = sort{
 
450
                        ($b =~ /(\d+)$/) <=> ($a =~ /(\d+)$/) || uc($a) cmp  uc($b)
 
451
                     }(glob($ReadConfigValues[$i]));
579
452
                  }
580
453
 
581
454
                  # We attempt to remove duplicates.
602
475
                     foreach my $dir ("$Config{'logdir'}/", "/var/adm/", "/var/log/", "") {
603
476
                        # We glob to obtain filenames.  We reverse in case
604
477
                        # we use the decimal suffix (.0, .1, etc.) in filenames
605
 
                        @TempLogFileList = reverse(glob($dir . $ReadConfigValues[$i]));
 
478
                        #@TempLogFileList = reverse(glob($dir . $ReadConfigValues[$i]));
 
479
                        @TempLogFileList = sort{
 
480
                           ($b =~ /(\d+)$/) <=> ($a =~ /(\d+)$/) || uc($a) cmp  uc($b)
 
481
                        }(glob($dir . $ReadConfigValues[$i]));
606
482
                        # And we check for existence once again, since glob
607
483
                        # may return the search pattern if no files found.
608
484
                        last if (@TempLogFileList && (-e $TempLogFileList[0]));
609
485
                     }
610
486
                  } else {
611
 
                     @TempLogFileList = reverse(glob($ReadConfigValues[$i]));
 
487
                     #@TempLogFileList = reverse(glob($ReadConfigValues[$i]));
 
488
                     @TempLogFileList = sort{
 
489
                        ($b =~ /(\d+)$/) <=> ($a =~ /(\d+)$/) || uc($a) cmp  uc($b)
 
490
                     }(glob($ReadConfigValues[$i]));
612
491
                  }
613
492
 
614
493
                  # We attempt to remove duplicates.  This time we also check
659
538
 
660
539
if ($Config{'debug'} > 5) {
661
540
   print "\nAll Services:\n";
662
 
   PrintStdArray @AllServices;
 
541
   &PrintStdArray(@AllServices);
663
542
   print "\nAll Log Files:\n";
664
 
   PrintStdArray @AllLogFiles;
 
543
   &PrintStdArray(@AllLogFiles);
665
544
   print "\nAll Shared:\n";
666
 
   PrintStdArray @AllShared;
 
545
   &PrintStdArray(@AllShared);
667
546
}
668
547
 
669
548
#############################################################################
735
614
 
736
615
if ($Config{'debug'} > 7) {
737
616
   print "\n\nAll Service Data:\n";
738
 
   PrintServiceData;
 
617
   &PrintServiceData();
739
618
   print "\nServices that will be processed:\n";
740
 
   PrintStdArray @ServiceList;
 
619
   &PrintStdArray(@ServiceList);
741
620
   print "\n\n";
742
621
   print "\n\nAll LogFile Data:\n";
743
 
   PrintLogFileData;
 
622
   &PrintLogFileData();
744
623
   print "\nLogFiles that will be processed:\n";
745
 
   PrintStdArray @LogFileList;
 
624
   &PrintStdArray(@LogFileList);
746
625
   print "\n\n";
747
626
}
748
627
 
797
676
 
798
677
$ENV{'LOGWATCH_DATE_RANGE'} = $Config{'range'};
799
678
$ENV{'LOGWATCH_GLOBAL_DETAIL'} = $Config{'detail'};
800
 
$ENV{'LOGWATCH_OUTPUT_TYPE'} = $Config{'output'};
 
679
$ENV{'LOGWATCH_OUTPUT_TYPE'} = $Config{'output'}; #8.0
 
680
$ENV{'LOGWATCH_FORMAT_TYPE'} = $Config{'format'}; #8.0
801
681
$ENV{'LOGWATCH_DEBUG'} = $Config{'debug'};
802
682
$ENV{'LOGWATCH_TEMP_DIR'} = $TempDir;
803
683
$ENV{'LOGWATCH_NUMERIC'} = $Config{'numeric'};
804
684
$ENV{'HOSTNAME'} = $Config{'hostname'};
805
685
$ENV{'OSname'} = $OSname;
806
686
 
 
687
#split and splitmail also play with LOGWATCH_ONLY_HOSTNAME which is not shown by debug
807
688
if ($Config{'hostlimit'}) {
808
 
   $ENV{'LOGWATCH_ONLY_HOSTNAME'} = $Config{'hostname'};
809
 
   $ENV{'LOGWATCH_ONLY_HOSTNAME'} =~ s/\..*//;
 
689
   #Pass the list to ENV with out touching it
 
690
   $ENV{'LOGWATCH_ONLY_HOSTNAME'} = $Config{'hostlimit'};
810
691
}
 
692
 
811
693
if ($Config{'debug'}>4) {
812
 
   foreach ('LOGWATCH_DATE_RANGE', 'LOGWATCH_GLOBAL_DETAIL', 'LOGWATCH_OUTPUT_TYPE', 
813
 
            'LOGWATCH_TEMP_DIR', 'LOGWATCH_DEBUG', 'LOGWATCH_ONLY_HOSTNAME') {
 
694
   foreach ('LOGWATCH_DATE_RANGE', 'LOGWATCH_GLOBAL_DETAIL', 'LOGWATCH_OUTPUT_TYPE',
 
695
            'LOGWATCH_FORMAT_TYPE', 'LOGWATCH_TEMP_DIR', 'LOGWATCH_DEBUG', 'LOGWATCH_ONLY_HOSTNAME') {
814
696
      if ($ENV{$_}) {
815
697
         print "export $_='$ENV{$_}'\n";
816
698
      }
865
747
       $CheckTime = $time_t[0] - 86400*2;
866
748
      } else {
867
749
         # range is wrong
868
 
         $CheckTime = time;
 
750
         print STDERR "ERROR: Range \'$Config{'range'}\' not understood\n";
 
751
         RangeHelpDM();
 
752
         exit 1;
869
753
      }
870
754
 
 
755
      #Archives are cat'd without any filters then cat'd along with the normal log file
871
756
      my @FileStat = stat($Archive);
872
757
      if ($CheckTime <= ($FileStat[9])) {
873
 
         if (($Archive =~ m/gz$/) && (-f "$Archive")) {
874
 
         #These system calls are not secure but we are getting closer
875
 
         #What needs to go is all the pipes and instead we need a command loop
876
 
         #For each filter to apply -mgt
 
758
         if (($Archive =~ m/gz$/) && (-f "$Archive") && (-s "$Archive")) {
877
759
            my $arguments = "$Archive >> $DestFile";
878
760
            system("$Config{'pathtozcat'} $arguments") == 0
879
 
               or die "system $Config{'pathtozcat'} failed: $?" 
880
 
         } elsif (($Archive =~ m/bz2$/) && (-f "$Archive")) {
881
 
         #These system calls are not secure but we are getting closer
882
 
         #What needs to go is all the pipes and instead we need a command loop
883
 
         #For each filter to apply -mgt
 
761
               or die "system '$Config{'pathtozcat'} $arguments' failed: $?" 
 
762
         } elsif (($Archive =~ m/bz2$/) && (-f "$Archive") && (-s "$Archive")) {
884
763
            my $arguments = "$Archive 2>/dev/null >> $DestFile";
885
764
            system("$Config{'pathtobzcat'} $arguments") == 0
886
 
               or die "system $Config{'pathtobzcat'} failed: $?" 
887
 
         } elsif (-f "$Archive") {
 
765
               or die "system '$Config{'pathtobzcat'} $arguments' failed: $?" 
 
766
         } elsif ((-f "$Archive") && (-s "$Archive")) {
888
767
            my $arguments = "$Archive  >> $DestFile";
889
768
            system("$Config{'pathtocat'} $arguments") == 0
890
 
               or die "system $Config{'pathtocat'} failed: $?" 
 
769
               or die "system '$Config{'pathtocat'} $arguments' failed: $?" 
891
770
         } #End if/elsif existence
892
771
      } #End if $CheckTime
893
772
 
905
784
         print "\n";
906
785
         next;
907
786
      }
 
787
      #FIXME - We have a bug report for filenames with spaces, can be caught here needs test -mgt
908
788
      $FileText .= ($ThisFile . " ");
909
789
   } #End foreach ThisFile
910
790
 
933
813
         }
934
814
      }
935
815
   }
 
816
 
 
817
   #Hostlimit filter need to add ability to negate this use "NoHostFilter = Yes" in logfile like samba -mgt
 
818
   if ( ($Config{'hostlimit'}) && (!$LogFileData{$LogFile}{'nohostfilter'}) ) {
 
819
      #Pass the list to ENV with out touching it
 
820
      $ENV{'LOGWATCH_ONLY_HOSTNAME'} = $Config{'nohostlimit'};
 
821
      $FilterText .= ("| $PerlVersion $BaseDir/scripts/shared/onlyhost");
 
822
   }
 
823
 
936
824
   if (opendir (LOGDIR, "$ConfigDir/scripts/logfiles/" . $LogFile)) {
937
825
      foreach (sort readdir(LOGDIR)) {
938
826
         unless ( -d "$ConfigDir/scripts/logfiles/$LogFile/$_") {
961
849
      if ($LogFile !~ /^[-_\w\d]+$/) {
962
850
         print STDERR "Unexpected filename: [[$LogFile]]. Not used\n"
963
851
      } else {
964
 
      #These system calls are not secure but we are getting closer
965
 
      #What needs to go is all the pipes and instead we need a command loop
966
 
      #For each filter to apply -mgt
 
852
         #System call does the log processing
967
853
         system("$Config{'pathtocat'} $Command") == 0
968
 
            or die "system $Config{'pathtocat'} $Command failed: $?" 
 
854
            or die "system '$Config{'pathtocat'} $Command' failed: $?" 
969
855
      }
970
856
   }
971
857
}
972
858
 
973
859
#populate the host lists if we're splitting hosts
 
860
#It seems this is run after the file is parsed so it is done 2 times?
 
861
#Can it be put inline with the above filters?
974
862
my @hosts;
975
 
if ($Config{'splithosts'} eq 1) {
 
863
if ($Config{'hostformat'} ne "none") { #8.0
976
864
   my $newlogfile;
977
865
   my @logarray;
978
866
   opendir (LOGDIR,$TempDir) || die "Cannot open dir";
979
867
   @logarray = readdir(LOGDIR);
980
868
   closedir (LOGDIR);
981
869
   my $ecpcmd = ("| $PerlVersion $BaseDir/scripts/shared/hostlist");
 
870
   #Note hostlist and hosthash [which is never used] exist to build list of host names seen
982
871
   foreach $newlogfile (@logarray) {
983
872
     my $eeefile = ("$TempDir" . "$newlogfile");
984
873
     if ((!(-d $eeefile)) && (!($eeefile =~ m/-archive/))) {
985
874
         system("$Config{'pathtocat'} $eeefile $ecpcmd") == 0
986
 
            or die "system $Config{'pathtocat'} $eeefile $ecpcmd failed: $?" 
 
875
            or die "system '$Config{'pathtocat'} $eeefile $ecpcmd' failed: $?" 
987
876
     }
988
877
   }
989
878
   #read in the final host list
991
880
   @hosts = <HOSTFILE>;
992
881
   close (HOSTFILE);
993
882
   chomp @hosts;
 
883
   #fixme check the sort?
994
884
   #@hosts = sort(@hosts);
995
885
}
996
886
 
1000
890
my $printing = '';
1001
891
my $emailopen = '';
1002
892
 
 
893
####################################################################
 
894
 
 
895
#Call Parse logs
 
896
if ($Config{'hostformat'} ne "none") {
 
897
   my $Host;
 
898
   foreach $Host (@hosts) {
 
899
      $printing = '';
 
900
      $ENV{'LOGWATCH_ONLY_HOSTNAME'} = $Host;
 
901
      $Config{'hostname'} = $Host; #resetting hostname here makes it appear in output header -mgt
 
902
      parselogs();
 
903
   } # ECP
 
904
} else {
 
905
   parselogs();
 
906
}
 
907
 
 
908
#Close Filehandle is needed -mgt
 
909
close(OUTFILE) unless ($Config{'output'} eq "stdout");
 
910
#############################################################################
 
911
 
 
912
exit(0);
 
913
 
 
914
#############################################################################
 
915
#END MAIN
 
916
#############################################################################
 
917
 
 
918
######################################################################
 
919
#sub getInt
 
920
#Notes: Called by CleanVars
 
921
######################################################################
 
922
sub getInt {
 
923
   my $word = shift;
 
924
   unless (defined($word)) { return $word; }
 
925
   my $tmpWord = lc $word;
 
926
   $tmpWord =~ s/\W//g;
 
927
   return $wordsToInts{$tmpWord} if (defined $wordsToInts{$tmpWord});
 
928
   unless ($word =~ s/^"(.*)"$/$1/) {
 
929
      return lc $word;
 
930
   }
 
931
   return $word;
 
932
}
 
933
              
 
934
######################################################################
 
935
#sub CleanVars 
 
936
#Notes: Called during #Load CONFIG, READ OPTIONS, make adjustments
 
937
######################################################################
 
938
sub CleanVars {
 
939
   foreach (keys %Config) {
 
940
      $Config{$_} = getInt($Config{$_});
 
941
   }
 
942
}
 
943
 
 
944
######################################################################
 
945
#sub PrintStdArray
 
946
#
 
947
######################################################################
 
948
sub PrintStdArray (@) {
 
949
   my @ThisArray = @_;
 
950
   my $i;
 
951
   for ($i=0;$i<=$#ThisArray;$i++) {
 
952
      print "[" . $i . "] = " . $ThisArray[$i] . "\n";
 
953
   }
 
954
}
 
955
 
 
956
######################################################################
 
957
#sub PrintConfig
 
958
#
 
959
######################################################################
 
960
sub PrintConfig () {
 
961
   # for debugging, print out config...
 
962
   foreach (keys %Config) {
 
963
      print $_ . ' -> ' . $Config{$_} . "\n";
 
964
   }
 
965
   print "Service List:\n";
 
966
   &PrintStdArray(@ServiceList);
 
967
   print "\n";
 
968
   print "LogFile List:\n";
 
969
   &PrintStdArray(@LogFileList);
 
970
   print "\n\n";
 
971
}
 
972
 
 
973
######################################################################
 
974
#sub PrintServiceData
 
975
#
 
976
######################################################################
 
977
# for debugging...
 
978
sub PrintServiceData () {
 
979
   my ($ThisKey1,$ThisKey2,$i);
 
980
   foreach $ThisKey1 (keys %ServiceData) {
 
981
      print "\nService Name: " . $ThisKey1 . "\n";
 
982
      foreach $ThisKey2 (keys %{$ServiceData{$ThisKey1}}) {
 
983
         next unless ($ThisKey2 =~ /^\d+-/);
 
984
         print "   $ThisKey2 = $ServiceData{$ThisKey1}{$ThisKey2}\n";
 
985
      }
 
986
      for ($i=0;$i<=$#{$ServiceData{$ThisKey1}{'logfiles'}};$i++) {
 
987
         print "   Logfile = " . $ServiceData{$ThisKey1}{'logfiles'}[$i] . "\n";
 
988
      }
 
989
   }
 
990
}
 
991
 
 
992
######################################################################
 
993
#sub PrintLogFileData
 
994
#
 
995
######################################################################
 
996
# for debugging...
 
997
sub PrintLogFileData () {
 
998
   my ($ThisKey1,$ThisKey2,$i);
 
999
   foreach $ThisKey1 (keys %LogFileData) {
 
1000
      print "\nLogfile Name: " . $ThisKey1 . "\n";
 
1001
      foreach $ThisKey2 (keys %{$LogFileData{$ThisKey1}}) {
 
1002
         next unless ($ThisKey2 =~ /^\d+-/);
 
1003
         print "   $ThisKey2 = $LogFileData{$ThisKey1}{$ThisKey2}\n";
 
1004
      }
 
1005
      for ($i=0;$i<=$#{$LogFileData{$ThisKey1}{'logfiles'}};$i++) {
 
1006
         print "   Logfile = " . $LogFileData{$ThisKey1}{'logfiles'}[$i] . "\n";
 
1007
      }
 
1008
      for ($i=0;$i<=$#{$LogFileData{$ThisKey1}{'archives'}};$i++) {
 
1009
         print "   Archive = " . $LogFileData{$ThisKey1}{'archives'}[$i] . "\n";
 
1010
      }
 
1011
      if ($LogFileData{$ThisKey1}{'nohostfilter'}) {
 
1012
         print "   NoHostFilter = " . $LogFileData{$ThisKey1}{'nohostfilter'} . "\n";
 
1013
      }
 
1014
   }
 
1015
}
 
1016
 
 
1017
######################################################################
 
1018
#sub ReadConfigFile
 
1019
#
 
1020
######################################################################
 
1021
sub ReadConfigFile {
 
1022
   my $FileName = $_[0];
 
1023
   my $Prefix = $_[1];
 
1024
 
 
1025
   if ( ! -f $FileName ) {
 
1026
      return(0);
 
1027
   }
 
1028
 
 
1029
   if ($Config{'debug'} > 5) {
 
1030
      print "ReadConfigFile: Opening " . $FileName . "\n";
 
1031
   }
 
1032
   open (READCONFFILE, $FileName) or die "Cannot open file $FileName: $!\n";
 
1033
   my $line;
 
1034
   while ($line = <READCONFFILE>) {
 
1035
      if ($Config{'debug'} > 9) {
 
1036
         print "ReadConfigFile: Read Line: " . $line;
 
1037
      }
 
1038
      $line =~ s/\#.*\\\s*$/\\/;
 
1039
      $line =~ s/\#.*$//;
 
1040
      next if ($line =~ /^\s*$/);
 
1041
 
 
1042
      if ($Prefix) {
 
1043
         next if ($line !~ m/\Q$Prefix:\E/);
 
1044
         $line =~ s/\Q$Prefix:\E//;
 
1045
      }
 
1046
 
 
1047
      if ($line =~ s/\\\s*$//) {
 
1048
          $line .= <READCONFFILE>;
 
1049
          redo unless eof(READCONFFILE);
 
1050
      }
 
1051
 
 
1052
      my ($name, $value) = split /=/, $line, 2;
 
1053
      $name =~ s/^\s+//; $name =~ s/\s+$//;
 
1054
      if ($value) { $value =~ s/^\s+//; $value =~ s/\s+$//; }
 
1055
      else { $value = ''; }
 
1056
 
 
1057
      push @ReadConfigNames, lc $name;
 
1058
      push @ReadConfigValues, getInt $value;
 
1059
      if ($Config{'debug'} > 7) {
 
1060
         print "ReadConfigFile: Name=" . $name . ", Value=" . $value . "\n";
 
1061
      }
 
1062
   }
 
1063
   close READCONFFILE;
 
1064
}
 
1065
 
 
1066
#########################################################################
 
1067
#sub Usage
 
1068
#
 
1069
#########################################################################
 
1070
sub Usage () {
 
1071
   # Show usage for this program
 
1072
   print "\nUsage: $0 [--detail <level>] [--logfile <name>] [--output <output_type>]\n" . 
 
1073
      "   [--format <format_type>] [--encode <enconding>] [--numeric]\n" .
 
1074
      "   [--mailto <addr>] [--archives] [--range <range>] [--debug <level>]\n" .
 
1075
      "   [--filename <filename>] [--help|--usage] [--version] [--service <name>]\n" .
 
1076
      "   [--hostformat <host_format type>] [--hostlimit <host1,host2>] [--html_wrap <num_characters>]\n\n";
 
1077
   print "--detail <level>: Report Detail Level - High, Med, Low or any #.\n";
 
1078
   print "--logfile <name>: *Name of a logfile definition to report on.\n";
 
1079
   print "--logdir <name>: Name of default directory where logs are stored.\n";
 
1080
   print "--service <name>: *Name of a service definition to report on.\n";
 
1081
   print "--output <output type>: Report Output - stdout [default], mail, file.\n"; #8.0
 
1082
   print "--format <formatting>: Report Format - text [default], html.\n"; #8.0
 
1083
   print "--encode <encoding>: Enconding to use - none [default], base64.\n"; #8.0
 
1084
   print "--mailto <addr>: Mail report to <addr>.\n";
 
1085
   print "--archives: Use archived log files too.\n";
 
1086
   print "--filename <filename>: Used to specify they filename to save to. --filename <filename> [Forces output to file].\n";
 
1087
   print "--range <range>: Date range: Yesterday, Today, All, Help\n";
 
1088
   print "                             where help will describe additional options\n";
 
1089
   print "--numeric: Display addresses numerically rather than symbolically and numerically\n";
 
1090
   print "           (saves  a  nameserver address-to-name lookup).\n";
 
1091
   print "--debug <level>: Debug Level - High, Med, Low or any #.\n";
 
1092
   print "--hostformat: Host Based Report Options - none [default], split, splitmail.\n"; #8.0
 
1093
   print "--hostlimit: Limit report to hostname - host1,host2.\n"; #8.0
 
1094
   print "--html_wrap <num_characters>: Default is 80.\n";
 
1095
   print "--version: Displays current version.\n";
 
1096
   print "--help: This message.\n";
 
1097
   print "--usage: Same as --help.\n";
 
1098
   print "* = Switch can be specified multiple times...\n\n";
 
1099
   exit (99);
 
1100
}
 
1101
############################################################################
 
1102
#END sub Usage
 
1103
#############################################################################
 
1104
 
 
1105
#############################################################################
 
1106
#sub initprint
 
1107
#
 
1108
#############################################################################
1003
1109
sub initprint {
1004
1110
   return if $printing;
1005
1111
 
1007
1113
   $OStitle = $OSname;
1008
1114
   $OStitle = "Solaris" if ($OSname eq "SunOS" && $release >= 2);
1009
1115
 
1010
 
   if ($Config{'print'} eq 1) {
 
1116
   if ($Config{'output'} eq "stdout") { #8.0 start with others?
1011
1117
      *OUTFILE = *STDOUT;
1012
 
   } elsif ($Config{'save'} ne "") {
1013
 
      open(OUTFILE,">>" . $Config{'save'}) or die "Can't open output file: $Config{'save'} $!\n";
 
1118
   } elsif ($Config{'output'} eq "file") {
 
1119
      open(OUTFILE,">>" . $Config{'filename'}) or die "Can't open output file: $Config{'filename'} $!\n";
1014
1120
   } else {
1015
 
      if (($Config{'multiemail'} eq 1) || ($emailopen eq "")) {
 
1121
   #fixme mailto 
 
1122
      if (($Config{'hostformat'} eq "splitmail") || ($emailopen eq "")) {
1016
1123
         #Use mailer = in logwatch.conf to set options. Default should be "sendmail -t"
1017
1124
         #In theory this should be able to handle many different mailers. I might need to add
1018
1125
         #some filter code on $Config{'mailer'} to make it more robust. -mgt
1023
1130
            print OUTFILE "To: $to\n";
1024
1131
         }
1025
1132
         print OUTFILE "From: $Config{'mailfrom'}\n";
1026
 
         print OUTFILE "Subject: Logwatch for $Config{'hostname'} (${OStitle})\n";
 
1133
         #If $Config{'subject'} exists lets use it.
 
1134
         #This does not allow for variable expansion as the default below does -mgt
 
1135
         if ($Config{'subject'}) {
 
1136
            print OUTFILE "Subject: $Config{'subject'}\n";
 
1137
         } else {
 
1138
            print OUTFILE "Subject: Logwatch for $Config{'hostname'} (${OStitle})\n";
 
1139
         }
1027
1140
         #Add MIME
1028
1141
         $out_mime = "MIME-Version: 1.0\n"; 
1029
 
         if ( $Config{encode} == 1 ) {
 
1142
         #Config{encode} switch
 
1143
         if ( $Config{'encode'} eq "base64" ) {
1030
1144
            $out_mime .= "Content-transfer-encoding: base64\n";
1031
1145
         } else {
1032
1146
            $out_mime .= "Content-Transfer-Encoding: 7bit\n";
1033
1147
         }
1034
 
         if ( $outtype_html ) {
 
1148
         #Config{output} html
 
1149
         if ( $Config{'format'} eq "html" ) {
1035
1150
            $out_mime .= "Content-Type: text/html; charset=\"iso-8859-1\"\n\n";
1036
1151
         } else {
1037
1152
            $out_mime .= "Content-Type: text/plain; charset=\"iso-8859-1\"\n\n";
1038
1153
         }
1039
1154
 
1040
 
         if (($Config{'splithosts'} eq 1) && ($Config{'multiemail'} eq 0)) {
 
1155
         if ($Config{'hostformat'} eq "split") { #8.0 check hostlimit also? or ne none?
1041
1156
            print OUTFILE "Reporting on hosts: @hosts\n";
1042
1157
         }
1043
1158
         $emailopen = 'y';
1044
 
      } #End if multiemail || emailopen 
 
1159
      } #End if hostformat || emailopen 
1045
1160
   } #End if printing/save/else
1046
1161
   $printing = 'y';
1047
1162
 
1048
1163
   # simple parse of the dates
1049
 
   my $simple_timematch = TimeFilter(" %Y-%b-%d %Hh %Mm %Ss ");
 
1164
   my $simple_timematch = &TimeFilter(" %Y-%b-%d %Hh %Mm %Ss ");
1050
1165
   my @simple_range = split(/\|/, $simple_timematch);
1051
1166
   if ($#simple_range > 1) {
1052
1167
       # delete all array entries, except first and last
1063
1178
   my $print_range = join("/",@simple_range);
1064
1179
 
1065
1180
   $index_par++;
1066
 
   if ( $outtype_html ) {
1067
 
      output( $index_par, "LOGWATCH Summary" . (($Config{'splithosts'} eq 1) ? ": $Config{'hostname'}" : ""), "start");
1068
 
      output( $index_par, "\n <h2><font color=\"blue\"> Logwatch $Version ($VDate)</font></h2>\n", "header");
 
1181
   if ( $Config{'format'} eq "html" ) {
 
1182
      &output( $index_par, "LOGWATCH Summary" . (($Config{'hostformat'} ne "none") ? ": $Config{'hostname'}" : ""), "start");
 
1183
      &output( $index_par, "       Logwatch Version: $Version ($VDate)\n", "line"); 
1069
1184
   }       else {
1070
 
      output( $index_par, "\n ################### Logwatch $Version ($VDate) #################### \n", "line");
 
1185
      &output( $index_par, "\n ################### Logwatch $Version ($VDate) #################### \n", "line");
1071
1186
   }
1072
1187
 
1073
 
   output( $index_par, "       Processing Initiated: " . localtime(time) . "\n", "line");
1074
 
   output( $index_par, "       Date Range Processed: $Config{'range'}\n", "line");
1075
 
   output( $index_par, "                             $print_range\n", "line") if ($Config{'range'} ne 'all');
1076
 
   output( $index_par, "                             Period is " . GetPeriod() . ".\n", "line")
1077
 
      if ($Config{'range'} ne 'all');
1078
 
   output( $index_par, "     Detail Level of Output: $Config{'detail'}\n", "line");
1079
 
   output( $index_par, "             Type of Output: $Config{'output'}\n", "line");
1080
 
   output( $index_par, "          Logfiles for Host: $Config{'hostname'}\n", "line");
 
1188
   &output( $index_par, "       Processing Initiated: " . localtime(time) . "\n", "line");
 
1189
   &output( $index_par, "       Date Range Processed: $Config{'range'}\n", "line");
 
1190
   &output( $index_par, "                             $print_range\n", "line")
 
1191
      if ($Config{'range'} ne 'all');
 
1192
   &output( $index_par, "                             Period is " . &GetPeriod() . ".\n", "line")
 
1193
      if ($Config{'range'} ne 'all');
 
1194
   &output( $index_par, "       Detail Level of Output: $Config{'detail'}\n", "line");
 
1195
   &output( $index_par, "       Type of Output/Format: $Config{'output'} / $Config{'format'}\n", "line");
 
1196
   &output( $index_par, "       Logfiles for Host: $Config{'hostname'}\n", "line");
1081
1197
 
1082
 
   if ( $outtype_html ) {
1083
 
      output( $index_par, "\n", "stop");
 
1198
   if ( $Config{'format'} eq "html" ) {
 
1199
      &output( $index_par, "\n", "stop");
1084
1200
   } else {
1085
 
      output( $index_par, " ################################################################## \n", "line");
 
1201
      &output( $index_par, " ################################################################## \n", "line");
1086
1202
   }
1087
1203
 
1088
1204
}
 
1205
####################################################################
 
1206
#END sub initprint
 
1207
####################################################################
1089
1208
 
 
1209
###################################################################
 
1210
#sub parselogs
 
1211
#
 
1212
###################################################################
1090
1213
sub parselogs {
1091
1214
   my $Service;
1092
1215
 
1144
1267
            }
1145
1268
         }
1146
1269
      }
1147
 
# ECP - insert the host stripping now
 
1270
      # ECP - insert the host stripping now
1148
1271
      my $HostStrip = " ";
1149
 
      if ($Config{'splithosts'} eq 1) {
1150
 
         $HostStrip .= ("$PerlVersion $BaseDir/scripts/shared/onlyhost");
 
1272
      if ($Config{'hostformat'} ne "none") { #8.0
 
1273
         ###############################################
 
1274
         # onlyhost reads $ENV{'LOGWATCH_ONLY_HOSTNAME'} and uses it to try and match
 
1275
         # based on $line =~ m/^... .. ..:..:.. $hostname\b/io
 
1276
         ###############################################
 
1277
         $HostStrip = "$PerlVersion $BaseDir/scripts/shared/onlyhost";
1151
1278
      }
1152
1279
      my $ServiceExec = "$BaseDir/scripts/services/$Service";
1153
1280
      if (-f "$ConfigDir/scripts/services/$Service") {
1201
1328
                  next LINE;
1202
1329
                  }
1203
1330
            }
1204
 
            initprint();
 
1331
            &initprint();
1205
1332
            if (($has_output == 0) and ($ServiceData{$Service}{'title'})) {
1206
1333
               $index_par++;
1207
 
               output($index_par, $ServiceData{$Service}{'title'}, "start" );
 
1334
               &output($index_par, $ServiceData{$Service}{'title'}, "start" );
1208
1335
               my $BeginVar;
1209
1336
               if ($ENV{'LOGWATCH_GLOBAL_DETAIL'} == $ENV{'LOGWATCH_DETAIL_LEVEL'}) {
1210
1337
                  $BeginVar = "Begin";
1211
1338
               } else {
1212
1339
                  $BeginVar = "Begin (detail=" . $ENV{'LOGWATCH_DETAIL_LEVEL'} . ")";
1213
1340
               }
1214
 
               if ( $outtype_html ) {
 
1341
               if ( $Config{'format'} eq "html" ) {
1215
1342
               #BODY <!-- SERVICE START -->
1216
 
                   output( $index_par, "\n <h3><font color=\"blue\">$ServiceData{$Service}{'title'} $BeginVar </font></h3>\n", "header");
 
1343
                   #&output( $index_par, "\n <h2>$ServiceData{$Service}{'title'}</h2>\n", "header");
1217
1344
               } else {
1218
 
                   output( $index_par, "\n --------------------- $ServiceData{$Service}{'title'} $BeginVar ------------------------ \n\n", "line");
 
1345
                   &output( $index_par, "\n --------------------- $ServiceData{$Service}{'title'} $BeginVar ------------------------ \n\n", "line");
1219
1346
               }
1220
1347
               $has_output = 1;
1221
1348
            }
1222
 
            output( $index_par, $ThisLine, "line");
 
1349
            &output( $index_par, $ThisLine, "line");
1223
1350
         }
1224
1351
         close (TESTFILE);
1225
1352
 
1226
1353
         if ($has_output and $ServiceData{$Service}{'title'}) {
1227
 
            if ( $outtype_html ) {
1228
 
                if ($Ignored > 0) {  output( $index_par, "\n $Ignored Ignored Lines\n", "header"); };
1229
 
                output( $index_par,  "\n <h3><font color=\"blue\">$ServiceData{$Service}{'title'} End </font></h3>\n", "header");
 
1354
            if ( $Config{'format'} eq "html" ) {
 
1355
                if ($Ignored > 0) {  &output( $index_par, "\n $Ignored Ignored Lines\n", "header"); };
 
1356
                #&output( $index_par,  "\n <h3><font color=\"blue\">$ServiceData{$Service}{'title'} End </font></h3>\n", "header");
1230
1357
            } else {
1231
 
                if ($Ignored > 0) { output( $index_par, "\n $Ignored Ignored Lines\n", "line"); };
1232
 
                output( $index_par,  "\n ---------------------- $ServiceData{$Service}{'title'} End ------------------------- \n\n", "line");
 
1358
                if ($Ignored > 0) { &output( $index_par, "\n $Ignored Ignored Lines\n", "line"); };
 
1359
                &output( $index_par,  "\n ---------------------- $ServiceData{$Service}{'title'} End ------------------------- \n\n", "line");
1233
1360
            }
1234
 
            output( $index_par, "\n", "stop");
 
1361
            &output( $index_par, "\n", "stop");
1235
1362
         }
1236
1363
      }
1237
1364
   }
1238
1365
 
1239
1366
   #HTML should be external to logwatch.pl -mgt 
1240
1367
   #These are steps only needed for HTML output
1241
 
   if ( $outtype_html ) {
 
1368
   if ( $Config{'format'} eq "html" ) {
1242
1369
      #HEADER 
1243
1370
      #Setup temp Variables to swap
1244
1371
      my %HTML_var;
1266
1393
      } 
1267
1394
 
1268
1395
      #Set up out_reference
1269
 
      output("ul","<a name=top><ul>", "ref_extra") if defined( $index_par );
 
1396
      &output("ul","<a name=top><ul>", "ref_extra") if defined( $index_par );
1270
1397
      foreach ( 0 .. $index_par ) {
1271
 
         output($_,$reports[$_], "ref") if defined( $reports[$_] );
 
1398
         &output($_,$reports[$_], "ref") if defined( $reports[$_] );
1272
1399
      }
1273
 
      output("ul","</ul></a>", "ref_extra") if defined( $index_par );
 
1400
      &output("ul","</ul></a>", "ref_extra") if defined( $index_par );
1274
1401
 
1275
1402
   }
1276
1403
   
1277
 
   if ( $outtype_html ) {
 
1404
   if ( $Config{'format'} eq "html" ) {
1278
1405
      $index_par++;
1279
 
      output( $index_par,  "Logwatch End", "start" );
1280
 
      output( $index_par,  "\n <h3><font color=\"blue\">Logwatch ended at ".  localtime(time) ."</font></h3>\n", "header") if ($printing);
1281
 
      output( $index_par, "\n", "stop");
 
1406
      &output( $index_par,  "Logwatch Ended at " . localtime(time) , "start" );
 
1407
      &output( $index_par, "\n", "stop");
1282
1408
   } else {
1283
 
      output( $index_par, $report_finish, "line") if ($printing);
 
1409
      &output( $index_par, $report_finish, "line") if ($printing);
1284
1410
   }
1285
1411
 
1286
1412
#Printing starts here $out_mime $out_head $out_reference $out_body $out_foot  
1287
1413
   print OUTFILE $out_mime if $out_mime;
1288
 
   if ( $Config{'encode'} == 1 ) {
 
1414
   if ( $Config{'encode'} eq "base64" ) {
1289
1415
      print OUTFILE encode_base64($out_head) if $out_head;
1290
1416
      print OUTFILE encode_base64($out_reference) if $out_reference;
1291
1417
      foreach ( 0 .. $index_par ) {
1292
1418
         print OUTFILE encode_base64($out_body{$_}) if defined( $out_body{$_} );
 
1419
#fixme
1293
1420
         $out_body{$_} = ''; #We should track this down out_body could be an array instead also -mgt
1294
1421
      }
1295
1422
      print OUTFILE encode_base64($out_foot) if $out_foot;
1304
1431
   }
1305
1432
#ends here
1306
1433
 
1307
 
   if ($Config{'multiemail'} eq 1) {
 
1434
   if ($Config{'hostformat'} eq "splitmail") { #8.0
1308
1435
      $out_foot = '';
1309
1436
      $out_head = '';
1310
1437
      $out_mime = '';
1311
1438
      $out_reference = '';
1312
1439
      @reports = ();
1313
 
      close(OUTFILE) unless ($Config{'print'} eq 1);
1314
 
   }
1315
 
}
1316
 
 
1317
 
if ($Config{'splithosts'} eq 1) {
1318
 
   my $Host;
1319
 
   foreach $Host (@hosts) {
1320
 
      $printing = '';
1321
 
      $ENV{'LOGWATCH_ONLY_HOSTNAME'} = $Host;
1322
 
      $ENV{'LOGWATCH_ONLY_HOSTNAME'} =~ s/\..*//;
1323
 
      $Config{'hostname'} = $Host;
1324
 
      parselogs();
1325
 
   } # ECP
1326
 
} else {
1327
 
   parselogs();
1328
 
}
1329
 
close(OUTFILE) unless ($Config{'print'} eq 1);
1330
 
#############################################################################
1331
 
 
1332
 
exit(0);
1333
 
 
 
1440
      close(OUTFILE) unless ($Config{'output'} eq "stdout"); #fixme should never be true -mgt
 
1441
   } 
 
1442
}
 
1443
#############################################################################
 
1444
#END parselogs
 
1445
#############################################################################
 
1446
 
 
1447
#############################################################################
 
1448
#sub output
 
1449
#
 
1450
#############################################################################
1334
1451
sub output {
1335
1452
   my ($index, $text, $type) = @_;
1336
1453
   #Types are start stop header line ref
1346
1463
   if ( $type eq "start" ) {
1347
1464
      $reports[$index] = "$text";
1348
1465
   #SERVICE table headers if ( $index eq 'E' ) { #never happens change out_body from hash back to array
1349
 
      if ( $outtype_html ) {
1350
 
         $out_body{$index} .= " <hr>
1351
 
         <h2><a name=\"$index\">$reports[$index]</a></h2>
1352
 
         <div class=service>
1353
 
         <table border=1 width=100%>\n";
 
1466
      if ( $Config{'format'} eq "html" ) {
 
1467
         $out_body{$index} .=
 
1468
         "<div class=service>
 
1469
         <table border=1 width=100%>
 
1470
           <tr><th>
 
1471
           <h2><a name=\"$index\">$reports[$index]</a></h2>
 
1472
           </tr></th>\n";
1354
1473
      }
1355
1474
   } 
1356
1475
 
1357
1476
   if ( $type eq "stop" ) {
1358
 
      if ( $outtype_html ) {
 
1477
      if ( $Config{'format'} eq "html" ) {
1359
1478
         $out_body{$index} .= "  </table></div>\n";
1360
1479
         $out_body{$index} .= "  <div class=return_link><p><a href=\"#top\">Back to Top</a></p></div>\n";
1361
1480
      }
1362
1481
   }
1363
1482
 
1364
1483
   if ( $type eq "header" ) {
1365
 
           if ($outtype_unformatted) {
 
1484
           if ( $Config{'format'} eq "text" ) {
1366
1485
              $out_body{$index} .= "$text \n";
1367
 
      } elsif ( $outtype_html ) {
 
1486
      } elsif ( $Config{'format'} eq "html" ) {
1368
1487
         #Covert spaces
1369
1488
         $text =~ s/  / \&nbsp;/go;
1370
1489
         #Covert tabs 1 to 4 ratio
1375
1494
         #This will make sure no unbroken string is longer then x characters
1376
1495
         $text =~ s/(\S{$Config{html_wrap}})/$1 /g;
1377
1496
         $out_body{$index} .= "<tr>\n    <th>$text</th>\n   </tr>\n";
1378
 
      } else {
 
1497
      } else { #fixme what is this formatted?
1379
1498
         $out_body{$index} .=
1380
1499
         sprintf( substr( $text, 0, $format[0] ) . ' ' x( $format[0] - length($text) ) . " \n" );
1381
1500
      }
1382
1501
   }
1383
1502
 
1384
1503
   if ( $type eq "line" ) {
1385
 
           if ($outtype_unformatted or $outtype_mail) {
 
1504
           if ( $Config{'format'} eq "text" ) {
1386
1505
         $out_body{$index} .= "$text ";
1387
 
      } elsif ( $outtype_html ) {
 
1506
      } elsif ( $Config{'format'} eq "html" ) {
1388
1507
         #Covert spaces
1389
1508
         $text =~ s/  / \&nbsp;/go;
1390
1509
         #Covert tabs 1 to 4 ratio
1401
1520
         } else {
1402
1521
            $out_body{$index} .= "  <tr>\n    <th align=left>$text</th>\n  </tr>\n";
1403
1522
         }
1404
 
      } else {
 
1523
      } else { #fixme formatted?
1405
1524
              if ( length($text) > $format[0] ) {
1406
1525
                 $out_body{$index} .=
1407
1526
                      sprintf( $text . "\n" . ' ' x $format[0] . ' ' );
1413
1532
   }
1414
1533
 
1415
1534
}
1416
 
 
 
1535
###########################################################################
 
1536
#END sub output
 
1537
###########################################################################
1417
1538
# vi: shiftwidth=3 tabstop=3 et