Statistics
| Branch: | Tag: | Revision:

root / src / Ganeti / Monitoring / Server.hs @ 006d6bc9

History | View | Annotate | Download (6.1 kB)

1 13cc7b84 Michele Tartara
{-# LANGUAGE OverloadedStrings #-}
2 13cc7b84 Michele Tartara
3 13cc7b84 Michele Tartara
{-| Implementation of the Ganeti confd server functionality.
4 13cc7b84 Michele Tartara
5 13cc7b84 Michele Tartara
-}
6 13cc7b84 Michele Tartara
7 13cc7b84 Michele Tartara
{-
8 13cc7b84 Michele Tartara
9 13cc7b84 Michele Tartara
Copyright (C) 2013 Google Inc.
10 13cc7b84 Michele Tartara
11 13cc7b84 Michele Tartara
This program is free software; you can redistribute it and/or modify
12 13cc7b84 Michele Tartara
it under the terms of the GNU General Public License as published by
13 13cc7b84 Michele Tartara
the Free Software Foundation; either version 2 of the License, or
14 13cc7b84 Michele Tartara
(at your option) any later version.
15 13cc7b84 Michele Tartara
16 13cc7b84 Michele Tartara
This program is distributed in the hope that it will be useful, but
17 13cc7b84 Michele Tartara
WITHOUT ANY WARRANTY; without even the implied warranty of
18 13cc7b84 Michele Tartara
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19 13cc7b84 Michele Tartara
General Public License for more details.
20 13cc7b84 Michele Tartara
21 13cc7b84 Michele Tartara
You should have received a copy of the GNU General Public License
22 13cc7b84 Michele Tartara
along with this program; if not, write to the Free Software
23 13cc7b84 Michele Tartara
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
24 13cc7b84 Michele Tartara
02110-1301, USA.
25 13cc7b84 Michele Tartara
26 13cc7b84 Michele Tartara
-}
27 13cc7b84 Michele Tartara
28 13cc7b84 Michele Tartara
module Ganeti.Monitoring.Server
29 13cc7b84 Michele Tartara
  ( main
30 13cc7b84 Michele Tartara
  , checkMain
31 13cc7b84 Michele Tartara
  , prepMain
32 13cc7b84 Michele Tartara
  ) where
33 13cc7b84 Michele Tartara
34 423b2dd5 Michele Tartara
import Control.Applicative
35 423b2dd5 Michele Tartara
import Control.Monad
36 db1ad1d5 Michele Tartara
import Control.Monad.IO.Class
37 db1ad1d5 Michele Tartara
import Data.ByteString.Char8 hiding (map, filter, find)
38 db1ad1d5 Michele Tartara
import Data.List
39 eb65c915 Michele Tartara
import Snap.Core
40 eb65c915 Michele Tartara
import Snap.Http.Server
41 eb65c915 Michele Tartara
import qualified Text.JSON as J
42 eb65c915 Michele Tartara
43 db1ad1d5 Michele Tartara
import qualified Ganeti.BasicTypes as BT
44 13cc7b84 Michele Tartara
import Ganeti.Daemon
45 aab0927c Michele Tartara
import qualified Ganeti.DataCollectors.Diskstats as Diskstats
46 ecb783f0 Michele Tartara
import qualified Ganeti.DataCollectors.Drbd as Drbd
47 8a049311 Michele Tartara
import qualified Ganeti.DataCollectors.InstStatus as InstStatus
48 006d6bc9 Michele Tartara
import qualified Ganeti.DataCollectors.Lv as Lv
49 ecb783f0 Michele Tartara
import Ganeti.DataCollectors.Types
50 eb65c915 Michele Tartara
import qualified Ganeti.Constants as C
51 13cc7b84 Michele Tartara
52 13cc7b84 Michele Tartara
-- * Types and constants definitions
53 13cc7b84 Michele Tartara
54 13cc7b84 Michele Tartara
-- | Type alias for checkMain results.
55 13cc7b84 Michele Tartara
type CheckResult = ()
56 13cc7b84 Michele Tartara
57 13cc7b84 Michele Tartara
-- | Type alias for prepMain results.
58 eb65c915 Michele Tartara
type PrepResult = Config Snap ()
59 eb65c915 Michele Tartara
60 eb65c915 Michele Tartara
-- | Version of the latest supported http API.
61 eb65c915 Michele Tartara
latestAPIVersion :: Int
62 eb65c915 Michele Tartara
latestAPIVersion = 1
63 eb65c915 Michele Tartara
64 ecb783f0 Michele Tartara
-- | Type describing a data collector basic information
65 ecb783f0 Michele Tartara
data DataCollector = DataCollector
66 ecb783f0 Michele Tartara
  { dName     :: String           -- ^ Name of the data collector
67 ecb783f0 Michele Tartara
  , dCategory :: Maybe DCCategory -- ^ Category (storage, instance, ecc)
68 ecb783f0 Michele Tartara
                                  --   of the collector
69 ecb783f0 Michele Tartara
  , dKind     :: DCKind           -- ^ Kind (performance or status reporting) of
70 ecb783f0 Michele Tartara
                                  --   the data collector
71 db1ad1d5 Michele Tartara
  , dReport   :: IO DCReport      -- ^ Report produced by the collector
72 ecb783f0 Michele Tartara
  }
73 ecb783f0 Michele Tartara
74 ecb783f0 Michele Tartara
-- | The list of available builtin data collectors.
75 ecb783f0 Michele Tartara
collectors :: [DataCollector]
76 ecb783f0 Michele Tartara
collectors =
77 aab0927c Michele Tartara
  [ DataCollector Diskstats.dcName Diskstats.dcCategory Diskstats.dcKind
78 aab0927c Michele Tartara
      Diskstats.dcReport
79 aab0927c Michele Tartara
  , DataCollector Drbd.dcName Drbd.dcCategory Drbd.dcKind Drbd.dcReport
80 8a049311 Michele Tartara
  , DataCollector InstStatus.dcName InstStatus.dcCategory InstStatus.dcKind
81 8a049311 Michele Tartara
      InstStatus.dcReport
82 006d6bc9 Michele Tartara
  , DataCollector Lv.dcName Lv.dcCategory Lv.dcKind Lv.dcReport
83 ecb783f0 Michele Tartara
  ]
84 ecb783f0 Michele Tartara
85 eb65c915 Michele Tartara
-- * Configuration handling
86 eb65c915 Michele Tartara
87 eb65c915 Michele Tartara
-- | The default configuration for the HTTP server.
88 eb65c915 Michele Tartara
defaultHttpConf :: Config Snap ()
89 eb65c915 Michele Tartara
defaultHttpConf =
90 eb65c915 Michele Tartara
  setAccessLog (ConfigFileLog C.daemonsExtraLogfilesGanetiMondAccess) .
91 eb65c915 Michele Tartara
  setCompression False .
92 eb65c915 Michele Tartara
  setErrorLog (ConfigFileLog C.daemonsExtraLogfilesGanetiMondError) $
93 eb65c915 Michele Tartara
  setVerbose False
94 eb65c915 Michele Tartara
  emptyConfig
95 13cc7b84 Michele Tartara
96 13cc7b84 Michele Tartara
-- * Helper functions
97 13cc7b84 Michele Tartara
98 13cc7b84 Michele Tartara
-- | Check function for the monitoring agent.
99 13cc7b84 Michele Tartara
checkMain :: CheckFn CheckResult
100 13cc7b84 Michele Tartara
checkMain _ = return $ Right ()
101 13cc7b84 Michele Tartara
102 13cc7b84 Michele Tartara
-- | Prepare function for monitoring agent.
103 13cc7b84 Michele Tartara
prepMain :: PrepFn CheckResult PrepResult
104 eb65c915 Michele Tartara
prepMain opts _ =
105 eb65c915 Michele Tartara
  return $
106 eb65c915 Michele Tartara
    setPort (maybe C.defaultMondPort fromIntegral (optPort opts))
107 eb65c915 Michele Tartara
      defaultHttpConf
108 eb65c915 Michele Tartara
109 eb65c915 Michele Tartara
-- * Query answers
110 eb65c915 Michele Tartara
111 eb65c915 Michele Tartara
-- | Reply to the supported API version numbers query.
112 eb65c915 Michele Tartara
versionQ :: Snap ()
113 423b2dd5 Michele Tartara
versionQ = writeBS . pack $ J.encode [latestAPIVersion]
114 423b2dd5 Michele Tartara
115 423b2dd5 Michele Tartara
-- | Version 1 of the monitoring HTTP API.
116 423b2dd5 Michele Tartara
version1Api :: Snap ()
117 423b2dd5 Michele Tartara
version1Api =
118 423b2dd5 Michele Tartara
  let returnNull = writeBS . pack $ J.encode J.JSNull :: Snap ()
119 423b2dd5 Michele Tartara
  in ifTop returnNull <|>
120 423b2dd5 Michele Tartara
     route
121 423b2dd5 Michele Tartara
       [ ("list", listHandler)
122 423b2dd5 Michele Tartara
       , ("report", reportHandler)
123 423b2dd5 Michele Tartara
       ]
124 423b2dd5 Michele Tartara
125 ecb783f0 Michele Tartara
-- | Get the JSON representation of a data collector to be used in the collector
126 ecb783f0 Michele Tartara
-- list.
127 ecb783f0 Michele Tartara
dcListItem :: DataCollector -> J.JSValue
128 ecb783f0 Michele Tartara
dcListItem dc =
129 ecb783f0 Michele Tartara
  J.JSArray
130 ecb783f0 Michele Tartara
    [ J.showJSON $ dName dc
131 ecb783f0 Michele Tartara
    , maybe J.JSNull J.showJSON $ dCategory dc
132 ecb783f0 Michele Tartara
    , J.showJSON $ dKind dc
133 ecb783f0 Michele Tartara
    ]
134 ecb783f0 Michele Tartara
135 423b2dd5 Michele Tartara
-- | Handler for returning lists.
136 423b2dd5 Michele Tartara
listHandler :: Snap ()
137 423b2dd5 Michele Tartara
listHandler =
138 ecb783f0 Michele Tartara
  dir "collectors" . writeBS . pack . J.encode $ map dcListItem collectors
139 423b2dd5 Michele Tartara
140 423b2dd5 Michele Tartara
-- | Handler for returning data collector reports.
141 423b2dd5 Michele Tartara
reportHandler :: Snap ()
142 423b2dd5 Michele Tartara
reportHandler =
143 423b2dd5 Michele Tartara
  route
144 423b2dd5 Michele Tartara
    [ ("all", allReports)
145 423b2dd5 Michele Tartara
    , (":category/:collector", oneReport)
146 e580e9f7 Michele Tartara
    ] <|>
147 e580e9f7 Michele Tartara
  errorReport
148 423b2dd5 Michele Tartara
149 db1ad1d5 Michele Tartara
-- | Return the report of all the available collectors.
150 423b2dd5 Michele Tartara
allReports :: Snap ()
151 6327828e Michele Tartara
allReports = do
152 6327828e Michele Tartara
  reports <- mapM (liftIO . dReport) collectors
153 6327828e Michele Tartara
  writeBS . pack . J.encode $ reports
154 423b2dd5 Michele Tartara
155 db1ad1d5 Michele Tartara
-- | Returns a category given its name.
156 db1ad1d5 Michele Tartara
-- If "collector" is given as the name, the collector has no category, and
157 db1ad1d5 Michele Tartara
-- Nothing will be returned.
158 db1ad1d5 Michele Tartara
catFromName :: String -> BT.Result (Maybe DCCategory)
159 db1ad1d5 Michele Tartara
catFromName "instance"   = BT.Ok $ Just DCInstance
160 db1ad1d5 Michele Tartara
catFromName "storage"    = BT.Ok $ Just DCStorage
161 db1ad1d5 Michele Tartara
catFromName "daemon"     = BT.Ok $ Just DCDaemon
162 db1ad1d5 Michele Tartara
catFromName "hypervisor" = BT.Ok $ Just DCHypervisor
163 db1ad1d5 Michele Tartara
catFromName "default"    = BT.Ok Nothing
164 db1ad1d5 Michele Tartara
catFromName _            = BT.Bad "No such category"
165 db1ad1d5 Michele Tartara
166 e580e9f7 Michele Tartara
errorReport :: Snap ()
167 e580e9f7 Michele Tartara
errorReport = do
168 e580e9f7 Michele Tartara
  modifyResponse $ setResponseStatus 404 "Not found"
169 e580e9f7 Michele Tartara
  writeBS "Unable to produce a report for the requested resource"
170 e580e9f7 Michele Tartara
171 e580e9f7 Michele Tartara
error404 :: Snap ()
172 e580e9f7 Michele Tartara
error404 = do
173 e580e9f7 Michele Tartara
  modifyResponse $ setResponseStatus 404 "Not found"
174 e580e9f7 Michele Tartara
  writeBS "Resource not found"
175 e580e9f7 Michele Tartara
176 423b2dd5 Michele Tartara
-- | Return the report of one collector
177 423b2dd5 Michele Tartara
oneReport :: Snap ()
178 423b2dd5 Michele Tartara
oneReport = do
179 db1ad1d5 Michele Tartara
  categoryName <- fmap (maybe mzero unpack) $ getParam "category"
180 db1ad1d5 Michele Tartara
  collectorName <- fmap (maybe mzero unpack) $ getParam "collector"
181 db1ad1d5 Michele Tartara
  category <-
182 db1ad1d5 Michele Tartara
    case catFromName categoryName of
183 db1ad1d5 Michele Tartara
      BT.Ok cat -> return cat
184 db1ad1d5 Michele Tartara
      BT.Bad msg -> fail msg
185 db1ad1d5 Michele Tartara
  collector <-
186 db1ad1d5 Michele Tartara
    case
187 db1ad1d5 Michele Tartara
      find (\col -> collectorName == dName col) $
188 db1ad1d5 Michele Tartara
        filter (\c -> category == dCategory c) collectors of
189 db1ad1d5 Michele Tartara
      Just col -> return col
190 db1ad1d5 Michele Tartara
      Nothing -> fail "Unable to find the requested collector"
191 db1ad1d5 Michele Tartara
  report <- liftIO $ dReport collector
192 db1ad1d5 Michele Tartara
  writeBS . pack . J.encode $ report
193 eb65c915 Michele Tartara
194 eb65c915 Michele Tartara
-- | The function implementing the HTTP API of the monitoring agent.
195 eb65c915 Michele Tartara
monitoringApi :: Snap ()
196 eb65c915 Michele Tartara
monitoringApi =
197 423b2dd5 Michele Tartara
  ifTop versionQ <|>
198 e580e9f7 Michele Tartara
  dir "1" version1Api <|>
199 e580e9f7 Michele Tartara
  error404
200 13cc7b84 Michele Tartara
201 13cc7b84 Michele Tartara
-- | Main function.
202 13cc7b84 Michele Tartara
main :: MainFn CheckResult PrepResult
203 eb65c915 Michele Tartara
main _ _ httpConf =
204 423b2dd5 Michele Tartara
  httpServe httpConf $ method GET monitoringApi