watch_logs
This is mostly a swatch (http://swatch.sourceforge.net/) generated file modified to handle what we're up to. It uses an internal 'open' of "tail -f" on the log file and runs as a background process. Rudimentary signal handling allow it to be 'HUP'-ed and restart it's file handle for the 'tail' There's a newer module that does this in pure Perl, I think. You may want to look at that. Uses sendmail -t to send out email.
#!/usr/bin/perl -w
#$Header: watch_messages.pl, v0.1 afb/04/05
#$!watch_messages.pl - watch messages log for drive failures
#
# stolen from swatch, w/ the libraries removed
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
use strict;
$SIG{'HUP'} = 'restartmeup';
$SIG{'TERM'} = 'goodbye';
$SIG{'CHLD'} = 'IGNORE';
## Constants
# use your own email/pagers
my $admin_team = "admin_team";
my $pagers = "page_all";
my $delay_between_pages = 120;
(my $Me = $0) =~ s%.*/%%;
my $BELL = "^G";
my $MAILER = "/usr/lib/sendmail -oi -t";
# subject for emails
my $subject = "swatch messages memory trouble!";
my $WRITE = "/usr/bin/write";
$| = 1;
sub restartmeup {
close TAIL;
print "Got a hup\n";
exec($0, @ARGV);
exit(0);
}
sub goodbye {
exit(0);
}
#
#
print "\n*** watch_messages version 0.1 (pid:$$) started at "
. `/bin/date` . "\n";
my $filename = shift || '/var/log/messages';
if (not open(TAIL, "/usr/bin/tail -f $filename|")) {
die "$0: cannot read run \"/usr/bin/tail -f $filename\": $!\n";
}
sub send_email {
my $login = (getpwuid($<))[0];
my %args = (
'ADDRESSES' => $login,
'SUBJECT' => 'Message from Swatch',
'MAILER' => '/usr/lib/sendmail -oi -t',
@_
);
(my $to_line = $args{'ADDRESSES'}) =~ s/:/,/g;
open(MAIL_PIPE, "| $args{'MAILER'}")
or (warn "$0: cannot open pipe to $args{MAILER}: $!\n" and return);
print MAIL_PIPE <<"EOF";
To: $to_line
Subject: $args{SUBJECT}
$args{'MESSAGE'}
EOF
close(MAIL_PIPE);
}
sub echo {
my %args = (
@_
);
my $normal = 1;
print "$args{'MESSAGE'}\n";
}
my $mail_it = 1;
my $page_it = 1;
my $echo_it = 0;
my $debug = 0;
my $in_meminfo_msg = "";
my $notified_time = '';
LOOP: while (<TAIL>) {
chomp;
my $S_ = $_;
@_ = split;
### quote all special shell chars ###
$S_ =~ s/([;&\(\)\|\^><\$`'\\])/\\$1/g;
my @S_ = split(/\s+/, $S_);
my $local_time = time;
# Apr 30 10:22:14 wiwbei unix: WARNING: Compaq Smart Array 4200 Controller
# if (/Compaq Smart Array 4200 Controller/)
# Apr 30 15:45:08 wiwbleo kernel: Mem-info:
# Apr 30 15:45:08 wiwbleo kernel: Zone:DMA freepages: 2880 min: 0 low: 0 high: 0
# Apr 30 15:45:08 wiwbleo kernel: Zone:Normal freepages: 1251 min: 1278 low: 4543 high: 6303
# Apr 30 15:45:08 wiwbleo kernel: Zone:HighMem freepages: 254 min: 255 low: 4606 high: 6909
# Apr 29 01:31:38 wiwbei unix: WARNING: Compaq Smart Array 4200 Controller
# Apr 29 01:31:38 wiwbei unix: Event Occured on ......... 04/29/2004
# Apr 29 01:31:38 wiwbei unix: Event Time................ 01:34:20
# Apr 29 01:31:38 wiwbei unix: Description...............
# Physical drive failure: SCSI port 2 ID 1
# Apr 29 01:31:38 wiwbei unix: Description............... State change,
# logical drive 1
if (/Mem-info/) {
print "Got: $_\n"
if $debug > 8;
$in_meminfo_msg = "$_\n";
echo('MESSAGE' => "$_", )
if $echo_it;
send_email('ADDRESSES' => "$admin_team",
'MESSAGE' => "$_",
'SUBJECT' => $subject,
MAILER => "$MAILER", )
if $mail_it;
# only page every 2 minutes
next if $notified_time + $delay_between_pages > $local_time;
send_email('ADDRESSES' => "$pagers",
'MESSAGE' => "$_",
'SUBJECT' => $subject,
MAILER => "$MAILER", )
if $page_it;
$notified_time = $local_time;
next;
}
# Apr 30 15:45:06 wiwbleo kernel: 10414 reserved pages
# Apr 30 15:45:06 wiwbleo kernel: 387065 pages shared
# Apr 30 15:45:06 wiwbleo kernel: 90 pages swap cached
# Apr 30 15:45:06 wiwbleo kernel: Out of Memory: Killed process 23209 (hpsmhd).
if ($in_meminfo_msg and /Out of Memory/) {
print "Kept: $_\n"
if $debug > 8;
echo('MESSAGE' => "$_", )
if $echo_it;
send_email('ADDRESSES' => "$admin_team",
'MESSAGE' => "$_",
'SUBJECT' => $subject,
MAILER => "$MAILER", )
if $mail_it;
# only page every 2 minutes
next if $notified_time + $delay_between_pages > $local_time;
send_email('ADDRESSES' => "$pagers",
'MESSAGE' => "$_",
'SUBJECT' => $subject,
MAILER => "$MAILER", )
if $page_it;
$notified_time = $local_time;
next;
}
print "Nope: $_\n"
if $debug > 8;
$in_meminfo_msg = "";
}
### Watcher Script END ###
Keywords:



