root / src / Ganeti / DataCollectors / Types.hs @ 2da679f7
History | View | Annotate | Download (5.2 kB)
1 |
{-# LANGUAGE TemplateHaskell #-} |
---|---|
2 |
|
3 |
{-| Implementation of the Ganeti data collector types. |
4 |
|
5 |
-} |
6 |
|
7 |
{- |
8 |
|
9 |
Copyright (C) 2012, 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.DataCollectors.Types |
29 |
( addStatus |
30 |
, DCCategory(..) |
31 |
, DCKind(..) |
32 |
, DCReport(..) |
33 |
, DCStatus(..) |
34 |
, DCStatusCode(..) |
35 |
, DCVersion(..) |
36 |
, CollectorData(..) |
37 |
, CollectorMap |
38 |
, buildReport |
39 |
, mergeStatuses |
40 |
) where |
41 |
|
42 |
import Data.Char |
43 |
import qualified Data.Map as Map |
44 |
import qualified Data.Sequence as Seq |
45 |
import Text.JSON |
46 |
|
47 |
import Ganeti.Constants as C |
48 |
import Ganeti.THH |
49 |
import Ganeti.Utils (getCurrentTime) |
50 |
|
51 |
-- | The possible classes a data collector can belong to. |
52 |
data DCCategory = DCInstance | DCStorage | DCDaemon | DCHypervisor |
53 |
deriving (Show, Eq) |
54 |
|
55 |
-- | The JSON instance for DCCategory. |
56 |
instance JSON DCCategory where |
57 |
showJSON = showJSON . map toLower . drop 2 . show |
58 |
readJSON = |
59 |
error "JSON read instance not implemented for type DCCategory" |
60 |
|
61 |
-- | The possible status codes of a data collector. |
62 |
data DCStatusCode = DCSCOk -- ^ Everything is OK |
63 |
| DCSCTempBad -- ^ Bad, but being automatically fixed |
64 |
| DCSCUnknown -- ^ Unable to determine the status |
65 |
| DCSCBad -- ^ Bad. External intervention required |
66 |
deriving (Show, Eq, Ord) |
67 |
|
68 |
-- | The JSON instance for CollectorStatus. |
69 |
instance JSON DCStatusCode where |
70 |
showJSON DCSCOk = showJSON (0 :: Int) |
71 |
showJSON DCSCTempBad = showJSON (1 :: Int) |
72 |
showJSON DCSCUnknown = showJSON (2 :: Int) |
73 |
showJSON DCSCBad = showJSON (4 :: Int) |
74 |
readJSON = error "JSON read instance not implemented for type DCStatusCode" |
75 |
|
76 |
-- | The status of a \"status reporting data collector\". |
77 |
$(buildObject "DCStatus" "dcStatus" |
78 |
[ simpleField "code" [t| DCStatusCode |] |
79 |
, simpleField "message" [t| String |] |
80 |
]) |
81 |
|
82 |
-- | The type representing the kind of the collector. |
83 |
data DCKind = DCKPerf -- ^ Performance reporting collector |
84 |
| DCKStatus -- ^ Status reporting collector |
85 |
deriving (Show, Eq) |
86 |
|
87 |
-- | The JSON instance for CollectorKind. |
88 |
instance JSON DCKind where |
89 |
showJSON DCKPerf = showJSON (0 :: Int) |
90 |
showJSON DCKStatus = showJSON (1 :: Int) |
91 |
readJSON = error "JSON read instance not implemented for type DCKind" |
92 |
|
93 |
-- | Type representing the version number of a data collector. |
94 |
data DCVersion = DCVerBuiltin | DCVersion String deriving (Show, Eq) |
95 |
|
96 |
-- | The JSON instance for DCVersion. |
97 |
instance JSON DCVersion where |
98 |
showJSON DCVerBuiltin = showJSON C.builtinDataCollectorVersion |
99 |
showJSON (DCVersion v) = showJSON v |
100 |
readJSON = error "JSON read instance not implemented for type DCVersion" |
101 |
|
102 |
-- | Type for the value field of the above map. |
103 |
data CollectorData = CPULoadData (Seq.Seq (Integer, [Int])) |
104 |
|
105 |
-- | Type for the map storing the data of the statefull DataCollectors. |
106 |
type CollectorMap = Map.Map String CollectorData |
107 |
|
108 |
-- | This is the format of the report produced by each data collector. |
109 |
$(buildObject "DCReport" "dcReport" |
110 |
[ simpleField "name" [t| String |] |
111 |
, simpleField "version" [t| DCVersion |] |
112 |
, simpleField "format_version" [t| Int |] |
113 |
, simpleField "timestamp" [t| Integer |] |
114 |
, optionalNullSerField $ |
115 |
simpleField "category" [t| DCCategory |] |
116 |
, simpleField "kind" [t| DCKind |] |
117 |
, simpleField "data" [t| JSValue |] |
118 |
]) |
119 |
|
120 |
-- | Add the data collector status information to the JSON representation of |
121 |
-- the collector data. |
122 |
addStatus :: DCStatus -> JSValue -> JSValue |
123 |
addStatus dcStatus (JSObject obj) = |
124 |
makeObj $ ("status", showJSON dcStatus) : fromJSObject obj |
125 |
addStatus dcStatus value = makeObj |
126 |
[ ("status", showJSON dcStatus) |
127 |
, ("data", value) |
128 |
] |
129 |
|
130 |
-- | Helper function for merging statuses. |
131 |
mergeStatuses :: (DCStatusCode, String) -> (DCStatusCode, [String]) |
132 |
-> (DCStatusCode, [String]) |
133 |
mergeStatuses (newStat, newStr) (storedStat, storedStrs) = |
134 |
let resStat = max newStat storedStat |
135 |
resStrs = |
136 |
if newStr == "" |
137 |
then storedStrs |
138 |
else storedStrs ++ [newStr] |
139 |
in (resStat, resStrs) |
140 |
|
141 |
-- | Utility function for building a report automatically adding the current |
142 |
-- timestamp (rounded up to seconds). |
143 |
-- If the version is not specified, it will be set to the value indicating |
144 |
-- a builtin collector. |
145 |
buildReport :: String -> DCVersion -> Int -> Maybe DCCategory -> DCKind |
146 |
-> JSValue -> IO DCReport |
147 |
buildReport name version format_version category kind jsonData = do |
148 |
now <- getCurrentTime |
149 |
let timestamp = now * 1000000000 :: Integer |
150 |
return $ |
151 |
DCReport name version format_version timestamp category kind |
152 |
jsonData |