41.HTTPD
[sahlberg/ctdb.git] / config / events.d / 41.httpd
index fae9f47f55e159a21f3a84d0adff226e436233c0..e94bde3fadd65c3ecc5573dd1697de8616a364d4 100755 (executable)
@@ -2,51 +2,79 @@
 # event script to manage httpd in a cluster environment
 
 . $CTDB_BASE/functions
-loadconfig ctdb
 
 detect_init_style
 
 case $CTDB_INIT_STYLE in
-       redhat)
-               CTDB_SERVICE_HTTP="httpd"
-               CTDB_CONFIG_HTTP="http"
-               ;;
-       suse)
-               CTDB_SERVICE_HTTP="apache2"
-               CTDB_CONFIG_HTTP="apache2"
-               ;;
-       ubuntu)
-               CTDB_SERVICE_HTTP="apache2"
-               CTDB_CONFIG_HTTP="apache2"
-               ;;
-       *)
-               # should not happen.
-               # for now use red hat style as default
-               CTDB_SERVICE_HTTP="httpd"
-               CTDB_CONFIG_HTTP="http"
-               ;;
+    redhat)
+       service_name="httpd"
+       service_config="http"
+       ;;
+    suse|debian|*)
+       service_name="apache2"
+       service_config="apache2"
+       ;;
 esac
 
-loadconfig "${CTDB_CONFIG_HTTP}"
+# RHEL5 sometimes use a SIGKILL to terminate httpd, which then leaks
+# semaphores.  This is a hack to clean them up.
+cleanup_httpd_semaphore_leak() {
+    killall -q -0 "$service_name" ||
+    for i in $(ipcs -s | awk '$3 == "apache" { print $2 }') ; do
+       ipcrm -s $i
+    done
+}
 
-[ "$CTDB_MANAGES_HTTPD" = "yes" ] || exit 0
+##########
 
-cmd="$1"
-shift
+service_start="cleanup_httpd_semaphore_leak; service $service_name start"
+service_stop="service $service_name stop; killall -q -9 $service_name || true"
+service_reconfigure="service $service_name restart"
 
-case $cmd in 
-     startup)
-        service "${CTDB_SERVICE_HTTP}" stop > /dev/null 2>&1
-        service "${CTDB_SERVICE_HTTP}" start
-        ;;
+loadconfig
 
-     shutdown)
-        service "${CTDB_SERVICE_HTTP}" stop
-        ;;
+ctdb_start_stop_service
 
-     monitor)
-        ctdb_check_tcp_ports "http" 80
-        ;;
+is_ctdb_managed_service || exit 0
+
+case "$1" in
+    startup)
+       ctdb_service_start
+       ;;
+
+    shutdown)
+       ctdb_service_stop
+       ;;
+
+    monitor)
+       if ctdb_service_needs_reconfigure ; then
+           ctdb_service_reconfigure
+           exit 0
+       fi
+
+       if ctdb_check_tcp_ports 80 >/dev/null 2>/dev/null ; then
+           ctdb_counter_init
+       else
+           ctdb_counter_incr
+
+           ctdb_check_counter_equal 5 || {
+               echo "HTTPD is not running. Trying to restart HTTPD."
+               ctdb_service_stop
+               ctdb_service_start
+               exit 0
+           }
+            ctdb_check_counter_limit 10 quiet|| {
+               echo "HTTPD is not running. Trying to restart HTTPD."
+               ctdb_service_stop
+               ctdb_service_start
+               exit 1
+           }
+       fi
+       ;;
+
+    *)
+       ctdb_standard_event_handler "$@"
+       ;;
 esac
 
 exit 0