zkServer.sh 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. #!/usr/bin/env bash
  2. # Licensed to the Apache Software Foundation (ASF) under one or more
  3. # contributor license agreements. See the NOTICE file distributed with
  4. # this work for additional information regarding copyright ownership.
  5. # The ASF licenses this file to You under the Apache License, Version 2.0
  6. # (the "License"); you may not use this file except in compliance with
  7. # the License. You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing, software
  12. # distributed under the License is distributed on an "AS IS" BASIS,
  13. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. # See the License for the specific language governing permissions and
  15. # limitations under the License.
  16. #
  17. # If this scripted is run out of /usr/bin or some other system bin directory
  18. # it should be linked to and not copied. Things like java jar files are found
  19. # relative to the canonical path of this script.
  20. #
  21. # use POSIX interface, symlink is followed automatically
  22. ZOOBIN="${BASH_SOURCE-$0}"
  23. ZOOBIN="$(dirname "${ZOOBIN}")"
  24. ZOOBINDIR="$(cd "${ZOOBIN}"; pwd)"
  25. if [ -e "$ZOOBIN/../libexec/zkEnv.sh" ]; then
  26. . "$ZOOBINDIR"/../libexec/zkEnv.sh
  27. else
  28. . "$ZOOBINDIR"/zkEnv.sh
  29. fi
  30. # See the following page for extensive details on setting
  31. # up the JVM to accept JMX remote management:
  32. # http://java.sun.com/javase/6/docs/technotes/guides/management/agent.html
  33. # by default we allow local JMX connections
  34. if [ "x$JMXLOCALONLY" = "x" ]
  35. then
  36. JMXLOCALONLY=false
  37. fi
  38. if [ "x$JMXDISABLE" = "x" ] || [ "$JMXDISABLE" = 'false' ]
  39. then
  40. echo "ZooKeeper JMX enabled by default" >&2
  41. if [ "x$JMXPORT" = "x" ]
  42. then
  43. # for some reason these two options are necessary on jdk6 on Ubuntu
  44. # accord to the docs they are not necessary, but otw jconsole cannot
  45. # do a local attach
  46. ZOOMAIN="-Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.local.only=$JMXLOCALONLY org.apache.zookeeper.server.quorum.QuorumPeerMain"
  47. else
  48. if [ "x$JMXAUTH" = "x" ]
  49. then
  50. JMXAUTH=false
  51. fi
  52. if [ "x$JMXSSL" = "x" ]
  53. then
  54. JMXSSL=false
  55. fi
  56. if [ "x$JMXLOG4J" = "x" ]
  57. then
  58. JMXLOG4J=true
  59. fi
  60. echo "ZooKeeper remote JMX Port set to $JMXPORT" >&2
  61. echo "ZooKeeper remote JMX authenticate set to $JMXAUTH" >&2
  62. echo "ZooKeeper remote JMX ssl set to $JMXSSL" >&2
  63. echo "ZooKeeper remote JMX log4j set to $JMXLOG4J" >&2
  64. if [ "x$JMXHOSTNAME" = "x" ]
  65. then
  66. ZOOMAIN="-Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.port=$JMXPORT -Dcom.sun.management.jmxremote.authenticate=$JMXAUTH -Dcom.sun.management.jmxremote.ssl=$JMXSSL -Dzookeeper.jmx.log4j.disable=$JMXLOG4J org.apache.zookeeper.server.quorum.QuorumPeerMain"
  67. else
  68. echo "ZooKeeper remote JMX Hostname set to $JMXHOSTNAME" >&2
  69. ZOOMAIN="-Dcom.sun.management.jmxremote -Djava.rmi.server.hostname=$JMXHOSTNAME -Dcom.sun.management.jmxremote.port=$JMXPORT -Dcom.sun.management.jmxremote.authenticate=$JMXAUTH -Dcom.sun.management.jmxremote.ssl=$JMXSSL -Dzookeeper.jmx.log4j.disable=$JMXLOG4J org.apache.zookeeper.server.quorum.QuorumPeerMain"
  70. fi
  71. fi
  72. else
  73. echo "JMX disabled by user request" >&2
  74. ZOOMAIN="org.apache.zookeeper.server.quorum.QuorumPeerMain"
  75. fi
  76. if [ "x$SERVER_JVMFLAGS" != "x" ]
  77. then
  78. JVMFLAGS="$SERVER_JVMFLAGS $JVMFLAGS"
  79. fi
  80. if [ "x$2" != "x" ]
  81. then
  82. ZOOCFG="$ZOOCFGDIR/$2"
  83. fi
  84. # if we give a more complicated path to the config, don't screw around in $ZOOCFGDIR
  85. if [ "x$(dirname "$ZOOCFG")" != "x$ZOOCFGDIR" ]
  86. then
  87. ZOOCFG="$2"
  88. fi
  89. if $cygwin
  90. then
  91. ZOOCFG=`cygpath -wp "$ZOOCFG"`
  92. # cygwin has a "kill" in the shell itself, gets confused
  93. KILL=/bin/kill
  94. else
  95. KILL=kill
  96. fi
  97. echo "Using config: $ZOOCFG" >&2
  98. case "$OSTYPE" in
  99. *solaris*)
  100. GREP=/usr/xpg4/bin/grep
  101. ;;
  102. *)
  103. GREP=grep
  104. ;;
  105. esac
  106. ZOO_DATADIR="$($GREP "^[[:space:]]*dataDir" "$ZOOCFG" | sed -e 's/.*=//')"
  107. ZOO_DATADIR="$(echo -e "${ZOO_DATADIR}" | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//')"
  108. ZOO_DATALOGDIR="$($GREP "^[[:space:]]*dataLogDir" "$ZOOCFG" | sed -e 's/.*=//')"
  109. # iff autocreate is turned off and the datadirs don't exist fail
  110. # immediately as we can't create the PID file, etc..., anyway.
  111. if [ -n "$ZOO_DATADIR_AUTOCREATE_DISABLE" ]; then
  112. if [ ! -d "$ZOO_DATADIR/version-2" ]; then
  113. echo "ZooKeeper data directory is missing at $ZOO_DATADIR fix the path or run initialize"
  114. exit 1
  115. fi
  116. if [ -n "$ZOO_DATALOGDIR" ] && [ ! -d "$ZOO_DATALOGDIR/version-2" ]; then
  117. echo "ZooKeeper txnlog directory is missing at $ZOO_DATALOGDIR fix the path or run initialize"
  118. exit 1
  119. fi
  120. ZOO_DATADIR_AUTOCREATE="-Dzookeeper.datadir.autocreate=false"
  121. fi
  122. if [ -z "$ZOOPIDFILE" ]; then
  123. if [ ! -d "$ZOO_DATADIR" ]; then
  124. mkdir -p "$ZOO_DATADIR"
  125. fi
  126. ZOOPIDFILE="$ZOO_DATADIR/zookeeper_server.pid"
  127. else
  128. # ensure it exists, otw stop will fail
  129. mkdir -p "$(dirname "$ZOOPIDFILE")"
  130. fi
  131. if [ ! -w "$ZOO_LOG_DIR" ] ; then
  132. mkdir -p "$ZOO_LOG_DIR"
  133. fi
  134. ZOO_LOG_FILE=${ZOO_LOG_FILE:-zookeeper-$USER-server-$HOSTNAME.log}
  135. _ZOO_DAEMON_OUT="$ZOO_LOG_DIR/zookeeper-$USER-server-$HOSTNAME.out"
  136. case $1 in
  137. start)
  138. echo -n "Starting zookeeper ... "
  139. if [ -f "$ZOOPIDFILE" ]; then
  140. if kill -0 `cat "$ZOOPIDFILE"` > /dev/null 2>&1; then
  141. echo $command already running as process `cat "$ZOOPIDFILE"`.
  142. exit 1
  143. fi
  144. fi
  145. nohup "$JAVA" $ZOO_DATADIR_AUTOCREATE "-Dzookeeper.log.dir=${ZOO_LOG_DIR}" \
  146. "-Dzookeeper.log.file=${ZOO_LOG_FILE}" \
  147. -XX:+HeapDumpOnOutOfMemoryError -XX:OnOutOfMemoryError='kill -9 %p' \
  148. -cp "$CLASSPATH" $JVMFLAGS $ZOOMAIN "$ZOOCFG" > "$_ZOO_DAEMON_OUT" 2>&1 < /dev/null &
  149. if [ $? -eq 0 ]
  150. then
  151. case "$OSTYPE" in
  152. *solaris*)
  153. /bin/echo "${!}\\c" > "$ZOOPIDFILE"
  154. ;;
  155. *)
  156. /bin/echo -n $! > "$ZOOPIDFILE"
  157. ;;
  158. esac
  159. if [ $? -eq 0 ];
  160. then
  161. sleep 1
  162. pid=$(cat "${ZOOPIDFILE}")
  163. if ps -p "${pid}" > /dev/null 2>&1; then
  164. echo STARTED
  165. else
  166. echo FAILED TO START
  167. exit 1
  168. fi
  169. else
  170. echo FAILED TO WRITE PID
  171. exit 1
  172. fi
  173. else
  174. echo SERVER DID NOT START
  175. exit 1
  176. fi
  177. ;;
  178. start-foreground)
  179. ZOO_CMD=(exec "$JAVA")
  180. if [ "${ZOO_NOEXEC}" != "" ]; then
  181. ZOO_CMD=("$JAVA")
  182. fi
  183. "${ZOO_CMD[@]}" $ZOO_DATADIR_AUTOCREATE "-Dzookeeper.log.dir=${ZOO_LOG_DIR}" \
  184. "-Dzookeeper.log.file=${ZOO_LOG_FILE}" \
  185. -XX:+HeapDumpOnOutOfMemoryError -XX:OnOutOfMemoryError='kill -9 %p' \
  186. -cp "$CLASSPATH" $JVMFLAGS $ZOOMAIN "$ZOOCFG"
  187. ;;
  188. print-cmd)
  189. echo "\"$JAVA\" $ZOO_DATADIR_AUTOCREATE -Dzookeeper.log.dir=\"${ZOO_LOG_DIR}\" \
  190. -Dzookeeper.log.file=\"${ZOO_LOG_FILE}\" \
  191. -XX:+HeapDumpOnOutOfMemoryError -XX:OnOutOfMemoryError='kill -9 %p' \
  192. -cp \"$CLASSPATH\" $JVMFLAGS $ZOOMAIN \"$ZOOCFG\" > \"$_ZOO_DAEMON_OUT\" 2>&1 < /dev/null"
  193. ;;
  194. stop)
  195. echo -n "Stopping zookeeper ... "
  196. if [ ! -f "$ZOOPIDFILE" ]
  197. then
  198. echo "no zookeeper to stop (could not find file $ZOOPIDFILE)"
  199. else
  200. $KILL $(cat "$ZOOPIDFILE")
  201. rm "$ZOOPIDFILE"
  202. sleep 1
  203. echo STOPPED
  204. fi
  205. exit 0
  206. ;;
  207. version)
  208. ZOOMAIN=org.apache.zookeeper.version.VersionInfoMain
  209. $JAVA -cp "$CLASSPATH" $ZOOMAIN 2> /dev/null
  210. ;;
  211. restart)
  212. shift
  213. "$0" stop ${@}
  214. sleep 3
  215. "$0" start ${@}
  216. ;;
  217. status)
  218. # -q is necessary on some versions of linux where nc returns too quickly, and no stat result is output
  219. isSSL="false"
  220. clientPortAddress=`$GREP "^[[:space:]]*clientPortAddress[^[:alpha:]]" "$ZOOCFG" | sed -e 's/.*=//'`
  221. if ! [ $clientPortAddress ]
  222. then
  223. clientPortAddress="localhost"
  224. fi
  225. clientPort=`$GREP "^[[:space:]]*clientPort[^[:alpha:]]" "$ZOOCFG" | sed -e 's/.*=//'`
  226. if ! [[ "$clientPort" =~ ^[0-9]+$ ]]
  227. then
  228. dataDir=`$GREP "^[[:space:]]*dataDir" "$ZOOCFG" | sed -e 's/.*=//'`
  229. myid=`cat "$dataDir/myid" 2> /dev/null`
  230. if ! [[ "$myid" =~ ^[0-9]+$ ]] ; then
  231. echo "myid could not be determined, will not able to locate clientPort in the server configs."
  232. else
  233. clientPortAndAddress=`$GREP "^[[:space:]]*server.$myid=.*;.*" "$ZOOCFG" | sed -e 's/.*=//' | sed -e 's/.*;//'`
  234. if [ ! "$clientPortAndAddress" ] ; then
  235. echo "Client port not found in static config file. Looking in dynamic config file."
  236. dynamicConfigFile=`$GREP "^[[:space:]]*dynamicConfigFile" "$ZOOCFG" | sed -e 's/.*=//'`
  237. clientPortAndAddress=`$GREP "^[[:space:]]*server.$myid=.*;.*" "$dynamicConfigFile" | sed -e 's/.*=//' | sed -e 's/.*;//'`
  238. fi
  239. if [ ! "$clientPortAndAddress" ] ; then
  240. echo "Client port not found in the server configs"
  241. else
  242. if [[ "$clientPortAndAddress" =~ ^.*:[0-9]+ ]] ; then
  243. if [[ "$clientPortAndAddress" =~ \[.*\]:[0-9]+ ]] ; then
  244. # Extracts address from address:port for example extracts 127::1 from "[127::1]:2181"
  245. clientPortAddress=`echo "$clientPortAndAddress" | sed -e 's|\[||' | sed -e 's|\]:.*||'`
  246. else
  247. clientPortAddress=`echo "$clientPortAndAddress" | sed -e 's/:.*//'`
  248. fi
  249. fi
  250. clientPort=`echo "$clientPortAndAddress" | sed -e 's/.*://'`
  251. fi
  252. fi
  253. fi
  254. if [ ! "$clientPort" ] ; then
  255. echo "Client port not found. Looking for secureClientPort in the static config."
  256. secureClientPort=`$GREP "^[[:space:]]*secureClientPort[^[:alpha:]]" "$ZOOCFG" | sed -e 's/.*=//'`
  257. if [ "$secureClientPort" ] ; then
  258. isSSL="true"
  259. clientPort=$secureClientPort
  260. clientPortAddress=`$GREP "^[[:space:]]*secureClientPortAddress[^[:alpha:]]" "$ZOOCFG" | sed -e 's/.*=//'`
  261. if ! [ $clientPortAddress ]
  262. then
  263. clientPortAddress="localhost"
  264. fi
  265. else
  266. echo "Unable to find either secure or unsecure client port in any configs. Terminating."
  267. exit 1
  268. fi
  269. fi
  270. echo "Client port found: $clientPort. Client address: $clientPortAddress. Client SSL: $isSSL."
  271. STAT=`"$JAVA" "-Dzookeeper.log.dir=${ZOO_LOG_DIR}" "-Dzookeeper.log.file=${ZOO_LOG_FILE}" \
  272. -cp "$CLASSPATH" $CLIENT_JVMFLAGS $JVMFLAGS org.apache.zookeeper.client.FourLetterWordMain \
  273. $clientPortAddress $clientPort srvr $isSSL 2> /dev/null \
  274. | $GREP Mode`
  275. if [ "x$STAT" = "x" ]
  276. then
  277. if [ "$isSSL" = "true" ] ; then
  278. echo " "
  279. echo "Note: We used secureClientPort ($secureClientPort) to establish connection, but we failed. The 'status'"
  280. echo " command establishes a client connection to the server to execute diagnostic commands. Please make sure you"
  281. echo " provided all the Client SSL connection related parameters in the CLIENT_JVMFLAGS environment variable! E.g.:"
  282. echo " CLIENT_JVMFLAGS=\"-Dzookeeper.clientCnxnSocket=org.apache.zookeeper.ClientCnxnSocketNetty"
  283. echo " -Dzookeeper.ssl.trustStore.location=/tmp/clienttrust.jks -Dzookeeper.ssl.trustStore.password=password"
  284. echo " -Dzookeeper.ssl.keyStore.location=/tmp/client.jks -Dzookeeper.ssl.keyStore.password=password"
  285. echo " -Dzookeeper.client.secure=true\" ./zkServer.sh status"
  286. echo " "
  287. fi
  288. echo "Error contacting service. It is probably not running."
  289. exit 1
  290. else
  291. echo $STAT
  292. exit 0
  293. fi
  294. ;;
  295. *)
  296. echo "Usage: $0 [--config <conf-dir>] {start|start-foreground|stop|version|restart|status|print-cmd}" >&2
  297. esac