comparison common/logwatch/http-error @ 0:956e484adc12

Initial public release of Puppet configs
author IBBoard <dev@ibboard.co.uk>
date Sat, 16 Aug 2014 19:47:38 +0000
parents
children 8316d4e55e92
comparison
equal deleted inserted replaced
-1:000000000000 0:956e484adc12
1 #!/usr/bin/perl -w
2 #
3 # $Id$
4 #
5 # Logwatch service for http error logs
6 # To be placed in
7 # /etc/logwatch/scripts/http-error
8 #
9 # Processes all messages and summarizes them
10 # Each message is given with a timestamp and RMS
11 #
12 ########################################################
13 ##(C) 2006 by Jeremias Reith <jr@terragate.net>
14 ## Modified 2009 by Michael Baierl
15 ## Covered under the included MIT/X-Consortium License:
16 ## http://www.opensource.org/licenses/mit-license.php
17 ## All modifications and contributions by other persons to
18 ## this script are assumed to have been donated to the
19 ## Logwatch project and thus assume the above copyright
20 ## and licensing terms. If you want to make contributions
21 ## under your own copyright or a different license this
22 ## must be explicitly stated in the contribution an the
23 ## Logwatch project reserves the right to not accept such
24 ## contributions. If you have made significant
25 ## contributions to this script and want to claim
26 ## copyright please contact logwatch-devel@lists.sourceforge.net.
27 #########################################################
28
29 use strict;
30 use Logwatch ':dates';
31 use Time::Local;
32 use POSIX qw(strftime);
33
34 my $date_format = '... %b %d %H:%M:%S %Y';
35 my $filter = TimeFilter($date_format);
36 my $detail = exists $ENV{'LOGWATCH_DETAIL_LEVEL'} ? $ENV{'LOGWATCH_DETAIL_LEVEL'} : 0;
37
38 # we do not use any Date:: package (or strptime) as they are probably not available
39 my %month2num = ( Jan => 0, Feb => 1, Mar => 2, Apr => 3,
40 May => 4, Jun => 5, Jul => 6, Aug => 7,
41 Sep => 8, Oct => 9, Nov => 10, Dec => 11 );
42
43 # array of message categories (we do not use a hash to keep the order)
44 # first element: catorory name
45 # second element: matching regexp ($1 should contain the message)
46 # third element: anonymous hash ref (stores message counts)
47 my @message_categories = (['Errors', qr/\[error\] (.*)$/o, {}],
48 ['Warnings', qr/\[warn\] (.*)$/o, {}],
49 ['Notices', qr/\[info\] (.*)$/o, {}]);
50
51 # skipping categories depending on detail level
52 pop(@message_categories) if $detail < 10;
53 pop(@message_categories) if $detail < 5;
54
55 # counting messages
56 while(<>) {
57 my $line = $_;
58 # skipping messages that are not within the requested range
59 next unless $line =~ /^\[($filter)\]/o;
60 # skip PHP messages (have a separate script)
61 next if $line =~ / PHP (Warning|Fatal error|Notice):/o;
62 # skip ModSecurity messages
63 next if $line =~ / ModSecurity:/o;
64 $1 =~ /(\w+) (\w+) (\d+) (\d+):(\d+):(\d+) (\d+)/;
65 my $time;
66
67 {
68 # timelocal is quite chatty
69 local $SIG{'__WARN__'} = sub {};
70 $time = timelocal($6, $5, $4, $3, $month2num{$2}, $7-1900);
71 }
72
73 foreach my $cur_cat (@message_categories) {
74 if($line =~ /$cur_cat->[1]/) {
75 my $msgs = $cur_cat->[2];
76 $msgs->{$1} = {count => '0',
77 first_occurrence => $time,
78 sum => 0,
79 sqrsum => 0} unless exists $msgs->{$1};
80 $msgs->{$1}->{'count'}++;
81 # summing up timestamps and squares of timestamps
82 # in order to calculate the rms
83 # using first occurrence of message as offset in calculation to
84 # prevent an integer overflow
85 $msgs->{$1}->{'sum'} += $time - $msgs->{$1}->{'first_occurrence'};
86 $msgs->{$1}->{'sqrsum'} += ($time - $msgs->{$1}->{'first_occurrence'}) ** 2;
87 last;
88 }
89 }
90 }
91
92
93 # generating summary
94 foreach my $cur_cat (@message_categories) {
95 # skipping non-requested message types
96 next unless keys %{$cur_cat->[2]};
97 my ($name, undef, $msgs) = @{$cur_cat};
98 print $name, ":\n";
99 my $last_count = 0;
100
101 # sorting messages by count
102 my @sorted_msgs = sort { $msgs->{$b}->{'count'} <=> $msgs->{$a}->{'count'} } keys %{$msgs};
103
104 foreach my $msg (@sorted_msgs) {
105 # grouping messages by number of occurrence
106 print "\n", $msgs->{$msg}->{'count'}, " times:\n" unless $last_count == $msgs->{$msg}->{'count'};
107 my $rms = 0;
108
109
110 # printing timestamp
111 print '[';
112
113 if($msgs->{$msg}->{'count'} > 1) {
114 # calculating rms
115 $rms = int(sqrt(
116 ($msgs->{$msg}->{'count'} *
117 $msgs->{$msg}->{'sqrsum'} -
118 $msgs->{$msg}->{'sum'}) /
119 ($msgs->{$msg}->{'count'} *
120 ($msgs->{$msg}->{'count'} - 1))));
121
122 print strftime($date_format, localtime($msgs->{$msg}->{'first_occurrence'}+int($rms/2)));
123
124 print ' +/-';
125
126 # printing rms
127 if($rms > 86400) {
128 print int($rms/86400) , ' day(s)';
129 } elsif($rms > 3600) {
130 print int($rms/3600) , ' hour(s)';
131 } elsif($rms > 60) {
132 print int($rms/60) , ' minute(s)';
133 } else {
134 print $rms, ' seconds';
135 }
136 } else {
137 # we have got this message a single time
138 print strftime($date_format, localtime($msgs->{$msg}->{'first_occurrence'}));
139 }
140
141 print '] ', $msg, "\n";
142 $last_count = $msgs->{$msg}->{'count'};
143 }
144
145 print "\n";
146 }
147