Revision db1ad1d5

b/src/Ganeti/Monitoring/Server.hs
33 33

  
34 34
import Control.Applicative
35 35
import Control.Monad
36
import Control.Monad.IO.Class
37
import Data.ByteString.Char8 hiding (map, filter, find)
38
import Data.List
36 39
import Snap.Core
37 40
import Snap.Http.Server
38
import Data.ByteString.Char8 hiding (map)
39 41
import qualified Text.JSON as J
40 42

  
43
import qualified Ganeti.BasicTypes as BT
41 44
import Ganeti.Daemon
42 45
import qualified Ganeti.DataCollectors.Drbd as Drbd
43 46
import Ganeti.DataCollectors.Types
......
62 65
                                  --   of the collector
63 66
  , dKind     :: DCKind           -- ^ Kind (performance or status reporting) of
64 67
                                  --   the data collector
68
  , dReport   :: IO DCReport      -- ^ Report produced by the collector
65 69
  }
66 70

  
67 71
-- | The list of available builtin data collectors.
68 72
collectors :: [DataCollector]
69 73
collectors =
70
  [ DataCollector Drbd.dcName Drbd.dcCategory Drbd.dcKind
74
  [ DataCollector Drbd.dcName Drbd.dcCategory Drbd.dcKind Drbd.dcReport
71 75
  ]
72 76

  
73

  
74 77
-- * Configuration handling
75 78

  
76 79
-- | The default configuration for the HTTP server.
......
134 137
    , (":category/:collector", oneReport)
135 138
    ]
136 139

  
137
-- | Return the report of all the available collectors
140
-- | Return the report of all the available collectors.
138 141
allReports :: Snap ()
139 142
allReports = writeText "TODO: return the reports of all the collectors"
140 143

  
144
-- | Returns a category given its name.
145
-- If "collector" is given as the name, the collector has no category, and
146
-- Nothing will be returned.
147
catFromName :: String -> BT.Result (Maybe DCCategory)
148
catFromName "instance"   = BT.Ok $ Just DCInstance
149
catFromName "storage"    = BT.Ok $ Just DCStorage
150
catFromName "daemon"     = BT.Ok $ Just DCDaemon
151
catFromName "hypervisor" = BT.Ok $ Just DCHypervisor
152
catFromName "default"    = BT.Ok Nothing
153
catFromName _            = BT.Bad "No such category"
154

  
141 155
-- | Return the report of one collector
142 156
oneReport :: Snap ()
143 157
oneReport = do
144
  category <- fmap (maybe mzero unpack) $ getParam "category"
145
  collector <- fmap (maybe mzero unpack) $ getParam "collector"
146
  writeBS . pack $
147
    "TODO: return the report for collector " ++ category
148
      ++ "/" ++ collector
158
  categoryName <- fmap (maybe mzero unpack) $ getParam "category"
159
  collectorName <- fmap (maybe mzero unpack) $ getParam "collector"
160
  category <-
161
    case catFromName categoryName of
162
      BT.Ok cat -> return cat
163
      BT.Bad msg -> fail msg
164
  collector <-
165
    case
166
      find (\col -> collectorName == dName col) $
167
        filter (\c -> category == dCategory c) collectors of
168
      Just col -> return col
169
      Nothing -> fail "Unable to find the requested collector"
170
  report <- liftIO $ dReport collector
171
  writeBS . pack . J.encode $ report
149 172

  
150 173
-- | The function implementing the HTTP API of the monitoring agent.
151 174
-- TODO: Currently it only replies to the API version query: implement all the

Also available in: Unified diff