Statistics
| Branch: | Tag: | Revision:

root / htools / Ganeti / Query / Node.hs @ f94a9680

History | View | Annotate | Download (8.5 kB)

1
{-| Implementation of the Ganeti Query2 node queries.
2

    
3
 -}
4

    
5
{-
6

    
7
Copyright (C) 2012 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.Query.Node
27
  ( NodeRuntime
28
  , nodeFieldsMap
29
  ) where
30

    
31
import Control.Applicative
32
import Data.List
33
import qualified Data.Map as Map
34
import qualified Text.JSON as J
35

    
36
import Ganeti.Config
37
import Ganeti.Objects
38
import Ganeti.JSON
39
import Ganeti.Rpc
40
import Ganeti.Query.Language
41
import Ganeti.Query.Common
42
import Ganeti.Query.Types
43

    
44
-- | NodeRuntime is the resulting type for NodeInfo call.
45
type NodeRuntime = Either RpcError RpcResultNodeInfo
46

    
47
-- | List of node live fields.
48
nodeLiveFieldsDefs :: [(FieldName, FieldTitle, FieldType, String, FieldDoc)]
49
nodeLiveFieldsDefs =
50
  [ ("bootid", "BootID", QFTText, "bootid",
51
     "Random UUID renewed for each system reboot, can be used\
52
     \ for detecting reboots by tracking changes")
53
  , ("cnodes", "CNodes", QFTNumber, "cpu_nodes",
54
     "Number of NUMA domains on node (if exported by hypervisor)")
55
  , ("csockets", "CSockets", QFTNumber, "cpu_sockets",
56
     "Number of physical CPU sockets (if exported by hypervisor)")
57
  , ("ctotal", "CTotal", QFTNumber, "cpu_total",
58
     "Number of logical processors")
59
  , ("dfree", "DFree", QFTUnit, "vg_free",
60
     "Available disk space in volume group")
61
  , ("dtotal", "DTotal", QFTUnit, "vg_size",
62
     "Total disk space in volume group used for instance disk allocation")
63
  , ("mfree", "MFree", QFTUnit, "memory_free",
64
     "Memory available for instance allocations")
65
  , ("mnode", "MNode", QFTUnit, "memory_dom0",
66
     "Amount of memory used by node (dom0 for Xen)")
67
  , ("mtotal", "MTotal", QFTUnit, "memory_total",
68
     "Total amount of memory of physical machine")
69
  ]
70

    
71
-- | Map each name to a function that extracts that value from
72
-- the RPC result.
73
nodeLiveFieldExtract :: FieldName -> RpcResultNodeInfo -> J.JSValue
74
nodeLiveFieldExtract "bootid" res =
75
  J.showJSON $ rpcResNodeInfoBootId res
76
nodeLiveFieldExtract "cnodes" res =
77
  jsonHead (rpcResNodeInfoHvInfo res) hvInfoCpuNodes
78
nodeLiveFieldExtract "csockets" res =
79
  jsonHead (rpcResNodeInfoHvInfo res) hvInfoCpuSockets
80
nodeLiveFieldExtract "ctotal" res =
81
  jsonHead (rpcResNodeInfoHvInfo res) hvInfoCpuTotal
82
nodeLiveFieldExtract "dfree" res =
83
  getMaybeJsonHead (rpcResNodeInfoVgInfo res) vgInfoVgFree
84
nodeLiveFieldExtract "dtotal" res =
85
  getMaybeJsonHead (rpcResNodeInfoVgInfo res) vgInfoVgSize
86
nodeLiveFieldExtract "mfree" res =
87
  jsonHead (rpcResNodeInfoHvInfo res) hvInfoMemoryFree
88
nodeLiveFieldExtract "mnode" res =
89
  jsonHead (rpcResNodeInfoHvInfo res) hvInfoMemoryDom0
90
nodeLiveFieldExtract "mtotal" res =
91
  jsonHead (rpcResNodeInfoHvInfo res) hvInfoMemoryTotal
92
nodeLiveFieldExtract _ _ = J.JSNull
93

    
94
-- | Helper for extracting field from RPC result.
95
nodeLiveRpcCall :: FieldName -> NodeRuntime -> Node -> ResultEntry
96
nodeLiveRpcCall fname (Right res) _ =
97
  case nodeLiveFieldExtract fname res of
98
    J.JSNull -> rsNoData
99
    x -> rsNormal x
100
nodeLiveRpcCall _ (Left err) _ =
101
    ResultEntry (rpcErrorToStatus err) Nothing
102

    
103
-- | Builder for node live fields.
104
nodeLiveFieldBuilder :: (FieldName, FieldTitle, FieldType, String, FieldDoc)
105
                     -> FieldData Node NodeRuntime
106
nodeLiveFieldBuilder (fname, ftitle, ftype, _, fdoc) =
107
  ( FieldDefinition fname ftitle ftype fdoc
108
  , FieldRuntime $ nodeLiveRpcCall fname
109
  , QffNormal)
110

    
111
-- | The docstring for the node role. Note that we use 'reverse in
112
-- order to keep the same order as Python.
113
nodeRoleDoc :: String
114
nodeRoleDoc =
115
  "Node role; " ++
116
  intercalate ", "
117
   (map (\role ->
118
          "\"" ++ nodeRoleToRaw role ++ "\" for " ++ roleDescription role)
119
   (reverse [minBound..maxBound]))
120

    
121
-- | Get node powered status.
122
getNodePower :: ConfigData -> Node -> ResultEntry
123
getNodePower cfg node =
124
  case getNodeNdParams cfg node of
125
    Nothing -> rsNoData
126
    Just ndp -> if null (ndpOobProgram ndp)
127
                  then rsUnavail
128
                  else rsNormal (nodePowered node)
129

    
130
-- | List of all node fields.
131
nodeFields :: FieldList Node NodeRuntime
132
nodeFields =
133
  [ (FieldDefinition "drained" "Drained" QFTBool "Whether node is drained",
134
     FieldSimple (rsNormal . nodeDrained), QffNormal)
135
  , (FieldDefinition "master_candidate" "MasterC" QFTBool
136
       "Whether node is a master candidate",
137
     FieldSimple (rsNormal . nodeMasterCandidate), QffNormal)
138
  , (FieldDefinition "master_capable" "MasterCapable" QFTBool
139
       "Whether node can become a master candidate",
140
     FieldSimple (rsNormal . nodeMasterCapable), QffNormal)
141
  , (FieldDefinition "name" "Node" QFTText "Node name",
142
     FieldSimple (rsNormal . nodeName), QffNormal)
143
  , (FieldDefinition "offline" "Offline" QFTBool
144
       "Whether node is marked offline",
145
     FieldSimple (rsNormal . nodeOffline), QffNormal)
146
  , (FieldDefinition "vm_capable" "VMCapable" QFTBool
147
       "Whether node can host instances",
148
     FieldSimple (rsNormal . nodeVmCapable), QffNormal)
149
  , (FieldDefinition "pip" "PrimaryIP" QFTText "Primary IP address",
150
     FieldSimple (rsNormal . nodePrimaryIp), QffNormal)
151
  , (FieldDefinition "sip" "SecondaryIP" QFTText "Secondary IP address",
152
     FieldSimple (rsNormal . nodeSecondaryIp), QffNormal)
153
  , (FieldDefinition "master" "IsMaster" QFTBool "Whether node is master",
154
     FieldConfig (\cfg node ->
155
                    rsNormal (nodeName node ==
156
                              clusterMasterNode (configCluster cfg))),
157
     QffNormal)
158
  , (FieldDefinition "group" "Group" QFTText "Node group",
159
     FieldConfig (\cfg node ->
160
                    rsMaybe (groupName <$> getGroupOfNode cfg node)),
161
     QffNormal)
162
  , (FieldDefinition "group.uuid" "GroupUUID" QFTText "UUID of node group",
163
     FieldSimple (rsNormal . nodeGroup), QffNormal)
164
  ,  (FieldDefinition "ndparams" "NodeParameters" QFTOther
165
        "Merged node parameters",
166
      FieldConfig ((rsMaybe .) . getNodeNdParams), QffNormal)
167
  , (FieldDefinition "custom_ndparams" "CustomNodeParameters" QFTOther
168
                       "Custom node parameters",
169
     FieldSimple (rsNormal . nodeNdparams), QffNormal)
170
  -- FIXME: the below could be generalised a bit, like in Python
171
  , (FieldDefinition "pinst_cnt" "Pinst" QFTNumber
172
       "Number of instances with this node as primary",
173
     FieldConfig (\cfg ->
174
                    rsNormal . length . fst . getNodeInstances cfg . nodeName),
175
     QffNormal)
176
  , (FieldDefinition "sinst_cnt" "Sinst" QFTNumber
177
       "Number of instances with this node as secondary",
178
     FieldConfig (\cfg ->
179
                    rsNormal . length . snd . getNodeInstances cfg . nodeName),
180
     QffNormal)
181
  , (FieldDefinition "pinst_list" "PriInstances" QFTOther
182
       "List of instances with this node as primary",
183
     FieldConfig (\cfg -> rsNormal . map instName . fst .
184
                          getNodeInstances cfg . nodeName), QffNormal)
185
  , (FieldDefinition "sinst_list" "SecInstances" QFTOther
186
       "List of instances with this node as secondary",
187
     FieldConfig (\cfg -> rsNormal . map instName . snd .
188
                          getNodeInstances cfg . nodeName), QffNormal)
189
  , (FieldDefinition "role" "Role" QFTText nodeRoleDoc,
190
     FieldConfig ((rsNormal .) . getNodeRole), QffNormal)
191
  , (FieldDefinition "powered" "Powered" QFTBool
192
       "Whether node is thought to be powered on",
193
     FieldConfig getNodePower, QffNormal)
194
  -- FIXME: the two fields below are incomplete in Python, part of the
195
  -- non-implemented node resource model; they are declared just for
196
  -- parity, but are not functional
197
  , (FieldDefinition "hv_state" "HypervisorState" QFTOther "Hypervisor state",
198
     missingRuntime, QffNormal)
199
  , (FieldDefinition "disk_state" "DiskState" QFTOther "Disk state",
200
     missingRuntime, QffNormal)
201
  ] ++
202
  map nodeLiveFieldBuilder nodeLiveFieldsDefs ++
203
  map buildNdParamField allNDParamFields ++
204
  timeStampFields ++
205
  uuidFields "Node" ++
206
  serialFields "Node" ++
207
  tagsFields
208

    
209
-- | The node fields map.
210
nodeFieldsMap :: FieldMap Node NodeRuntime
211
nodeFieldsMap =
212
  Map.fromList $ map (\v@(f, _, _) -> (fdefName f, v)) nodeFields