Add basic HTTP server functionalities to Mond
[ganeti-github.git] / src / Ganeti / Monitoring / Server.hs
1 {-# LANGUAGE OverloadedStrings #-}
2
3 {-| Implementation of the Ganeti confd server functionality.
4
5 -}
6
7 {-
8
9 Copyright (C) 2013 Google Inc.
10
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.
15
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.
20
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
24 02110-1301, USA.
25
26 -}
27
28 module Ganeti.Monitoring.Server
29 ( main
30 , checkMain
31 , prepMain
32 ) where
33
34 import Snap.Core
35 import Snap.Http.Server
36 import Data.Text
37 import qualified Text.JSON as J
38
39 import Ganeti.Daemon
40 import qualified Ganeti.Constants as C
41
42 -- * Types and constants definitions
43
44 -- | Type alias for checkMain results.
45 type CheckResult = ()
46
47 -- | Type alias for prepMain results.
48 type PrepResult = Config Snap ()
49
50 -- | Version of the latest supported http API.
51 latestAPIVersion :: Int
52 latestAPIVersion = 1
53
54 -- * Configuration handling
55
56 -- | The default configuration for the HTTP server.
57 defaultHttpConf :: Config Snap ()
58 defaultHttpConf =
59 setAccessLog (ConfigFileLog C.daemonsExtraLogfilesGanetiMondAccess) .
60 setCompression False .
61 setErrorLog (ConfigFileLog C.daemonsExtraLogfilesGanetiMondError) $
62 setVerbose False
63 emptyConfig
64
65 -- * Helper functions
66
67 -- | Check function for the monitoring agent.
68 checkMain :: CheckFn CheckResult
69 checkMain _ = return $ Right ()
70
71 -- | Prepare function for monitoring agent.
72 prepMain :: PrepFn CheckResult PrepResult
73 prepMain opts _ =
74 return $
75 setPort (maybe C.defaultMondPort fromIntegral (optPort opts))
76 defaultHttpConf
77
78 -- * Query answers
79
80 -- | Reply to the supported API version numbers query.
81 versionQ :: Snap ()
82 versionQ = writeText . pack $ J.encode [latestAPIVersion]
83
84 -- | The function implementing the HTTP API of the monitoring agent.
85 -- TODO: Currently it only replies to the API version query: implement all the
86 -- missing features.
87 monitoringApi :: Snap ()
88 monitoringApi =
89 ifTop versionQ
90
91 -- | Main function.
92 main :: MainFn CheckResult PrepResult
93 main _ _ httpConf =
94 httpServe httpConf monitoringApi