This script does the following
1) check whether the site is available
2) if not available then clear apc cache
3) if still not available for more thne 5 counts then restart phpfpm and send mail
4) logging happens at every level
================
#!/usr/bin/perl -w
use strict;
use warnings;
use Data::Dumper;
use Getopt::Long;
use WWW::Mechanize ;
use JSON -support_by_pp;
use POSIX qw/ strftime /;
use Net::SMTP;
my $host ;
my $string ;
my $mech = WWW::Mechanize->new(timeout => 5);
my $time = strftime("%d-%m-%Y", localtime());
my $log = '/var/log/php-fpm/check_php-fpm.log';
my $url = 'http://www.yourwebsite.com/';
my $file_counter = '/var/log/php-fpm/filecounter';
my $count ;
if ( eval{ $mech->get($url) } ) {
logit("OK");
$count = get_count();
if ($count > 0 ) {
print "setting count to 0\n";
$count = '0';
add_count($count);
}
}else{
$count = get_count();
add_count($count+1);
logit("apc FAILED -- removing opcode and users cache -- increasing counter to $count");
system("curl http://127.0.0.1/clear-apc.php > /dev/null 2>&1 ");
if ($count > 4 ) {
logit ("restarting php-fpm ");
system("/etc/init.d/php-fpm reload");
sendemail(" Reloading APC $url [ $time ] "," Not able to get content for $url , reloading php-fpm [ $time ]");
print "setting counter back to 0 \n";
add_count('0');
}
}
sub get_count {
open my $fh, '<', "$file_counter";
read $fh, my $string, -s $fh;
close $fh;
chomp($string);
return $string || '0';
}
sub add_count {
my $count = shift;
open my $fh, '>', "$file_counter";
print $fh $count;
close $fh;
}
sub sendemail {
#usage sendemail("subject","mssg");
my $from = 'from-email-id' ;
my $sub = $_[0] ;
my $content = $_[1] ;
my $to = 'to-emailid' ;
my $relayhost = 'mailserver' || 'localhost';
my $smtp = Net::SMTP->new("$relayhost",
Debug => 1,
);
$smtp->mail($from);
$smtp->to($to);
$smtp->data();
$smtp->datasend("Subject: $sub");
$smtp->datasend("\n");
$smtp->datasend("\n");
$smtp->datasend("$content");
$smtp->dataend();
$smtp->quit();
}
sub logit {
my $s = shift;
my $logtimestamp = strftime("%d-%m-%Y -- %H:%M:%S", localtime());
print "$s\n";
my $fh;
open($fh, '>>', "$log") or die "$log: $!";
print $fh "[$logtimestamp] : $s\n";
close($fh);
}
No comments:
Post a Comment