scripts: Rework notify.sh to use notify.d/ directory
authorMartin Schwenke <martin@meltin.net>
Fri, 17 May 2013 06:42:25 +0000 (16:42 +1000)
committerAmitay Isaacs <amitay@gmail.com>
Thu, 23 May 2013 06:18:23 +0000 (16:18 +1000)
This makes it easier to add notification handlers.

Signed-off-by: Martin Schwenke <martin@meltin.net>
Makefile.in
config/notify.d.README [new file with mode: 0755]
config/notify.sh
packaging/RPM/ctdb.spec.in

index 86b4870825249d78d97dcc3d455532f82469d60f..80acfd5b945c8153b935f5534581c3a0e9b08caf 100755 (executable)
@@ -319,6 +319,7 @@ install: all manpages $(PMDA_INSTALL)
        mkdir -p $(DESTDIR)$(etcdir)/ctdb/events.d
        mkdir -p $(DESTDIR)$(etcdir)/ctdb/nfs-rpc-checks.d
        mkdir -p $(DESTDIR)$(etcdir)/sudoers.d/
+       mkdir -p $(DESTDIR)$(etcdir)/ctdb/notify.d
        mkdir -p $(DESTDIR)$(docdir)/ctdb
        ${INSTALLCMD} -m 644 ctdb.pc $(DESTDIR)$(libdir)/pkgconfig
        ${INSTALLCMD} -m 755 bin/ctdb $(DESTDIR)$(bindir)
@@ -376,6 +377,7 @@ install: all manpages $(PMDA_INSTALL)
        if [ -f doc/ltdbtool.1.html ];then ${INSTALLCMD} -m 644 doc/ltdbtool.1.html $(DESTDIR)$(docdir)/ctdb; fi
        if [ -f doc/ping_pong.1.html ];then ${INSTALLCMD} -m 644 doc/ping_pong.1.html $(DESTDIR)$(docdir)/ctdb; fi
        if [ ! -f $(DESTDIR)$(etcdir)/ctdb/notify.sh ];then ${INSTALLCMD} -m 755 config/notify.sh $(DESTDIR)$(etcdir)/ctdb; fi
+       if [ ! -f $(DESTDIR)$(etcdir)/ctdb/notify.d/README ];then ${INSTALLCMD} -m 755 config/notify.d.README $(DESTDIR)$(etcdir)/ctdb/notify.d/README ; fi
        ${INSTALLCMD} -m 755 config/debug-hung-script.sh $(DESTDIR)$(etcdir)/ctdb
        if [ ! -f $(DESTDIR)$(etcdir)/ctdb/ctdb-crash-cleanup.sh ];then ${INSTALLCMD} -m 755 config/ctdb-crash-cleanup.sh $(DESTDIR)$(etcdir)/ctdb; fi
        if [ ! -f $(DESTDIR)$(etcdir)/ctdb/gcore_trace.sh ];then ${INSTALLCMD} -m 755 config/gcore_trace.sh $(DESTDIR)$(etcdir)/ctdb; fi
diff --git a/config/notify.d.README b/config/notify.d.README
new file mode 100755 (executable)
index 0000000..ffce7fa
--- /dev/null
@@ -0,0 +1,44 @@
+This directory should contain executable programs to handle CTDB event
+notifications.  The first and only argument passed to each program is
+the event, which is one of:
+
+  init, setup, startup, unhealthy, healthy
+
+To use notifications with this directory then you need to set:
+
+  CTDB_NOTIFY_SCRIPT=/etc/ctdb/notify.sh
+
+in your CTDB configuration file.
+
+An example script that sends SNMP traps for unhealthy/healthy might
+look like this:
+
+  #!/bin/sh
+
+  case "$1" in
+      unhealthy)
+          # Send an SNMP trap saying that the node is unhealthy:
+          snmptrap -m ALL -v 1 -c public 10.1.1.105 ctdb \
+              $(hostname) 0 0 $(date +"%s") ctdb.nodeHealth.0 i 1
+          ;;
+      healthy)
+          # Send an SNMP trap saying that the node is healthy again:
+          snmptrap -m ALL -v 1 -c public 10.1.1.105 ctdb \
+             $(hostname) 0 0 $(date +"%s") ctdb.nodeHealth.0 i 0
+         ;;
+  esac
+
+Alternatively, email could be sent:
+
+  #!/bin/sh
+
+  case "$1" in
+      unhealthy)
+          mail -s "$(hostname) is UNHEALTHY" foo@example.com </dev/null >/dev/null 2>&1
+          ;;
+      healthy)
+          mail -s "$(hostname) is HEALTHY" foo@example.com </dev/null >/dev/null 2>&1
+          ;;
+  esac
+
+When adding programs please note the exclusion patterns in notify.sh.
index 8d3eb87f202b2b7eb636306904c1c28377215d40..dfcb81a5516132bb54fed832945f2f88cc620a68 100755 (executable)
@@ -3,47 +3,25 @@
 # This script is activated by setting CTDB_NOTIFY_SCRIPT=/etc/ctdb/notify.sh
 # in /etc/sysconfig/ctdb
 
-# This is script is invoked from ctdb when node UNHEALTHY flag changes.
-# and can be used to send SNMPtraps, email, etc
-# when the status of a node changes
-
-
-event="$1"
-shift
-
-case $event in
-       unhealthy)
-#
-#               Send an snmptrap that the node is unhealthy :
-#              snmptrap -m ALL -v 1 -c public 10.1.1.105 ctdb `hostname` 0 0 `date +"%s"` ctdb.nodeHealth.0 i 1
-#
-#               or send an email :
-#               mail foo@bar -s "`hostname` is UNHEALTHY"   ...
-#
-#               or do something else ...
-               ;;
-       healthy)
-#
-#               Send an snmptrap that the node is healthy again :
-#              snmptrap -m ALL -v 1 -c public 10.1.1.105 ctdb `hostname` 0 0 `date +"%s"` ctdb.nodeHealth.0 i 0
-#
-#               or send an email :
-#               mail foo@bar -s "`hostname` is HEALTHY"   ...
-#
-#               or do something else ...
-               ;;
-       startup)
-       #               do some extra magic when ctdb has finished the initial
-       #               recovery?
-               ;;
-
-       setup)
-       #               do some extra magic when ctdb has setup itself?
-               ;;
-
-       init)
-       #               do some extra magic when ctdb has started?
-               ;;
-esac
-
-exit 0
+# This is script is invoked from ctdb when certain events happen.  See
+# /etc/ctdb/notify.d/README for more details.
+
+d=$(dirname $0)
+nd="${d}/notify.d"
+
+ok=true
+
+for i in "${nd}/"* ; do
+    # Don't run files matching basename
+    case "${i##*/}" in
+       *~|*,|*.rpm*|*.swp|README) continue ;;
+    esac
+
+    # Files must be executable
+    [ -x "$i" ] || continue
+
+    # Flag failures
+    "$i" "$1" || ok=false
+done
+
+$ok
index 83261c5483997bf956ce48e31df176a25975bdbf..910b59acd3006d24684c27999ebe5e0be609ffc7 100644 (file)
@@ -127,6 +127,7 @@ rm -rf $RPM_BUILD_ROOT
 %config(noreplace) %{_sysconfdir}/ctdb/gcore_trace.sh
 %config(noreplace) %{_sysconfdir}/ctdb/functions
 %attr(755,root,root) %{initdir}/ctdb
+%attr(755,root,root) %{_sysconfdir}/ctdb/notify.d
 
 %{_docdir}/ctdb/README
 %{_docdir}/ctdb/COPYING
@@ -162,6 +163,7 @@ rm -rf $RPM_BUILD_ROOT
 %config(noreplace) %{_sysconfdir}/ctdb/nfs-rpc-checks.d/40.mountd.check
 %config(noreplace) %{_sysconfdir}/ctdb/nfs-rpc-checks.d/50.rquotad.check
 %{_sysconfdir}/ctdb/statd-callout
+%{_sysconfdir}/ctdb/notify.d/README
 %{_sbindir}/ctdbd
 %{_bindir}/ctdb
 %{_bindir}/smnotify