1 {-# LANGUAGE TemplateHaskell #-}
3 {-| Implementation of the Ganeti data collector types.
9 Copyright (C) 2012, 2013 Google Inc.
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.
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.
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
28 module Ganeti.DataCollectors.Types
37 import Ganeti.Constants as C
39 import Ganeti.Utils (getCurrentTime)
41 -- | The possible classes a data collector can belong to.
42 data DCCategory = DCInstance | DCStorage | DCDaemon | DCHypervisor
45 -- | The JSON instance for DCCategory.
46 instance JSON DCCategory where
47 showJSON = showJSON . show
49 error "JSON read instance not implemented for type DCCategory"
51 -- | Type representing the version number of a data collector.
52 data DCVersion = DCVerBuiltin | DCVersion String deriving (Show, Eq)
54 -- | The JSON instance for DCVersion.
55 instance JSON DCVersion where
56 showJSON DCVerBuiltin = showJSON C.builtinDataCollectorVersion
57 showJSON (DCVersion v) = showJSON v
58 readJSON = error "JSON read instance not implemented for type DCVersion"
60 -- | This is the format of the report produced by each data collector.
61 $(buildObject "DCReport" "dcReport"
62 [ simpleField "name" [t| String |]
63 , simpleField "version" [t| DCVersion |]
64 , simpleField "format_version" [t| Int |]
65 , simpleField "timestamp" [t| Integer |]
66 , optionalNullSerField $
67 simpleField "category" [t| DCCategory |]
68 , simpleField "data" [t| JSValue |]
71 -- | Utility function for building a report automatically adding the current
72 -- timestamp (rounded up to seconds).
73 -- If the version is not specified, it will be set to the value indicating
74 -- a builtin collector.
75 buildReport :: String -> DCVersion -> Int -> Maybe DCCategory -> JSValue
77 buildReport name version format_version category jsonData = do
79 let timestamp = now * 1000000000 :: Integer
80 return $ DCReport name version format_version timestamp category jsonData