OSDN Git Service

Update version to 0.2.
[osdn-codes/ppp-resource-agents.git] / PPPConnection
1 #!/bin/sh
2 #
3 #   OCF resource agent for PPP connection
4 #
5 #   License:      GNU General Public License version 2 (GPL2)
6 #   (c) 2017 Tatsuki Sugiura <sugi@nemui.org> and OSDN Corporation <tech@osdn.jp>
7 #
8 # This program is free software; you can redistribute it and/or modify
9 # it under the terms of version 2 of the GNU General Public License as
10 # published by the Free Software Foundation.
11 #
12 # This program is distributed in the hope that it would be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
15 #
16 # Further, this software is distributed without any warranty that it is
17 # free of the rightful claim of any third person regarding infringement
18 # or the like.  Any license provided herein, whether implied or
19 # otherwise, applies only to this software file.  Patent licenses, if
20 # any, provided herein do not apply to combinations of this program with
21 # other software, or any other product whatsoever.
22 #
23 # You should have received a copy of the GNU General Public License
24 # along with this program; if not, write the Free Software Foundation,
25 # Inc., 59 Temple Place - Suite 330, Boston MA 02111-1307, USA.
26 #
27
28 # Init
29 : ${OCF_FUNCTIONS_DIR=${OCF_ROOT}/lib/heartbeat}
30 . ${OCF_FUNCTIONS_DIR}/ocf-shellfuncs
31 . ${OCF_FUNCTIONS_DIR}/findif.sh
32
33 # Defaults
34 OCF_RESKEY_expected_ip4_default=""
35 OCF_RESKEY_expected_ip6_default=""
36 OCF_RESKEY_isp_default="provider"
37 OCF_RESKEY_pon_retries_default="30"
38 OCF_RESKEY_pon_wait_sec_default="5"
39 OCF_RESKEY_poff_wait_sec_default="3"
40 OCF_RESKEY_ip_wait_sec_default="30"
41
42 : ${OCF_RESKEY_expected_ip4=${OCF_RESKEY_expected_ip4_default}}
43 : ${OCF_RESKEY_expected_ip6=${OCF_RESKEY_expected_ip6_default}}
44 : ${OCF_RESKEY_isp=${OCF_RESKEY_isp_default}}
45 : ${OCF_RESKEY_pon_retries=${OCF_RESKEY_pon_retries_default}}
46 : ${OCF_RESKEY_pon_wait_sec=${OCF_RESKEY_pon_wait_sec_default}}
47 : ${OCF_RESKEY_poff_wait_sec=${OCF_RESKEY_poff_wait_sec_default}}
48 : ${OCF_RESKEY_ip_wait_sec=${OCF_RESKEY_ip_wait_sec_default}}
49
50 meta_data() {
51         cat <<EOM
52 <?xml version="1.0"?>
53 <!DOCTYPE resource-agent SYSTEM "ra-api-1.dtd">
54 <resource-agent name="PPPConnection">
55   <version>0.2</version>
56   <longdesc lang="en">
57 Handle ppp connection as OCF resource. This resource calls pon/poff to connect ppp.
58   </longdesc>
59   <shortdesc lang="en">
60 Handle ppp connection as OCF resource. This resource calls pon/poff to connect ppp.
61   </shortdesc>
62   <parameters>
63     <parameter name="expected_ip4" unique="1" required="0">
64       <longdesc lang="en">
65       Expected IPv4 address. If you specify this parameter,
66       startup and monitor proecesses will wait the address associated with ppp interface.
67       </longdesc>
68       <shortdesc lang="en">Expected IPv4 address</shortdesc>
69       <content type="string" default="" />
70     </parameter>
71     <parameter name="expected_ip6" unique="1" required="0">
72       <longdesc lang="en">
73       Expected IPv6 address. If you specify this parameter,
74       startup and monitor proecesses will wait the address associated with ppp interface.
75       </longdesc>
76       <shortdesc lang="en">Expected IPv6 address</shortdesc>
77       <content type="string" default="" />
78     </parameter>
79     <parameter name="isp" unique="1" required="0">
80       <longdesc lang="en">
81       ISP name. This will be passed to pon/poff, and pppd will be monitored with specified isp.
82       Default is "provider".
83       </longdesc>
84       <shortdesc lang="en">ISP name (default: provider)</shortdesc>
85       <content type="string" default="provider" />
86     </parameter>
87     <parameter name="pon_retries" unique="0" required="0">
88       <longdesc lang="en">
89       Retry limit for pon
90       </longdesc>
91       <shortdesc lang="en">Retry limit for pon</shortdesc>
92       <content type="integer" default="30" />
93     </parameter>
94     <parameter name="pon_wait_sec" unique="0" required="0">
95       <longdesc lang="en">
96       Resource will wait specified seconds for ppp connection up. When it's exceeded, retry to run pon.
97       </longdesc>
98       <shortdesc lang="en">Wait time for pon on each tries</shortdesc>
99       <content type="integer" default="5" />
100     </parameter>
101     <parameter name="poff_wait_sec" unique="0" required="0">
102       <longdesc lang="en">
103       Resource will wait specified seconds for ppp connection down. When it's exceeded, retry to run poff.
104       </longdesc>
105       <shortdesc lang="en">Wait time for poff on each tries</shortdesc>
106       <content type="integer" default="3" />
107     </parameter>
108     <parameter name="ip_wait_sec" unique="0" required="0">
109       <longdesc lang="en">
110       Resource will wait specified seconds for ppp interface aquires expected IP address provided by expected_ip4 or expected_ip6.
111       </longdesc>
112       <shortdesc lang="en">Wait time for expected ip</shortdesc>
113       <content type="integer" default="30" />
114     </parameter>
115   </parameters>
116   <actions>
117     <action name="start"        timeout="60s" />
118     <action name="stop"         timeout="60s" />
119     <action name="monitor"      timeout="10s" interval="30s" depth="0" />
120     <action name="meta-data"    timeout="5s" />
121     <action name="validate-all" timeout="10s" />
122   </actions>
123 </resource-agent>
124 EOM
125
126   exit $OCF_SUCCESS
127 }
128
129 ppp_usage() {
130         cat <<END
131 usage: $0 {start|stop|status|monitor|validate-all|meta-data}
132
133 Expects to have a fully populated OCF RA-compliant environment set.
134 END
135 }
136
137 ppp_validate() {
138   ppp_init
139   check_binary ps
140   check_binary $AWK
141   check_binary $EGREP
142   check_binary grep
143   check_binary pon
144   check_binary poff
145
146
147   if [ ! -z "$IP4" -o ! -z "$IP6" ]; then
148     check_binary $IP2UTIL
149   fi
150
151   if [ -z "$ISP" ]; then
152     ocf_log err "Isp name is not specified for PPPConnection"
153     exit $OCF_ERR_CONFIGURED
154   fi
155 }
156
157 ppp_init() {
158   ISP="$OCF_RESKEY_isp"
159   IP4="$OCF_RESKEY_expected_ip4"
160   IP6="$OCF_RESKEY_expected_ip6"
161 }
162
163 ppp_start() {
164   ppp_init
165   tries=0
166   while ! ppp_check_pppd; do
167     tries=`expr $tries + 1`
168     ocf_run pon $ISP
169     if [ "$tries" -gt "$OCF_RESKEY_pon_retries" ]; then
170       ocf_log err "Retry limit for pon is exceeded. Giving up!"
171       return $OCF_ERR_GENERIC
172     fi
173     if [ "$tries" -eq 1 ]; then
174       sleep 1
175     else
176       sleep $OCF_RESKEY_pon_wait_sec
177     fi
178   done
179   return $OCF_SUCCESS
180 }
181
182 ppp_stop() {
183   ppp_init
184   tries=0
185   while ppp_check_pppd; do
186     tries=`expr $tries + 1`
187     ocf_run poff ${ISP}
188     if [ "$tries" -eq 1 ]; then
189       sleep 1
190       continue
191     elif [ "$tries" -gt 10 ]; then
192       ocf_log err "ppp connection is still active with isp '${ISP}', giving up to stop!"
193       return $OCF_ERR_GENERIC
194     elif [ "$tries" -gt 8 ]; then
195       ocf_log warn "pppd still running with isp '${ISP}'. Trying to send KILL signal..."
196       kill -KILL `ps -ewwwo pid,args | $EGREP "[p]ppd call ${ISP}\$" | $AWK '{print $1}'`
197     elif [ "$tries" -gt 5 ]; then
198       ocf_log warn "pppd still running with isp '${ISP}'. Trying to send TERM signal..."
199       kill -TERM `ps -ewwwo pid,args | $EGREP "[p]ppd call ${ISP}\$" | $AWK '{print $1}'`
200     fi
201     sleep $OCF_RESKEY_poff_wait_sec
202   done
203   return $OCF_SUCCESS
204 }
205
206 ppp_check_pppd() {
207   ppp_init
208   ps -ewwo args | $EGREP -q "[p]ppd call ${ISP}\$" || return 1
209   return 0
210 }
211
212 ppp_check_ip() {
213   ppp_init
214   if [ ! -z "$IP4" ]; then
215     $IP2UTIL -4 a list type ppp | grep -q "inet $IP4 " || return 1
216   fi
217   if [ ! -z "$IP6" ]; then
218     $IP2UTIL -6 a list type ppp | grep -q "inet6 $IP6 " || return 1
219   fi
220   return 0
221 }
222
223 ppp_echo_status() {
224   if ! ppp_check_pppd; then
225     echo "disconnected"
226     return 0
227   fi
228   tries=0
229   while ! ppp_check_ip; do
230     tries=`expr $tries + 1`
231     if [ "$tries" -gt "$OCF_RESKEY_ip_wait_sec" ]; then
232       ocf_log err "Timeout for waiting expected IP address (${IP4} ${IP6})."
233       echo "connecting"
234       return 0
235     fi
236     sleep 1
237   done
238   echo "connected"
239   return 0
240 }
241
242 ppp_monitor() {
243   ppp_init
244   status=`ppp_echo_status`
245   case $status in
246     connected)
247       return $OCF_SUCCESS
248       ;;
249     connecting)
250       return $OCF_ERR_GENERIC
251       ;;
252     disconnected)
253       return $OCF_NOT_RUNNING
254       ;;
255     *)
256       return $OCF_ERR_GENERIC
257       ;;
258   esac
259 }
260
261
262 case $__OCF_ACTION in
263   meta-data)
264     meta_data
265     ;;
266   usage|help)
267     ppp_usage
268     exit $OCF_SUCCESS
269     ;;
270 esac
271
272 ppp_validate
273
274 case $__OCF_ACTION in
275   start)
276     ppp_start
277     exit $?
278     ;;
279   stop)
280     ppp_stop
281     exit $?
282     ;;
283   status)
284     ip_status=`ppp_echo_status`
285     if [ $ip_status = "connected" ]; then
286       echo "running"
287       exit $OCF_SUCCESS
288     else
289       echo "stopped"
290       exit $OCF_NOT_RUNNING
291     fi
292     ;;
293   monitor)
294     ppp_monitor
295     exit $?
296     ;;
297   validate-all)
298     exit $OCF_SUCCESS
299     ;;
300   *)
301     ppp_usage
302     exit $OCF_ERR_UNIMPLEMENTED
303     ;;
304 esac
305
306 # vim: ft=sh sw=2