you might also want to check out the one under source control: http://hg.linux-ha.org/dev/file/tip/resources/OCF/mysql.in
On 1/8/07, Achim Stumpf <[EMAIL PROTECTED]> wrote:
Hi list, I have rewritten a mysql ocf script which I found on http://k2k.ds14.agh.edu.pl/egee-ha/mysql Basically I corrected the exit codes and I have rewritten some functions which i didn't like. In my test cluster it works fine. I haven't found any mysql ocf script shipped with your source code, so maybe you like mine. If you have any advice, what should be changed or what else you need of mine, so that you are able to integrate it in your sources, let me know please. cheers, Achim #!/bin/sh # # $Id: db2.in,v 1.8 2006/01/26 18:00:05 lars Exp $ # # mysql # # Description: Manages a MySQL database as Linux-HA resource # (made from db2 ocf script) # # # Author: Alan Robertson # Author: Jakub Janczak <kubek2k (at) gmail (dot) com> # Author: Achim Stumpf <newgrp <at> gmx <dot> de> (rewritten and correction of exit codes) # Support: [EMAIL PROTECTED] # License: GNU General Public License (GPL) # Copyright: (C) 2002 - 2005 International Business Machines, Inc. (? k2k) # # An example usage in /etc/ha.d/haresources: # node1 10.0.0.170 mysql # # See usage() function below for more details... # # OCF instance parameters: # OCF_RESKEY_mysql_config # OCF_RESKEY_mysql_datadir # OCF_RESKEY_mysql_user # OCF_RESKEY_mysql_group # OCF_RESKEY_mysql_test_table # OCF_RESKEY_mysql_test_user # OCF_RESKEY_mysql_test_passwd ####################################################################### # Initialization: . /usr/lib/heartbeat/ocf-shellfuncs ####################################################################### SH=/bin/sh # Default values - You can tweak them out MYSQL_CONFIG="/etc/my.cnf" MYSQL_DATADIR="/var/lib/mysql" MYSQL_USER="mysql" MYSQL_GROUP="mysql" MYSQL_TEST_TABLE="mysql.user" MYSQL_PIDFILE=/var/run/mysqld/mysqld.pid MYSQL_SOCKET=/var/lib/mysql/mysql.sock usage() { methods=`mysql_methods` methods=`echo $methods | tr ' ' '|'` cat <<-! usage: `basename $0` ($methods) `basename $0` manages a MySQL Database as an HA resource. The 'start' operation starts the database. The 'stop' operation stops the database. The 'status' operation reports whether the database is running The 'monitor' operation reports whether the database seems to be working The 'validate-all' operation reports whether the parameters are valid The 'meta-data' operation shows the meta data message ! } meta_data() { cat <<END <?xml version="1.0"?> <!DOCTYPE resource-agent SYSTEM "ra-api-1.dtd"> <resource-agent name="mysql"> <version>1.0</version> <longdesc lang="en"> Resource script for mysql. It manages a MySQL Database instance as an HA resource. </longdesc> <shortdesc lang="en">mysql resource agent</shortdesc> <parameters> <parameter name="mysql_config" unique="0" required="0"> <longdesc lang="en"> Configuration file </longdesc> <shortdesc lang="en">mysql_config</shortdesc> <content type="string" default="/etc/my.cnf" /> </parameter> </parameters> <parameters> <parameter name="mysql_datadir" unique="0" required="0"> <longdesc lang="en"> Directory containing databases </longdesc> <shortdesc lang="en">mysql_datadir</shortdesc> <content type="string" default="/var/lib/mysql" /> </parameter> </parameters> <parameters> <parameter name="mysql_user" unique="0" required="0"> <longdesc lang="en"> User running mysql daemon </longdesc> <shortdesc lang="en">mysql_user</shortdesc> <content type="string" default="mysql" /> </parameter> </parameters> <parameters> <parameter name="mysql_group" unique="0" required="0"> <longdesc lang="en"> Group running mysql daemon </longdesc> <shortdesc lang="en">mysql_group</shortdesc> <content type="string" default="mysql" /> </parameter> </parameters> <parameters> <parameter name="mysql_test_table" unique="0" required="0"> <longdesc lang="en"> Table to be tested in monitor statement (in <database>.<table> notation) </longdesc> <shortdesc lang="en">mysql test table</shortdesc> <content type="string" default="mysql.user" /> </parameter> </parameters> <parameters> <parameter name="mysql_test_user" unique="0" required="0"> <longdesc lang="en"> Table to be tested in monitor statement (in <database>.<table> notation) </longdesc> <shortdesc lang="en">mysql test user</shortdesc> </parameter> </parameters> <parameters> <parameter name="mysql_test_passwd" unique="0" required="0"> <longdesc lang="en"> Table to be tested in monitor statement (in <database>.<table> notation) </longdesc> <shortdesc lang="en">mysql test user password</shortdesc> </parameter> </parameters> <actions> <action name="start" timeout="120" /> <action name="stop" timeout="120" /> <action name="status" timeout="60" /> <action name="monitor" depth="0" timeout="30" interval="10" start-delay="10" /> <action name="validate-all" timeout="5" /> <action name="meta-data" timeout="5" /> </actions> </resource-agent> END } # # methods: What methods/operations do we support? # mysql_methods() { cat <<-! start stop status monitor validate-all meta-data ! } mysql_silent_status() { if [ ! -e $MYSQL_PIDFILE ]; then ocf_log info "No pid file found" return $OCF_NOT_RUNNING; fi pid=`cat $MYSQL_PIDFILE`; if [ -d /proc -a -d /proc/1 ]; then [ -d /proc/$pid ] else # This assumes we are root kill -0 $pid >/dev/null 2>&1 fi } mysql_status() { if mysql_silent_status; then ocf_log info "MySQL running" return $OCF_SUCCESS; else ocf_log info "MySQL not running" return $OCF_NOT_RUNNING; fi } mysql_start() { if mysql_silent_status; then ocf_log info "MySQL already running" return $OCF_SUCCESS else config=$1 datadir=$2 user=$3 group=$4 touch /var/log/mysqld.log chown $user:$group /var/log/mysqld.log chmod 0640 /var/log/mysqld.log [ -x /sbin/restorecon ] && /sbin/restorecon /var/log/mysqld.log if [ ! -d $datadir/mysql ] ; then ocf_log info "Initializing MySQL database: " /usr/bin/mysql_install_db --datadir=$datadir ret=$? chown -R $user:$group $datadir if [ $ret -ne 0 ] ; then ocf_log err "Problem with initialization"; exit $OCF_ERR_GENERIC fi fi chown -R $user:$group $datadir chmod 0755 $datadir /usr/bin/mysqld_safe --defaults-file=$config --pid-file=$MYSQL_PIDFILE --socket=$MYSQL_SOCKET --datadir=$datadir --user=$user >/dev/null 2>&1 & ret=$? # Spin for a maximum of N seconds waiting for the server to come up. # Rather than assuming we know a valid username, accept an "access # denied" response as meaning the server is functioning. if [ $ret -eq 0 ]; then STARTTIMEOUT=10 while [ $STARTTIMEOUT -gt 0 ]; do RESPONSE=`/usr/bin/mysqladmin -uUNKNOWN_MYSQL_USER ping 2>&1` && break echo "$RESPONSE" | grep -q "Access denied for user" > /dev/null 2>&1 && break sleep 1 let STARTTIMEOUT=${STARTTIMEOUT}-1 done if [ $STARTTIMEOUT -eq 0 ]; then ocf_log err "Timeout error occurred trying to start MySQL Daemon." return $OCF_ERR_GENERIC else ocf_log info "MySQL started" return $OCF_SUCCESS fi else ocf_log err "mysqld_safe failed" return $ret fi touch /var/lock/subsys/mysqld return $ret fi } mysql_stop() { if mysql_silent_status then pid=`cat $MYSQL_PIDFILE 2> /dev/null ` if kill $pid > /dev/null 2>&1 then if [ -d /proc -a -d /proc/1 ]; then count=0; while [ -d /proc/$pid ] && [ $count -lt 10 ] do sleep 1; kill $pid >/dev/null 2>&1 ocf_log info "Killing MySQL PID $pid" count=$((count+1)) done fi if [ ! -d /proc/$pid ]; then ocf_log info "MySQL stopped"; else ocf_log err "MySQL stop timeout exceeded"; return $OCF_ERR_GENERIC fi else ocf_log err "MySQL couldn't be stopped" return $OCF_ERR_GENERIC fi rm -f /var/lock/subsys/mysqld rm -f $datadir/mysql.sock else # If MySQL ist not running already, it should return 0 to be OCF compliant ocf_log info "MySQL is not running." fi return $OCF_SUCCESS } mysql_monitor() { if mysql_silent_status ; then test_table=$1 test_user=$2 test_passwd=$3 if [ -z $test_user ]; then buf=`echo "SELECT * FROM $test_table" | mysql --user=root --password=lol --socket=$MYSQL_SOCKET -O connect_timeout=1 2>&1` ret=$? else buf=`echo "SELECT * FROM $test_table" | mysql --user=$test_user --password=$test_passwd --socket=$MYSQL_SOCKET -O connect_timeout=1 2>&1` ret=$? fi if [ ! $ret -eq 0 ]; then ocf_log err "MySQL $test_table monitor failed:"; if [ ! -z "$buf" ]; then ocf_log err $buf; fi return $OCF_ERR_GENERIC; else ocf_log info "MySQL monitor succeded"; return $OCF_SUCCESS; fi else ocf_log info "MySQL not running"; return $OCF_NOT_RUNNING; fi } validate_all() { if [ ! -f $config ]; then ocf_log err "Config $config doesn't exist"; exit $OCF_ERR_ARGS; fi if [ ! -d $datadir ]; then ocf_log err "Datadir $datadir dosen't exist"; exit $OCF_ERR_ARGS; fi grep $user /etc/passwd >/dev/null 2>&1 if [ ! $? -eq 0 ]; then ocf_log err "User $user doesn't exit"; exit $OCF_ERR_ARGS; fi grep $group /etc/group >/dev/null 2>&1 if [ ! $? -eq 0 ]; then ocf_log err "Group $group doesn't exist"; exit $OCF_ERR_ARGS; fi } # initializing the parameters if [ -z $OCF_RESKEY_mysql_config ]; then config=$MYSQL_CONFIG; else config=$OCF_RESKEY_mysql_config; fi if [ -z $OCF_RESKEY_mysql_datadir ]; then datadir=$MYSQL_DATADIR; else datadir=$OCF_RESKEY_mysql_datadir; fi if [ -z $OCF_RESKEY_mysql_user ]; then user=$MYSQL_USER; else user=$OCF_RESKEY_mysql_user; fi if [ -z $OCF_RESKEY_mysql_group ]; then group=$MYSQL_GROUP; else group=$OCF_RESKEY_mysql_group; fi if [ -z $OCF_RESKEY_mysql_test_table ]; then test_table=$MYSQL_TEST_TABLE; else test_table=$OCF_RESKEY_mysql_test_table; fi if [ ! -z $OCF_RESKEY_mysql_test_user ]; then test_user=$OCF_RESKEY_mysql_test_user; fi if [ ! -z $OCF_RESKEY_mysql_test_passwd ]; then test_passwd=$OCF_RESKEY_mysql_test_passwd; fi # What kind of method was invoked? case "$1" in start) mysql_start $config $datadir $user $group exit $?;; stop) mysql_stop exit $?;; status) mysql_status exit $?;; monitor) mysql_monitor $test_table $test_user $test_passwd exit $?;; validate-all) validate_all exit $?;; meta-data) meta_data exit $OCF_SUCCESS;; *) usage exit $OCF_ERR_UNIMPLEMENTED;; esac _______________________________________________________ Linux-HA-Dev: [email protected] http://lists.linux-ha.org/mailman/listinfo/linux-ha-dev Home Page: http://linux-ha.org/
_______________________________________________________ Linux-HA-Dev: [email protected] http://lists.linux-ha.org/mailman/listinfo/linux-ha-dev Home Page: http://linux-ha.org/
