Statistics
| Branch: | Tag: | Revision:

root / src / Ganeti / Query / Node.hs @ c81b97f2

History | View | Annotate | Download (9.6 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 Ganeti.Utils (niceSort)
46

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

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

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

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

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

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

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

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

    
212
-- | The node fields map.
213
fieldsMap :: FieldMap Node Runtime
214
fieldsMap =
215
  Map.fromList $ map (\v@(f, _, _) -> (fdefName f, v)) nodeFields
216

    
217
-- | Collect live data from RPC query if enabled.
218
--
219
-- FIXME: Check which fields we actually need and possibly send empty
220
-- hvs\/vgs if no info from hypervisor\/volume group respectively is
221
-- required
222
collectLiveData:: Bool -> ConfigData -> [Node] -> IO [(Node, Runtime)]
223
collectLiveData False _ nodes =
224
  return $ zip nodes (repeat $ Left (RpcResultError "Live data disabled"))
225
collectLiveData True cfg nodes = do
226
  let vgs = maybeToList . clusterVolumeGroupName $ configCluster cfg
227
      hvs = [getDefaultHypervisor cfg]
228
      step n (bn, gn, em) =
229
        let ndp' = getNodeNdParams cfg n
230
        in case ndp' of
231
             Just ndp -> (bn, n : gn,
232
                          (nodeName n, ndpExclusiveStorage ndp) : em)
233
             Nothing -> (n : bn, gn, em)
234
      (bnodes, gnodes, emap) = foldr step ([], [], []) nodes
235
  rpcres <- executeRpcCall gnodes (RpcCallNodeInfo vgs hvs (Map.fromList emap))
236
  -- FIXME: The order of nodes in the result could be different from the input
237
  return $ zip bnodes (repeat $ Left (RpcResultError "Broken configuration"))
238
           ++ rpcres