Revision d7e9323b

b/Makefile.am
520 520
	src/Ganeti/Daemon.hs \
521 521
	src/Ganeti/DataCollectors/CLI.hs \
522 522
	src/Ganeti/DataCollectors/Drbd.hs \
523
	src/Ganeti/DataCollectors/InstStatus.hs \
524
	src/Ganeti/DataCollectors/InstStatusTypes.hs \
523 525
	src/Ganeti/DataCollectors/Program.hs \
524 526
	src/Ganeti/DataCollectors/Types.hs \
525 527
	src/Ganeti/Errors.hs \
b/src/Ganeti/DataCollectors/InstStatus.hs
1
{-| Instance status data collector.
2

  
3
-}
4

  
5
{-
6

  
7
Copyright (C) 2013 Google Inc.
8

  
9
This program is free software; you can redistribute it and/or modify
10
it under the terms of the GNU General Public License as published by
11
the Free Software Foundation; either version 2 of the License, or
12
(at your option) any later version.
13

  
14
This program is distributed in the hope that it will be useful, but
15
WITHOUT ANY WARRANTY; without even the implied warranty of
16
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17
General Public License for more details.
18

  
19
You should have received a copy of the GNU General Public License
20
along with this program; if not, write to the Free Software
21
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
22
02110-1301, USA.
23

  
24
-}
25

  
26
module Ganeti.DataCollectors.InstStatus
27
  ( main
28
  , options
29
  , arguments
30
  ) where
31

  
32

  
33
import Data.Maybe
34
import qualified Data.Map as Map
35
import Network.BSD (getHostName)
36
import qualified Text.JSON as J
37

  
38
import qualified Ganeti.BasicTypes as BT
39
import Ganeti.Confd.Client
40
import Ganeti.Confd.Types
41
import Ganeti.Common
42
import Ganeti.DataCollectors.CLI
43
import Ganeti.DataCollectors.InstStatusTypes
44
import Ganeti.Hypervisor.Xen
45
import Ganeti.Objects
46
import Ganeti.Utils
47

  
48
-- * Command line options
49

  
50
options :: IO [OptType]
51
options = return
52
  [ oConfdAddr
53
  , oConfdPort
54
  ]
55

  
56
-- | The list of arguments supported by the program.
57
arguments :: [ArgCompletion]
58
arguments = []
59

  
60
-- | Get the list of instances ([primary], [secondary]) on the given node.
61
-- Implemented as a function, even if used a single time, to specify in a
62
-- convenient and elegant way the return data type, required in order to
63
-- prevent incurring in the monomorphism restriction.
64
-- The server address and the server port parameters are mainly intended
65
-- for testing purposes. If they are Nothing, the default values will be used.
66
getInstances
67
  :: String
68
  -> Maybe String
69
  -> Maybe Int
70
  -> IO (BT.Result ([Ganeti.Objects.Instance], [Ganeti.Objects.Instance]))
71
getInstances node srvAddr srvPort = do
72
  client <- getConfdClient srvAddr srvPort
73
  reply <- query client ReqNodeInstances $ PlainQuery node
74
  return $
75
    case fmap (J.readJSON . confdReplyAnswer) reply of
76
      Just (J.Ok instances) -> BT.Ok instances
77
      Just (J.Error msg) -> BT.Bad msg
78
      Nothing -> BT.Bad "No answer from the Confd server"
79

  
80
-- Builds the status of an instance using runtime information about the Xen
81
-- Domains, their uptime information and the static information provided by
82
-- the ConfD server.
83
buildStatus :: Map.Map String Domain -> Map.Map Int UptimeInfo -> Instance
84
  -> IO InstStatus
85
buildStatus domains uptimes inst = do
86
  let name = instName inst
87
      currDomain = Map.lookup name domains
88
      idNum = fmap domId currDomain
89
      currUInfo = idNum >>= (`Map.lookup` uptimes)
90
      uptime = fmap uInfoUptime currUInfo
91
      adminState = instAdminState inst
92
  return $
93
    InstStatus
94
      name
95
      (instUuid inst)
96
      adminState
97
      uptime
98
      (instMtime inst)
99

  
100
-- | Main function.
101
main :: Options -> [String] -> IO ()
102
main opts _ = do
103
  curNode <- getHostName
104
  let node = fromMaybe curNode $ optNode opts
105
  answer <- getInstances node (optConfdAddr opts) (optConfdPort opts)
106
  inst <- exitIfBad "Can't get instance info from ConfD" answer
107
  domains <- getInferredDomInfo
108
  uptimes <- getUptimeInfo
109
  let primaryInst =  fst inst
110
  iStatus <- mapM (buildStatus domains uptimes) primaryInst
111
  putStrLn $ J.encode iStatus
b/src/Ganeti/DataCollectors/InstStatusTypes.hs
1
{-# LANGUAGE TemplateHaskell #-}
2
{-| Type declarations specific for the instance status data collector.
3

  
4
-}
5

  
6
{-
7

  
8
Copyright (C) 2013 Google Inc.
9

  
10
This program is free software; you can redistribute it and/or modify
11
it under the terms of the GNU General Public License as published by
12
the Free Software Foundation; either version 2 of the License, or
13
(at your option) any later version.
14

  
15
This program is distributed in the hope that it will be useful, but
16
WITHOUT ANY WARRANTY; without even the implied warranty of
17
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18
General Public License for more details.
19

  
20
You should have received a copy of the GNU General Public License
21
along with this program; if not, write to the Free Software
22
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
23
02110-1301, USA.
24

  
25
-}
26

  
27
module Ganeti.DataCollectors.InstStatusTypes
28
  ( InstStatus(..)
29
  ) where
30

  
31

  
32
import Ganeti.Objects
33
import Ganeti.THH
34

  
35
-- | Data type representing the status of an instance to be returned.
36
$(buildObject "InstStatus" "iStat"
37
  [ simpleField "name"         [t| String |]
38
  , simpleField "uuid"         [t| String |]
39
  , simpleField "adminState"   [t| AdminState |]
40
  , optionalNullSerField $
41
    simpleField "uptime"       [t| String |]
42
  , simpleField "mtime"        [t| Double |]
43
  ])
b/src/Ganeti/DataCollectors/Program.hs
29 29
import Ganeti.DataCollectors.CLI (Options)
30 30

  
31 31
import qualified Ganeti.DataCollectors.Drbd as Drbd
32
import qualified Ganeti.DataCollectors.InstStatus as InstStatus
32 33

  
33 34
-- | Supported binaries.
34 35
personalities :: PersonalityList Options
35 36
personalities = [ ("drbd",   (Drbd.main, Drbd.options, Drbd.arguments,
36
                             "gathers and displays DRBD statistics in JSON\
37
                             \ format"))
37
                              "gathers and displays DRBD statistics in JSON\
38
                              \ format"))
39
                , ("inst-status-xen", (InstStatus.main, InstStatus.options,
40
                                       InstStatus.arguments,
41
                                       "gathers and displays the status of the\
42
                                       \ instances in JSON format"))
38 43
                ]

Also available in: Unified diff