File: Xinetd

package info (click to toggle)
resource-agents 1%3A4.0.0~rc1-4
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 3,644 kB
  • ctags: 2,191
  • sloc: sh: 47,713; ansic: 4,074; perl: 3,457; makefile: 663; xml: 89
file content (250 lines) | stat: -rwxr-xr-x 5,869 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
#!/bin/sh
#
# Startup/shutdown script for services managed by xinetd.
#
#	Copyright (C) 2003 Charlie Brooks
#	Copyright (C) 2011 Ulrich Windl
#
# WARNING:	Tested ONLY on SLES11 SP1 at this time.
#
# Author:	Charlie Brooks <ha@HBCS.Org>
# Description:	given parameters of a service name and start|stop|status,
#		will enable, disable or report on a specified xinetd service
# Config:	all services must have a descriptor file in /etc/xinetd.d
# Support:	users@clusterlabs.org
# License:	GNU General Public License (GPL)
#
#	  OCF parameters are as below:
#		OCF_RESKEY_service

#######################################################################
# Initialization:

: ${OCF_FUNCTIONS_DIR=${OCF_ROOT}/lib/heartbeat}
. ${OCF_FUNCTIONS_DIR}/ocf-shellfuncs

service=$OCF_RESKEY_service
SVCDEF=/etc/xinetd.d/$service

#######################################################################

meta_data() {
	cat <<END
<?xml version="1.0"?>
<!DOCTYPE resource-agent SYSTEM "ra-api-1.dtd">
<resource-agent name="Xinetd">
<version>1.0</version>

<longdesc lang="en">
Resource script for Xinetd. It starts/stops services managed
by xinetd by enabling or disabling them in the configuration file.

The xinetd daemon itself must be running: we are not going to start or
stop it ourselves.

All services should have a line saying either "disable=yes" or "disable=no".
The script just changes those settings before reloading xinetd.

Important: in case the services managed by the cluster are the
only ones enabled, you should specify the -stayalive option for
xinetd or it will exit on Heartbeat stop. Alternatively, you may
enable some internal service such as echo.
</longdesc>
<shortdesc lang="en">Manages a service of Xinetd</shortdesc>

<parameters>
<parameter name="service" unique="0" required="1">
<longdesc lang="en">
The name of the service managed by xinetd. 
</longdesc>
<shortdesc lang="en">service name</shortdesc>
<content type="string" default="" />
</parameter>
</parameters>

<actions>
<action name="start" timeout="20s" />
<action name="stop" timeout="20s" />
<action name="restart" timeout="20s" />
<action name="status" depth="0" timeout="10" interval="10" />
<action name="monitor" depth="0" timeout="10" interval="10" />
<action name="validate-all" timeout="5" />
<action name="meta-data" timeout="5" />
</actions>
</resource-agent>
END
}

get_xinetd_pid() {
    ps -e -o pid,comm | $AWK '$2 == "xinetd" { print $1 }'
}

# force xinetd to reload the service descriptions
hup_inetd () {
    # don't rely on the pid file, but lookup xinetd in the list of
    # processes
    local pid
    pid=`get_xinetd_pid`
    if [ "$pid" ]; then
	if kill -s HUP $pid; then
            ocf_log info "asked xinetd to reload by sending SIGHUP to process $pid!"
	else
            ocf_exit_reason "could not send SIGHUP to process $pid!"
            exit $OCF_ERR_GENERIC
	fi
    else
	ocf_exit_reason "xinetd process not found!"
	exit $OCF_ERR_GENERIC
    fi
}

# check "disable = X", printing X
check_service()
{
    ocf_log "info" "checking \"disable\" in $1"
    local result=$(sed -nre 's/^[ 	]*disable[ 	]*=[ 	]*([^ 	]+)[# 	]*/\1/p' $1)
    echo "$result"
}

# change "disable = X" to desired value
change_service()
{
    ocf_log "info" "setting \"disable = $1\" in $2"
    if ! sed -i -re 's/^([ 	]*disable[ 	]*=[ 	]*)([^ 	]+)([# 	]*)/\1'"$1"'\3/' $2
    then
	ocf_log "err" "could not edit $2"
	return 1
    fi
    return 0
}

xup_status () {
    local disabled="$(check_service $SVCDEF)"
    if [ "${disabled:=no}" = no ]; then
	echo running
	return $OCF_SUCCESS
    elif [ "$disabled" = yes ]; then
	echo stopped
	return $OCF_NOT_RUNNING
    else
	echo unknown
	return $OCF_ERR_CONFIGURED
    fi
}

xup_start () {
    if [ "running" = "`xup_status`" ]; then
	ocf_log info "service $service already started"
	exit $OCF_SUCCESS
    fi
    ocf_log "info" "enabling in $SVCDEF"
    if change_service "no" $SVCDEF; then
        hup_inetd 
    fi
}

xup_stop () {
    if [ "stopped" = "`xup_status`" ]; then
	ocf_log info "service $service already stopped"
	exit $OCF_SUCCESS
    fi
    ocf_log "info" "disabling in $SVCDEF"
    if change_service "yes" $SVCDEF; then
        hup_inetd 
    fi
}

xup_usage () {
    echo "Usage: $0 {start|stop|restart|status|monitor|validate-all|meta-data}"
    return 0
}

xup_validate_all () {
    if [ ! -f "$SVCDEF" ]; then
        ocf_exit_reason "service $service missing $SVCDEF"
	return $OCF_ERR_INSTALLED
    fi
    return $OCF_SUCCESS
}

if [ $# -ne 1 ]; then
    xup_usage
    exit $OCF_ERR_ARGS
fi

# These operations do not require OCF instance parameters to be set
case "$1" in
    meta-data)
	meta_data
	exit $OCF_SUCCESS
	;;
    usage)
	xup_usage
	exit $OCF_SUCCESS
	;;
esac

if [ -z "$OCF_RESKEY_service" ]; then
    ocf_exit_reason "please define \"service\" parameter"
    if [ "$1" = "start" ]; then 
	exit $OCF_ERR_CONFIGURED
    else
	exit $OCF_NOT_RUNNING
    fi
fi

# Is xinetd running at all
if [ -z "`get_xinetd_pid`" ]; then
    case "$1" in
    stop) exit $OCF_SUCCESS;;
    start)
	ocf_exit_reason "xinetd not running, we manage just xinetd services, not the daemon itself"
	exit $OCF_ERR_INSTALLED
	;;
    status|monitor)
	if ocf_is_probe; then
	    exit $OCF_NOT_RUNNING
	else
	    ocf_exit_reason "xinetd stopped"
	    exit $OCF_ERR_GENERIC
	fi
	;;
    esac
fi

# Make sure the OCF_RESKEY_service is a valid xinetd service name
if [ ! -f $SVCDEF ]; then
    ocf_exit_reason "service definition $SVCDEF not found!"
    if [ "$1" = "start" ]; then 
	exit $OCF_ERR_INSTALLED
    else
	exit $OCF_NOT_RUNNING
    fi
fi

# See how we were called.
case "$1" in
    start)
	xup_start
	;;
    stop)
	xup_stop
	;;
    restart)
	$0 stop
	$0 start
	;;
    status)
	xup_status
	;;
    monitor)
	xup_status > /dev/null
	;;
    validate-all)
	xup_validate_all
	;;
    *)
	xup_usage
	exit $OCF_ERR_UNIMPLEMENTED
esac
exit $?