From 6421b1835e97e56f7242451cf1d1fd33f3343c06 Mon Sep 17 00:00:00 2001 From: Michael Prokop Date: Fri, 23 Aug 2013 11:40:02 +0200 Subject: [PATCH] Helper script to check for mysql service in service scripts Addressing https://bugtracker.sipwise.com/view.php?id=3487 --- debian/ngcp-ngcpcfg.install | 1 + helper/check-for-mysql | 58 +++++++++++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+) create mode 100755 helper/check-for-mysql diff --git a/debian/ngcp-ngcpcfg.install b/debian/ngcp-ngcpcfg.install index 22b93107..7387b546 100644 --- a/debian/ngcp-ngcpcfg.install +++ b/debian/ngcp-ngcpcfg.install @@ -6,6 +6,7 @@ helper/sort-yml usr/share/ngcp-ngcpcfg/helper/ helper/sync-db usr/share/ngcp-ngcpcfg/helper/ helper/tt2-wrapper usr/share/ngcp-ngcpcfg/helper/ helper/validate-yml usr/share/ngcp-ngcpcfg/helper/ +helper/check-for-mysql usr/share/ngcp-ngcpcfg/helper/ lib/* usr/lib/ngcp-ngcpcfg/ sbin/ngcp-network usr/sbin/ sbin/ngcp-sync-constants usr/sbin/ diff --git a/helper/check-for-mysql b/helper/check-for-mysql new file mode 100755 index 00000000..6481b791 --- /dev/null +++ b/helper/check-for-mysql @@ -0,0 +1,58 @@ +#!/bin/bash + +if [ $# -gt 0 ] ; then + COUNT=$1 +else + COUNT=60 +fi + +if ! [ $COUNT -ge 0 2>/dev/null ] ; then + echo "Usage: $0 " + exit 1 +fi + +if ! [ -x /usr/sbin/mysqld ] || ! [ -x /usr/bin/mysqladmin ] ; then + echo "Error: mysqld or mysqladmin not present. Exiting." >&2 + exit 1 +fi + +mysqld_get_param() { + /usr/sbin/mysqld --print-defaults 2>/dev/null \ + | tr " " "\n" \ + | grep -- "--$1" \ + | tail -n 1 \ + | cut -d= -f2 +} + +mysqld_status () { + ping_output=$(/usr/bin/mysqladmin --defaults-file=/etc/mysql/debian.cnf ping 2>&1) + ping_alive=$(( ! $? )) + + ps_alive=0 + pidfile=$(mysqld_get_param pid-file) + + if [ -f "$pidfile" ] && ps $(cat $pidfile) &>/dev/null ; then + ps_alive=1 + fi + + if [ "$1" = "check_alive" -a $ping_alive = 1 ] || + [ "$1" = "check_dead" -a $ping_alive = 0 -a $ps_alive = 0 ]; then + return 0 + fi + + return 1 +} + +printf "Checking MySQL service status (for a maximum of %s seconds): " "$COUNT" +for i in $(seq 1 $COUNT) ; do + if mysqld_status check_alive warn; then + printf "available.\n" + exit 0 + else + printf "." + fi + sleep 1 +done + +printf "still not available, giving up.\n" +exit 1