Statistics
| Branch: | Tag: | Revision:

root / src / Ganeti / Query / Node.hs @ 06fb92cf

History | View | Annotate | Download (10.3 kB)

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

    
3
 -}
4

    
5
{-
6

    
7
Copyright (C) 2012, 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.Query.Node
27
  ( Runtime
28
  , fieldsMap
29
  , collectLiveData
30
  ) where
31

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

    
38
import Ganeti.Config
39
import Ganeti.Objects
40
import Ganeti.JSON
41
import Ganeti.Rpc
42
import Ganeti.Query.Language
43
import Ganeti.Query.Common
44
import Ganeti.Query.Types
45
import qualified Ganeti.Types as T
46
import Ganeti.Utils (niceSort)
47

    
48
-- | Runtime is the resulting type for NodeInfo call.
49
type Runtime = Either RpcError RpcResultNodeInfo
50

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

    
79
-- | Map each name to a function that extracts that value from
80
-- the RPC result.
81
nodeLiveFieldExtract :: FieldName -> RpcResultNodeInfo -> J.JSValue
82
nodeLiveFieldExtract "bootid" res =
83
  J.showJSON $ rpcResNodeInfoBootId res
84
nodeLiveFieldExtract "cnodes" res =
85
  jsonHead (rpcResNodeInfoHvInfo res) hvInfoCpuNodes
86
nodeLiveFieldExtract "csockets" res =
87
  jsonHead (rpcResNodeInfoHvInfo res) hvInfoCpuSockets
88
nodeLiveFieldExtract "ctotal" res =
89
  jsonHead (rpcResNodeInfoHvInfo res) hvInfoCpuTotal
90
nodeLiveFieldExtract "dfree" res =
91
  getMaybeJsonHead (rpcResNodeInfoVgInfo res) vgInfoVgFree
92
nodeLiveFieldExtract "dtotal" res =
93
  getMaybeJsonHead (rpcResNodeInfoVgInfo res) vgInfoVgSize
94
nodeLiveFieldExtract "spfree" res =
95
  getMaybeJsonElem (rpcResNodeInfoVgInfo res) 1 vgInfoVgFree
96
nodeLiveFieldExtract "sptotal" res =
97
  getMaybeJsonElem (rpcResNodeInfoVgInfo res) 1 vgInfoVgSize
98
nodeLiveFieldExtract "mfree" res =
99
  jsonHead (rpcResNodeInfoHvInfo res) hvInfoMemoryFree
100
nodeLiveFieldExtract "mnode" res =
101
  jsonHead (rpcResNodeInfoHvInfo res) hvInfoMemoryDom0
102
nodeLiveFieldExtract "mtotal" res =
103
  jsonHead (rpcResNodeInfoHvInfo res) hvInfoMemoryTotal
104
nodeLiveFieldExtract _ _ = J.JSNull
105

    
106
-- | Helper for extracting field from RPC result.
107
nodeLiveRpcCall :: FieldName -> Runtime -> Node -> ResultEntry
108
nodeLiveRpcCall fname (Right res) _ =
109
  case nodeLiveFieldExtract fname res of
110
    J.JSNull -> rsNoData
111
    x -> rsNormal x
112
nodeLiveRpcCall _ (Left err) _ =
113
    ResultEntry (rpcErrorToStatus err) Nothing
114

    
115
-- | Builder for node live fields.
116
nodeLiveFieldBuilder :: (FieldName, FieldTitle, FieldType, String, FieldDoc)
117
                     -> FieldData Node Runtime
118
nodeLiveFieldBuilder (fname, ftitle, ftype, _, fdoc) =
119
  ( FieldDefinition fname ftitle ftype fdoc
120
  , FieldRuntime $ nodeLiveRpcCall fname
121
  , QffNormal)
122

    
123
-- | The docstring for the node role. Note that we use 'reverse' in
124
-- order to keep the same order as Python.
125
nodeRoleDoc :: String
126
nodeRoleDoc =
127
  "Node role; " ++
128
  intercalate ", "
129
   (map (\role ->
130
          "\"" ++ nodeRoleToRaw role ++ "\" for " ++ roleDescription role)
131
   (reverse [minBound..maxBound]))
132

    
133
-- | Get node powered status.
134
getNodePower :: ConfigData -> Node -> ResultEntry
135
getNodePower cfg node =
136
  case getNodeNdParams cfg node of
137
    Nothing -> rsNoData
138
    Just ndp -> if null (ndpOobProgram ndp)
139
                  then rsUnavail
140
                  else rsNormal (nodePowered node)
141

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

    
221
-- | The node fields map.
222
fieldsMap :: FieldMap Node Runtime
223
fieldsMap =
224
  Map.fromList $ map (\v@(f, _, _) -> (fdefName f, v)) nodeFields
225

    
226
-- | Collect live data from RPC query if enabled.
227
--
228
-- FIXME: Check which fields we actually need and possibly send empty
229
-- hvs\/vgs if no info from hypervisor\/volume group respectively is
230
-- required
231
collectLiveData:: Bool -> ConfigData -> [Node] -> IO [(Node, Runtime)]
232
collectLiveData False _ nodes =
233
  return $ zip nodes (repeat $ Left (RpcResultError "Live data disabled"))
234
collectLiveData True cfg nodes = do
235
  let vgs = maybeToList . clusterVolumeGroupName $ configCluster cfg
236
      -- FIXME: This currently sets every storage unit to LVM
237
      storage_units = zip (repeat T.StorageLvmVg) vgs ++
238
                      zip (repeat T.StorageLvmPv) vgs
239
      hvs = [getDefaultHypervisor cfg]
240
      step n (bn, gn, em) =
241
        let ndp' = getNodeNdParams cfg n
242
        in case ndp' of
243
             Just ndp -> (bn, n : gn,
244
                          (nodeName n, ndpExclusiveStorage ndp) : em)
245
             Nothing -> (n : bn, gn, em)
246
      (bnodes, gnodes, emap) = foldr step ([], [], []) nodes
247
  rpcres <- executeRpcCall gnodes (RpcCallNodeInfo storage_units hvs
248
    (Map.fromList emap))
249
  -- FIXME: The order of nodes in the result could be different from the input
250
  return $ zip bnodes (repeat $ Left (RpcResultError "Broken configuration"))
251
           ++ rpcres