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