Instead of building the report as part of the "Main" function, have it
built by its own dedicated function, so that it will be able to export it
directly to the monitoring daemon when needed.

Signed-off-by: Michele Tartara <[email protected]>
---
 src/Ganeti/DataCollectors/InstStatus.hs | 23 +++++++++++++++--------
 1 file changed, 15 insertions(+), 8 deletions(-)

diff --git a/src/Ganeti/DataCollectors/InstStatus.hs 
b/src/Ganeti/DataCollectors/InstStatus.hs
index f3a8c1c..ecde3a2 100644
--- a/src/Ganeti/DataCollectors/InstStatus.hs
+++ b/src/Ganeti/DataCollectors/InstStatus.hs
@@ -38,7 +38,7 @@ module Ganeti.DataCollectors.InstStatus
 import Control.Exception.Base
 import Data.Maybe
 import qualified Data.Map as Map
-import Network.BSD (getHostName)
+import Network.BSD
 import qualified Text.JSON as J
 
 import qualified Ganeti.BasicTypes as BT
@@ -179,15 +179,22 @@ buildStatus domains uptimes inst = do
       trail
       status
 
--- | Main function.
-main :: Options -> [String] -> IO ()
-main opts _ = do
-  curNode <- getHostName
-  let node = fromMaybe curNode $ optNode opts
-  answer <- getInstances node (optConfdAddr opts) (optConfdPort opts)
+-- | Build the report of this data collector, containing all the information
+-- about the status of the instances.
+buildInstStatusReport :: Maybe String -> Maybe Int -> IO DCReport
+buildInstStatusReport srvAddr srvPort = do
+  node <- getHostName
+  answer <- getInstances node srvAddr srvPort
   inst <- exitIfBad "Can't get instance info from ConfD" answer
   domains <- getInferredDomInfo
   uptimes <- getUptimeInfo
   let primaryInst =  fst inst
   iStatus <- mapM (buildStatus domains uptimes) primaryInst
-  putStrLn $ J.encode iStatus
+  let jsonReport = J.showJSON iStatus
+  buildReport dcName dcVersion dcFormatVersion dcCategory dcKind jsonReport
+
+-- | Main function.
+main :: Options -> [String] -> IO ()
+main opts _ = do
+  report <- buildInstStatusReport (optConfdAddr opts) (optConfdPort opts)
+  putStrLn $ J.encode report
-- 
1.8.2.1

Reply via email to