Add DCStatus data type for the data collectors
authorMichele Tartara <mtartara@google.com>
Thu, 14 Mar 2013 13:09:27 +0000 (13:09 +0000)
committerMichele Tartara <mtartara@google.com>
Thu, 28 Mar 2013 13:13:14 +0000 (14:13 +0100)
Also adds the DCStatusCode, part of DCStatus, and the addStatus
utility function for adding the "status" field to an already existing
JSValue.

The design document is updated to have the status codes sorted by increasing
seriousness.

Signed-off-by: Michele Tartara <mtartara@google.com>
Reviewed-by: Bernardo Dal Seno <bdalseno@google.com>

doc/design-monitoring-agent.rst
src/Ganeti/DataCollectors/Types.hs

index 5f40fea..9957ac2 100644 (file)
@@ -185,14 +185,14 @@ in its ``data`` section, at least the following field:
       There is no need of external intervention.
 
     ``2``
-      The collector can determine that something is wrong and Ganeti has no
-      way to fix it autonomously. External intervention is required.
-
-    ``4``
       The collector has failed to understand whether the status is good or
       bad. Further analysis is required. Interpret this status as a
       potentially dangerous situation.
 
+    ``4``
+      The collector can determine that something is wrong and Ganeti has no
+      way to fix it autonomously. External intervention is required.
+
   ``message``
     A message to better explain the reason of the status.
     The exact format of the message string is data collector dependent.
index bef28c9..8a119ab 100644 (file)
@@ -26,9 +26,12 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 -}
 
 module Ganeti.DataCollectors.Types
-  ( DCReport(..)
+  ( addStatus
   , DCCategory(..)
   , DCKind(..)
+  , DCReport(..)
+  , DCStatus(..)
+  , DCStatusCode(..)
   , DCVersion(..)
   , buildReport
   ) where
@@ -49,6 +52,27 @@ instance JSON DCCategory where
   readJSON =
     error "JSON read instance not implemented for type DCCategory"
 
+-- | The possible status codes of a data collector.
+data DCStatusCode = DCSCOk      -- ^ Everything is OK
+                  | DCSCTempBad -- ^ Bad, but being automatically fixed
+                  | DCSCUnknown -- ^ Unable to determine the status
+                  | DCSCBad     -- ^ Bad. External intervention required
+                  deriving (Show, Eq, Ord)
+
+-- | The JSON instance for CollectorStatus.
+instance JSON DCStatusCode where
+  showJSON DCSCOk      = showJSON (0 :: Int)
+  showJSON DCSCTempBad = showJSON (1 :: Int)
+  showJSON DCSCUnknown = showJSON (2 :: Int)
+  showJSON DCSCBad     = showJSON (4 :: Int)
+  readJSON = error "JSON read instance not implemented for type DCStatusCode"
+
+-- | The status of a \"status reporting data collector\".
+$(buildObject "DCStatus" "dcStatus"
+  [ simpleField "code"    [t| DCStatusCode |]
+  , simpleField "message" [t| String |]
+  ])
+
 -- | The type representing the kind of the collector.
 data DCKind = DCKPerf   -- ^ Performance reporting collector
             | DCKStatus -- ^ Status reporting collector
@@ -81,6 +105,16 @@ $(buildObject "DCReport" "dcReport"
   , simpleField "data"           [t| JSValue |]
   ])
 
+-- | Add the data collector status information to the JSON representation of
+-- the collector data.
+addStatus :: DCStatus -> JSValue -> JSValue
+addStatus dcStatus (JSObject obj) =
+  makeObj $ ("status", showJSON dcStatus) : fromJSObject obj
+addStatus dcStatus value = makeObj
+  [ ("status", showJSON dcStatus)
+  , ("data", value)
+  ]
+
 -- | Utility function for building a report automatically adding the current
 -- timestamp (rounded up to seconds).
 -- If the version is not specified, it will be set to the value indicating