Perl Code Sample
#!/usr/bin/perl -w
# Using tail -f to monitor log file while you are debugging is not quite efficient.
# For example, PuTTY can only scrollback certain number of lines, and when there is
# more lines to display, you can't see all of it. Another problem with using tail -f
# is sometimes you forget to clear your screen when you start a fresh attempt,
# therefore difficult to distinquish new data from old data. When there is more than one log
# file to keep your eyes on, using tail -f, you would have to keep your eyes on
# multiple terminal screen. While using this script, you will still have to use tail -f,
# and multiple terminals, but the data is written to files, and when all terminal stop
# scrolling, you can press Ctrl+C to terminate this script, and open result files in your
# favorite editor, search for interesting pattern, etc...
use strict;
my $map = {}; # keys are name of log files to monitor, values are names of output file
$map->{'/var/log/qmail/current'} = "/tmp/logCapture/qmail_current";
$map->{'/var/log/qmail/smtpd/current'} = "/tmp/logCapture/qmail_smtpd_current";
my $currentPosition = {};
my $startup = 1;
while(1) {
if ($startup) {
foreach my $file (keys(%{$map})) {
# find the current position of the log file
open(FIN,"<$file") || die("Can not open $file for reading");
seek(FIN,0,2); # seek to end of file
$currentPosition->{$file} = tell(FIN);
close(FIN);
my $outfile = $map->{$file};
# clear out previous captured data
open(FOU,">$outfile") || die("Cannot open $outfile for writing");
close(FOU);
}
print "Done finding ends of files. Do whatever you need to do, and press Ctrl+c when the log files stop scrolling.\n";
$startup = 0;
} else {
foreach my $file (keys(%{$map})) {
open(FIN,"<$file") || die("Cannot open $file for reading $@ $! $^E $??");
my $outfile = $map->{$file};
open(FOU,">>$outfile") || die("Cannot open $outfile for writing");
my $seekto = $currentPosition->{$file};
seek(FIN,$seekto,0);
while(my $line = ) {
print FOU $line;
}
close(FOU);
$currentPosition->{$file} = tell(FIN);
}
}
}
check_clicker.pl:
#!/usr/bin/perl -w
# Plugin for Nagios to monitor a service
use strict;
use Getopt::Long;
use DBI;
use DBD::mysql;
use Data::Dumper qw(Dumper);
my $appWriteDB = `grep 'appWriteDB' /etc/mg/setup.conf | cut -f2-3 -d ':'`;
chomp($appWriteDB);
my $dsn = "DBI:mysql:database=mg;host=$appWriteDB";
my $dbh = DBI->connect($dsn,"mg","kojehixo2");
my $sth = $dbh->prepare("select gurl, unix_timestamp(sendTime) as unix from testDriveGurls where sendTime <= now() order by sendTime ASC limit 1");
$sth->execute();
my $result = $sth->fetchrow_hashref();
my $file = "/tmp/clicker/detect_deadlock";
if ($result) {
my $gurl = $result->{'gurl'};
if (open(FIN,"<$file")) {
my $line = ;
close(FIN);
chomp($line);
my ($g,$t) = split(/\t/,$line,2);
if (($gurl eq $g) && (time - 60 > $t)) {
print "CRITICAL - Clicker queue seems stuck\n";
exit(2);
}
} else {
print "CRITICAL - Can not open $file for reading\n";
exit(2);
}
if (open(FOU,">$file")) {
print FOU $gurl,"\t",time;
close(FOU);
} else {
print "CRITICAL - Can not open $file for writing\n";
exit(2);
}
if (time - 60 > $result->{'unix'}) {
my $minutes = int( (time - $result->{'unix'}) / 60 );
print "CRITICAL - Clicker is $minute behind\n";
exit(2);
}
}
print "OK\n";
exit(0);
# 0: OK
# 1: Warning
# 2: Critical
page_revision: 0, last_edited: 1227299098|%e %b %Y, %H:%M %Z (%O ago)





