Reputation: 1075
I have the following script that I plan to use to execute a bash command on a remote server. Every time this script runs it should check whether the remote MySQL (PXC) server is undergoing SST.
#!/usr/bin/perl
use strict;
use warnings;
my $time = localtime();
my $file = '/db-common-list/names.txt';
open my $info, $file or die "Could not open $file: $!";
while ( my $hostname = <$info> ) {
my $wsrep_check = `ssh $hostname ps -ef |grep mysql | grep wsrep_sst_xtrabackup-v2`;
if ( index($wsrep_check, 'State transfer in progress, setting sleep higher mysqld') != -1 ) {
print "$time: Server $hostname";
last if $. == 2;
}
}
close $info;
Inside /db-common-list/names.txt
is a list of databases that the script should loop through line by line. So it looks something like this:
db-test-1
db-test-2
db-test-3
db-test-4
...
But when I run the script, the command line just hangs and never displays anything, at which point I have to manually force the script to stop executing. So after several minutes with nothing but the script just hanging in the terminal, I use Ctrl-D to stop the script and I get this:
thegeorgia@cron-db$ ./test.cron
Connection to db-test-1 closed.
Connection to db-test-2 closed.
thegeorgia@cron-db$ ./test.cron
I can ssh and ping these remote as demonstrated below servers so thats not the issue for sure:
thegeorgis@cron-db$ ping db-test-1
PING db-test-1 (10.1.4.205) 56(84) bytes of data.
64 bytes from db-test-1 (10.1.4.205): icmp_seq=1 ttl=64 time=0.263 ms
64 bytes from db-test-1 (10.1.4.205): icmp_seq=2 ttl=64 time=0.222 ms
All servers involved are running Ubuntu.
NOTE: As suggested by Borodin, i added the chomp as follows:
while( my $hostname = <$info>) {
my $server = chomp( $hostname );
my $wsrep_check = `ssh $server ls`;
if ( index($wsrep_check, 'State transfer in progress, setting sleep higher mysqld') != -1 ){
print "$time: Server $server";
}
last if $. == 2;
}
And when i run the perl script, i now get the following error:
ssh: connect to host 1 port 22: Invalid argument
ssh: connect to host 1 port 22: Invalid argument
SOLUTION:
#!/usr/bin/perl -w
use strict;
use warnings;
my $time = localtime();
my $file = '/db-common-list/names.txt';
open my $info, $file or die "Could not open $file: $!";
while( my $hostname = <$info>) {
chomp( $hostname );
my $wsrep_check = `ssh $hostname ps -ef |grep mysql | grep wsrep_sst_xtrabackup-v2`;
if ( $wsrep_check ne "" ){
print "$time: Server $hostname\n";
}
}
close $info;
Upvotes: 1
Views: 524
Reputation: 126732
You need to
chomp $hostname;
before your ssh command to remove the trailing newline from the read
I also suspect that your last if $. == 2
should be outside the if
block
Perl has built-in support for regular expressions, so there is rarely any need for a call to index
if ( index($wsrep_check, 'State transfer in progress, setting sleep higher mysqld') != -1 ) { ... }
would usually be written
if ( $wsrep_check =~ /State transfer in progress, setting sleep higher mysqld/ ) { ... }
but what you have written is fine
Upvotes: 3