ctdb-scripts: Move event scripts to events/legacy/ directory
[samba.git] / ctdb / config / events / legacy / 41.httpd.script
1 #!/bin/sh
2 # event script to manage httpd in a cluster environment
3
4 [ -n "$CTDB_BASE" ] || \
5     CTDB_BASE=$(d=$(dirname "$0") ; cd -P "$d" ; dirname "$PWD")
6
7 . "${CTDB_BASE}/functions"
8
9 detect_init_style
10
11 case $CTDB_INIT_STYLE in
12 redhat)
13         service_name="httpd"
14         ;;
15 suse|debian|*)
16         service_name="apache2"
17         ;;
18 esac
19
20 load_script_options
21
22 ctdb_setup_state_dir "service" "$service_name"
23
24 # RHEL5 sometimes use a SIGKILL to terminate httpd, which then leaks
25 # semaphores.  This is a hack to clean them up.
26 cleanup_httpd_semaphore_leak() {
27     killall -q -0 "$service_name" ||
28     for i in $(ipcs -s | awk '$3 == "apache" { print $2 }') ; do
29         ipcrm -s "$i"
30     done
31 }
32
33 ##########
34
35 service_start ()
36 {
37     cleanup_httpd_semaphore_leak
38     service $service_name start
39 }
40 service_stop ()
41 {
42     service $service_name stop
43     killall -q -9 $service_name || true
44 }
45
46 [ "$CTDB_MANAGES_HTTPD" = "yes" ] || exit 0
47
48 case "$1" in
49 startup)
50         service_start
51         ctdb_counter_init
52         ;;
53
54 shutdown)
55         service_stop
56         ;;
57
58 monitor)
59         if ctdb_check_tcp_ports 80 >/dev/null 2>/dev/null ; then
60                 ctdb_counter_init
61         else
62                 ctdb_counter_incr
63                 num_fails=$(ctdb_counter_get)
64                 if [ "$num_fails" -eq 2 ] ; then
65                         echo "HTTPD is not running. Trying to restart HTTPD."
66                         service_stop
67                         service_start
68                         exit 0
69                 elif [ "$num_fails" -ge 5 ] ; then
70                         echo "HTTPD is not running. Trying to restart HTTPD."
71                         service_stop
72                         service_start
73                         exit 1
74                 fi
75         fi
76         ;;
77 esac
78
79 exit 0
80