From 8c5419ee54033bbe89172c13bb822a725a78e0f4 Mon Sep 17 00:00:00 2001
From: Michele Tartara <mtartara@google.com>
Date: Thu, 14 Mar 2013 13:34:36 +0000
Subject: [PATCH] Export the data collected by DRBD in JSON format

Write an intermediate function computing the JSON data before adding them
to the DCReport.

Directly export the data computed by that function (with default parameters)
allowing them to be used (in the future) by the monitoring agent.

Signed-off-by: Michele Tartara <mtartara@google.com>
Reviewed-by: Bernardo Dal Seno <bdalseno@google.com>
---
 src/Ganeti/DataCollectors/Drbd.hs | 26 +++++++++++++++-----------
 1 file changed, 15 insertions(+), 11 deletions(-)

diff --git a/src/Ganeti/DataCollectors/Drbd.hs b/src/Ganeti/DataCollectors/Drbd.hs
index b52e2ce56..7f2855fad 100644
--- a/src/Ganeti/DataCollectors/Drbd.hs
+++ b/src/Ganeti/DataCollectors/Drbd.hs
@@ -32,6 +32,7 @@ module Ganeti.DataCollectors.Drbd
   , dcFormatVersion
   , dcCategory
   , dcKind
+  , dcData
   ) where
 
 
@@ -87,6 +88,10 @@ dcCategory = Just DCStorage
 dcKind :: DCKind
 dcKind = DCKStatus
 
+-- | The data exported by the data collector, taken from the default location.
+dcData :: IO J.JSValue
+dcData = buildJsonReport defaultFile Nothing
+
 -- * Command line options
 
 options :: IO [OptType]
@@ -121,21 +126,19 @@ getPairingInfo (Just filename) = do
       J.Ok instMinor -> BT.Ok instMinor
       J.Error msg -> BT.Bad msg
 
--- | This function builds a report with the DRBD status.
-buildDRBDReport :: FilePath -> Maybe FilePath -> IO DCReport
-buildDRBDReport statusFile pairingFile = do
+-- | This function computes the JSON representation of the DRBD status.
+buildJsonReport :: FilePath -> Maybe FilePath -> IO J.JSValue
+buildJsonReport statusFile pairingFile = do
   contents <-
     ((E.try $ readFile statusFile) :: IO (Either IOError String)) >>=
       exitIfBad "reading from file" . either (BT.Bad . show) BT.Ok
   pairingResult <- getPairingInfo pairingFile
   pairing <- exitIfBad "Can't get pairing info" pairingResult
-  jsonData <-
-    case A.parse (drbdStatusParser pairing) $ pack contents of
-      A.Fail unparsedText contexts errorMessage -> exitErr $
-        show (Prelude.take defaultCharNum $ unpack unparsedText) ++ "\n"
-          ++ show contexts ++ "\n" ++ errorMessage
-      A.Done _ drbdStatus -> return $ J.showJSON drbdStatus
-  buildReport dcName dcVersion dcFormatVersion dcCategory dcKind jsonData
+  case A.parse (drbdStatusParser pairing) $ pack contents of
+    A.Fail unparsedText contexts errorMessage -> exitErr $
+      show (Prelude.take defaultCharNum $ unpack unparsedText) ++ "\n"
+        ++ show contexts ++ "\n" ++ errorMessage
+    A.Done _ drbdStatus -> return $ J.showJSON drbdStatus
 
 -- | Main function.
 main :: Options -> [String] -> IO ()
@@ -144,5 +147,6 @@ main opts args = do
       pairingFile = optDrbdPairing opts
   unless (null args) . exitErr $ "This program takes exactly zero" ++
                                   " arguments, got '" ++ unwords args ++ "'"
-  report <- buildDRBDReport statusFile pairingFile
+  report <- buildJsonReport statusFile pairingFile >>=
+    buildReport dcName dcVersion dcFormatVersion dcCategory dcKind
   putStrLn $ J.encode report
-- 
GitLab