diff options
author | Sergey Poznyakoff <gray@gnu.org.ua> | 2019-04-05 15:56:17 +0200 |
---|---|---|
committer | Sergey Poznyakoff <gray@gnu.org.ua> | 2019-04-05 15:56:44 +0200 |
commit | 50f12cd8b8ad504ceb320d340ce5ec3ab91b25c7 (patch) | |
tree | 184316ccd215b475eac982e20a26a0081d44416a | |
parent | 8a247f7194c6fefc9357c16ba37d06f87f84e1c7 (diff) | |
download | savane-gray-50f12cd8b8ad504ceb320d340ce5ec3ab91b25c7.tar.gz savane-gray-50f12cd8b8ad504ceb320d340ce5ec3ab91b25c7.tar.bz2 |
New utility: sv_dbping
-rw-r--r-- | backend/Makefile.PL | 1 | ||||
-rw-r--r-- | backend/install/Makefile.am | 2 | ||||
-rw-r--r-- | backend/install/sv_dbping | 163 |
3 files changed, 165 insertions, 1 deletions
diff --git a/backend/Makefile.PL b/backend/Makefile.PL index fc7f72d..8cc1c5c 100644 --- a/backend/Makefile.PL +++ b/backend/Makefile.PL @@ -55,6 +55,7 @@ WriteMakefile( 'install/sv_config', 'install/sv_sed', + 'install/sv_dbping', 'download/sv_gpgcheckfiles', diff --git a/backend/install/Makefile.am b/backend/install/Makefile.am index 5cc8a07..70053f4 100644 --- a/backend/install/Makefile.am +++ b/backend/install/Makefile.am @@ -1,2 +1,2 @@ -EXTRA_DIST=sv_config sv_setup.pl sv_sed +EXTRA_DIST=sv_config sv_setup.pl sv_sed sv_dbping diff --git a/backend/install/sv_dbping b/backend/install/sv_dbping new file mode 100644 index 0000000..69a5ccc --- /dev/null +++ b/backend/install/sv_dbping @@ -0,0 +1,163 @@ +#! /usr/bin/perl +# Copyright (C) 2019 Sergey Poznyakoff <gray@gnu.org> +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3, or (at your option) +# any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see <http://www.gnu.org/licenses/>. + +use strict; +use Savane::Conf; +use Getopt::Long qw(:config gnu_getopt no_ignore_case); +use DBI; +use Pod::Usage; + +my $sys_dbname = GetConf('sql.database'); +my $sys_dbhost = GetConf('sql.host'); +my $sys_dbuser = GetConf('sql.user'); +my $sys_dbpasswd = GetConf('sql.password'); +my $sys_dbparams = GetConf('sql.params'); + +my $quiet; +my $progress; +my $retries = 60; + +GetOptions('help|h' => sub { pod2usage(-exitstatus => 0, -verbose => 2) }, + 'usage' => sub { pod2usage(-exitstatus => 0, -verbose => 0) }, + 'p|progress' => \$progress, + 'q|quiet' => \$quiet, + 'retries|n=n' => \$retries) + or exit(64); + +my $source = 'DBI:mysql:database='.$sys_dbname + . ':host='.$sys_dbhost + . ':'.$sys_dbparams; + +$progress = 0 if $quiet; + +if ($progress) { + print join(' ',@ARGV,'') if @ARGV; + $| = 1 +} + +while ($retries--) { + if (my $dbd = DBI->connect($source, $sys_dbuser, $sys_dbpasswd, + { RaiseError => 0, + PrintError => 0 })) { + my $ok = $dbd->do("SELECT 1"); + $dbd->disconnect; + if ($ok) { + unless ($quiet) { + print ' ' if $progress; + print "Success\n"; + } + exit(0); + } + } + print '.' if $progress; + sleep(1); +} +unless ($quiet) { + print ' ' if $progress; + print "FAILED\n"; +} +exit(1); + +=head1 NAME + +sv_dbping - ping MySQL database + +=head1 SYNOPSIS + +B<sv_dbping> +[B<-pq>] +[B<-n> I<COUNT>] +[B<--progress>] +[B<--quiet>] +[B<--retries=I<COUNT>>] +[I<ARG>...] + +=head1 DESCRIPTION + +Checks if the Savane MySQL database can be contacted. In case of failure, +retries attempts predefined number of times (default - 60), with 1 second +interval between retries. Exits with code 0 on success, 1 on failure and +64 on invalid invocation. Unless given the B<-q> (B<--quiet>) option, +prints on stdout a descriptive message indicating termination status +prior to exiting. + +=head1 OPTIONS + +=over 4 + +=item B<-p>, B<--progress> + +Show progress indicator (one dot for each subsequent retry). If command line +arguments are supplied, the are output before the progress indicator. This +allows for specifying a descriptive message, e.g.: + + sv_dbping Connecting to the database + +=item B<-q>, B<--quiet> + +Quiet mode. Don't print anything, just exit with appropriate error status. + +=item B<-n>, B<--retries=>I<COUNT> + +Change number of retries. + +=back + +=head1 CONFIGURATION VARIABLES + +=over 4 + +=item B<sql.database> + +=item B<sql.user> + +=item B<sql.password> + +=item B<sql.host> + +=item B<sql.params> + +Database credentials. + +=back + +=head1 EXIT CODES + +=over 8 + +=item B<0> + +Successful termination. + +=item B<1> + +Failure. Could not connect to the database. + +=item B<64> + +Command line usage error. + +=back + +=head1 SEE ALSO + +B<sv_config>(1). + +=head1 AUTHOR + +Sergey Poznyakoff <gray@gnu.org> + +=cut |