Statistics
| Branch: | Tag: | Revision:

root / src / Ganeti / Monitoring / Server.hs @ aab0927c

History | View | Annotate | Download (6 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 ecb783f0 Michele Tartara
import Ganeti.DataCollectors.Types
49 eb65c915 Michele Tartara
import qualified Ganeti.Constants as C
50 13cc7b84 Michele Tartara
51 13cc7b84 Michele Tartara
-- * Types and constants definitions
52 13cc7b84 Michele Tartara
53 13cc7b84 Michele Tartara
-- | Type alias for checkMain results.
54 13cc7b84 Michele Tartara
type CheckResult = ()
55 13cc7b84 Michele Tartara
56 13cc7b84 Michele Tartara
-- | Type alias for prepMain results.
57 eb65c915 Michele Tartara
type PrepResult = Config Snap ()
58 eb65c915 Michele Tartara
59 eb65c915 Michele Tartara
-- | Version of the latest supported http API.
60 eb65c915 Michele Tartara
latestAPIVersion :: Int
61 eb65c915 Michele Tartara
latestAPIVersion = 1
62 eb65c915 Michele Tartara
63 ecb783f0 Michele Tartara
-- | Type describing a data collector basic information
64 ecb783f0 Michele Tartara
data DataCollector = DataCollector
65 ecb783f0 Michele Tartara
  { dName     :: String           -- ^ Name of the data collector
66 ecb783f0 Michele Tartara
  , dCategory :: Maybe DCCategory -- ^ Category (storage, instance, ecc)
67 ecb783f0 Michele Tartara
                                  --   of the collector
68 ecb783f0 Michele Tartara
  , dKind     :: DCKind           -- ^ Kind (performance or status reporting) of
69 ecb783f0 Michele Tartara
                                  --   the data collector
70 db1ad1d5 Michele Tartara
  , dReport   :: IO DCReport      -- ^ Report produced by the collector
71 ecb783f0 Michele Tartara
  }
72 ecb783f0 Michele Tartara
73 ecb783f0 Michele Tartara
-- | The list of available builtin data collectors.
74 ecb783f0 Michele Tartara
collectors :: [DataCollector]
75 ecb783f0 Michele Tartara
collectors =
76 aab0927c Michele Tartara
  [ DataCollector Diskstats.dcName Diskstats.dcCategory Diskstats.dcKind
77 aab0927c Michele Tartara
      Diskstats.dcReport
78 aab0927c Michele Tartara
  , DataCollector Drbd.dcName Drbd.dcCategory Drbd.dcKind Drbd.dcReport
79 8a049311 Michele Tartara
  , DataCollector InstStatus.dcName InstStatus.dcCategory InstStatus.dcKind
80 8a049311 Michele Tartara
      InstStatus.dcReport
81 ecb783f0 Michele Tartara
  ]
82 ecb783f0 Michele Tartara
83 eb65c915 Michele Tartara
-- * Configuration handling
84 eb65c915 Michele Tartara
85 eb65c915 Michele Tartara
-- | The default configuration for the HTTP server.
86 eb65c915 Michele Tartara
defaultHttpConf :: Config Snap ()
87 eb65c915 Michele Tartara
defaultHttpConf =
88 eb65c915 Michele Tartara
  setAccessLog (ConfigFileLog C.daemonsExtraLogfilesGanetiMondAccess) .
89 eb65c915 Michele Tartara
  setCompression False .
90 eb65c915 Michele Tartara
  setErrorLog (ConfigFileLog C.daemonsExtraLogfilesGanetiMondError) $
91 eb65c915 Michele Tartara
  setVerbose False
92 eb65c915 Michele Tartara
  emptyConfig
93 13cc7b84 Michele Tartara
94 13cc7b84 Michele Tartara
-- * Helper functions
95 13cc7b84 Michele Tartara
96 13cc7b84 Michele Tartara
-- | Check function for the monitoring agent.
97 13cc7b84 Michele Tartara
checkMain :: CheckFn CheckResult
98 13cc7b84 Michele Tartara
checkMain _ = return $ Right ()
99 13cc7b84 Michele Tartara
100 13cc7b84 Michele Tartara
-- | Prepare function for monitoring agent.
101 13cc7b84 Michele Tartara
prepMain :: PrepFn CheckResult PrepResult
102 eb65c915 Michele Tartara
prepMain opts _ =
103 eb65c915 Michele Tartara
  return $
104 eb65c915 Michele Tartara
    setPort (maybe C.defaultMondPort fromIntegral (optPort opts))
105 eb65c915 Michele Tartara
      defaultHttpConf
106 eb65c915 Michele Tartara
107 eb65c915 Michele Tartara
-- * Query answers
108 eb65c915 Michele Tartara
109 eb65c915 Michele Tartara
-- | Reply to the supported API version numbers query.
110 eb65c915 Michele Tartara
versionQ :: Snap ()
111 423b2dd5 Michele Tartara
versionQ = writeBS . pack $ J.encode [latestAPIVersion]
112 423b2dd5 Michele Tartara
113 423b2dd5 Michele Tartara
-- | Version 1 of the monitoring HTTP API.
114 423b2dd5 Michele Tartara
version1Api :: Snap ()
115 423b2dd5 Michele Tartara
version1Api =
116 423b2dd5 Michele Tartara
  let returnNull = writeBS . pack $ J.encode J.JSNull :: Snap ()
117 423b2dd5 Michele Tartara
  in ifTop returnNull <|>
118 423b2dd5 Michele Tartara
     route
119 423b2dd5 Michele Tartara
       [ ("list", listHandler)
120 423b2dd5 Michele Tartara
       , ("report", reportHandler)
121 423b2dd5 Michele Tartara
       ]
122 423b2dd5 Michele Tartara
123 ecb783f0 Michele Tartara
-- | Get the JSON representation of a data collector to be used in the collector
124 ecb783f0 Michele Tartara
-- list.
125 ecb783f0 Michele Tartara
dcListItem :: DataCollector -> J.JSValue
126 ecb783f0 Michele Tartara
dcListItem dc =
127 ecb783f0 Michele Tartara
  J.JSArray
128 ecb783f0 Michele Tartara
    [ J.showJSON $ dName dc
129 ecb783f0 Michele Tartara
    , maybe J.JSNull J.showJSON $ dCategory dc
130 ecb783f0 Michele Tartara
    , J.showJSON $ dKind dc
131 ecb783f0 Michele Tartara
    ]
132 ecb783f0 Michele Tartara
133 423b2dd5 Michele Tartara
-- | Handler for returning lists.
134 423b2dd5 Michele Tartara
listHandler :: Snap ()
135 423b2dd5 Michele Tartara
listHandler =
136 ecb783f0 Michele Tartara
  dir "collectors" . writeBS . pack . J.encode $ map dcListItem collectors
137 423b2dd5 Michele Tartara
138 423b2dd5 Michele Tartara
-- | Handler for returning data collector reports.
139 423b2dd5 Michele Tartara
reportHandler :: Snap ()
140 423b2dd5 Michele Tartara
reportHandler =
141 423b2dd5 Michele Tartara
  route
142 423b2dd5 Michele Tartara
    [ ("all", allReports)
143 423b2dd5 Michele Tartara
    , (":category/:collector", oneReport)
144 e580e9f7 Michele Tartara
    ] <|>
145 e580e9f7 Michele Tartara
  errorReport
146 423b2dd5 Michele Tartara
147 db1ad1d5 Michele Tartara
-- | Return the report of all the available collectors.
148 423b2dd5 Michele Tartara
allReports :: Snap ()
149 6327828e Michele Tartara
allReports = do
150 6327828e Michele Tartara
  reports <- mapM (liftIO . dReport) collectors
151 6327828e Michele Tartara
  writeBS . pack . J.encode $ reports
152 423b2dd5 Michele Tartara
153 db1ad1d5 Michele Tartara
-- | Returns a category given its name.
154 db1ad1d5 Michele Tartara
-- If "collector" is given as the name, the collector has no category, and
155 db1ad1d5 Michele Tartara
-- Nothing will be returned.
156 db1ad1d5 Michele Tartara
catFromName :: String -> BT.Result (Maybe DCCategory)
157 db1ad1d5 Michele Tartara
catFromName "instance"   = BT.Ok $ Just DCInstance
158 db1ad1d5 Michele Tartara
catFromName "storage"    = BT.Ok $ Just DCStorage
159 db1ad1d5 Michele Tartara
catFromName "daemon"     = BT.Ok $ Just DCDaemon
160 db1ad1d5 Michele Tartara
catFromName "hypervisor" = BT.Ok $ Just DCHypervisor
161 db1ad1d5 Michele Tartara
catFromName "default"    = BT.Ok Nothing
162 db1ad1d5 Michele Tartara
catFromName _            = BT.Bad "No such category"
163 db1ad1d5 Michele Tartara
164 e580e9f7 Michele Tartara
errorReport :: Snap ()
165 e580e9f7 Michele Tartara
errorReport = do
166 e580e9f7 Michele Tartara
  modifyResponse $ setResponseStatus 404 "Not found"
167 e580e9f7 Michele Tartara
  writeBS "Unable to produce a report for the requested resource"
168 e580e9f7 Michele Tartara
169 e580e9f7 Michele Tartara
error404 :: Snap ()
170 e580e9f7 Michele Tartara
error404 = do
171 e580e9f7 Michele Tartara
  modifyResponse $ setResponseStatus 404 "Not found"
172 e580e9f7 Michele Tartara
  writeBS "Resource not found"
173 e580e9f7 Michele Tartara
174 423b2dd5 Michele Tartara
-- | Return the report of one collector
175 423b2dd5 Michele Tartara
oneReport :: Snap ()
176 423b2dd5 Michele Tartara
oneReport = do
177 db1ad1d5 Michele Tartara
  categoryName <- fmap (maybe mzero unpack) $ getParam "category"
178 db1ad1d5 Michele Tartara
  collectorName <- fmap (maybe mzero unpack) $ getParam "collector"
179 db1ad1d5 Michele Tartara
  category <-
180 db1ad1d5 Michele Tartara
    case catFromName categoryName of
181 db1ad1d5 Michele Tartara
      BT.Ok cat -> return cat
182 db1ad1d5 Michele Tartara
      BT.Bad msg -> fail msg
183 db1ad1d5 Michele Tartara
  collector <-
184 db1ad1d5 Michele Tartara
    case
185 db1ad1d5 Michele Tartara
      find (\col -> collectorName == dName col) $
186 db1ad1d5 Michele Tartara
        filter (\c -> category == dCategory c) collectors of
187 db1ad1d5 Michele Tartara
      Just col -> return col
188 db1ad1d5 Michele Tartara
      Nothing -> fail "Unable to find the requested collector"
189 db1ad1d5 Michele Tartara
  report <- liftIO $ dReport collector
190 db1ad1d5 Michele Tartara
  writeBS . pack . J.encode $ report
191 eb65c915 Michele Tartara
192 eb65c915 Michele Tartara
-- | The function implementing the HTTP API of the monitoring agent.
193 eb65c915 Michele Tartara
monitoringApi :: Snap ()
194 eb65c915 Michele Tartara
monitoringApi =
195 423b2dd5 Michele Tartara
  ifTop versionQ <|>
196 e580e9f7 Michele Tartara
  dir "1" version1Api <|>
197 e580e9f7 Michele Tartara
  error404
198 13cc7b84 Michele Tartara
199 13cc7b84 Michele Tartara
-- | Main function.
200 13cc7b84 Michele Tartara
main :: MainFn CheckResult PrepResult
201 eb65c915 Michele Tartara
main _ _ httpConf =
202 423b2dd5 Michele Tartara
  httpServe httpConf $ method GET monitoringApi