comparison common/logwatch/scripts_php @ 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 bafd91f195de
comparison
equal deleted inserted replaced
-1:000000000000 0:956e484adc12
1 #!/usr/bin/perl -w
2 #
3 # $Id$
4 #
5 # Logwatch service for php error logs
6 # To be placed in
7 # /etc/logwatch/scripts/php
8 #
9 # Processes all messages and summarizes them
10 # Each message is given with a timestamp and RMS
11 #
12 # (C) 2006 by Jeremias Reith <jr@terragate.net>
13 # Modified 2009 by Michael Baierl
14
15
16 use strict;
17 use Logwatch ':dates';
18 use Time::Local;
19 use POSIX qw(strftime);
20
21 # [Sun Dec 14 00:27:22 2008]
22 #my $date_format = '%d-%b-%Y %H:%M:%S';
23 my $date_format = '... %b %d %H:%M:%S %Y';
24 my $filter = TimeFilter($date_format);
25 my $detail = exists $ENV{'LOGWATCH_DETAIL_LEVEL'} ? $ENV{'LOGWATCH_DETAIL_LEVEL'} : 0;
26
27 # we do not use any Date:: package (or strptime) as they are probably not available
28 my %month2num = ( Jan => 0, Feb => 1, Mar => 2, Apr => 3,
29 May => 4, Jun => 5, Jul => 6, Aug => 7,
30 Sep => 8, Oct => 9, Nov => 10, Dec => 11 );
31
32 # array of message categories (we do not use a hash to keep the order)
33 # first element: catorory name
34 # second element: matching regexp ($1 should contain the message)
35 # third element: anonymous hash ref (stores message counts)
36 my @message_categories = (['Fatal errors', qr/\] PHP Fatal error: (.*)$/o, {}],
37 ['Warnings', qr/\] PHP Warning: (.*)$/o, {}],
38 ['Notices', qr/\] PHP Notice: (.*)$/o, {}]);
39
40 # skipping categories depending on detail level
41 pop(@message_categories) if $detail < 10;
42 pop(@message_categories) if $detail < 5;
43
44 # counting messages
45 while(<>) {
46 my $line = $_;
47 # skipping messages that are not within the requested range
48 next unless $line =~ /^\[($filter)\]/o;
49 $1 =~ /(\w+) (\w+) (\d+) (\d+):(\d+):(\d+) (\d+)/;
50 my $time;
51
52 {
53 # timelocal is quite chatty
54 local $SIG{'__WARN__'} = sub {};
55 $time = timelocal($6, $5, $4, $3, $month2num{$2}, $7-1900);
56 }
57
58 foreach my $cur_cat (@message_categories) {
59 if($line =~ /$cur_cat->[1]/) {
60 my $msgs = $cur_cat->[2];
61 $msgs->{$1} = {count => '0',
62 first_occurrence => $time,
63 sum => 0,
64 sqrsum => 0} unless exists $msgs->{$1};
65 $msgs->{$1}->{'count'}++;
66 # summing up timestamps and squares of timestamps
67 # in order to calculate the rms
68 # using first occurrence of message as offset in calculation to
69 # prevent an integer overflow
70 $msgs->{$1}->{'sum'} += $time - $msgs->{$1}->{'first_occurrence'};
71 $msgs->{$1}->{'sqrsum'} += ($time - $msgs->{$1}->{'first_occurrence'}) ** 2;
72 last;
73 }
74 }
75 }
76
77
78 # generating summary
79 foreach my $cur_cat (@message_categories) {
80 # skipping non-requested message types
81 next unless keys %{$cur_cat->[2]};
82 my ($name, undef, $msgs) = @{$cur_cat};
83 print $name, ":\n";
84 my $last_count = 0;
85
86 # sorting messages by count
87 my @sorted_msgs = sort { $msgs->{$b}->{'count'} <=> $msgs->{$a}->{'count'} } keys %{$msgs};
88
89 foreach my $msg (@sorted_msgs) {
90 # grouping messages by number of occurrence
91 print "\n", $msgs->{$msg}->{'count'}, " times:\n" unless $last_count == $msgs->{$msg}->{'count'};
92 my $rms = 0;
93
94
95 # printing timestamp
96 print '[';
97
98 if($msgs->{$msg}->{'count'} > 1) {
99 # calculating rms
100 $rms = int(sqrt(
101 ($msgs->{$msg}->{'count'} *
102 $msgs->{$msg}->{'sqrsum'} -
103 $msgs->{$msg}->{'sum'}) /
104 ($msgs->{$msg}->{'count'} *
105 ($msgs->{$msg}->{'count'} - 1))));
106
107 print strftime($date_format, localtime($msgs->{$msg}->{'first_occurrence'}+int($rms/2)));
108
109 print ' +/-';
110
111 # printing rms
112 if($rms > 86400) {
113 print int($rms/86400) , ' day(s)';
114 } elsif($rms > 3600) {
115 print int($rms/3600) , ' hour(s)';
116 } elsif($rms > 60) {
117 print int($rms/60) , ' minute(s)';
118 } else {
119 print $rms, ' seconds';
120 }
121 } else {
122 # we have got this message a single time
123 print strftime($date_format, localtime($msgs->{$msg}->{'first_occurrence'}));
124 }
125
126 print '] ', $msg, "\n";
127 $last_count = $msgs->{$msg}->{'count'};
128 }
129
130 print "\n";
131 }
132