Old functions_static.ksh
1 #
2
3 # CDDL HEADER START
4 #
5 # The contents of this file are subject to the terms of the
6 # Common Development and Distribution License (the License).
7 # You may not use this file except in compliance with the License.
8 #
9 # You can obtain a copy of the license at usr/src/CDDL.txt
10 # or http://www.opensolaris.org/os/licensing.
11 # See the License for the specific language governing permissions
12 # and limitations under the License.
13 #
14 # When distributing Covered Code, include this CDDL HEADER in each
15 # file and include the License file at usr/src/CDDL.txt.
16 # If applicable, add the following below this CDDL HEADER, with the
17 # fields enclosed by brackets [] replaced with your own identifying
18 # information: Portions Copyright [yyyy] [name of copyright owner]
19 #
20 # CDDL HEADER END
21 #
22
23 #
24 # Copyright 2007 Sun Microsystems, Inc. All rights reserved.
25 # Use is subject to license terms.
26 #
27
28 #ident "%Z%%M% %I% %E% SMI"
29
30 TASK_COMMAND=""
31
32 ZONENAME=/usr/bin/zonename
33
34 SCLOGGER=/usr/cluster/lib/sc/scds_syslog
35 LOGGER=/usr/bin/logger
36 SCHA_RESOURCE_SETSTATUS=/usr/cluster/bin/scha_resource_setstatus
37 SCHA_RESOURCE_GET=/usr/cluster/bin/scha_resource_get
38 SCHA_RESOURCEGROUP_GET=/usr/cluster/bin/scha_resourcegroup_get
39 PMFADM=/usr/cluster/bin/pmfadm
40 UNAME=/usr/bin/uname
41 ECHO=/usr/bin/echo
42 AWK=/usr/bin/awk
43 EGREP=/usr/bin/egrep
44 PROJECTS=/usr/bin/projects
45
46 terminate()
47 {
48
49 debug_message "Function: terminate - Begin"
50 ${SET_DEBUG}
51
52 exiting_func=${1}
53 exit_code=${2}
54
55 # determine the right return code, it is either the return code from the functions or
56 # the appropriate smf return code
57
58 if in_cluster
59 then
60
61 # called in a clustered global zone
62
63 debug_message "Method: ${MYNAME} ${exiting_func} - End (${exit_code})"
64 exit ${exit_code}
65
66 else
67 if [ -n "${SMF_FMRI}" ]
68 then
69 if [ "${exit_code}" -ne 0 ]
70 then
71
72 # honour the gds specific probe values like 100 or 201
73
74 if [ "${exiting_func}" == "probe" -o "${exiting_func}" == "validate" ]
75 then
76 debug_message "Method: ${MYNAME} ${exiting_func} - End (${exit_code})"
77 exit ${exit_code}
78 else
79 debug_message "Method: ${MYNAME} ${exiting_func} - End (${SMF_EXIT_ERR_PERM})"
80 exit ${SMF_EXIT_ERR_PERM}
81 fi
82 fi
83
84 debug_message "Method: ${MYNAME} ${exiting_func} - End (${SMF_EXIT_OK})"
85 exit ${SMF_EXIT_OK}
86 else
87 debug_message "Method: ${MYNAME} ${exiting_func} - End (${exit_code})"
88 exit ${exit_code}
89 fi
90 fi
91
92 debug_message "Function: terminate - End"
93
94 }
95 syslog_tag()
96 {
97 #
98 # Data Service message format
99 #
100
101 ${SET_DEBUG}
102
103 print "SC[${PKG:-??}.${METHOD:-??}]:${RESOURCEGROUP:-??}:${RESOURCE:-??}"
104 }
105
106 scds_syslog()
107 {
108
109 #
110 # Log a message
111 #
112
113 ${SET_DEBUG}
114
115 if [ -f ${SCLOGGER} ]
116 then
117 ${SCLOGGER} "$@" &
118 else
119
120 # eliminate -m and honour -p and -t option
121 while getopts 'p:t:m' opt
122 do
123 case "${opt}" in
124 t) TAG=${OPTARG};;
125 p) PRI=${OPTARG};;
126 esac
127 done
128
129 shift $((${OPTIND} - 1))
130 LOG_STRING=`/usr/bin/printf "$@"`
131
132 ${LOGGER} -p ${PRI} -t ${TAG} ${LOG_STRING}
133 fi
134
135 }
136
137 rgs_zonename()
138 {
139
140 # Determine wether the host specified by uname -n is combined with a zonename in the
141 # current resourcegroups nodelist. The seperator beween nodename and zonename is ":".
142 #
143 # This function assume the resource group name preset in the variable ${RESOURCEGROUP} and should be called
144 #
145 # $(rgs_zonename)
146 #
147 # It passes back the zonename or nothing.
148
149 debug_message "Function: rg_zonename - Begin "
150 ${SET_DEBUG}
151
152 nodes_zone=
153 nodename=`${UNAME} -n`
154 node=`${SCHA_RESOURCEGROUP_GET} -G ${RESOURCEGROUP} -O NODELIST|grep ${nodename}`
155
156 if ${ECHO} ${node} | grep : >/dev/null 2>&1
157 then
158 nodes_zone=`${ECHO} ${node} | ${AWK} -F: '{print $2}'`
159
160 fi
161
162 print ${nodes_zone}
163
164 debug_message "Function: rg_zonename - End "
165 }
166
167 debug_message()
168 {
169 #
170 # Output a debug message to syslog if required
171 #
172
173 if [ -n "${DEBUG}" ]
174 then
175
176 # determine if we should display a message and do it
177
178 if [ "${DEBUG}" = "ALL" ]
179 then
180 SET_DEBUG="set -x"
181
182 DEBUG_TEXT=${1}
183
184 scds_syslog -p daemon.debug -t $(syslog_tag) -m \
185 "%s" "${DEBUG_TEXT}"
186 else
187
188 # check if the actual resource matches one of the list of resources
189 # if it matches, display a message
190
191 DEBUG=`echo ${DEBUG}|tr "," " "`
192 for i in ${DEBUG}
193 do
194 if [ "${i}" = "${RESOURCE}" ]
195 then
196 SET_DEBUG="set -x"
197
198 DEBUG_TEXT=${1}
199
200 scds_syslog -p daemon.debug -t $(syslog_tag) -m \
201 "%s" "${DEBUG_TEXT}"
202 fi
203 done
204 fi
205 else
206 SET_DEBUG=
207 fi
208 }
209
210 log_message()
211 {
212 #
213 # Output a message to syslog as required
214 #
215
216 debug_message "Function: log_message - Begin"
217 ${SET_DEBUG}
218
219 if [ -s "${LOGFILE}" ]
220 then
221 PRIORITY=${1}
222 HEADER=${2}
223
224 #
225 # Ensure the while loop only reads a closed file
226 #
227
228 strings ${LOGFILE} > ${LOGFILE}.copy
229 while read MSG_TXT
230 do
231 scds_syslog -p daemon.${PRIORITY} -t $(syslog_tag) -m \
232 "%s - %s" \
233 "${HEADER}" "${MSG_TXT}"
234 done < ${LOGFILE}.copy
235
236 fi
237
238 debug_message "Function: log_message - End"
239 }
240
241 srm_function()
242 {
243 debug_message "Function: srm_function - Begin"
244 ${SET_DEBUG}
245
246 USER=${1}
247
248 #
249 # If Solaris 8 just return
250 #
251
252 if [ `/usr/bin/uname -r` = "5.8" ];
253 then
254 return 0
255 fi
256
257 #
258 # Retrieve RESOURCE_PROJECT_NAME
259 #
260
261 if in_cluster
262 then
263 RESOURCE_PROJECT_NAME=`${SCHA_RESOURCE_GET} -R ${RESOURCE} -G ${RESOURCEGROUP} -O RESOURCE_PROJECT_NAME`
264
265 #
266 # Retrieve RG_PROJECT_NAME if RESOURCE_PROJECT_NAME is not set
267 #
268
269 if [ -z "${RESOURCE_PROJECT_NAME}" ] || [ "${RESOURCE_PROJECT_NAME}" = "default" ];then
270
271 RESOURCE_PROJECT_NAME=`${SCHA_RESOURCEGROUP_GET} -G ${RESOURCEGROUP} -O RG_PROJECT_NAME`
272 fi
273 else
274 RESOURCE_PROJECT_NAME=${ZONE_PROJECT}
275 fi
276
277 #
278 # Return if no projects are defined
279 #
280
281 if [ -z "${RESOURCE_PROJECT_NAME}" ] || [ "${RESOURCE_PROJECT_NAME}" = "default" ]; then
282 return 0
283 fi
284
285 #
286 # Validate that $USER belongs to the project defined by
287 # ${RESOURCE_PROJECT_NAME}
288 #
289
290 PROJ_MEMBER=`${PROJECTS} ${USER} | ${EGREP} "^${RESOURCE_PROJECT_NAME} | ${RESOURCE_PROJECT_NAME} | ${RESOURCE_PROJECT_NAME}$|^${RESOURCE_PROJECT_NAME}$"`
291
292 if [ -z "${PROJ_MEMBER}" ];
293 then
294 # SCMSGS
295 # @explanation
296 # The specified user does not belong to the project defined by
297 # Resource_project_name or Rg_project_name.
298 # @user_action
299 # Add the user to the defined project in /etc/project.
300 scds_syslog -p daemon.err -t $(syslog_tag) -m \
301 "srm_function - The user %s does not belongs to project %s" \
302 "${USER}" "${RESOURCE_PROJECT_NAME}"
303 return 1
304 else
305 debug_message "srm_function - User ${USER} belongs to project ${RESOURCE_PROJECT_NAME}"
306 fi
307
308 #
309 # Set TASK_COMMAND
310 #
311
312 TASK_COMMAND="/usr/bin/newtask -p ${RESOURCE_PROJECT_NAME}"
313
314 debug_message "Function: srm_function - End"
315
316 return 0
317 }
318
319 zone_function()
320 {
321 debug_message "Function: zone_function - Begin"
322 ${SET_DEBUG}
323
324 #
325 # Initialize PZONEOPT as empty
326 PZONEOPT=""
327
328 #
329 # If Solaris does not have /usr/bin/zonename just return 0
330 # else add "-z <zonename>" to PZONEOPT
331 #
332
333 if [ -x "${ZONENAME}" ];
334 then
335 PZONEOPT="-z `${ZONENAME}`"
336 fi
337
338 debug_message "Function: zone_function - End"
339 return 0
340 }
341
342 in_cluster()
343 {
344 #
345 # determine if we are started in a clustered global zone
346 #
347
348 debug_message "Function: in_cluster - Begin"
349 ${SET_DEBUG}
350
351 in_cluster_val=0
352
353 if [ ! -d /etc/cluster ]
354 then
355 in_cluster_val=1
356 fi
357
358 debug_message "Function: in_cluster - End"
359
360 return ${in_cluster_val}
361 }
362
363 start_dependency()
364 {
365 debug_message "Function: start_dependency - Begin"
366 ${SET_DEBUG}
367
368 # RETRIEVE START_TIMEOUT
369
370 if [-z "${ZONE_START_TIMOUT}" ]
371 then
372 START_TIMEOUT=`standard_resource_get START_TIMEOUT`
373 else
374 START_TIMEOUT=${ZONE_START_TIMOUT}
375 fi
376
377 # 80 % OF THE START-TIMEOUT CAN BE SPENT ON WAITING
378
379 MAX_START_TIMEOUT=`expr ${START_TIMEOUT} \* 80 \/ 100`
380
381 # GET CURRENT TIME IN SEC ON 24H BASE
382
383 CUR_HOUR=`date '+%H'`
384 CUR_MIN=`date '+%M'`
385 CUR_SEC=`date '+%S'`
386 CUR_TIME=`expr ${CUR_HOUR} \* 3600 + ${CUR_MIN} \* 60 + ${CUR_SEC}`
387
388 # RUN A TEST LOOP UNTIL THE DEPENDENT RESOURCE IS UP OR
389 # A TIMEOUT HAS OCCURED
390
391 while [ 1 -eq 1 ]
392 do
393
394 # GET NEW CURRENT TIMEOUT
395 NEW_HOUR=`date '+%H'`
396 NEW_MIN=`date '+%M'`
397 NEW_SEC=`date '+%S'`
398 NEW_TIME=`expr ${NEW_HOUR} \* 3600 + ${NEW_MIN} \* 60 + ${NEW_SEC}`
399
400 # HAVE WE EXEEDED TIMEOUT
401
402 s1=`expr ${CUR_TIME} + ${MAX_START_TIMEOUT}`
403
404 if [ ${s1} -le ${NEW_TIME} ]; then
405 # SCMSGS
406 # @explanation
407 # The start of a dependent resource takes too long.
408 # @user_action
409 # None
410 scds_syslog -p daemon.err -t $(syslog_tag) -m \
411 "start_dependency: Exeeded ${MAX_START_TIMEOUT} seconds for waiting on dependent resource for resource ${RESOURCE} to come online"
412
413 St=1
414 break
415 fi
416
417 # CALL check_start_dependency
418
419 debug_message "Function: start_dependency - Call check_start_dependency function with argument "$*
420
421 check_start_dependency $*
422 St=$?
423
424 if [ ${St} -eq 0 ]; then
425 St=0
426 break
427 fi
428
429 # Wait 5 seconds
430
431 sleep 5
432 done
433
434
435 debug_message "Function: start_dependency - End"
436
437 return ${St}
438 }
439
440 restart_dependency()
441 {
442 debug_message "Function: restart_dependency - Begin"
443 ${SET_DEBUG}
444
445
446 # CALL check_restart_dependency
447
448 debug_message "Function: start_dependency - Call check_restart_dependency function with argument "$*
449 check_restart_dependency $*
450 St=$?
451
452 if [ ${St} -ne 0 ]; then
453 # SCMSGS
454 # @explanation
455 # The dependent resource was restarted, the resource is restarting
456 # now.
457 # @explanation-2
458 # A restart of the dependant resource has been noticed,
459 # it might be necessary to restart the resource depending on
460 # @explanation-3
461 # a restart of the dependant resource has been noticed, it might be
462 # necessary to restart the resource depending on
463 # @explanation-4
464 # a restart of the dependant resource has been noticed, it might be
465 # necessary to restart the resource depending on
466 # @user_action
467 # None
468 # @user_action-2
469 # check the validity of the service. there might be a dependency
470 # problem, a sub resource has been restarted, and functionality
471 # might have been impaired
472 # @user_action-3
473 # check the validity of the service. there might be a dependency
474 # problem, a sub resource has been restarted, and functionality
475 # might have been impaired
476 # @user_action-4
477 # check the validity of the service. there might be a dependency
478 # problem, a sub resource has been restarted, and functionality
479 # might have been impaired
480 scds_syslog -p daemon.err -t $(syslog_tag) -m \
481 "restart_dependency - Dependent resource to resource %s has been restarted, restart this resource %s" \
482 "${RESOURCE}" "${RESOURCE}"
483
484 St=100
485 else
486 St=0
487 fi
488
489 debug_message "Function: restart_dependency - End"
490
491 return ${St}
492 }