[Checkins] SVN: gocept.zeoraid/trunk/ Fix for #463763: the controller script now fails immediately if a server is

Christian Theune ct at gocept.com
Sat Nov 14 03:55:25 EST 2009


Log message for revision 105639:
  Fix for #463763: the controller script now fails immediately if a server is
  not available.
  
  Also, make the controller script use Nagios-compatible exit codes.
  

Changed:
  U   gocept.zeoraid/trunk/CHANGES.txt
  U   gocept.zeoraid/trunk/src/gocept/zeoraid/scripts/controller.py
  U   gocept.zeoraid/trunk/src/gocept/zeoraid/scripts/tests.py

-=-
Modified: gocept.zeoraid/trunk/CHANGES.txt
===================================================================
--- gocept.zeoraid/trunk/CHANGES.txt	2009-11-14 08:06:57 UTC (rev 105638)
+++ gocept.zeoraid/trunk/CHANGES.txt	2009-11-14 08:55:25 UTC (rev 105639)
@@ -5,9 +5,16 @@
 1.0b5 (unreleased)
 ------------------
 
-- Nothing changed yet.
+- Add a note to the deployment documentation that strongly advises people to
+  use a deployment recipe for setting up their ZEO servers to avoid buildout
+  killing volatile files.
 
+- Fix #463763: Make the controller script not wait endlessly for a ZEORaid
+  server to come up but fail immediately.
 
+- Make the controller script use exit codes that are Nagios-compatible.
+
+
 1.0b4 (2009-11-13)
 ------------------
 

Modified: gocept.zeoraid/trunk/src/gocept/zeoraid/scripts/controller.py
===================================================================
--- gocept.zeoraid/trunk/src/gocept/zeoraid/scripts/controller.py	2009-11-14 08:06:57 UTC (rev 105638)
+++ gocept.zeoraid/trunk/src/gocept/zeoraid/scripts/controller.py	2009-11-14 08:55:25 UTC (rev 105639)
@@ -38,10 +38,24 @@
 """
 
 import ZEO.ClientStorage
+import optparse
+import sys
 import logging
-import optparse
 
+logging.getLogger().setLevel(100)
 
+NAGIOS_OK = 0
+NAGIOS_WARNING = 1
+NAGIOS_CRITICAL = 2
+NAGIOS_UNKNOWN = 3
+
+STATUS_TO_NAGIOS = dict(
+    optimal=NAGIOS_OK,
+    failed=NAGIOS_CRITICAL,
+    degraded=NAGIOS_CRITICAL,
+    recovering=NAGIOS_WARNING)
+
+
 class RAIDManager(object):
 
     def __init__(self, host, port, storage):
@@ -50,10 +64,18 @@
         self.storage = storage
 
         self.raid = ZEO.ClientStorage.ClientStorage(
-            (self.host, self.port), storage=self.storage, wait=1, read_only=1)
+            (self.host, self.port), storage=self.storage, read_only=1,
+            wait=False)
+        if not self.raid.is_connected():
+            self.raid.close()
+            raise RuntimeError(
+                'Could not connect to ZEO server at %s:%s' %
+                (self.host, self.port))
 
     def cmd_status(self):
-        print self.raid.raid_status()
+        status = self.raid.raid_status()
+        print status
+        return STATUS_TO_NAGIOS[status]
 
     def cmd_details(self):
         ok, recovering, failed, recovery_status = self.raid.raid_details()
@@ -63,15 +85,19 @@
         print "\toptimal\t\t", ok
         print "\trecovering\t", recovering, recovery_status
         print "\tfailed\t\t", failed
+        return STATUS_TO_NAGIOS[self.cmd_status()]
 
     def cmd_recover(self, storage):
         print self.raid.raid_recover(storage)
+        return NAGIOS_OK
 
     def cmd_disable(self, storage):
         print self.raid.raid_disable(storage)
+        return NAGIOS_OK
 
     def cmd_reload(self):
         self.raid.raid_reload()
+        return NAGIOS_OK
 
 
 def main(host="127.0.0.1", port=8100, storage="1"):
@@ -95,11 +121,14 @@
 
     command, subargs = args[0], args[1:]
 
-    logging.getLogger().addHandler(logging.StreamHandler())
-    m = RAIDManager(options.host, options.port, options.storage)
-    command = getattr(m, 'cmd_%s' % command)
-    command(*subargs)
+    try:
+        m = RAIDManager(options.host, options.port, options.storage)
+        command = getattr(m, 'cmd_%s' % command)
+        result = command(*subargs)
+    except Exception, e:
+        print str(e)
+        result = NAGIOS_CRITICAL
+    sys.exit(result)
 
-
 if __name__ == '__main__':
     main()

Modified: gocept.zeoraid/trunk/src/gocept/zeoraid/scripts/tests.py
===================================================================
--- gocept.zeoraid/trunk/src/gocept/zeoraid/scripts/tests.py	2009-11-14 08:06:57 UTC (rev 105638)
+++ gocept.zeoraid/trunk/src/gocept/zeoraid/scripts/tests.py	2009-11-14 08:55:25 UTC (rev 105639)
@@ -12,13 +12,13 @@
 #
 ##############################################################################
 
+from zope.testing import doctest, renormalizing
+import gocept.zeoraid.scripts.controller
 import re
+import unittest
 import zc.buildout.testing
 
-import unittest
-from zope.testing import doctest, renormalizing
 
-
 def setUp(test):
     zc.buildout.testing.buildoutSetUp(test)
     zc.buildout.testing.install_develop('gocept.zeoraid', test)
@@ -49,11 +49,20 @@
     ])
 
 
+class TestController(unittest.TestCase):
+
+    def test_disconnected_fails(self):
+        self.assertRaises(RuntimeError,
+            gocept.zeoraid.scripts.controller.RAIDManager,
+            '127.0.0.10', '4000', '1')
+
+
 def test_suite():
-    return unittest.TestSuite((
-        doctest.DocFileSuite(
+    suite = unittest.TestSuite()
+    suite.addTest(doctest.DocFileSuite(
             'recipe.txt',
             setUp=setUp, tearDown=zc.buildout.testing.buildoutTearDown,
             checker=checker,
-            optionflags=doctest.REPORT_NDIFF | doctest.ELLIPSIS),
-        ))
+            optionflags=doctest.REPORT_NDIFF | doctest.ELLIPSIS))
+    suite.addTest(unittest.makeSuite(TestController))
+    return suite



More information about the checkins mailing list