script to clear php-fpm / restart php-fpm on continuous 503 http error



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 $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);
}

grab pattern from start and end block -- input record separator

usage of local $/   or  $INPUT_RECORD_SEPARATOR 
INPUT script 

start
name:agnello
dob:2 april
address:123 street
end
start 
name:babit
dob:13 april
address:3 street
end
start 
name:ganesh
dob:1 april
address:23 street
end


i need to get the OUTPUT data in the following format 

name:agnello, dob:23 april ,address:123 street
name:babit,dob:13 april,address:3 street
name:ganesh,dob:1 april,address:23 street

i came up with this , is there a better way to do this : 
===============================
#!/usr/bin/perl 

use strict;
use warnings;

open my $FH , 'data.txt' or die "cannot open file $!";
read $FH, my $string, -s $FH;
close($FH);


my @string = split ( /start/ , $string ) ;

my %data;

foreach  ( @string ) {
chomp;
next if /^$/ ;
s/^ $//g;
s/end//;

my @data = split(/\n/, "$_");
  foreach my $i (@data) {
   print "$i,";

     }
print "\n";
}


alternate script is : 
========================
#!/usr/bin/env perl

use strict;
use warnings;

# --------------------------------------

use charnames qw( :full :short   );
use English   qw( -no_match_vars );  # Avoids regex performance penalty

# --------------------------------------
sub read_record {
  my $record_fh = shift @_;

# all records are from "start\n" to "end\n"
  local $INPUT_RECORD_SEPARATOR = "end\n";
  my $record = <$record_fh>;

# $record undef at end of file
  if( defined $record ){

# removes "end\n"
    chomp $record;

print "$record";
exit ;
# removes "start\n"
    $record =~ s{ \A start \s* }{}msx;
  }

  return $record;
}

# --------------------------------------
while( my $record = read_record( *DATA )){

# remove trailing "\n"
  chomp $record;

# replace field separators with commas
  $record =~ s{ \n+ }{,}gmsx;
# display the record
  print "$record\n";
}


__DATA__
start
name:agnello
dob:2 april
address:123 street
end
start
name:babit
dob:13 april
address:3 street
end
start
name:ganesh
dob:1 april
address:23 street
end










Agnello George sent you a video: "AWS Webcast - AWS OpsWorks Continuous Integration Demo"

Agnello George has shared a video with you on YouTube
AWS Webcast - AWS OpsWorks Continuous Integration Demo
In this session we'll discuss and demonstrate key concepts and design patterns for continuous deployment and integration using technologies like AWS OpsWorks and Chef to enable better control of applications and infrastructures.
©2013 YouTube, LLC 901 Cherry Ave, San Bruno, CA 94066

IT Security Specialist



Designation: IT Security Specialist




Requirements
* Should have experience in IT Security Domain
* Detailed knowledge on end point compliance enforcement through NAC
* Delivery of secure end point systems management.
* Have strong knowledge on IT security, IT audit and IT Asset life cycle Management.
* Desire to have hands on experience on various security and network products and technologies (e.g. Firewalls, IDS, IPS, HIDS, Symantec Antivirus and Active Directory)
* Architecting and deploying security technologies
* Security information event management (SIEM)
* Expert knowledge on Network Security Controls enforcement at gateway and endpoint.
* Experience in managing large end point systems deployment with respective AV and Asset.
* Analytical and Problem Solving Skills
* Should have good documentation skills - Writing security policies & procedures for Team, baseline and guideline documents etc.
* CCNA / MCSE / ITIL / ISO certifications
* CISSP / CISA / SANs certifications will add value

Other Articles

Enter your email address: