Advertisement
Guest User

nginx

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