Old functions.ksh
  1 #
  2 # CDDL HEADER START
  3 #
  4 # The contents of this file are subject to the terms of the
  5 # Common Development and Distribution License (the License).
  6 # You may not use this file except in compliance with the License.
  7 #
  8 # You can obtain a copy of the license at usr/src/CDDL.txt
  9 # or http://www.opensolaris.org/os/licensing.
 10 # See the License for the specific language governing permissions
 11 # and limitations under the License.
 12 #
 13 # When distributing Covered Code, include this CDDL HEADER in each
 14 # file and include the License file at usr/src/CDDL.txt.
 15 # If applicable, add the following below this CDDL HEADER, with the
 16 # fields enclosed by brackets [] replaced with your own identifying
 17 # information: Portions Copyright [yyyy] [name of copyright owner]
 18 #
 19 # CDDL HEADER END
 20 #
 21 
 22 #
 23 # Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
 24 # Use is subject to license terms.
 25 #
 26 
 27 #ident  "%Z%%M% %I%     %E% SMI"
 28 
 29 PKG=SUNWscPostgreSQL
 30 METHOD=`basename $0`
 31 TASK_COMMAND=""
 32 
 33 ZONENAME=/usr/bin/zonename
 34 DIRNAME=/usr/bin/dirname
 35 
 36 SCLOGGER=/usr/cluster/lib/sc/scds_syslog
 37 PMFADM=/usr/cluster/bin/pmfadm
 38 GETENT=/usr/bin/getent
 39 AWK=/usr/bin/awk
 40 NAWK=/usr/bin/nawk
 41 CAT=/usr/bin/cat
 42 EGREP=/usr/bin/egrep
 43 GREP=/usr/bin/grep
 44 ENV=/usr/bin/env
 45 HEAD=/usr/bin/head
 46 PS=/usr/bin/ps
 47 PGREP=/usr/bin/pgrep
 48 KILL=/usr/bin/kill
 49 IPCS=/usr/bin/ipcs
 50 IPCRM=/usr/bin/ipcrm
 51 ECHO=/usr/bin/echo
 52 TR=/usr/bin/tr
 53 SU=/usr/bin/su
 54 RM=/usr/bin/rm
 55 ZLOGIN=/usr/sbin/zlogin
 56 TEST=/usr/bin/test
 57 PING=/usr/sbin/ping
 58 SED=/usr/bin/sed
 59 IFCONFIG=/usr/sbin/ifconfig
 60 
 61 get_fmri_parameters ()
 62 {
 63 
 64 # extract the smf properties, you need to call your agent commands
 65 
 66         debug_message "Function: get_fmri_parameters - Begin "
 67         ${SET_DEBUG}
 68 
 69 # Resource name
 70 
 71         RESOURCE=`/usr/bin/svcprop -p parameters/Resource ${SMF_FMRI}`
 72 
 73 # Resource Group
 74 
 75         RESOURCEGROUP=`/usr/bin/svcprop -p parameters/Resource_group ${SMF_FMRI}`
 76 
 77 # Start Project
 78 
 79         Project=:default
 80         if /usr/bin/svcprop ${SMF_FMRI}|grep start/project >/dev/null
 81         then
 82                 Project=`/usr/bin/svcprop -p start/project ${SMF_FMRI}`
 83                 if [ "${Project}" != ":default" ]
 84                 then
 85                         PROJ_OPT=" -P ${Project}"
 86                         ZONE_PROJECT=${Project}
 87                 fi
 88         fi
 89 
 90 # Parameter file
 91 
 92         PARFILE=`/usr/bin/svcprop -p parameters/Parameter_File ${SMF_FMRI}`
 93 
 94         debug_message "Function: get_fmri_parameters - End "
 95 }
 96 
 97 set_shell_specifics()
 98 {
 99 
100         # Set variables to construct sourcing and output redirection depending 
101         # on the login shell of the user
102 
103         debug_message "Function: set_shell_specifics - Begin"
104         ${SET_DEBUG}
105 
106         ENVSC=
107 
108         if /usr/bin/getent passwd ${USER} | /usr/bin/awk -F: '{print $7}' | /usr/bin/grep csh > /dev/null
109         then
110 
111                 # C shell specifics
112 
113                 # sourcing
114 
115                 if [ -n "${ENVSCRIPT}" ]
116                 then
117                         ENVSC="source ${ENVSCRIPT};"
118                 fi
119 
120                 # brackets for subshells
121 
122                 OPEN_BRACKET="("
123                 CLOSE_BRACKET=")"
124 
125                 # redirection
126 
127                 OUTPUT_APP=">>& ${LOGFILE}"
128                 OUTPUT=">& ${LOGFILE}"
129                 CAT_OUTPUT="> /tmp/${RESOURCE}-${USER}-cat-out"
130                 CAT_ERRPUT=">& /tmp/${RESOURCE}-${USER}-cat-err"
131                 TBL_OUTPUT="> /tmp/${RESOURCE}-${USER}-tbl-out"
132                 TBL_ERRPUT=">& /tmp/${RESOURCE}-${USER}-tbl-err"
133         else
134 
135                 # Korn shell specifics
136 
137                 # sourcing
138 
139                 if [ -n "${ENVSCRIPT}" ]
140                 then
141                         ENVSC=". ${ENVSCRIPT};"
142                 fi
143 
144                 # no subshell needed in a ksh 
145 
146                 OPEN_BRACKET=
147                 CLOSE_BRACKET=
148 
149                 # redirection
150 
151                 OUTPUT_APP=">> ${LOGFILE} 2>&1"
152                 OUTPUT="> ${LOGFILE} 2>&1"
153                 CAT_OUTPUT="> /tmp/${RESOURCE}-${USER}-cat-out"
154                 CAT_ERRPUT="2> /tmp/${RESOURCE}-${USER}-cat-err"
155                 TBL_OUTPUT="> /tmp/${RESOURCE}-${USER}-tbl-out"
156                 TBL_ERRPUT="2> /tmp/${RESOURCE}-${USER}-tbl-err"
157         fi
158 
159 
160         debug_message "Function: set_shell_specifics - End"
161 }
162 
163 create_pfile()
164 {
165 
166         # Creation of the parameter file. This function is used at registration
167         # time only.
168 
169         debug_message "Function: create_pfile - Begin "
170         ${SET_DEBUG}
171 
172         if [ -z "${PFILE}" ]
173         then
174                 ${ECHO} "ERROR: set the variable parameter file"
175                 return 1
176         fi
177         
178         # determine wether the parameter file needs to be created in a zone or not
179 
180         zonecmd=
181 
182         pfile_tmp="/tmp/pgspfile.${RS}"
183 
184         # get the zonename of the resource group
185 
186         target_zone=$(rgs_zonename) 
187         if [ -n "${target_zone}" ]
188         then
189                 zonecmd="${ZLOGIN} ${target_zone}"
190         fi
191 
192         # check the directory of the parameter file
193 
194         pdir=`${zonecmd} ${DIRNAME} ${PFILE}`
195 
196         # test for the existance of the parameter files directory either in the local or
197         # the global zone according to the zone entry in the nodelist
198 
199         if ! ${zonecmd} ${TEST} -d ${pdir}
200         then
201                 ${ECHO} "ERROR: set the variable parameter file in an existing directory"
202                 return 1
203         fi
204 
205         # create the parameter file 
206 
207         ${ECHO} "Prepare the parameter file ${PFILE}"
208 
209         ${CAT} <<EOF > ${pfile_tmp}
210         
211 #
212 # Content for the parameter file
213 #
214 #            USER - The Solaris user, which owns the PostgreSQL database
215 #          PGROOT - Contains the path to the PostgreSQL directory. Below this
216 #                   directory the postgres binaries are located in the ./bin
217 #                   directory.
218 #          PGDATA - Contains the path to the databases of this specific PostgreSQL
219 #                   instance. The pg_hba.conf needs to be here.
220 #          PGPORT - Port where the postmaster process will be listening to.
221 #          PGHOST - Hostname where the postmaster process is listening to, or a directory where the
222 #                   Unix socket file is stored.
223 #                   If set to a valid hostname, the PGHOST variable forces the probe to 
224 #                   traverse the TCP/IP stack. If the PGHOST variable is empty or starts with a "/", 
225 #                   the probe will use a socket. If the PGHOST variable starts with a "/", the entry must
226 #                   be the directory which contains the socket file. 
227 #       PGLOGFILE - Logfile where the log messages of the postmaster will be stored.
228 # LD_LIBRARY_PATH - This path contains all the necessary libraries for this PostgreSQL
229 #                   installation.
230 #                   Optional
231 #       ENVSCRIPT - Script to contain PostgreSQL specific runtime variables.
232 #                   Optional
233 #            SCDB - This specific PostgreSQL database will be monitored.
234 #          SCUSER - PostgresSQL user to connect to the $SCDB database.
235 #         SCTABLE - Table name in the $SCDB database. This table name will be manipulated
236 #                   to check if PostgreSQL is alive. This table will be generated at database
237 #                   preparation time.
238 #          SCPASS - Password of the SCUSER. If no password is provided the authentication method 
239 #                   for the SCDB database needs to be trusted for requests from the localhost.
240 #                   Optional
241 #        NOCONRET - Return code for connection errors. This return code has to follow the rules
242 #                   for the generic data service. The value has to be between 1 and 100.
243 #                   100/NOCONRET defines the number of consecutive probes to ignore for failed
244 #                   connections. A restart or failover will occur, if the number is exeeded within
245 #                   the retry interval.
246 
247 
248 USER=${USER}
249 PGROOT=${PGROOT}
250 PGDATA=${PGDATA}
251 PGPORT=${PGPORT}
252 PGHOST=${PGHOST}
253 PGLOGFILE=${PGLOGFILE}
254 LD_LIBRARY_PATH=${LD_LIBRARY_PATH}
255 ENVSCRIPT=${ENVSCRIPT}
256 SCDB=${SCDB}
257 SCUSER=${SCUSER}
258 SCTABLE=${SCTABLE}
259 SCPASS=${SCPASS}
260 NOCONRET=${NOCONRET}
261 
262 EOF
263         if [ $? -ne 0 ]
264         then
265                 ${ECHO} "ERROR: could not create the temporary parameter file ${pfile_tmp}"
266                 return 1
267         fi
268 
269         # create the parameter file either in the global or in the prepared target zone
270 
271         if [ -n "${target_zone}" ]
272         then
273                 ${CAT} ${pfile_tmp} | ${zonecmd} ${CAT} - \>${PFILE} 
274                 if [ $? -ne 0 ]
275                 then
276                         ${ECHO} "ERROR: could not create the parameter file ${PFILE}"
277                         return 1
278                 fi
279         else
280                 ${CAT} ${pfile_tmp} > ${PFILE}
281                 if [ $? -ne 0 ]
282                 then
283                         ${ECHO} "ERROR: could not create the parameter file ${PFILE}"
284                         return 1
285                 fi
286         fi
287                 
288         debug_message "Function: create_pfile - End "
289         return 0
290 }
291 
292 validate_options()
293 {
294         debug_message "Function: validate_options - Begin"
295         ${SET_DEBUG}
296 
297         #
298         # Ensure all mandatory options are set
299         #
300 
301         for i in RESOURCE RESOURCEGROUP PARFILE
302         do
303                 case ${i} in
304                         RESOURCE)
305                         if [ -z "${RESOURCE}" ]; then
306                                 # SCMSGS
307                                 # @explanation
308                                 # The start, stop or probe command requires an
309                                 # option which is not set.
310                                 # @user_action
311                                 # Fix the start, stop or probe command in the
312                                 # SUNW.gds resource.
313                                 scds_syslog -p daemon.err -t $(syslog_tag) -m \
314                                 "Function: validate_options: %s Option %s not set" \
315                                 "${METHOD}" "-R"
316                                 return 1
317                         fi;;
318 
319                         RESOURCEGROUP)
320                         if [ -z "${RESOURCEGROUP}" ]; then
321                                 scds_syslog -p daemon.err -t $(syslog_tag) -m \
322                                 "Function: validate_options: %s Option %s not set" \
323                                 "${METHOD}" "-G"
324                                 return 1
325                         fi;;
326 
327                         PARFILE)
328                         if [ -z "${PARFILE}" ]; then
329                                 scds_syslog -p daemon.err -t $(syslog_tag) -m \
330                                 "Function: validate_options: %s Option %s not set" \
331                                 "${METHOD}" "-P"
332                                 return 1
333                         fi;;
334                 esac
335         done
336 
337         debug_message "Function: validate_options - End"
338 }
339 
340 validate()
341 {
342         #
343         # Validate
344         #
345         
346         debug_message "Function: validate - Begin"
347         ${SET_DEBUG}
348 
349         rc_validate=0
350 
351         if ! val_parfile ${PARFILE} "USER PGROOT PGDATA PGPORT PGLOGFILE SCDB SCUSER SCTABLE NOCONRET"
352         then
353                 debug_message "Function: validate - End"
354                 rc_validate=1
355                 return ${rc_validate}
356         fi
357 
358         . ${PARFILE}
359 
360         for i in `${CAT} ${PARFILE} |grep -v "^#"|grep -v "^ "|nawk -F= '{print $1}'`
361         do
362                 case $i in
363                         USER)
364 
365                         # Test the PostgresSQL OS user
366 
367                         if [ -z "${USER}" ]; then
368                                 # SCMSGS
369                                 # @explanation
370                                 # A mandatory variable is unset in the
371                                 # parameter file.
372                                 # @user_action
373                                 # Fix the parameter file and provide a value
374                                 # for the variable in the parameter file
375                                 scds_syslog -p daemon.err -t $(syslog_tag) -m \
376                                 "Function: validate: The %s variable is not set, but it is required" \
377                                 "USER"
378                                 rc_validate=1
379                         else
380                                 id ${USER} >/dev/null 2>&1
381                                 if [ $? -ne 0 ]; then
382                                         # SCMSGS
383                                         # @explanation
384                                         # The user mentioned in the parameter
385                                         # file is not defined in the OS.
386                                         # @user_action
387                                         # Fix the parameter file and provide
388                                         # an existing user for the variable
389                                         # USER.
390                                         scds_syslog -p daemon.err -t $(syslog_tag) -m \
391                                                 "Function: validate: User %s does not exist, an existing user is required" \
392                                                 "${USER}"
393                                         rc_validate=1
394                                 else 
395                                         debug_message "Function: validate - USER OK"    
396                                 fi
397                         fi;;
398 
399                         PGROOT)
400 
401                         # Test the PG Root variable
402                               
403                         if [ -z "${PGROOT}" ]; then
404                                 scds_syslog -p daemon.err -t $(syslog_tag) -m \
405                                 "Function: validate: The %s variable is not set, but it is required" \
406                                 "PGROOT"
407                                 rc_validate=1
408                         else
409                                 if [ ! -d ${PGROOT} ]; then
410                                         # SCMSGS
411                                         # @explanation
412                                         # The directory mentioned in the
413                                         # parameter file for the PGROOT or
414                                         # PGDATA variable does not exist.
415                                         # @user_action
416                                         # Fix the parameter file and provide
417                                         # an existing directoy for the
418                                         # variable PGROOT or PGDATA.
419                                         scds_syslog -p daemon.err -t $(syslog_tag) -m \
420                                         "Function: validate: Directory %s does not exist, an existing directory is required" \
421                                         "${PGROOT}"
422                                         rc_validate=1
423                                 fi
424 
425                                 # test if it is a postgres installation
426 
427                                 if [ ! -f ${PGROOT}/bin/pg_ctl ]
428                                 then
429                                         # SCMSGS
430                                         # @explanation
431                                         # The directory mentioned in the
432                                         # PGROOT variable does not contain the
433                                         # PostgreSQL binaries in its bin
434                                         # directory.
435                                         # @user_action
436                                         # Provide the directory which does
437                                         # contain at least the PostgreSQL
438                                         # binaries in the path ./bin.
439                                         scds_syslog -p daemon.err -t $(syslog_tag) -m \
440                                         "Function: validate: Directory %s does not contain the PostgreSQL binaries" \
441                                         "${PGROOT}"
442                                         rc_validate=1
443                                 else 
444                                         debug_message "Function: validate - PGROOT OK"  
445                                 fi
446                         fi;;
447 
448                         PGDATA)
449 
450                         # Test the PG Data variable
451                               
452                         if [ -z "${PGDATA}" ]; then
453                                 scds_syslog -p daemon.err -t $(syslog_tag) -m \
454                                 "Function: validate: The %s variable is not set, but it is required" \
455                                 "PGDATA"
456                                 rc_validate=1
457                         else
458                                 if [ ! -d ${PGDATA} ]; then
459                                         scds_syslog -p daemon.err -t $(syslog_tag) -m \
460                                         "Function: validate: Directory %s does not exist, an existing directory is required" \
461                                         "${PGDATA}"
462                                         rc_validate=1
463                                 fi
464 
465                                 # test if it is a postgres database cluster
466 
467                                 if [ ! -f ${PGDATA}/postgresql.conf ]
468                                 then
469                                         # SCMSGS
470                                         # @explanation
471                                         # A directory is specified in the
472                                         # PGDATA variable which does not
473                                         # contain a postgresql.conf file.
474                                         # @user_action
475                                         # Specify a directory in the PGDATA
476                                         # variable in the parmeter file, which
477                                         # contains the postgresql.conf file.
478                                         scds_syslog -p daemon.err -t $(syslog_tag) -m \
479                                         "Function: validate: Directory %s does not contain the PostgreSQL configuration files" \
480                                         "${PGDATA}"
481                                         rc_validate=1
482                                 else 
483                                         debug_message "Function: validate - PGDATA OK"  
484                                 fi
485                         fi;;
486 
487                         PGPORT) 
488 
489                         # Test the PG Port variable
490                               
491                         if [ -z "${PGPORT}" ]; then
492                                 scds_syslog -p daemon.err -t $(syslog_tag) -m \
493                                 "Function: validate: The %s variable is not set, but it is required" \
494                                 "PGPORT"
495                                 rc_validate=1
496                         else
497 
498                                 # test if the port is numeric
499 
500                                 if  ! let x=${PGPORT} >/dev/null 2>&1
501                                 then
502                                         # SCMSGS
503                                         # @explanation
504                                         # In the parameter file, there is a
505                                         # non numeric character in the value
506                                         # for the PGPORT variable.
507                                         # @user_action
508                                         # Fix the PGPORT variable in the
509                                         # parameter file.
510                                         scds_syslog -p daemon.err -t $(syslog_tag) -m \
511                                         "Function: validate: Port %s is not numeric" \
512                                         "${PGPORT}"
513                                         rc_validate=1
514                                 else 
515                                         debug_message "Function: validate - PGPORT OK"  
516                                 fi
517                         fi;;
518 
519                         PGHOST)
520 
521                         # test the PGHOST variable ony if it is defined
522 
523                         if [ -n "${PGHOST}" ]
524                         then
525 
526                                 # strip of leading spaces
527 
528                                 PGHOST=`print ${PGHOST}|${SED} 's/^ *//'`
529 
530                                 if ${ECHO} ${PGHOST} | ${GREP} "^/" >/dev/null 2>&1
531                                 then
532                         
533                                         if [ ! -d "${PGHOST}" ]
534                                         then
535                                                 
536                                                 # SCMSGS
537                                                 # @explanation
538                                                 # The directory specified in the PGHOST variable does not
539                                                 # exist.
540                                                 # @user_action
541                                                 # Create the directory. None if it was a lost mount.
542                                                 scds_syslog -p daemon.err -t $(syslog_tag) -m \
543                                                 "Function: validate - Directory for the socket file %s does not exist" \
544                                                 "${PGHOST}"
545                                                 rc_validate=1
546                         
547                                         else
548                                                 debug_message "Function: validate - PGHOST OK"  
549                                         fi
550                                 
551                                 else
552                                         if ! ${GETENT} hosts ${PGHOST} >/dev/null 2>&1
553                                         then
554                                                 
555                                                 # SCMSGS
556                                                 # @explanation
557                                                 # The host specified in the PGHOST variable is not
558                                                 # resolvable.
559                                                 # @user_action
560                                                 # Create the the host entry in /etc/hosts.
561                                                 scds_syslog -p daemon.err -t $(syslog_tag) -m \
562                                                 "Function: validate - The host %s is not resolvable" \
563                                                 "${PGHOST}"
564                                                 rc_validate=1
565                                                 
566                                         else
567                                                 debug_message "Function: validate - PGHOST resolvable"  
568                                         fi
569 
570                                         # Validate if the address of the PGHOST variable is configured UP
571                                         # on a adapter of the host. This validation works in
572                                         # global zones, local zones and in failover zones.
573                                         # This method was preferred over checking the dependency tree,
574                                         # because it works in failover zones, even if there is no logical
575                                         # host configured. 
576 
577                                         # Set the laguage to C to avoid localisation problems with ifconfig
578 
579                                         LANG=C
580 
581                                         addr=`${GETENT} hosts ${PGHOST}| ${AWK} '{print $1}'`
582 
583                                         # Check if the address is configured up on the node
584         
585                                         adapter_success=1
586                                         for i in `${IFCONFIG} -a |${GREP} UP|${AWK} '{print $1}' |${EGREP} -v "zone|inet"|${GREP} ":$"|${SED} s/:$//`
587                                         do
588 
589                                                 ${IFCONFIG} ${i}|${GREP} ${addr} >/dev/null 2>&1
590                                                 if [ ${?} -eq 0 ]
591                                                 then
592                                                         adapter_success=0
593                                                 fi
594 
595                                         done
596 
597                                         if [ ${adapter_success} -ne 0 ]
598                                         then
599                                                 
600                                                 # SCMSGS
601                                                 # @explanation
602                                                 # The host specified in the PGHOST variable is not
603                                                 # configured up on the hosts adapters.
604                                                 # @user_action
605                                                 # Fix either the network configuration or the PGHOST variable
606                                                 scds_syslog -p daemon.err -t $(syslog_tag) -m \
607                                                 "Function: validate - The host %s is not configured UP on the hosts adapters" \
608                                                 "${PGHOST}"
609                                                 rc_validate=1
610                                         else
611                                                 debug_message "Function: validate - PGHOST is configured on the nodes adapters" 
612                                         fi
613                                 fi
614                         fi;;
615 
616                         PGLOGFILE)
617 
618                         # Test the Logfile variable
619                               
620                         if [ -z "${PGLOGFILE}" ]; then
621                                 scds_syslog -p daemon.err -t $(syslog_tag) -m \
622                                 "Function: validate: The %s variable is not set, but it is required" \
623                                 "PGLOGFILE"
624                                 rc_validate=1
625                         else
626 
627                                 # test if the directory for Logfile exists
628 
629                                 Logdir=`/usr/bin/dirname ${PGLOGFILE}`
630                                 if [ ! -d ${Logdir} ]; then
631                                         # SCMSGS
632                                         # @explanation
633                                         # The path to the filename variable
634                                         # PGLOGFILE does not exist.
635                                         # @user_action
636                                         # Qualify the filename of the parameter
637                                         # files PGLOGFILE variable in an
638                                         # existing directory.
639                                         scds_syslog -p daemon.err -t $(syslog_tag) -m \
640                                         "Function: validate: Directory for logfile %s does not exist, an existing directory is required" \
641                                         "${PGLOGFILE}"
642                                         rc_validate=1
643                                 else 
644                                         debug_message "Function: validate - PGLOGFILE OK"       
645                                 fi
646                         fi;;
647 
648                         LD_LIBRARY_PATH)
649 
650                         # test LD_LIBRARY_PATH if it is set
651 
652                         if [ -n "${LD_LIBRARY_PATH}" ]
653                         then
654                                 export LD_LIBRARY_PATH
655                                 if ! ${PGROOT}/bin/psql --help >/dev/null 2>&1
656                                 then
657                                         # SCMSGS
658                                         # @explanation
659                                         # The LD_LIBRARY_PATH is not valid to
660                                         # call the postgres binary.
661                                         # @user_action
662                                         # Qualify the LD_LIBRARY_PATH in the
663                                         # parameter file until it is
664                                         # sufficient for the psql binary.
665                                         scds_syslog -p daemon.err -t $(syslog_tag) -m \
666                                         "Function: validate: The LD_LIBRARY_PATH %s is not valid for this PostgreSQL installation" \
667                                         "${LD_LIBRARY_PATH}"
668                                         rc_validate=1
669                                 else 
670                                         debug_message "Function: validate - LD_LIBRARY_PATH OK" 
671                                 fi
672                         fi;;
673 
674                         ENVSCRIPT) 
675 
676                         # test if the environment script is a syntactically valid script
677                         # In the smf context it needs to be a ksh script.
678                         # In the GDS context the script type ksh/csh is dependent from the login shell of the user.
679 
680                         if [ -n "${ENVSCRIPT}" ]
681                         then
682                                 if [ -f ${ENVSCRIPT} ]
683                                 then
684                                         if ${GETENT} passwd ${USER} | ${AWK} -F: '{print $7}' | ${GREP} "csh" > /dev/null  && [ -z "${SMF_FMRI}" ]
685                                         then
686                                                 if ! /usr/bin/csh -n ${ENVSCRIPT} > /dev/null 2>&1
687                                                 then
688                                                         # SCMSGS
689                                                         # @explanation
690                                                         # The environment
691                                                         # script spcified in
692                                                         # the parameter file
693                                                         # needs to be a valid
694                                                         # c shell script,
695                                                         # because the login
696                                                         # shell of the
697                                                         # PostgreSQL user is c
698                                                         # shell compliant.
699                                                         # @user_action
700                                                         # Fix the environment
701                                                         # script until it
702                                                         # passes csh -n
703                                                         # scriptname.
704                                                         scds_syslog -p daemon.err -t $(syslog_tag) -m \
705                                                         "Function: validate: The Environment script %s is not a valid c shell script" \
706                                                         "${ENVSCRIPT}"
707                                                         rc_validate=1
708                                                 else 
709                                                         debug_message "Function: validate - ENVSCRIPT OK"       
710                                                 fi
711                                         else
712                                                 if ! /usr/bin/ksh -n ${ENVSCRIPT} >/dev/null 2>&1
713                                                 then
714                                                         # SCMSGS
715                                                         # @explanation
716                                                         # The environment
717                                                         # script spcified in
718                                                         # the parameter file
719                                                         # needs to be a valid
720                                                         # korn shell script,
721                                                         # because the login
722                                                         # shell of the
723                                                         # PostgreSQL user is
724                                                         # korn shell
725                                                         # compliant.
726                                                         # @user_action
727                                                         # Fix the environment
728                                                         # script until it
729                                                         # passes ksh -n
730                                                         # scriptname.
731                                                         scds_syslog -p daemon.err -t $(syslog_tag) -m \
732                                                         "Function: validate: The Environment script %s is not a valid korn shell script" \
733                                                         "${ENVSCRIPT}"
734                                                         rc_validate=1
735                                                 else 
736                                                         debug_message "Function: validate - ENVSCRIPT OK"       
737                                                 fi
738                                         fi
739                                 else
740                                         # SCMSGS
741                                         # @explanation
742                                         # The filename specified in the
743                                         # parameter files ENVSCRIPT variable
744                                         # does not exist.
745                                         # @user_action
746                                         # Fix the parameter file and specify a
747                                         # valid Environment script.
748                                         scds_syslog -p daemon.err -t $(syslog_tag) -m \
749                                         "Function: validate: The Environment script %s does not exist" \
750                                         "${ENVSCRIPT}"
751                                         rc_validate=1
752                         
753                                 fi
754                         fi;;
755 
756                         SCDB) 
757 
758                         # Test if the test database variable is defined
759                               
760                         if [ -z "${SCDB}" ]; then
761                                 scds_syslog -p daemon.err -t $(syslog_tag) -m \
762                                 "Function: validate: The %s variable is not set, but it is required" \
763                                 "SCDB"
764                                 rc_validate=1
765                         else 
766                                 debug_message "Function: validate - SCDB OK"    
767                         fi;;
768 
769                         SCUSER)
770 
771                         # Test if the database user variable is defined
772                               
773                         if [ -z "${SCUSER}" ]; then
774                                 scds_syslog -p daemon.err -t $(syslog_tag) -m \
775                                 "Function: validate: The %s variable is not set, but it is required" \
776                                 "SCUSER"
777                                 rc_validate=1
778                         else 
779                                 debug_message "Function: validate - SCUSER OK"  
780                         fi;;
781 
782                         SCTABLE)
783 
784                         # Test if the database table variable is defined
785                               
786                         if [ -z "${SCTABLE}" ]; then
787                                 scds_syslog -p daemon.err -t $(syslog_tag) -m \
788                                 "Function: validate: The %s variable is not set, but it is required" \
789                                 "SCTABLE"
790                                 rc_validate=1
791                         else 
792                                 debug_message "Function: validate - SCTABLE OK" 
793                         fi;;
794 
795                         NOCONRET)
796 
797                         # Test the No Connection return code  variable
798                               
799                         if [ -z "${NOCONRET}" ]; then
800                                 scds_syslog -p daemon.err -t $(syslog_tag) -m \
801                                 "Function: validate: The %s variable is not set, but it is required" \
802                                 "NOCONRET"
803                                 rc_validate=1
804                         else
805 
806                                 # test if the NOCONRET is numeric
807 
808                                 if  ! let x=${NOCONRET} >/dev/null 2>&1
809                                 then
810                                         # SCMSGS
811                                         # @explanation
812                                         # The value for the NOCONRET variable
813                                         # contains a non numeric character.
814                                         # @user_action
815                                         # Fix the NOCONRET variable in the
816                                         # parameter file.
817                                         scds_syslog -p daemon.err -t $(syslog_tag) -m \
818                                         "Function: validate: Return code for failed connections %s is not numeric" \
819                                         "${NOCONRET}"
820                                         rc_validate=1
821                                 else 
822                                         if [ ${NOCONRET} -gt 100 ]; then
823                                                 # SCMSGS
824                                                 # @explanation
825                                                 # The value of the NOCONRET
826                                                 # variable in the parameter
827                                                 # file exeeds 100.
828                                                 # @user_action
829                                                 # Fix the parameter file with
830                                                 # a value below 100.
831                                                 scds_syslog -p daemon.err -t $(syslog_tag) -m \
832                                                 "Function: validate: Return code for failed connections %s is greater 100" \
833                                                 "${NOCONRET}"
834                                                 rc_validate=1
835                                         else
836                                                 debug_message "Function: validate - NOCONRET OK"        
837                                         fi
838                                 fi
839                         fi;;
840                 esac
841         done
842 
843         debug_message "Function: validate - End"
844         return ${rc_validate}
845 }
846 
847 validate_probe()
848 {
849         # Validate ony for non existant files or lost directories
850 
851         debug_message "Function: validate_probe - Begin"
852         ${SET_DEBUG}
853         
854         rc_val_probe=0
855         
856         # If the parameter file does not pass the short validation, try a failover
857         # A failover is the appropriate action, because the directory existed
858         # at startup of the resource.
859 
860         if ! val_parfile ${PARFILE}
861         then
862 
863 
864                 # SCMSGS
865                 # @explanation
866                 # The file specified in the PARFILE variable does not
867                 # exist any more.
868                 # @user_action
869                 # Create the directory and restore the parameter file. None if it was a lost mount.
870                 scds_syslog -p daemon.err -t $(syslog_tag) -m \
871                 "Function: validate_probe - Directory for the parameter file %s does not exist any more, a faiover will occur" \
872                 "${PARFILE}"
873 
874                 rc_val_probe=201
875         fi
876 
877         # source the parameter file
878 
879         . ${PARFILE}
880 
881         # Check the PGHOST variable.
882         # If the PGHOST variable starts with a / then it should be a directory which contains the socket file.
883         # If this directory is not available, a failover will be initiated.
884         # If it does not contain a / then it has to be a hostname or an address, in this case the hostname 
885         # has to respond to ping. 
886 
887         if [ -n "${PGHOST}" ]
888         then
889 
890                 # strip of leading spaces
891 
892                 PGHOST=`print ${PGHOST}|${SED} 's/^ *//'`
893 
894                 if ${ECHO} ${PGHOST} | ${GREP} "^/" >/dev/null 2>&1
895                 then
896         
897                         if [ ! -d "${PGHOST}" ]
898                         then
899                                 
900                                 # SCMSGS
901                                 # @explanation
902                                 # The directory specified in the PGHOST variable does not
903                                 # exist any more.
904                                 # @user_action
905                                 # Create the directory. None if it was a lost mount.
906                                 scds_syslog -p daemon.err -t $(syslog_tag) -m \
907                                 "Function: validate_probe - Directory for the socket file %s does not exist any more, a faiover will occur" \
908                                 "${PGHOST}"
909                                 rc_val_probe=201
910                         else
911                                 debug_message "Function: validate_probe - the directory in PGHOST exists"
912                         fi
913                 
914                 else
915 
916                         # If an address is specified, test it with ping. Initiate a failover if the address 
917                         # is unavailable.       
918                         # This needs to be done, because an unpingable address will cause the psql commands to hang
919                         # The test will detect accidentally unconfigured interfaces, just a failover or restart of 
920                         # the resource group can cure this problem.
921 
922                         # This ping test is here to prevent a predictable timeout, it is needed as long as 
923                         # SUNW.LogicalHost does not probe the ipaddresses. As soon as this changes the
924                         # ping test should be removed.
925 
926                         if ! ${PING} ${PGHOST} 1 >/dev/null 2>&1
927                         then
928                                 # SCMSGS
929                                 # @explanation
930                                 # The address specified in the PGHOST variable
931                                 # is unavailable.
932                                 # @user_action
933                                 # None, a failover will occur
934                                 scds_syslog -p daemon.err -t $(syslog_tag) -m \
935                                 "check_pgs: The host %s is not accessible, a failover will occur" \
936                                 "${PGHOST}"
937         
938                                 rc_val_probe=201
939                         else
940                                 debug_message "Function: validate_probe - the host in PGHOST is pingable"
941                         fi
942                 fi
943         fi
944 
945         debug_message "Function: validate_probe - End"
946         return $rc_val_probe
947 }
948 
949 val_parfile()
950 {
951         # Common valdation for parameter file
952         #
953         # If the parameter is in $2, there will be an intensive validation.
954         # If $2 is empty, the validation will just check for the existance of the file.
955         
956         debug_message "Function: val_parfile - Begin"
957         ${SET_DEBUG}
958 
959         rc_val_parfile=0
960 
961         # Validate that parameter file exists
962 
963         PARFILE=${1}
964         PARLIST=${2}
965 
966         if [ ! -f "${PARFILE}" ]; then
967                 # SCMSGS
968                 # @explanation
969                 # The parameter file does not exist in the parameter file
970                 # directory.
971                 # @user_action
972                 # Restore the parameter file or re-register the resource.
973                 scds_syslog -p daemon.err -t $(syslog_tag) -m \
974                   "Function: val_parfile - File %s does not exist" \
975                   "${PARFILE}"
976                 rc_val_parfile=1
977         else
978                 debug_message "Function: val_parfile - ${PARFILE} exists"       
979         fi
980 
981         # Test the semantics only, if the parameter list is specified.
982         # This should not be done when called from validate_probe.
983 
984         if [ -n "${2}" ]
985         then
986         
987                 # Test if the parameter file is a valid ksh script
988         
989                 if ! ksh -n ${PARFILE} >/dev/null 2>&1
990                 then
991                         # SCMSGS
992                         # @explanation
993                         # The parameter file is not a valid shell script.
994                         # @user_action
995                         # Correct the parameter file. It must pass ksh -n <file name>.
996                         scds_syslog -p daemon.err -t $(syslog_tag) -m \
997                           "Function: validate - Syntax errors in %s" \
998                           "${PARFILE}"
999                        rc_val_parfile=1
1000                 else
1001                         debug_message "Function: val_parfile - validated ${PARFILE}"    
1002                 fi
1003         
1004                 # Test if all the mandatory variables are included and set correctly in the parameter file
1005         
1006                 PARAMETERS=`${CAT} ${PARFILE} |grep -v "^#"|grep -v "^ "|nawk -F= '{print $1}'`
1007         
1008                 for i in ${PARLIST}
1009                 do
1010                         if ! `echo ${PARAMETERS} |grep ${i} > /dev/null `; then
1011                                 # SCMSGS
1012                                 # @explanation
1013                                 # The referenced necessary parameter is not mentioned
1014                                 # in the parameter file.
1015                                 # @user_action
1016                                 # Specify the parameter as a key value pair in the
1017                                 # parameter file.
1018                                 scds_syslog -p daemon.err -t $(syslog_tag) -m \
1019                                   "Function: val_parfile - %s not specified in %s, which is required" \
1020                                   "${i}" "${PARFILE}"
1021                                 rc_val_parfile=1
1022                         else
1023                                 debug_message "Function: val_parfile - ${i} included in ${PARFILE}"     
1024                         fi
1025                 done
1026         fi
1027 
1028         debug_message "Function: val_parfile - End"
1029         return ${rc_val_parfile}
1030 }
1031 
1032 
1033 Remove_shared_memory()
1034 {
1035         debug_message "Function: Remove_shared_memory - Start called with the following arguments "$*
1036         ${SET_DEBUG}
1037 
1038         User=$1
1039 
1040         if [ -n "${DEBUG}" ]
1041         then
1042                 debug_message "IPC Status of the current zone BEFORE removal of non-attached segments created by user ${User}"
1043 
1044                 # remove the logfile, before creating it to survive potential noclobber settings
1045 
1046                 ${RM} ${LOGFILE} 2>/dev/null
1047                 ${IPCS} -mcopb > ${LOGFILE}
1048 
1049                 log_message debug ipcs
1050                 ${RM} ${LOGFILE}
1051         fi
1052 
1053         ${IPCS} -mcopb |${GREP} " ${User} "| ${AWK} ' \
1054         {if (NF == 12 && $9 == 0 ) print $2,$5,$6,$11,$12; else \
1055         if (NF == 11  && $8 == 0 ) print $1,$4,$5,$10,$11 }' | \
1056         while read SHMID SHMUSER GROUP CPID LPID
1057         do
1058                 if ${PS} -p ${CPID} > /dev/null
1059                 then
1060                         debug_message "PostgresSQL SHMID: ${SHMID} - CPID ${CPID} is running"
1061                 else
1062                         if ${PS} -p ${LPID} > /dev/null
1063                         then
1064                                 debug_message "PostgresSQL SHMID: ${SHMID} - LPID ${LPID} is running"
1065                         else
1066                                 SHMID=`${ECHO} ${SHMID} | ${TR} 'm' ' '`
1067                                 ${IPCRM} -m ${SHMID} > /dev/null
1068                                 debug_message "PostgreSQL SHMID: ${SHMID} - removed"
1069                                 flag=deleted
1070                         fi
1071                 fi
1072         done
1073 
1074         if [ "${flag}" ]; then
1075                 # SCMSGS
1076                 # @explanation
1077                 # Remaining IPC shared memory segments have been removed.
1078                 # These segements are a leftover of the previous PostgreSQL
1079                 # instance.
1080                 # @user_action
1081                 # None
1082                 scds_syslog -p daemon.notice -t $(syslog_tag) -m \
1083                 "All PostgreSQL non-attached IPC shared memory segments removed"
1084         fi
1085 
1086         if [ -n "${DEBUG}" ]
1087         then
1088                 debug_message "IPC Status AFTER removal of non-attached segments created by user ${User}"
1089 
1090                 ${IPCS} -mcopb > ${LOGFILE}
1091 
1092                 log_message debug ipcs
1093                 $RM ${LOGFILE}
1094         fi
1095 
1096         debug_message "Function: Remove_shared_memory - End"
1097 }
1098 
1099 start_pgs()
1100 {
1101         #
1102         # Start PostgreSQL
1103         #
1104 
1105         debug_message "Function: start_pgs - Begin"
1106         ${SET_DEBUG}
1107 
1108         # construct the necessary redirection and source variables
1109 
1110         set_shell_specifics
1111 
1112         # Define the appropriate newtask command.
1113         # The Project will be derived according to the call method,
1114         # either from the smf service or from the cluster resource / resource group
1115 
1116         srm_function ${USER}
1117 
1118         # remove shared memory which may be there from a killed postmaster
1119 
1120         Remove_shared_memory ${USER}
1121 
1122         # construct the necessary environment variables
1123 
1124         LIBPATH=
1125         if [ -n "${LD_LIBRARY_PATH}" ]
1126         then
1127                 LIBPATH="${ENV} LD_LIBRARY_PATH=${LD_LIBRARY_PATH}"
1128         fi
1129 
1130         PORT="PGPORT=${PGPORT}"
1131         DATA="PGDATA=${PGDATA}"
1132 
1133         # remove the logfile, before creating it to survive potential noclobber settings
1134 
1135         ${RM} ${LOGFILE} 2>/dev/null
1136 
1137         #
1138         # Start PostgreSql, the postmaster searches for databases in the directory of the PGDATA variable,
1139         # and it will listen on the port of the PGORT varaible. The logfile will be stored in the file name
1140         # contained in the PGLOGFILE variable.
1141         # The postmaster is started via the pg_ctl utility.
1142         #
1143 
1144         if [ -z "${SMF_FMRI}" ]
1145         then
1146                 ${SU} - ${USER} -c " ${ENVSC} ${TASK_COMMAND} ${LIBPATH} ${ENV} ${DATA} ${ENV} ${PORT} ${PGROOT}/bin/pg_ctl start -l ${PGLOGFILE} ${OUTPUT}" > /dev/null
1147                 rc_start_command=$?
1148         else
1149 
1150                 # start under the right user in an smf manifest you source a ksh script, regardless of the users login shell
1151 
1152                 if [ -n "${ENVSCRIPT}" ]
1153                 then
1154                         . ${ENVSCRIPT}
1155                 fi
1156 
1157                 ${LIBPATH} ${ENV} ${DATA} ${ENV} ${PORT} ${PGROOT}/bin/pg_ctl start -l ${PGLOGFILE} >${LOGFILE} 2>&1
1158                 rc_start_command=$?
1159         fi
1160 
1161         debug_message "Function: start_pgs - End"
1162         return ${rc_start_command}
1163 }
1164 
1165 stop_pgs()
1166 {
1167         #
1168         # Stop PostgreSQL
1169         #
1170 
1171         debug_message "Function: stop_pgs - Begin"
1172         ${SET_DEBUG}
1173 
1174         # construct the necessary redirection and source variables
1175 
1176         set_shell_specifics
1177 
1178         # Define the appropriate newtask command.
1179         # The Project will be derived according to the call method,
1180         # either from the smf service or from the cluster resource / resource group
1181 
1182         srm_function ${USER}
1183 
1184         # construct the necessary environment variables
1185 
1186         LIBPATH=
1187         if [ -n "${LD_LIBRARY_PATH}" ]
1188         then
1189                 LIBPATH="${ENV} LD_LIBRARY_PATH=${LD_LIBRARY_PATH}"
1190         fi
1191 
1192         PORT="PGPORT=${PGPORT}"
1193         DATA="PGDATA=${PGDATA}"
1194 
1195         # remember the process id of the parent postmaster
1196 
1197         pgrspid=`${HEAD} -1 ${PGDATA}/postmaster.pid`
1198 
1199         # remove the logfile, before creating it to survive potential noclobber settings
1200 
1201         ${RM} ${LOGFILE} 2>/dev/null
1202 
1203         # Perform a fast shutdown first. The server is specified by the triple user, PGPORT and the PGDATA variable.
1204         # The fast shutdown will disconnect the clients and stop the server processes.
1205 
1206         # SCMSGS
1207         # @explanation
1208         # The PostgreSQL database server is shut down with the specified
1209         # option.
1210         # @user_action
1211         # None
1212         scds_syslog -p daemon.notice -t $(syslog_tag) -m \
1213              "stop_pgs: Stop PostgreSQL with the option %s " \
1214              "fast"
1215         
1216         if [ -z "${SMF_FMRI}" ]
1217         then
1218                 ${SU} - ${USER} -c " ${ENVSC} ${TASK_COMMAND} ${LIBPATH} ${ENV} ${DATA} ${ENV} ${PORT} ${PGROOT}/bin/pg_ctl stop -m fast ${OUTPUT}" > /dev/null
1219                 rc_stop_command=$?
1220         else
1221 
1222                 # stop under the right user in a smf manifest
1223 
1224                 if [ -n "${ENVSCRIPT}" ]
1225                 then
1226                         . ${ENVSCRIPT}
1227                 fi
1228 
1229                 ${LIBPATH} ${ENV} ${DATA} ${ENV} ${PORT} ${PGROOT}/bin/pg_ctl stop -m fast >${LOGFILE} 2>&1
1230                 rc_stop_command=$?
1231         fi
1232 
1233         # Determine if postgres is really stopped, if not perform an immediate shutdown.
1234         # The server is specified by the triple user, PGPORT and the PGDATA variable.
1235         # The immediate shutdown stop the server processes without disconnecting the clients.
1236         
1237         if ${PS} -u ${USER} -o pid,ppid |${GREP} -w ${pgrspid}>/dev/null
1238         then
1239                 
1240                 scds_syslog -p daemon.notice -t $(syslog_tag) -m \
1241                      "stop_pgs: Stop PostgreSQL with the option %s " \
1242                      "immediate"
1243                 
1244                 if [ -z "${SMF_FMRI}" ]
1245                 then
1246                         ${SU} - ${USER} -c "${ENVSC} ${TASK_COMMAND} ${LIBPATH} ${ENV} ${DATA} ${ENV} ${PORT} ${PGROOT}/bin/pg_ctl stop -m immediate ${OUTPUT_APP}" > /dev/null
1247                         rc_stop_command=$?
1248                 else
1249         
1250                         # stop under the right user in a smf manifest
1251         
1252                         if [ -n "${ENVSCRIPT}" ]
1253                         then
1254                                 . ${ENVSCRIPT}
1255                         fi
1256 
1257                         ${LIBPATH} ${ENV} ${DATA} ${ENV} ${PORT} ${PGROOT}/bin/pg_ctl stop -m immediate >>${LOGFILE} 2>&1
1258                         rc_stop_command=$?
1259                 fi
1260         fi
1261 
1262         # determine if postgres is really stopped, if not kill the parent of the postmaster with -9
1263         
1264         if ${PS} -u ${USER} -o pid,ppid |${GREP} -w ${pgrspid}>/dev/null
1265         then
1266                 # SCMSGS
1267                 # @explanation
1268                 # The previous stop attempts for PostgreSQL failed. The server
1269                 # is now killed with kill -9.
1270                 # @user_action
1271                 # None
1272                 scds_syslog -p daemon.notice -t $(syslog_tag) -m \
1273                      "stop_pgs: Stop PostgreSQL process %s with kill -9 " \
1274                      "${pgrspid}"
1275 
1276                 ${KILL} -9 ${pgrspid}
1277                 rc_stop_command=$?
1278         fi
1279 
1280         debug_message "Function: stop_pgs - End"
1281         return ${rc_stop_command}
1282 }
1283 
1284 check_pgs()
1285 {
1286         # 
1287         # Probe PostgreSQL
1288         #
1289 
1290         debug_message "Function: check_pgs - Begin"
1291         ${SET_DEBUG}
1292 
1293         rc_check_command=0
1294 
1295         # construct the necessary redirection and source variables
1296 
1297         set_shell_specifics
1298 
1299         # Define the appropriate newtask command.
1300         # The Project will be derived according to the call method,
1301         # either from the smf service or from the cluster resource / resource group
1302 
1303         srm_function ${USER}
1304 
1305         # Determine if the gds start process is already completed, or the database start is far enough to allow connections
1306         # The criteria is the existence of the postmaster.pid file in the PGDATA directory.
1307         # As long as the GDS start is not complete, probe error messages will be suppressed.
1308 
1309         wait_for_online=0
1310         
1311         ${PGREP} -u root -f "gds_svc_start .*-R ${RESOURCE} " >/dev/null
1312         if [ ${?} -eq 0 ]
1313         then
1314                 debug_message "Function: check_pgs - wait for online detected" 
1315                 wait_for_online=1
1316                 if [ -f ${PGDATA}/postmaster.pid ]
1317                 then
1318                         pgrspid=`${HEAD} -1 ${PGDATA}/postmaster.pid`
1319                         if ! ${PS} -u ${USER} -o pid,ppid |${GREP} -w ${pgrspid}>/dev/null
1320                         then
1321                                 debug_message "Function: check_pgs - wait for online detected, postmaster is not running" 
1322                                 rc_check_command=100
1323                         else
1324                                 debug_message "Function: check_pgs - wait for online detected, postmaster is running proceed with normal checks" 
1325                         fi
1326                 else
1327                         debug_message "Function: check_pgs - wait for online detected, postmaster pid file ${PGDATA}/postmaster.pid not found" 
1328                         rc_check_command=100
1329                 fi
1330         fi
1331 
1332         if [ ${rc_check_command} -ne 0 ]
1333         then
1334                 debug_message "Function: check_pgs - End"
1335                 return ${rc_check_command}
1336         fi
1337 
1338         # construct the necessary environment variables
1339 
1340         LIBPATH=
1341         if [ -n "${LD_LIBRARY_PATH}" ]
1342         then
1343                 LIBPATH="${ENV} LD_LIBRARY_PATH=${LD_LIBRARY_PATH}"
1344         fi
1345 
1346         # the variable PASSWORD is set to the database user login passowrd
1347 
1348         PASSWORD=
1349         if [ -n "${SCPASS}" ]
1350         then
1351                 PASSWORD="${ENV} PGPASSWORD=${SCPASS}"
1352         fi
1353         PORT="PGPORT=${PGPORT}"
1354         DATA="PGDATA=${PGDATA}"
1355 
1356         if [ -n "${PGHOST}" ]
1357         then
1358                 HOST="${ENV} PGHOST=${PGHOST}"
1359         fi
1360 
1361         # remove the temporary output files before creating them to survive potential noclobber settings
1362 
1363         ${RM} /tmp/${RESOURCE}-${USER}-cat-out 2>/dev/null
1364         ${RM} /tmp/${RESOURCE}-${USER}-cat-err 2>/dev/null
1365         ${RM} /tmp/${RESOURCE}-${USER}-tbl-out 2>/dev/null
1366         ${RM} /tmp/${RESOURCE}-${USER}-tbl-err 2>/dev/null
1367 
1368         # construct the sql commands for the test
1369 
1370         DBCAT="select datname from pg_database"
1371         DBTRUNC="truncate ${SCTABLE}"
1372         DBINS="insert into ${SCTABLE} (sccol) values('hello im there')"
1373         DBSEL="select * from ${SCTABLE}"
1374 
1375         # check if the catalog of the postgres database is accessible
1376 
1377         debug_message "Function: check_pgs - check if the database catalog is accessible with ${SCDB} "
1378 
1379         ${SU} - ${USER} -c "${OPEN_BRACKET} ${TASK_COMMAND} ${LIBPATH} $HOST ${PASSWORD} ${ENV} ${DATA} ${ENV} ${PORT} ${PGROOT}/bin/psql -d ${SCDB} -U ${SCUSER} -c \"${DBCAT}\" ${CAT_OUTPUT} ${CLOSE_BRACKET} ${CAT_ERRPUT} " >/dev/null
1380         rc_check_command=$?
1381 
1382         # Return ${NOCONRET} if the connect fails, otherwise proceed with the check
1383 
1384         if [ ${rc_check_command} -ne 0 ]
1385         then
1386 
1387                 debug_message "Function: check_pgs - connect to database ${SCDB} failed"
1388 
1389                 if [ ${wait_for_online} -eq 0 ]
1390                 then
1391                         cp /tmp/${RESOURCE}-${USER}-cat-err ${LOGFILE}
1392                         log_message err "check_pgs: rc<${NOCONRET}>"
1393                 fi
1394 
1395                 debug_message "Function: check_pgs - End"
1396                 rc_check_command=${NOCONRET} 
1397 
1398         else
1399 
1400                 # check if the test database is in the database catalog
1401 
1402                 if ! ${EGREP} " ${SCDB}$| ${SCDB} " /tmp/${RESOURCE}-${USER}-cat-out >/dev/null 2>&1
1403                 then
1404 
1405                         debug_message "Function: check_pgs - The test database ${SCDB} is not in the database catalog"
1406 
1407                         if [ ${wait_for_online} -eq 0 ]
1408                         then
1409                                 # SCMSGS
1410                                 # @explanation
1411                                 # The database to monitor does not exist in
1412                                 # the database catalog.
1413                                 # @user_action
1414                                 # Fix the parameter file to contain the right
1415                                 # database name in the SCDB variable, or
1416                                 # prepare the database.
1417                                 scds_syslog -p daemon.err -t $(syslog_tag) -m \
1418                                         "check_pgs: The test database %s is not in the database catalog" \
1419                                         "${SCDB}"
1420                         fi
1421 
1422                         rc_check_command=100
1423                 else
1424                         debug_message "Function: check_pgs - check if the database catalog is accessible with ${SCDB} was successful"
1425                 fi
1426         fi
1427 
1428         # manipulate the table SCTABLE and exit 100 if the inserted string is not there, exit 10 if the connection to
1429         # the database fails
1430 
1431         if [ ${rc_check_command} -eq 0 ]
1432         then
1433 
1434                 debug_message "Function: check_pgs - manipulate the table ${SCTABLE}"
1435 
1436                 ${SU} - ${USER} -c "${OPEN_BRACKET} ${TASK_COMMAND} ${LIBPATH} $HOST ${PASSWORD} ${ENV} ${DATA} ${ENV} ${PORT} ${PGROOT}/bin/psql -d ${SCDB} -U ${SCUSER} -c \"${DBTRUNC};${DBINS};${DBSEL}\" ${TBL_OUTPUT} ${CLOSE_BRACKET} ${TBL_ERRPUT} " >/dev/null
1437                 rc_check_command=$?
1438 
1439                 # Return ${NOCONRET} if the connect fails, otherwise proceed with the check
1440 
1441                 if [ ${rc_check_command} -ne 0 ]
1442                 then
1443 
1444                         debug_message "Function: check_pgs - connect to database ${SCDB} failed"
1445 
1446                         if [ ${wait_for_online} -eq 0 ]
1447                         then
1448                                 cp /tmp/${RESOURCE}-${USER}-tbl-err ${LOGFILE}
1449                                 log_message err "check_pgs: rc<${NOCONRET}>"
1450                         fi
1451 
1452                         rc_check_command=${NOCONRET}
1453 
1454                 else
1455 
1456                         # check if the test table could be manipulated successfully
1457 
1458 
1459                         if ! ${GREP} "hello im there" /tmp/${RESOURCE}-${USER}-tbl-out >/dev/null 2>&1
1460                         then
1461 
1462                                 if [ ${wait_for_online} -eq 0 ]
1463                                 then
1464                                         # SCMSGS
1465                                         # @explanation
1466                                         # The monitoring action on the
1467                                         # specified table failed.
1468                                         # @user_action
1469                                         # None
1470                                         scds_syslog -p daemon.err -t $(syslog_tag) -m \
1471                                                 "check_pgs: The test database manipulation failed for database %s, user %s and table %s" \
1472                                                 "${SCDB}" "${SCUSER}" "$SCTABLE}"
1473                                 fi
1474 
1475                                 rc_check_command=100
1476                                 debug_message "Function: check_pgs - manipulation of the table ${SCTABLE} failed"
1477 
1478                         else
1479 
1480                                 debug_message "Function: check_pgs - manipulation of the table ${SCTABLE} successful"
1481 
1482                         fi
1483                 fi
1484         fi      
1485 
1486         debug_message "Function: check_pgs - End"
1487         return ${rc_check_command}
1488 }