Integrate DRBD with the report generation infrastructure
authorMichele Tartara <mtartara@google.com>
Tue, 8 Jan 2013 14:18:52 +0000 (15:18 +0100)
committerMichele Tartara <mtartara@google.com>
Wed, 16 Jan 2013 13:56:52 +0000 (14:56 +0100)
With this commit, the DRBD data collector does not only print the data
it extracts, but includes them in the proper JSON structure common to
all the data collectors, as prescribed by the design document.

Signed-off-by: Michele Tartara <mtartara@google.com>
Reviewed-by: Iustin Pop <iustin@google.com>

src/Ganeti/DataCollectors/Drbd.hs

index 0a3b6a3..8f0aeb4 100644 (file)
@@ -46,6 +46,7 @@ import Ganeti.Common
 import Ganeti.Confd.Client
 import Ganeti.Confd.Types
 import Ganeti.DataCollectors.CLI
+import Ganeti.DataCollectors.Types
 import Ganeti.Utils
 
 
@@ -61,6 +62,14 @@ defaultFile = C.drbdStatusFile
 defaultCharNum :: Int
 defaultCharNum = 80*20
 
+-- | The name of this data collector.
+dcName :: String
+dcName = "drbd"
+
+-- | The version number for the data format of this data collector.
+dcFormatVersion :: Int
+dcFormatVersion = 1
+
 options :: IO [OptType]
 options =
   return
@@ -95,22 +104,28 @@ getPairingInfo (Just filename) = do
       J.Ok instMinor -> BT.Ok instMinor
       J.Error msg -> BT.Bad msg
 
--- | Main function.
-main :: Options -> [String] -> IO ()
-main opts args = do
-  let proc_drbd = fromMaybe defaultFile $ optDrbdStatus opts
-      instMinor = optDrbdPairing opts
-  unless (null args) . exitErr $ "This program takes exactly zero" ++
-                                  " arguments, got '" ++ unwords args ++ "'"
+-- | This function builds a report with the DRBD status.
+buildDRBDReport :: FilePath -> Maybe FilePath -> IO DCReport
+buildDRBDReport statusFile pairingFile = do
   contents <-
-    ((E.try $ readFile proc_drbd) :: IO (Either IOError String)) >>=
+    ((E.try $ readFile statusFile) :: IO (Either IOError String)) >>=
       exitIfBad "reading from file" . either (BT.Bad . show) BT.Ok
-  pairingResult <- getPairingInfo instMinor
+  pairingResult <- getPairingInfo pairingFile
   pairing <- exitIfBad "Can't get pairing info" pairingResult
-  output <-
+  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.encode drbdStatus
-  putStrLn output
+      A.Done _ drbdStatus -> return $ J.showJSON drbdStatus
+  buildReport dcName Nothing dcFormatVersion jsonData
+
+-- | Main function.
+main :: Options -> [String] -> IO ()
+main opts args = do
+  let statusFile = fromMaybe defaultFile $ optDrbdStatus opts
+      pairingFile = optDrbdPairing opts
+  unless (null args) . exitErr $ "This program takes exactly zero" ++
+                                  " arguments, got '" ++ unwords args ++ "'"
+  report <- buildDRBDReport statusFile pairingFile
+  putStrLn $ J.encode report