The global status is computed from the statuses of the single instances.

The output json format is adapted to include this piece of information, as
prescribed by the design document.

Signed-off-by: Michele Tartara <[email protected]>
---
 src/Ganeti/DataCollectors/InstStatus.hs      | 15 ++++++++++++++-
 src/Ganeti/DataCollectors/InstStatusTypes.hs |  6 ++++++
 2 files changed, 20 insertions(+), 1 deletion(-)

diff --git a/src/Ganeti/DataCollectors/InstStatus.hs 
b/src/Ganeti/DataCollectors/InstStatus.hs
index ecde3a2..ce7cb45 100644
--- a/src/Ganeti/DataCollectors/InstStatus.hs
+++ b/src/Ganeti/DataCollectors/InstStatus.hs
@@ -36,6 +36,7 @@ module Ganeti.DataCollectors.InstStatus
 
 
 import Control.Exception.Base
+import Data.List
 import Data.Maybe
 import qualified Data.Map as Map
 import Network.BSD
@@ -179,6 +180,17 @@ buildStatus domains uptimes inst = do
       trail
       status
 
+-- | Compute the status code and message, given the current DRBD data
+-- The final state will have the code corresponding to the worst code of
+-- all the devices, and the error message given from the concatenation of the
+-- non-empty error messages.
+computeGlobalStatus :: [InstStatus] -> DCStatus
+computeGlobalStatus instStatusList =
+  let dcstatuses = map iStatStatus instStatusList
+      statuses = map (\s -> (dcStatusCode s, dcStatusMessage s)) dcstatuses
+      (code, strList) = foldr mergeStatuses (DCSCOk, [""]) statuses
+  in DCStatus code $ intercalate "\n" strList
+
 -- | Build the report of this data collector, containing all the information
 -- about the status of the instances.
 buildInstStatusReport :: Maybe String -> Maybe Int -> IO DCReport
@@ -190,7 +202,8 @@ buildInstStatusReport srvAddr srvPort = do
   uptimes <- getUptimeInfo
   let primaryInst =  fst inst
   iStatus <- mapM (buildStatus domains uptimes) primaryInst
-  let jsonReport = J.showJSON iStatus
+  let globalStatus = computeGlobalStatus iStatus
+      jsonReport = J.showJSON $ ReportData iStatus globalStatus
   buildReport dcName dcVersion dcFormatVersion dcCategory dcKind jsonReport
 
 -- | Main function.
diff --git a/src/Ganeti/DataCollectors/InstStatusTypes.hs 
b/src/Ganeti/DataCollectors/InstStatusTypes.hs
index 84c2ef1..5803c3d 100644
--- a/src/Ganeti/DataCollectors/InstStatusTypes.hs
+++ b/src/Ganeti/DataCollectors/InstStatusTypes.hs
@@ -26,6 +26,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 
 module Ganeti.DataCollectors.InstStatusTypes
   ( InstStatus(..)
+  , ReportData(..)
   ) where
 
 
@@ -47,3 +48,8 @@ $(buildObject "InstStatus" "iStat"
   , simpleField "state_reason" [t| ReasonTrail |]
   , simpleField "status"       [t| DCStatus |]
   ])
+
+$(buildObject "ReportData" "rData"
+  [ simpleField "instances" [t| [InstStatus] |]
+  , simpleField "status"    [t| DCStatus |]
+  ])
-- 
1.8.2.1

Reply via email to