Advertisement
Guest User

nginx

a guest
Oct 12th, 2011
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 21.36 KB | None | 0 0
  1. #!/bin/sh
  2. #
  3. # High-Availability nginx OCF resource agent
  4. #
  5. # nginx
  6. #
  7. # Description: starts/stops nginx servers.
  8. #
  9. # Author: Alan Robertson
  10. # Dejan Muhamedagic
  11. # This code is based significantly on the apache resource agent
  12. #
  13. # Support: linux-ha@lists.linux-ha.org
  14. #
  15. # License: GNU General Public License (GPL)
  16. #
  17. # Copyright: (C) 2002-2010 International Business Machines
  18. #
  19. #
  20. # Our parsing of the nginx config files is very rudimentary.
  21. # It'll work with lots of different configurations - but not every
  22. # possible configuration.
  23. #
  24. # Patches are being accepted ;-)
  25. #
  26. # OCF parameters:
  27. # OCF_RESKEY_configfile
  28. # OCF_RESKEY_httpd
  29. # OCF_RESKEY_port
  30. # OCF_RESKEY_options
  31. # OCF_RESKEY_status10regex
  32. # OCF_RESKEY_status10url
  33. # OCF_RESKEY_client
  34. # OCF_RESKEY_testurl
  35. # OCF_RESKEY_test20regex
  36. # OCF_RESKEY_test20conffile
  37. # OCF_RESKEY_test20name
  38. # OCF_RESKEY_external_monitor30_cmd
  39. #
  40. #
  41. # TO DO:
  42. # More extensive tests of extended monitor actions
  43. # Look at the --with-http_stub_status_module for validating
  44. # the configuration? (or is that automatically done?)
  45. # Checking could certainly result in better error
  46. # messages.
  47. # Allow for the fact that the config file and so on might all be
  48. # on shared disks - this affects the validate-all option.
  49.  
  50.  
  51. : ${OCF_FUNCTIONS_DIR=$OCF_ROOT/resource.d/heartbeat}
  52. . ${OCF_FUNCTIONS_DIR}/.ocf-shellfuncs
  53. HA_VARRUNDIR=${HA_VARRUN}
  54.  
  55. #######################################################################
  56. #
  57. # Configuration options - usually you don't need to change these
  58. #
  59. #######################################################################
  60. #
  61. NGINXDLIST="/usr/sbin/nginx /usr/local/sbin/nginx"
  62.  
  63. # default options for http clients
  64. # NB: We _always_ test a local resource, so it should be
  65. # safe to connect from the local interface.
  66. WGETOPTS="-O- -q -L --no-proxy --bind-address=127.0.0.1"
  67. CURLOPTS="-o - -Ss -L --interface lo"
  68.  
  69. LOCALHOST="http://localhost"
  70. NGINXDOPTS=""
  71. #
  72. #
  73. # End of Configuration options
  74. #######################################################################
  75.  
  76. CMD=`basename $0`
  77.  
  78. # The config-file-pathname is the pathname to the configuration
  79. # file for this web server. Various appropriate defaults are
  80. # assumed if no config file is specified.
  81. usage() {
  82. cat <<-!
  83. usage: $0 action
  84.  
  85. action:
  86. start start nginx
  87.  
  88. stop stop nginx
  89.  
  90. reload reload the nginx configuration
  91.  
  92. status return the status of web server, running or stopped
  93.  
  94. monitor return TRUE if the web server appears to be working.
  95. For this to be supported you must configure mod_status
  96. and give it a server-status URL - or configure what URL
  97. you wish to be monitored. You have to have installed
  98. either curl or wget for this to work.
  99.  
  100. meta-data show meta data message
  101.  
  102. validate-all validate the instance parameters
  103. !
  104. exit $1
  105. }
  106.  
  107. #
  108. # run the http client
  109. #
  110. curl_func() {
  111. cl_opts="$CURLOPTS $test_httpclient_opts"
  112. if
  113. [ x != "x$test_user" ]
  114. then
  115. echo "-u $test_user:$test_password" |
  116. curl -K - $cl_opts "$1"
  117. else
  118. curl $cl_opts "$1"
  119. fi
  120. }
  121. wget_func() {
  122. auth=""
  123. cl_opts="$WGETOPTS $test_httpclient_opts"
  124. [ x != "x$test_user" ] &&
  125. auth="--http-user=$test_user --http-passwd=$test_password"
  126. wget $auth $cl_opts "$1"
  127. }
  128. #
  129. # rely on whatever the user provided
  130. userdefined() {
  131. $test_httpclient $test_httpclient_opts "$1"
  132. }
  133.  
  134. #
  135. # find a good http client
  136. #
  137. findhttpclient() {
  138. # prefer curl if present...
  139. if
  140. [ "x$CLIENT" != x ]
  141. then
  142. echo "$CLIENT"
  143. elif
  144. which curl >/dev/null 2>&1
  145. then
  146. echo "curl"
  147. elif
  148. which wget >/dev/null 2>&1
  149. then
  150. echo "wget"
  151. else
  152. return 1
  153. fi
  154. }
  155. gethttpclient() {
  156. [ -z "$test_httpclient" ] &&
  157. test_httpclient=$ourhttpclient
  158. case "$test_httpclient" in
  159. curl|wget) echo ${test_httpclient}_func;; #these are supported
  160. *) echo userdefined;;
  161. esac
  162. }
  163.  
  164. # test configuration good?
  165. is_testconf_sane() {
  166. if
  167. [ "x$test_regex" = x -o "x$test_url" = x ]
  168. then
  169. ocf_log err "test regular expression or test url empty"
  170. return 1
  171. fi
  172. if
  173. [ "x$test_user$test_password" != x -a \( "x$test_user" = x -o "x$test_password" = x \) ]
  174. then
  175. ocf_log err "bad user authentication for extended test"
  176. return 1
  177. fi
  178. return 0
  179. }
  180. #
  181. # read the test definition from the config
  182. #
  183. readtestconf() {
  184. test_name="$1" # we look for this one or the first one if empty
  185. lcnt=0
  186. readdef=""
  187. test_url="" test_regex=""
  188. test_user="" test_password=""
  189. test_httpclient="" test_httpclient_opts=""
  190.  
  191. while
  192. read key value
  193. do
  194. lcnt=$((lcnt+1))
  195. if
  196. [ "$readdef" ]
  197. then
  198. case "$key" in
  199. "url") test_url="$value" ;;
  200. "user") test_user="$value" ;;
  201. "password") test_password="$value" ;;
  202. "client") test_httpclient="$value" ;;
  203. "client_opts") test_httpclient_opts="$value" ;;
  204. "match") test_regex="$value" ;;
  205. "end") break ;;
  206. "#"*|"") ;;
  207. *) ocf_log err "$lcnt: $key: unknown keyword"; return 1 ;;
  208. esac
  209. else
  210. [ "$key" = "test" ] &&
  211. [ -z "$test_name" -o "$test_name" = "$value" ] &&
  212. readdef=1
  213. fi
  214. done
  215. }
  216.  
  217. nginxcat() {
  218. awk '
  219. function procline() {
  220. split($0,a);
  221. if( a[1]~/^[Ii]nclude$/ ) {
  222. procinclude(a[2]);
  223. } else {
  224. if( a[1]=="root" ) {
  225. rootdir=a[2];
  226. gsub("\"","",rootdir);
  227. }
  228. print;
  229. }
  230. }
  231. function printfile(infile, a) {
  232. while( (getline<infile) > 0 ) {
  233. procline();
  234. }
  235. close(infile);
  236. }
  237. function allfiles(dir, cmd,f) {
  238. cmd="find -L "dir" -type f";
  239. while( ( cmd | getline f ) > 0 ) {
  240. printfile(f);
  241. }
  242. close(cmd);
  243. }
  244. function listfiles(pattern, cmd,f) {
  245. cmd="ls "pattern" 2>/dev/null";
  246. while( ( cmd | getline f ) > 0 ) {
  247. printfile(f);
  248. }
  249. close(cmd);
  250. }
  251. function procinclude(spec) {
  252. if( rootdir!="" && spec!~/^\// ) {
  253. spec=rootdir"/"spec;
  254. }
  255. if( isdir(spec) ) {
  256. allfiles(spec); # read all files in a directory (and subdirs)
  257. } else {
  258. listfiles(spec); # there could be jokers
  259. }
  260. }
  261. function isdir(s) {
  262. return !system("test -d \""s"\"");
  263. }
  264. { procline(); }
  265. ' $1 |
  266. sed 's/#.*//;s/[[:blank:]]*$//;s/^[[:blank:]]*//' |
  267. grep -v '^$'
  268. }
  269.  
  270. #
  271. # set parameters (as shell vars) from our nginx config file
  272. #
  273. get_nginx_params() {
  274. configfile=$1
  275. shift 1
  276. vars=`echo $@ | sed 's/ /,/g'`
  277.  
  278. eval `
  279. nginxcat $configfile | awk -v vars="$vars" '
  280. BEGIN{
  281. split(vars,v,",");
  282. for( i in v )
  283. vl[i]=tolower(v[i]);
  284. }
  285. {
  286. for( i in v )
  287. if( tolower($1)==vl[i] ) {
  288. print v[i]"="$2
  289. delete vl[i]
  290. break
  291. }
  292. }
  293. '`
  294. }
  295.  
  296. #
  297. # Return the location(s) that are handled by the given handler
  298. #
  299. FindLocationForHandler() {
  300. PerlScript='while (<>) {
  301. /^\s*location\s+([^ \s{]+)\s*{/i && ($loc=$1);
  302. /^\s*stub_status\s+on\s*;$2/i && print "$loc\n";
  303. }'
  304. nginxcat $1 | perl -e "$PerlScript"
  305. }
  306.  
  307. #
  308. # Check if the port is valid
  309. #
  310. CheckPort() {
  311. lclport="$1"
  312. case "$lclport" in
  313. *:[0-9]*) lclport=`echo "$lclport" | sed 's%^[^:][^:]*:%%'`
  314. esac
  315. ocf_is_decimal "$lclport" && [ $lclport -gt 0 -a $lclport -lt 65537 ]
  316. }
  317.  
  318. buildlocalurl() {
  319. [ "x$listen" != "x" ] &&
  320. echo "http://${listen}" ||
  321. echo "${LOCALHOST}:${PORT}"
  322. }
  323. #
  324. # Get all the parameters we need from the Nginx config file
  325. #
  326. GetParams() {
  327. ConfigFile=$1
  328. DEFAULT_PID=`echo "$NGINX_CONFIGURATION" | sed -e 's%.*--pid-path=%%' -e 's% *--.*%%'`
  329. if
  330. [ ! -f $ConfigFile ]
  331. then
  332. return 1
  333. fi
  334.  
  335. get_nginx_params $ConfigFile root pid listen
  336. case $PidFile in
  337. "") PidFile=$DEFAULT_PID ;;
  338. *) ;;
  339. esac
  340.  
  341. for p in "$PORT" "$listen" 80
  342. do
  343. if
  344. CheckPort "$p"
  345. then
  346. PORT="$p"
  347. break
  348. fi
  349. done
  350.  
  351. echo $listen | grep ':' >/dev/null || # Listen could be just port spec
  352. listen="localhost:$listen"
  353.  
  354. #
  355. # It's difficult to figure out whether the server supports
  356. # the status operation.
  357. # (we start our server with -DSTATUS - just in case :-))
  358. #
  359. # Typically (but not necessarily) the status URL is /nginx_status
  360. #
  361. # For us to think status will work, we have to have the following things:
  362. #
  363. # - The server-status handler has to be mapped to some URL somewhere
  364. #
  365. # We assume that:
  366. #
  367. # - the "main" web server at $PORT will also support it if we can find it
  368. # somewhere in the file
  369. # - it will be supported at the same URL as the one we find in the file
  370. #
  371. # If this doesn't work for you, then set the status10url attribute.
  372. #
  373. if
  374. [ "X$STATUSURL" = "X" ]
  375. then
  376. StatusURL=`FindLocationForHandler $1 nginx_status | tail -1`
  377. STATUSURL="`buildlocalurl`$StatusURL"
  378. fi
  379. test ! -z "$PidFile"
  380. }
  381.  
  382. #
  383. # return TRUE if a process with given PID is running
  384. #
  385. ProcessRunning() {
  386. NginxPID=$1
  387. # Use /proc if it looks like it's here...
  388. if
  389. [ -d /proc -a -d /proc/1 ]
  390. then
  391. [ -d /proc/$NginxPID ]
  392. else
  393. # This assumes we're running as root...
  394. kill -0 "$NginxPID" >/dev/null 2>&1
  395. fi
  396. }
  397.  
  398.  
  399. silent_status() {
  400. if
  401. [ -f $PidFile -a -s $PidFile ] && ocf_is_decimal "`cat $PidFile`"
  402. then
  403. ProcessRunning `cat $PidFile`
  404. else
  405. : No pid file
  406. false
  407. fi
  408. }
  409.  
  410. start_nginx() {
  411. if
  412. silent_status
  413. then
  414. ocf_log info "$CMD already running (pid $NginxPID)"
  415. return $OCF_SUCCESS
  416. fi
  417. if
  418. ocf_run $NGINXD -t -c $CONFIGFILE
  419. then
  420. : Configuration file $CONFIGFILE looks OK
  421. else
  422. return $OCF_ERR_CONFIGURED
  423. fi
  424. NGINX_VERSION=`$NGINXD -v 2>&1`
  425. ocf_log info "Starting $NGINXD - $NGINX_VERSION"
  426. ocf_log info "$NGINXD build configuration: $NGINX_CONFIGURATION"
  427. if
  428. ocf_run $NGINXD $NGINXDOPTS $OPTIONS -c $CONFIGFILE
  429. then
  430. : $NGINXD started without errors!
  431. else
  432. return $OCF_ERR_GENERIC
  433. fi
  434. tries=0
  435. # This looks like a potential infinite loop - but it's not in practice
  436. # The LRM will time us out and kill us if nginx never starts working.
  437. while
  438. monitor_nginx
  439. ec=$?
  440. if
  441. [ $ec -eq $OCF_NOT_RUNNING ]
  442. then
  443. tries=`expr $tries + 1`
  444. ocf_log info "Waiting for $NGINXD -c $CONFIGFILE to come up (try $tries)"
  445. true
  446. else
  447. false
  448. fi
  449. do
  450. sleep 1
  451. done
  452. return $ec
  453. }
  454.  
  455. stop_nginx() {
  456. if
  457. silent_status
  458. then
  459. if
  460. kill $NginxPID
  461. then
  462. tries=0
  463. while
  464. ProcessRunning $NginxPID && [ $tries -lt 10 ]
  465. do
  466. sleep 1
  467. kill $NginxPID >/dev/null
  468. ocf_log info "Killing nginx PID $NginxPID"
  469. tries=`expr $tries + 1`
  470. done
  471. else
  472. ocf_log warn "Killing nginx PID $NginxPID FAILED."
  473. fi
  474. if
  475. ProcessRunning $NginxPID
  476. then
  477. ocf_log info "$CMD still running ($NginxPID)."
  478. false
  479. else
  480. ocf_log info "$CMD stopped."
  481. fi
  482. else
  483. ocf_log info "$CMD is not running."
  484. fi
  485.  
  486. #
  487. # I'm not convinced this is a wonderful idea (AlanR)
  488. #
  489. for sig in SIGTERM SIGHUP SIGKILL
  490. do
  491. if
  492. pgrep -f "$NGINXD.*$CONFIGFILE" >/dev/null
  493. then
  494. pkill -$sig -f $NGINXD.*$CONFIGFILE >/dev/null
  495. ocf_log info "nginxd children were signalled ($sig)"
  496. sleep 1
  497. else
  498. break
  499. fi
  500. done
  501. }
  502.  
  503. reload_nginx() {
  504. if
  505. silent_status
  506. then
  507. if
  508. kill -1 $NginxPID
  509. then
  510. : $NGINX reload signal to $NginxPID succeeded
  511. return $OCF_SUCCESS
  512. fi
  513. return $OCF_ERR_GENERIC
  514. fi
  515. start_nginx
  516. }
  517.  
  518. status_nginx() {
  519. silent_status
  520. rc=$?
  521. if
  522. [ $rc -eq 0 ]
  523. then
  524. ocf_log info "$CMD is running (pid $NginxPID)."
  525. return $OCF_SUCCESS
  526. else
  527. ocf_log info "$CMD is stopped."
  528. return $OCF_NOT_RUNNING
  529. fi
  530. }
  531.  
  532. fixtesturl() {
  533. echo $test_url | grep -qs "^http" && return
  534. test_url="`buildlocalurl`$test_url"
  535. }
  536.  
  537. monitor_nginx_external() {
  538. if
  539. [ -z "$EXTMONITOR" ]
  540. then
  541. ocf_log err "$External level 30 Monitor Command not configured."
  542. return $OCF_ERR_CONFIGURED
  543. fi
  544. extbase=`echo $EXTMONITOR | sed 's% .*%%'`
  545. if
  546. case "$extbase" in
  547. /*) test -f "$extbase" -a -x "$extbase";;
  548. *) which "$extbase" >/dev/null 2>&1
  549. esac
  550. then
  551. : OK - $extbase seems to be there...
  552. else
  553. ocf_log err "$External monitor command [$extbase] is not installed."
  554. return $OCF_ERR_CONFIGURED
  555. fi
  556. if
  557. $extbase
  558. then
  559. : OK - $extbase succeeded
  560. else
  561. ocf_log err "$extbase reported failure [rc=$?]"
  562. return $OCF_NOT_RUNNING
  563. fi
  564. return $OCF_SUCCESS
  565. }
  566.  
  567.  
  568. monitor_nginx_extended() {
  569. if
  570. [ -f "$TESTCONFFILE" -a -r "$TESTCONFFILE" ]
  571. then
  572. readtestconf < $TESTCONFFILE
  573. else
  574. test_url="$TESTURL"
  575. test_regex="$TESTREGEX20"
  576. fi
  577. whattorun=`gethttpclient`
  578. fixtesturl
  579. is_testconf_sane || return $OCF_ERR_CONFIGURED
  580. $whattorun "$test_url" | grep -Ei "$test_regex" > /dev/null
  581. }
  582.  
  583. monitor_nginx_basic() {
  584. if
  585. [ -z "$STATUSURL" ]
  586. then
  587. ocf_log err "status10url parameter empty"
  588. return $OCF_ERR_CONFIGURED
  589. elif
  590. [ -z "$ourhttpclient" ]
  591. then
  592. ocf_log err "could not find a http client; make sure that either wget or curl is available"
  593. return $OCF_ERR_CONFIGURED
  594. fi
  595. ${ourhttpclient}_func "$STATUSURL" | grep -Ei "$TESTREGEX" > /dev/null
  596. }
  597.  
  598. monitor_nginx() {
  599. silent_status
  600. if
  601. [ $? -ne 0 ]
  602. then
  603. ocf_log info "$CMD not running"
  604. return $OCF_NOT_RUNNING
  605. fi
  606. if
  607. [ -z "$OCF_CHECK_LEVEL" ] || [ "$OCF_CHECK_LEVEL" -lt 10 ]
  608. then
  609. return 0
  610. fi
  611. ourhttpclient=`findhttpclient` # we'll need one
  612. if
  613. [ "$OCF_CHECK_LEVEL" -lt 20 ]
  614. then
  615. monitor_nginx_basic
  616. elif
  617. [ "$OCF_CHECK_LEVEL" -lt 30 ]
  618. then
  619. monitor_nginx_extended
  620. else
  621. monitor_nginx_external
  622. fi
  623. }
  624.  
  625. metadata_nginx(){
  626. cat <<END
  627. <?xml version="1.0"?>
  628. <!DOCTYPE resource-agent SYSTEM "ra-api-1.dtd">
  629. <resource-agent name="nginx">
  630. <version>1.0</version>
  631.  
  632. <longdesc lang="en">
  633. This is the resource agent for the Nginx web/proxy server.
  634. This resource agent does not monitor POP or IMAP servers, as
  635. we don't know how to determine meaningful status for them.
  636.  
  637. The start operation ends with a loop in which monitor is
  638. repeatedly called to make sure that the server started and that
  639. it is operational. Hence, if the monitor operation does not
  640. succeed within the start operation timeout, the nginx resource
  641. will end with an error status.
  642.  
  643. The default monitor operation will verify that nginx is running.
  644.  
  645. The level 10 monitor operation by default will try and fetch the /nginx_status
  646. page - which is commented out in sample nginx configurations.
  647. Make sure that the /nginx_status page works and that the access
  648. is restricted to localhost (address 127.0.0.1) plus whatever
  649. places _outside the cluster_ you want to monitor the server from.
  650. See the status10url and status10regex attributes for more details.
  651.  
  652. The level 20 monitor operation will perform a more complex set of tests
  653. from a configuration file.
  654.  
  655. The level 30 monitor operation will run an external command to perform
  656. an arbitrary monitoring operation.
  657.  
  658. </longdesc>
  659. <shortdesc lang="en">Manages an Nginx web/proxy server instance</shortdesc>
  660.  
  661. <parameters>
  662.  
  663. <parameter name="configfile" required="0" unique="1">
  664. <longdesc lang="en">
  665. The full pathname of the Nginx configuration file.
  666. This file is parsed to provide defaults for various other
  667. resource agent parameters.
  668. </longdesc>
  669. <shortdesc lang="en">configuration file path</shortdesc>
  670. <content type="string"/>
  671. </parameter>
  672.  
  673. <parameter name="httpd">
  674. <longdesc lang="en">
  675. The full pathname of the httpd binary (optional).
  676. </longdesc>
  677. <shortdesc lang="en">httpd binary path</shortdesc>
  678. <content type="string" default="/usr/sbin/httpd" />
  679. </parameter>
  680.  
  681. <parameter name="port" >
  682. <longdesc lang="en">
  683. A port number that we can probe for status information
  684. using the statusurl.
  685. This will default to the port number found in the
  686. configuration file, or 80, if none can be found
  687. in the configuration file.
  688.  
  689. </longdesc>
  690. <shortdesc lang="en">httpd port</shortdesc>
  691. <content type="integer" />
  692. </parameter>
  693.  
  694. <parameter name="status10url">
  695. <longdesc lang="en">
  696. The URL to monitor (the nginx server status page by default) when given a level 10 monitor operation.
  697. If left unspecified, it will be inferred from
  698. the nginx configuration file, or defaulted to /nginx_status.
  699.  
  700. If you set this, make sure that it succeeds *only* from the
  701. localhost (127.0.0.1) and no other cluster nodes.
  702. Otherwise, the cluster software may complain
  703. about it being active on multiple nodes.
  704. </longdesc>
  705. <shortdesc lang="en">url name</shortdesc>
  706. <content type="string" />
  707. </parameter>
  708.  
  709. <parameter name="status10regex">
  710. <longdesc lang="en">
  711. Regular expression to match in the output of status10url.
  712. Case insensitive.
  713. </longdesc>
  714. <shortdesc lang="en">monitor regular expression</shortdesc>
  715. <content type="string" default="Reading: [0-9]+ Writing: [0-9]+ Waiting: [0-9]+"/>
  716. </parameter>
  717.  
  718. <parameter name="testclient">
  719. <longdesc lang="en">
  720. Client to use to query to Nginx for level 10 and level 20 tests.
  721. If not specified, the RA will try to find one on the system.
  722. Currently, wget and curl are supported, with curl being preferred.
  723. For example, you can set this paramter to "wget" if you prefer that to curl.
  724. </longdesc>
  725. <shortdesc lang="en">http client</shortdesc>
  726. <content type="string" />
  727. </parameter>
  728.  
  729. <parameter name="testurl">
  730. <longdesc lang="en">
  731. URL to test. If it does not start with "http", then it's
  732. considered to be relative to the document root address.
  733. </longdesc>
  734. <shortdesc lang="en">Level 10 monitor url</shortdesc>
  735. <content type="string" />
  736. </parameter>
  737.  
  738. <parameter name="test20regex">
  739. <longdesc lang="en">
  740. Regular expression to match in the output of testurl.
  741. Case insensitive.
  742. </longdesc>
  743. <shortdesc lang="en">Level 20 monitor regular expression</shortdesc>
  744. <content type="string" />
  745. </parameter>
  746.  
  747. <parameter name="testconffile">
  748. <longdesc lang="en">
  749. A file which contains a more complex test configuration. Could be useful if
  750. you have to check more than one web application or in case sensitive
  751. info should be passed as arguments (passwords). Furthermore,
  752. using a config file is the only way to specify certain parameters.
  753.  
  754. Please see README.webapps for examples and file description.
  755. </longdesc>
  756. <shortdesc lang="en">Level 20 test configuration file</shortdesc>
  757. <content type="string" />
  758. </parameter>
  759.  
  760. <parameter name="test20name">
  761. <longdesc lang="en">
  762. Name of the test within the test configuration file.
  763. </longdesc>
  764. <shortdesc lang="en">Level 20 test name</shortdesc>
  765. <content type="string" />
  766. </parameter>
  767.  
  768. <parameter name="external_monitor30_cmd">
  769. <longdesc lang="en">
  770. Command string to run which implements level 30 monitoring.
  771. </longdesc>
  772. <shortdesc lang="en">Level 30 test string</shortdesc>
  773. <content type="string" />
  774. </parameter>
  775.  
  776. <parameter name="options">
  777. <longdesc lang="en">
  778. Extra options to apply when starting nginx.
  779. </longdesc>
  780. <shortdesc lang="en">nginx start options</shortdesc>
  781. <content type="string" />
  782. </parameter>
  783.  
  784. </parameters>
  785.  
  786. <actions>
  787. <action name="start" timeout="40s" />
  788. <action name="stop" timeout="60s" />
  789. <action name="reload" timeout="40s" />
  790. <action name="status" timeout="30s" />
  791. <action name="monitor" timeout="30s" depth="0" interval="10s" />
  792. <action name="monitor" timeout="30s" depth="10" interval="30s" />
  793. <action name="monitor" timeout="45s" depth="20" />
  794. <action name="monitor" timeout="60s" depth="30" />
  795. <action name="meta-data" timeout="5" />
  796. <action name="validate-all" timeout="5" />
  797. </actions>
  798. </resource-agent>
  799. END
  800.  
  801. exit $OCF_SUCCESS
  802. }
  803.  
  804. validate_all_nginx() {
  805. if
  806. CheckPort $PORT
  807. # We are sure to succeed here, since we forced $PORT to be valid in GetParams()
  808. then
  809. : OK
  810. else
  811. ocf_log err "Port number $PORT is invalid!"
  812. exit $OCF_ERR_ARGS
  813. fi
  814.  
  815. if
  816. [ -z $STATUSURL ]
  817. then
  818. : OK to be empty
  819. else
  820. case $STATUSURL in
  821. http://*/*) ;;
  822. *) ocf_log err "Invalid STATUSURL $STATUSURL"
  823. exit $OCF_ERR_ARGS ;;
  824. esac
  825. fi
  826. if
  827. [ ! -x $NGINXD ]
  828. then
  829. ocf_log err "NGINXD $NGINXD not found or is not an executable!"
  830. exit $OCF_ERR_ARGS
  831. fi
  832. if
  833. [ ! -f $CONFIGFILE ]
  834. then
  835. # We are sure to succeed here, since we have parsed $CONFIGFILE before getting here
  836. ocf_log err "Configuration file $CONFIGFILE not found!"
  837. exit $OCF_ERR_CONFIGURED
  838. fi
  839. if
  840. ocf_run $NGINXD -t -c $CONFIGFILE
  841. then
  842. : Cool $NGINXD likes $CONFIGFILE
  843. else
  844. ocf_log err "$NGINXD -t -c $CONFIGFILE reported a configuration error."
  845. return $OCF_ERR_CONFIGURED
  846. fi
  847. return $OCF_SUCCESS
  848. }
  849.  
  850. if
  851. [ $# -eq 1 ]
  852. then
  853. COMMAND=$1
  854. NGINXD="$OCF_RESKEY_httpd"
  855. PORT="$OCF_RESKEY_port"
  856. STATUSURL="$OCF_RESKEY_status10url"
  857. CONFIGFILE="$OCF_RESKEY_configfile"
  858. OPTIONS="$OCF_RESKEY_options"
  859. CLIENT=${OCF_RESKEY_client}
  860. TESTREGEX=${OCF_RESKEY_status10regex:-'Reading: [0-9]+ Writing: [0-9]+ Waiting: [0-9]+'}
  861. TESTURL="$OCF_RESKEY_status10url"
  862. TESTREGEX20=${OCF_RESKEY_test20regex}
  863. TESTCONFFILE="$OCF_RESKEY_test20conffile"
  864. TESTNAME="$OCF_RESKEY_test20name"
  865. EXTMONITOR="$OCF_RESKEY_external_monitor30_cmd"
  866. else
  867. usage $OCF_ERR_ARGS
  868. fi
  869.  
  870. LSB_STATUS_STOPPED=3
  871. if
  872. [ "X$NGINXD" = X -o ! -f "$NGINXD" -o ! -x "$NGINXD" ]
  873. then
  874. NGINXD=
  875. for h in $NGINXDLIST
  876. do
  877. if
  878. [ -f "$h" -a -x "$h" ]
  879. then
  880. NGINXD="$h"
  881. break
  882. fi
  883. done
  884. # It is possible that we still do not have a valid httpd at this stage
  885. if
  886. [ -z "$NGINXD" ]
  887. then
  888. case $COMMAND in
  889. stop) exit $OCF_SUCCESS;;
  890. monitor) exit $OCF_NOT_RUNNING;;
  891. status) exit $LSB_STATUS_STOPPED;;
  892. meta-data) metadata_nginx;;
  893. esac
  894. ocf_log err "nginx binary not found! Please verify you've installed it"
  895. exit $OCF_ERR_INSTALLED
  896. fi
  897. # Let the user know that the $NGINXD used is the one (s)he specified via $OCF_RESKEY_httpd
  898. if
  899. [ ! -z "$OCF_RESKEY_httpd" ]
  900. then
  901. ocf_log info "Using $NGINXD as nginx"
  902. fi
  903. fi
  904.  
  905. httpd_basename=`basename $NGINXD`
  906. case $httpd_basename in
  907. *-*) httpd_basename=`echo "$httpd_basename" | sed -e 's%\-.*%%'`;;
  908. esac
  909. NGINX_CONFIGURATION=`$NGINXD -V 2>&1 |grep 'configure arguments:'`
  910. DEFAULT_CONFIG=`echo "$NGINX_CONFIGURATION" | sed -e 's%.*--conf-path=%%' -e 's% *--.*%%'`
  911.  
  912. case "$CONFIGFILE" in
  913. "") CONFIGFILE=$DEFAULT_CONFIG;;
  914. *) ;;
  915. esac
  916.  
  917. if
  918. [ ! -f "$CONFIGFILE" ]
  919. then
  920. case $COMMAND in
  921. stop) ocf_log warn "$CONFIGFILE not found - nginx considered stopped"
  922. exit $OCF_SUCCESS;;
  923. monitor) exit $OCF_NOT_RUNNING;;
  924. status) exit $LSB_STATUS_STOPPED;;
  925. esac
  926. fi
  927.  
  928. if
  929. [ "X$COMMAND" = Xmeta-data ] || GetParams $CONFIGFILE
  930. then
  931. : OK
  932. else
  933. ocf_log err "Cannot parse config file [$CONFIGFILE]"
  934. exit $OCF_ERR_CONFIGURED
  935. fi
  936.  
  937. case $COMMAND in
  938. start) start_nginx;;
  939. stop) stop_nginx;;
  940. reload) reload_nginx;;
  941. status) status_nginx;;
  942. monitor) monitor_nginx;;
  943. meta-data) metadata_nginx;;
  944. validate-all) validate_all_nginx;;
  945. *) usage $OCF_ERR_UNIMPLEMENTED;;
  946. esac
  947.  
  948.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement