Statistics
| Branch: | Tag: | Revision:

root / src / Ganeti / DataCollectors / Types.hs @ c32c4e4d

History | View | Annotate | Download (5.3 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
  , getCategoryName
41
  ) where
42

    
43
import Data.Char
44
import qualified Data.Map as Map
45
import qualified Data.Sequence as Seq
46
import Text.JSON
47

    
48
import Ganeti.Constants as C
49
import Ganeti.THH
50
import Ganeti.Utils (getCurrentTime)
51

    
52
-- | The possible classes a data collector can belong to.
53
data DCCategory = DCInstance | DCStorage | DCDaemon | DCHypervisor
54
  deriving (Show, Eq)
55

    
56
-- | Get the category name and return it as a string.
57
getCategoryName :: DCCategory -> String
58
getCategoryName dcc = map toLower . drop 2 . show $ dcc
59

    
60
-- | The JSON instance for DCCategory.
61
instance JSON DCCategory where
62
  showJSON = showJSON . getCategoryName
63
  readJSON =
64
    error "JSON read instance not implemented for type DCCategory"
65

    
66
-- | The possible status codes of a data collector.
67
data DCStatusCode = DCSCOk      -- ^ Everything is OK
68
                  | DCSCTempBad -- ^ Bad, but being automatically fixed
69
                  | DCSCUnknown -- ^ Unable to determine the status
70
                  | DCSCBad     -- ^ Bad. External intervention required
71
                  deriving (Show, Eq, Ord)
72

    
73
-- | The JSON instance for CollectorStatus.
74
instance JSON DCStatusCode where
75
  showJSON DCSCOk      = showJSON (0 :: Int)
76
  showJSON DCSCTempBad = showJSON (1 :: Int)
77
  showJSON DCSCUnknown = showJSON (2 :: Int)
78
  showJSON DCSCBad     = showJSON (4 :: Int)
79
  readJSON = error "JSON read instance not implemented for type DCStatusCode"
80

    
81
-- | The status of a \"status reporting data collector\".
82
$(buildObject "DCStatus" "dcStatus"
83
  [ simpleField "code"    [t| DCStatusCode |]
84
  , simpleField "message" [t| String |]
85
  ])
86

    
87
-- | The type representing the kind of the collector.
88
data DCKind = DCKPerf   -- ^ Performance reporting collector
89
            | DCKStatus -- ^ Status reporting collector
90
            deriving (Show, Eq)
91

    
92
-- | The JSON instance for CollectorKind.
93
instance JSON DCKind where
94
  showJSON DCKPerf   = showJSON (0 :: Int)
95
  showJSON DCKStatus = showJSON (1 :: Int)
96
  readJSON = error "JSON read instance not implemented for type DCKind"
97

    
98
-- | Type representing the version number of a data collector.
99
data DCVersion = DCVerBuiltin | DCVersion String deriving (Show, Eq)
100

    
101
-- | The JSON instance for DCVersion.
102
instance JSON DCVersion where
103
  showJSON DCVerBuiltin = showJSON C.builtinDataCollectorVersion
104
  showJSON (DCVersion v) = showJSON v
105
  readJSON = error "JSON read instance not implemented for type DCVersion"
106

    
107
-- | Type for the value field of the above map.
108
data CollectorData = CPULoadData (Seq.Seq (Integer, [Int]))
109

    
110
-- | Type for the map storing the data of the statefull DataCollectors.
111
type CollectorMap = Map.Map String CollectorData
112

    
113
-- | This is the format of the report produced by each data collector.
114
$(buildObject "DCReport" "dcReport"
115
  [ simpleField "name"           [t| String |]
116
  , simpleField "version"        [t| DCVersion |]
117
  , simpleField "format_version" [t| Int |]
118
  , simpleField "timestamp"      [t| Integer |]
119
  , optionalNullSerField $
120
      simpleField "category"     [t| DCCategory |]
121
  , simpleField "kind"           [t| DCKind |]
122
  , simpleField "data"           [t| JSValue |]
123
  ])
124

    
125
-- | Add the data collector status information to the JSON representation of
126
-- the collector data.
127
addStatus :: DCStatus -> JSValue -> JSValue
128
addStatus dcStatus (JSObject obj) =
129
  makeObj $ ("status", showJSON dcStatus) : fromJSObject obj
130
addStatus dcStatus value = makeObj
131
  [ ("status", showJSON dcStatus)
132
  , ("data", value)
133
  ]
134

    
135
-- | Helper function for merging statuses.
136
mergeStatuses :: (DCStatusCode, String) -> (DCStatusCode, [String])
137
              -> (DCStatusCode, [String])
138
mergeStatuses (newStat, newStr) (storedStat, storedStrs) =
139
  let resStat = max newStat storedStat
140
      resStrs =
141
        if newStr == ""
142
          then storedStrs
143
          else storedStrs ++ [newStr]
144
  in (resStat, resStrs)
145

    
146
-- | Utility function for building a report automatically adding the current
147
-- timestamp (rounded up to seconds).
148
-- If the version is not specified, it will be set to the value indicating
149
-- a builtin collector.
150
buildReport :: String -> DCVersion -> Int -> Maybe DCCategory -> DCKind
151
            -> JSValue -> IO DCReport
152
buildReport name version format_version category kind jsonData = do
153
  now <- getCurrentTime
154
  let timestamp = now * 1000000000 :: Integer
155
  return $
156
    DCReport name version format_version timestamp category kind
157
      jsonData