OSDN Git Service

RAがSysVinit依存だったためSysVinit,Systemdに係らず
[ultramonkey-l7/ultramonkey-l7-v3.git] / doc / heartbeat-ra / L7vsd
1 #!/bin/sh
2 #
3 # This program is free software; you can redistribute it and/or modify
4 # it under the terms of version 2 of the GNU General Public License as
5 # published by the Free Software Foundation.
6 #
7 # This program is distributed in the hope that it would be useful, but
8 # WITHOUT ANY WARRANTY; without even the implied warranty of
9 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10 #
11 # Further, this software is distributed without any warranty that it is
12 # free of the rightful claim of any third person regarding infringement
13 # or the like.  Any license provided herein, whether implied or
14 # otherwise, applies only to this software file.  Patent licenses, if
15 # any, provided herein do not apply to combinations of this program with
16 # other software, or any other product whatsoever.
17 #
18 # You should have received a copy of the GNU General Public License
19 # along with this program; if not, write the Free Software Foundation,
20 # Inc., 59 Temple Place - Suite 330, Boston MA 02111-1307, USA.
21 #
22
23 #######################################################################
24 # Initialization:
25
26 . ${OCF_ROOT}/resource.d/heartbeat/.ocf-shellfuncs
27
28 L7SOCKFILE=${OCF_RESKEY_socket_default:-/var/run/l7vs/l7vs}
29
30 #######################################################################
31
32 meta_data() {
33         cat <<END
34 <?xml version="1.0"?>
35 <!DOCTYPE resource-agent SYSTEM "ra-api-1.dtd">
36 <resource-agent name="L7vsd" version="1.0">
37 <version>1.0</version>
38
39 <longdesc lang="en">
40 This is a L7vsd Resource Agent.
41 </longdesc>
42 <shortdesc lang="en">L7vsd resource agent</shortdesc>
43
44 <parameters>
45 <parameter name="socket" unique="0" required="0">
46 <longdesc lang="en">
47 The socket to be used for l7vsadm.
48 </longdesc>
49 <shortdesc lang="en">l7vsadm socket</shortdesc>
50 <content type="string" default="${OCF_RESKEY_socket_default}"/>
51 </parameter>
52 </parameters>
53
54 <actions>
55 <action name="start"        timeout="60" />
56 <action name="stop"         timeout="60" />
57 <action name="monitor"      timeout="60" interval="10" depth="0" start-delay="0" />
58 <action name="meta-data"    timeout="5" />
59 </actions>
60 </resource-agent>
61 END
62 }
63
64 #######################################################################
65
66 l7vsd_usage() {
67         cat <<END
68 usage: $0 {start|stop|monitor|meta-data}
69
70 END
71 }
72
73 ###############################
74 # Resource start Method
75 ###############################
76 l7vsd_start() {
77         ocf_log info "l7vsd is starting ..."
78         l7vsd_monitor info
79         RET=$?
80         if [ $RET -eq $OCF_SUCCESS ]; then
81                 ocf_log info "l7vsd is already running."
82                 return $OCF_SUCCESS
83         elif [ $RET -eq $OCF_NOT_RUNNING ]; then
84                 L7SOCKDIR=`dirname L7SOCKFILE`
85                 if [ ! -d $L7SOCKDIR ] ; then
86                         ocf_log info "Creating l7vsadm socket dir: $L7SOCKDIR"
87                         mkdir -p $L7SOCKDIR
88                 fi
89                 if [ -e $L7SOCKFILE ] ; then
90                         ocf_log info "Delete l7vsadm socket filer: $L7SOCKFILE"
91                         rm -rf $L7SOCKFILE
92                 fi
93                 /usr/sbin/l7vsd > /dev/null 2>&1
94                 RET=$?
95                 if [ $RET -ne 0 ]; then
96                         MSG="l7vsd start error!"
97                         outputLog err ${OCF_ERR_GENERIC} $MSG
98                         return $OCF_ERR_GENERIC
99                 fi
100                 l7vsd_status
101                 if [ $? -eq $OCF_SUCCESS ]; then
102                         # l7vsd is running
103                         ocf_log info "l7vsd starts."
104                         return $OCF_SUCCSESS
105                 fi
106         fi
107         MSG="l7vsd does not work. (ps=$RET) "
108         outputLog err ${OCF_ERR_GENERIC} ${MSG}
109         return $OCF_ERR_GENERIC
110 }
111
112 ###############################
113 # Resource stop Method 
114 ###############################
115 l7vsd_stop() {
116         ocf_log info "l7vsd stopping ..."
117         l7vsd_status
118         RET=$?
119         if [ $RET -eq $OCF_NOT_RUNNING ]; then
120                 ocf_log info "l7vsd stopped."
121                 return $OCF_SUCCESS
122         fi
123         pkill -f "/usr/sbin/l7vsd"
124         count=0
125         while [ $count -le 10 ]
126         do
127                 l7vsd_status
128                 RET=$?
129                 if [ $RET -eq $OCF_NOT_RUNNING ]; then
130                         ocf_log info "l7vsd stopped."
131                         return $OCF_SUCCESS
132                 fi
133                 count=`expr $count + 1`
134                 sleep 1
135         done
136         l7vsd_pkill
137         RET=$?
138         rm -rf $L7SOCKFILE
139         return $RET
140 }
141
142 ###############################
143 # Logging Method
144 ###############################
145 outputLog(){
146         MODE=$1
147         shift
148         case $MODE in
149                 info)   RET=$1; shift;
150                         ocf_log $MODE "[$0 ${__OCF_ACTION}] OK;return=$RET" "$@";;
151                 err)    RET=$1; shift;
152                         ocf_log $MODE "[$0 ${__OCF_ACTION}] NG;return=$RET" "$@";;
153         esac
154 }
155
156 ###############################
157 # kill process
158 ###############################
159 l7vsd_pkill(){
160         pkill -9 -f "/usr/sbin/l7vsd"
161         ocf_log info "kill l7vsd process!"
162         while true
163         do
164                 sleep 1
165                 l7vsd_status
166                 RET=$?
167                 if [ $RET -eq $OCF_NOT_RUNNING ]; then
168                      # stop OK
169                      ocf_log info "l7vsd process stopped!"
170                      return $OCF_SUCCESS
171                 fi
172         done
173 }
174
175 ###############################
176 # Get Resource Status Method
177 ###############################
178 l7vsd_status(){
179         RET=0
180         RET=`pgrep -fox "/usr/sbin/l7vsd" | wc -l`
181         if [ $RET -eq 1 ]; then
182                 return $OCF_SUCCESS
183         elif [ $RET -eq 0 ]; then
184                 MSG="l7vsd is not running."
185                 outputLog $loglevel ${OCF_NOT_RUNNING} ${MSG}
186                 return $OCF_NOT_RUNNING
187         else
188                 MSG="l7vsd status ERROR!: (ps=$RET)"
189                 outputLog err ${OCF_ERR_GENERIC} ${MSG}
190                 return $OCF_ERR_GENERIC
191         fi
192 }
193
194 ###############################
195 # Get Resource Monitor Method
196 ###############################
197 l7vsd_monitor() {
198         local loglevel
199         loglevel=${1:-err}
200         if ocf_is_probe; then
201                 loglevel="info"
202         fi
203
204         l7vsd_status
205         RET=$?
206         if [ $RET -eq $OCF_SUCCESS ]; then
207                 # status OK
208                 return $OCF_SUCCESS
209         elif [ $RET -eq $OCF_NOT_RUNNING ]; then
210                 # status NG
211                 return $OCF_NOT_RUNNING
212         fi
213         MSG="l7vsd duplicated. "
214         outputLog err ${OCF_ERR_GENERIC} ${MSG}
215         return $OCF_ERR_GENERIC
216 }
217
218 case $__OCF_ACTION in
219 meta-data)      meta_data
220                 exit $OCF_SUCCESS ;;
221 start)          l7vsd_start;;
222 stop)           l7vsd_stop;;
223 monitor)        l7vsd_monitor;;
224 usage|help)     l7vsd_usage
225                 exit $OCF_SUCCESS
226                 ;;
227 *)              l7vsd_usage
228                 exit $OCF_ERR_UNIMPLEMENTED
229     
230                 ;;
231 esac
232 rc=$?
233 ocf_log debug "${OCF_RESOURCE_INSTANCE} $__OCF_ACTION : $rc"
234 exit $rc