通过远程 http API 来控制 lnmp 环境的重启perl脚本

#!/usr/bin/perl
use DBD::mysql;
use strict;
use warnings;
use DBI;
use utf8;
binmode(STDOUT, ':encoding(utf8)');
binmode(STDIN, ':encoding(utf8)');
binmode(STDERR, ':encoding(utf8)');
use Time::Local;
my $addtime=time();
my $dbh;
my ($a,$b,$c,$d,$f)=(,,,,);
sub getpid{
my $ser= pop @_;
my $pidinfo=`ps -ef |grep $ser|grep root |grep -v grep|awk '{print \$2}'`;
chomp $pidinfo;
return $pidinfo;
} sub re_mysql{
my $qpid=&getpid('mysql');
my $zhuangtai=`/etc/init.d/mysql restart`;
my $hpid=&getpid('mysql');
if($qpid != $hpid){
return ;
}
else {
return ;
}
} sub re_redis{
my $qpid=&getpid('redis');
my $redisstop= `/bin/kill - $qpid`;
my $redisstart= `/usr/local/redis-2.6./src/redis-server /etc/redis.conf`;
my $hpid=&getpid('redis');
if($qpid != $hpid){
return ;
}
else {
return ;
}
} sub re_ftp{
my $qpid=&getpid('pure-ftp');
my $a=`/etc/init.d/pureftpd restart`;
my $hpid=&getpid('pure-ftp');
if($qpid != $hpid){
return ;
}
else {
return ;
}
} sub re_php{
my $qpid=&getpid('php-cgi');
my $a=`/usr/local/php/sbin/php-fpm restart`;
my $hpid=&getpid('php-cgi');
if($qpid != $hpid){
return ;
}
else {
return ;
}
}
sub re_nginx{
my $qpid=&getpid('nginx');
my $a=`/usr/local/nginx/sbin/nginx -s reload`;
my $hpid=&getpid('nginx');
if($qpid != $hpid){
return ;
}
else {
return ;
}
} sub addsql {
my ($filename,$dir,$server,$cmd)=@_;
eval { $dbh = DBI->connect("DBI:mysql:database=systemcron;host=127.0.0.1","账户","密码", {'RaiseError' => });};
if ($@){
return ;
}
else{
my $sth = $dbh->prepare("INSERT INTO systemcron.croninfo (filename,dir,service,cmd,addtime) VALUES (?,?,?,?,?)");
eval {$sth->execute($filename,$dir,$server,$cmd,$addtime);};
if ($@){
$dbh->disconnect();
return ;
}
else{
$dbh->disconnect();
return ;
}
}
} sub addtosql {
my ($filename,$dir,$server,$addtime,$status)=@_;
my $operationtime=time();
eval { $dbh = DBI->connect("DBI:mysql:database=systemcron;host=127.0.0.1","账户","密码", {'RaiseError' => });};
if ($@){
return ;
}
else{
my $sth = $dbh->prepare("UPDATE systemcron.croninfo SET `status`=?,`operationtime`=? WHERE `filename`=? and `dir`=? and `service`=? and `addtime`=?");
eval {$sth->execute($status,$operationtime,$filename,$dir,$server,$addtime);};
if ($@){
$dbh->disconnect();
return ;
}
else{
$dbh->disconnect();
return ;
}
}
}
sub select{
my ($filename)=@_;
my $dbh = DBI->connect("DBI:mysql:database=systemcron;host=127.0.0.1","账户","密码", {'RaiseError' => });
my $sth = $dbh->prepare("SELECT * FROM `croninfo` WHERE `filename` =? and `status`= 1");
$sth->execute($filename);
$sth->fetchrow_array();
} my @ini=`curl "http:xxxx.com"`;
foreach (@ini){
my @inilist=split(" ",$_);
my ($filename,$dir,$server)=@inilist;
shift @inilist,$filename;
shift @inilist,$dir;
shift @inilist,$server;
my $cmd=join(" ",@inilist);
my $cmdsql=join("",@inilist);
if (&select($filename)){
print "OK\n";
}
else{
&addsql($filename,$dir,$server,$cmdsql);
`$cmd`;
`/usr/bin/rsync -vrptgo --progress --delete xxx.xxx.xxx.xxx::file/$filename /tmp/file/`;
`/bin/cp /tmp/file/$filename $dir`;
my @serlist=split(/\|/,$server);
foreach (@serlist){
if (/mysql/i){
$a=&re_mysql();
}
elsif (/redis/i){
$b=&re_redis();
}
elsif (/php-fpm/i){
$c=&re_php();
}
elsif (/ftp/i){
$d=&re_ftp();
}
elsif (/nginx/i){
$f=&re_nginx();
}
}
if ($a +$b +$c +$d +$f){
my $status=-;
&addtosql($filename,$dir,$server,$addtime,$status);
}
else{
my $status=;
&addtosql($filename,$dir,$server,$addtime,$status);
}
}
}

本脚本 通过 rsync 服务器 来抓取一些关键性的配置文件 并对客户端集群进行 配置文件分发 并能执行 以root身份执行 一些 shell, 可以通过API接口 来重启 mysql nginx php redis 并且记录进入数据库 并记录本次操作的执行结果,以便执行上次不成功的操作  本脚本 应当写入 crontab里面, 实现了类似 文件集中分发部署的效果。

上一篇:perl 切换 dnspod 域名记录


下一篇:使用Python和Perl绘制北京跑步地图