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