📄 mguardian
字号:
#!/bin/bash# The contents of this file are subject to the MonetDB Public License# Version 1.1 (the "License"); you may not use this file except in# compliance with the License. You may obtain a copy of the License at# http://monetdb.cwi.nl/Legal/MonetDBLicense-1.1.html## Software distributed under the License is distributed on an "AS IS"# basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the# License for the specific language governing rights and limitations# under the License.## The Original Code is the MonetDB Database System.## The Initial Developer of the Original Code is CWI.# Portions created by CWI are Copyright (C) 1997-2007 CWI.# All Rights Reserved.# The mguardian looks after the healthyness of invidual servers# A mguardian process is run for each mserver unless you specify# otherwise.# The mguardian control information is kept in .mguardian and# consists of the <delay,pid> pairfunction usage(){ echo "Usage: mguardian [options] [mserver(s)]" echo " --start Start looking after mserver(s)[default]" echo " --stop Stop looking after mserver(s) " echo " --delay Set check delay to N seconds [300]" echo " --status Status of all servers" echo " --log Display the server log" echo " --help This list of options"}function get() { needle=$1 shift while [ $# -gt 0 ]; do arg=${1#${needle}=} [[ ${#arg} -lt ${#1} ]] && echo $arg shift done}tmp="$(get "--dbfarm" $*)"if [ ! -z "$tmp" ]then DBFARM="$tmp" DBLOGS=$DBFARM/../dblogs echo "mguardian working on " $DBFARMelse DBFARM=$MONETDB5_PREFIX/var/MonetDB5/dbfarm DBLOGS=$MONETDB5_PREFIX/var/MonetDB5/dblogsfiDELAY=300ACTION=starttrap '' 2 3case "$1" in "--start" ) ACTION=start shift ;; "--stop" ) ACTION=stop shift ;; "--delay="* ) delay="${1#--delay=}" if [ "$delay" != "" ]; then DELAY=$delay fi ACTION=delay shift ;; "--help" | * ) usage exit 0 ;;esaccd $DBFARMif [ ! -z "$*" ]then DBLIST="$*"else DBLIST="`ls`"fitmp="$(get "--dbname" $*)"if [ ! -z "$tmp" ]then DBLIST="$tmp"fifunction getPID (){ active="" if [ -f $1/.gdk_lock ] then pid=`sed -e "s/.*PID=//" -e "s/ .*//" -e 2q $1/.gdk_lock` if [ "$pid" != "" ] then active=`ps --no-heading --format pid -p $pid` fi fi echo $active}# before we restart we have to test for a bad system environment,# such as a full diskrestartcount=0maxtrials=8mailtrigger=1restartwindow=60started=`date +%s`function restart_server(){ # FIXME: add support for $DBLOGS someday; it can also grow beyond # disk capacities... full=`df $DBFARM/$1 |sed -e 1d -e "s/%.*//" -e "s/.* //"` if [[ "$restartcount" > $maxtrials ]] then return fi if [ "$full" -gt 90 ] then echo "Running out of disk space" echo `date +"%F %T"` "WARNING disk use $full%" >>monetdb.log if [[ "$restartcount" > $mailtrigger ]] thenmail -s "Notification: mserver5 encounters disk limits" $USER <<!EOFThe mguardian process $$ running on node `uname -n` has detectedthat you are running low on disk space.The endangered database is located in '$DBFARM/$1'.`df -h $DBFARM/$1 $DBLOGS/$1 | tail -n +2 | sort | uniq`The space taken by the database:`du -shc $DBFARM/$1 $DBLOGS/$1`Please take precautions against loosing your data.!EOF fi fi now=`date +%s` now=`expr $now - $started ` if [[ "$now" < "$restartwindow" ]] then if [[ "$restartcount" > $mailtrigger ]] then if [[ "$restartcount" == $maxtrials ]] then mail -s "Notification: mserver5 stopped" $USER <<!EOFThe mguardian process $$ running on node `uname -n` has attemptedto restart the server against database '$1' $maxtrials times.The server remains down until you have checked the situation.!EOF fi now=`date` mail -s "Notification: mserver5 restarted" $USER <<!EOFThe mguardian process $$ running on node `uname -n` has restarteddatabase '$1' because it appeared not to be running any more.Please inspect the monetdb log for further details.The mguardian process is removed when the server is properlyterminated with e.g. 'monetdb --stop --dbname=$1'.Alternatively, you can safely kill process $$, but then theserver is left unattended.!EOF # todo: add some information on the how manieth message this # is, and implement a gag, in order to stop sending mail # (the *2 each time is not enough, imho, it should stop # trying and send a message that the database is down and # stays down) mailtrigger=`expr $mailtrigger + $mailtrigger` fi restartcount=`expr $restartcount + 1 ` fi if [ -f $1/.mserver5.args ] then started=`date +%s` if [ -f $1/.mserver5.dbinit ] then STARTUP=$DBFARM/$1/.mserver5.dbinit fi if [ -f $1/.mserver5.args ] then ARGS=`cat $1/.mserver5.args` fi CONFIG="--config=$DBFARM/$1/.monetdb.conf" (cd $1; mserver5 --set daemon=yes --dbname=$1 $CONFIG $ARGS $STARTUP)& echo `date +"%F %T"` " RESTART $1 $args" >>monetdb.log fi}function getDelay(){ delay=`grep delay $1/.monetdb.conf| sed -e "s/\t.*//" -e "s/delay=//"` if [ "$delay" = "" ] then delay=11 fi echo $delay}#for each mserver5 a separate mguardian process is started#it runs in the background until the mguardian control file disappearsfunction check_mserver(){ delay=`getDelay $1` mypid=$! echo "$delay" >$1/.mguardian.delay echo "$!" >$1/.mguardian.pid echo `date +"%F %T"` " GUARD $1 " >>monetdb.log while [ -f $1/.mguardian.pid ] do active=`getPID $1` if [ "$active" = "" ] then restart_server $1 fi # we should pick a cheaper way to detect changes to the delay delay=`getDelay $1` sleep $delay & sid="$!" echo "$sid" >$1/.mguardian.sleep trap "kill $sid" 1 wait trap 1 done echo `date +"%F %T"` " LEAVE $1 " >>monetdb.log exit}#ignore a server also involves killing any running mguardian# for it. function stop_guardian(){ pid=`getPID $1` if [ -f $DBFARM/$1/.mguardian.pid ] then pid=`cat $DBFARM/$1/.mguardian.pid` echo -n "stop mguardian for " $DBFARM/$1 "..." kill -TERM $pid echo "ok" echo `date +"%F %T"` " LEAVE $1 " >>monetdb.log rm $1/.mguardian.pid fi}#echo "mguardian dblist $DBLIST"case $ACTION in "start" ) for i in $DBLIST do ( check_mserver $i ) & done ;; "stop" ) for i in $DBLIST do if [ -d $i ] then stop_guardian $i fi done ;; "delay" ) for i in $DBLIST do if [ -d $i ] then if [ -f $i/.mguardian.sleep ] then #wakeup the mguardian process pid=`cat $i/.mguardian.sleep` kill -HUP $pid fi echo "$DELAY" >$i/.mguardian.delay fi done ;;esac
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -