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