Generate report for a single collector
authorMichele Tartara <mtartara@google.com>
Mon, 29 Apr 2013 14:49:51 +0000 (14:49 +0000)
committerMichele Tartara <mtartara@google.com>
Tue, 30 Apr 2013 07:56:30 +0000 (09:56 +0200)
Allow to ask the monitoring daemon for the report of one specific data
collector.

Signed-off-by: Michele Tartara <mtartara@google.com>
Reviewed-by: Helga Velroyen <helgav@google.com>

src/Ganeti/Monitoring/Server.hs

index 9c31fa0..9a9af40 100644 (file)
@@ -33,11 +33,14 @@ module Ganeti.Monitoring.Server
 
 import Control.Applicative
 import Control.Monad
+import Control.Monad.IO.Class
+import Data.ByteString.Char8 hiding (map, filter, find)
+import Data.List
 import Snap.Core
 import Snap.Http.Server
-import Data.ByteString.Char8 hiding (map)
 import qualified Text.JSON as J
 
+import qualified Ganeti.BasicTypes as BT
 import Ganeti.Daemon
 import qualified Ganeti.DataCollectors.Drbd as Drbd
 import Ganeti.DataCollectors.Types
@@ -62,15 +65,15 @@ data DataCollector = DataCollector
                                   --   of the collector
   , dKind     :: DCKind           -- ^ Kind (performance or status reporting) of
                                   --   the data collector
+  , dReport   :: IO DCReport      -- ^ Report produced by the collector
   }
 
 -- | The list of available builtin data collectors.
 collectors :: [DataCollector]
 collectors =
-  [ DataCollector Drbd.dcName Drbd.dcCategory Drbd.dcKind
+  [ DataCollector Drbd.dcName Drbd.dcCategory Drbd.dcKind Drbd.dcReport
   ]
 
-
 -- * Configuration handling
 
 -- | The default configuration for the HTTP server.
@@ -134,18 +137,38 @@ reportHandler =
     , (":category/:collector", oneReport)
     ]
 
--- | Return the report of all the available collectors
+-- | Return the report of all the available collectors.
 allReports :: Snap ()
 allReports = writeText "TODO: return the reports of all the collectors"
 
+-- | Returns a category given its name.
+-- If "collector" is given as the name, the collector has no category, and
+-- Nothing will be returned.
+catFromName :: String -> BT.Result (Maybe DCCategory)
+catFromName "instance"   = BT.Ok $ Just DCInstance
+catFromName "storage"    = BT.Ok $ Just DCStorage
+catFromName "daemon"     = BT.Ok $ Just DCDaemon
+catFromName "hypervisor" = BT.Ok $ Just DCHypervisor
+catFromName "default"    = BT.Ok Nothing
+catFromName _            = BT.Bad "No such category"
+
 -- | Return the report of one collector
 oneReport :: Snap ()
 oneReport = do
-  category <- fmap (maybe mzero unpack) $ getParam "category"
-  collector <- fmap (maybe mzero unpack) $ getParam "collector"
-  writeBS . pack $
-    "TODO: return the report for collector " ++ category
-      ++ "/" ++ collector
+  categoryName <- fmap (maybe mzero unpack) $ getParam "category"
+  collectorName <- fmap (maybe mzero unpack) $ getParam "collector"
+  category <-
+    case catFromName categoryName of
+      BT.Ok cat -> return cat
+      BT.Bad msg -> fail msg
+  collector <-
+    case
+      find (\col -> collectorName == dName col) $
+        filter (\c -> category == dCategory c) collectors of
+      Just col -> return col
+      Nothing -> fail "Unable to find the requested collector"
+  report <- liftIO $ dReport collector
+  writeBS . pack . J.encode $ report
 
 -- | The function implementing the HTTP API of the monitoring agent.
 -- TODO: Currently it only replies to the API version query: implement all the