1 {-# LANGUAGE OverloadedStrings #-}
3 {-| Implementation of the Ganeti confd server functionality.
9 Copyright (C) 2013 Google Inc.
11 This program is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation; either version 2 of the License, or
14 (at your option) any later version.
16 This program is distributed in the hope that it will be useful, but
17 WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 General Public License for more details.
21 You should have received a copy of the GNU General Public License
22 along with this program; if not, write to the Free Software
23 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
28 module Ganeti
.Monitoring
.Server
34 import Control
.Applicative
36 import Control
.Monad
.IO.Class
37 import Data
.ByteString
.Char8
hiding (map, filter, find)
40 import Snap
.Http
.Server
41 import qualified Text
.JSON
as J
43 import qualified Ganeti
.BasicTypes
as BT
45 import qualified Ganeti
.DataCollectors
.Drbd
as Drbd
46 import Ganeti
.DataCollectors
.Types
47 import qualified Ganeti
.Constants
as C
49 -- * Types and constants definitions
51 -- | Type alias for checkMain results.
54 -- | Type alias for prepMain results.
55 type PrepResult
= Config Snap
()
57 -- | Version of the latest supported http API.
58 latestAPIVersion
:: Int
61 -- | Type describing a data collector basic information
62 data DataCollector
= DataCollector
63 { dName
:: String -- ^ Name of the data collector
64 , dCategory
:: Maybe DCCategory
-- ^ Category (storage, instance, ecc)
66 , dKind
:: DCKind
-- ^ Kind (performance or status reporting) of
68 , dReport
:: IO DCReport
-- ^ Report produced by the collector
71 -- | The list of available builtin data collectors.
72 collectors
:: [DataCollector
]
74 [ DataCollector Drbd
.dcName Drbd
.dcCategory Drbd
.dcKind Drbd
.dcReport
77 -- * Configuration handling
79 -- | The default configuration for the HTTP server.
80 defaultHttpConf
:: Config Snap
()
82 setAccessLog
(ConfigFileLog C
.daemonsExtraLogfilesGanetiMondAccess
) .
83 setCompression
False .
84 setErrorLog
(ConfigFileLog C
.daemonsExtraLogfilesGanetiMondError
) $
90 -- | Check function for the monitoring agent.
91 checkMain
:: CheckFn CheckResult
92 checkMain _
= return $ Right
()
94 -- | Prepare function for monitoring agent.
95 prepMain
:: PrepFn CheckResult PrepResult
98 setPort
(maybe C
.defaultMondPort
fromIntegral (optPort opts
))
103 -- | Reply to the supported API version numbers query.
105 versionQ
= writeBS
. pack
$ J
.encode
[latestAPIVersion
]
107 -- | Version 1 of the monitoring HTTP API.
108 version1Api
:: Snap
()
110 let returnNull
= writeBS
. pack
$ J
.encode J
.JSNull
:: Snap
()
111 in ifTop returnNull
<|>
113 [ ("list", listHandler
)
114 , ("report", reportHandler
)
117 -- | Get the JSON representation of a data collector to be used in the collector
119 dcListItem
:: DataCollector
-> J
.JSValue
122 [ J
.showJSON
$ dName dc
123 , maybe J
.JSNull J
.showJSON
$ dCategory dc
124 , J
.showJSON
$ dKind dc
127 -- | Handler for returning lists.
128 listHandler
:: Snap
()
130 dir
"collectors" . writeBS
. pack
. J
.encode
$ map dcListItem collectors
132 -- | Handler for returning data collector reports.
133 reportHandler
:: Snap
()
136 [ ("all", allReports
)
137 , (":category/:collector", oneReport
)
140 -- | Return the report of all the available collectors.
141 allReports
:: Snap
()
142 allReports
= writeText
"TODO: return the reports of all the collectors"
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"
155 -- | Return the report of one collector
158 categoryName
<- fmap (maybe mzero unpack
) $ getParam
"category"
159 collectorName
<- fmap (maybe mzero unpack
) $ getParam
"collector"
161 case catFromName categoryName
of
162 BT
.Ok cat
-> return cat
163 BT
.Bad msg
-> fail msg
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
173 -- | The function implementing the HTTP API of the monitoring agent.
174 -- TODO: Currently it only replies to the API version query: implement all the
176 monitoringApi
:: Snap
()
182 main
:: MainFn CheckResult PrepResult
184 httpServe httpConf
$ method GET monitoringApi