root / src / Ganeti / Query / Node.hs @ b23226d5
History | View | Annotate | Download (10 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.Logging |
40 |
import Ganeti.Objects |
41 |
import Ganeti.JSON |
42 |
import Ganeti.Rpc |
43 |
import Ganeti.Query.Language |
44 |
import Ganeti.Query.Common |
45 |
import Ganeti.Query.Types |
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 |
, ("mfree", "MFree", QFTUnit, "memory_free", |
68 |
"Memory available for instance allocations") |
69 |
, ("mnode", "MNode", QFTUnit, "memory_dom0", |
70 |
"Amount of memory used by node (dom0 for Xen)") |
71 |
, ("mtotal", "MTotal", QFTUnit, "memory_total", |
72 |
"Total amount of memory of physical machine") |
73 |
] |
74 |
|
75 |
-- | Map each name to a function that extracts that value from |
76 |
-- the RPC result. |
77 |
nodeLiveFieldExtract :: FieldName -> RpcResultNodeInfo -> J.JSValue |
78 |
nodeLiveFieldExtract "bootid" res = |
79 |
J.showJSON $ rpcResNodeInfoBootId res |
80 |
nodeLiveFieldExtract "cnodes" res = |
81 |
jsonHead (rpcResNodeInfoHvInfo res) hvInfoCpuNodes |
82 |
nodeLiveFieldExtract "csockets" res = |
83 |
jsonHead (rpcResNodeInfoHvInfo res) hvInfoCpuSockets |
84 |
nodeLiveFieldExtract "ctotal" res = |
85 |
jsonHead (rpcResNodeInfoHvInfo res) hvInfoCpuTotal |
86 |
nodeLiveFieldExtract "dfree" res = |
87 |
getMaybeJsonHead (rpcResNodeInfoVgInfo res) vgInfoVgFree |
88 |
nodeLiveFieldExtract "dtotal" res = |
89 |
getMaybeJsonHead (rpcResNodeInfoVgInfo res) vgInfoVgSize |
90 |
nodeLiveFieldExtract "mfree" res = |
91 |
jsonHead (rpcResNodeInfoHvInfo res) hvInfoMemoryFree |
92 |
nodeLiveFieldExtract "mnode" res = |
93 |
jsonHead (rpcResNodeInfoHvInfo res) hvInfoMemoryDom0 |
94 |
nodeLiveFieldExtract "mtotal" res = |
95 |
jsonHead (rpcResNodeInfoHvInfo res) hvInfoMemoryTotal |
96 |
nodeLiveFieldExtract _ _ = J.JSNull |
97 |
|
98 |
-- | Helper for extracting field from RPC result. |
99 |
nodeLiveRpcCall :: FieldName -> Runtime -> Node -> ResultEntry |
100 |
nodeLiveRpcCall fname (Right res) _ = |
101 |
case nodeLiveFieldExtract fname res of |
102 |
J.JSNull -> rsNoData |
103 |
x -> rsNormal x |
104 |
nodeLiveRpcCall _ (Left err) _ = |
105 |
ResultEntry (rpcErrorToStatus err) Nothing |
106 |
|
107 |
-- | Builder for node live fields. |
108 |
nodeLiveFieldBuilder :: (FieldName, FieldTitle, FieldType, String, FieldDoc) |
109 |
-> FieldData Node Runtime |
110 |
nodeLiveFieldBuilder (fname, ftitle, ftype, _, fdoc) = |
111 |
( FieldDefinition fname ftitle ftype fdoc |
112 |
, FieldRuntime $ nodeLiveRpcCall fname |
113 |
, QffNormal) |
114 |
|
115 |
-- | The docstring for the node role. Note that we use 'reverse' in |
116 |
-- order to keep the same order as Python. |
117 |
nodeRoleDoc :: String |
118 |
nodeRoleDoc = |
119 |
"Node role; " ++ |
120 |
intercalate ", " |
121 |
(map (\role -> |
122 |
"\"" ++ nodeRoleToRaw role ++ "\" for " ++ roleDescription role) |
123 |
(reverse [minBound..maxBound])) |
124 |
|
125 |
-- | Get node powered status. |
126 |
getNodePower :: ConfigData -> Node -> ResultEntry |
127 |
getNodePower cfg node = |
128 |
case getNodeNdParams cfg node of |
129 |
Nothing -> rsNoData |
130 |
Just ndp -> if null (ndpOobProgram ndp) |
131 |
then rsUnavail |
132 |
else rsNormal (nodePowered node) |
133 |
|
134 |
-- | List of all node fields. |
135 |
nodeFields :: FieldList Node Runtime |
136 |
nodeFields = |
137 |
[ (FieldDefinition "drained" "Drained" QFTBool "Whether node is drained", |
138 |
FieldSimple (rsNormal . nodeDrained), QffNormal) |
139 |
, (FieldDefinition "master_candidate" "MasterC" QFTBool |
140 |
"Whether node is a master candidate", |
141 |
FieldSimple (rsNormal . nodeMasterCandidate), QffNormal) |
142 |
, (FieldDefinition "master_capable" "MasterCapable" QFTBool |
143 |
"Whether node can become a master candidate", |
144 |
FieldSimple (rsNormal . nodeMasterCapable), QffNormal) |
145 |
, (FieldDefinition "name" "Node" QFTText "Node name", |
146 |
FieldSimple (rsNormal . nodeName), QffHostname) |
147 |
, (FieldDefinition "offline" "Offline" QFTBool |
148 |
"Whether node is marked offline", |
149 |
FieldSimple (rsNormal . nodeOffline), QffNormal) |
150 |
, (FieldDefinition "vm_capable" "VMCapable" QFTBool |
151 |
"Whether node can host instances", |
152 |
FieldSimple (rsNormal . nodeVmCapable), QffNormal) |
153 |
, (FieldDefinition "pip" "PrimaryIP" QFTText "Primary IP address", |
154 |
FieldSimple (rsNormal . nodePrimaryIp), QffNormal) |
155 |
, (FieldDefinition "sip" "SecondaryIP" QFTText "Secondary IP address", |
156 |
FieldSimple (rsNormal . nodeSecondaryIp), QffNormal) |
157 |
, (FieldDefinition "master" "IsMaster" QFTBool "Whether node is master", |
158 |
FieldConfig (\cfg node -> |
159 |
rsNormal (nodeName node == |
160 |
clusterMasterNode (configCluster cfg))), |
161 |
QffNormal) |
162 |
, (FieldDefinition "group" "Group" QFTText "Node group", |
163 |
FieldConfig (\cfg node -> |
164 |
rsMaybeNoData (groupName <$> getGroupOfNode cfg node)), |
165 |
QffNormal) |
166 |
, (FieldDefinition "group.uuid" "GroupUUID" QFTText "UUID of node group", |
167 |
FieldSimple (rsNormal . nodeGroup), QffNormal) |
168 |
, (FieldDefinition "ndparams" "NodeParameters" QFTOther |
169 |
"Merged node parameters", |
170 |
FieldConfig ((rsMaybeNoData .) . getNodeNdParams), QffNormal) |
171 |
, (FieldDefinition "custom_ndparams" "CustomNodeParameters" QFTOther |
172 |
"Custom node parameters", |
173 |
FieldSimple (rsNormal . nodeNdparams), QffNormal) |
174 |
-- FIXME: the below could be generalised a bit, like in Python |
175 |
, (FieldDefinition "pinst_cnt" "Pinst" QFTNumber |
176 |
"Number of instances with this node as primary", |
177 |
FieldConfig (\cfg -> |
178 |
rsNormal . length . fst . getNodeInstances cfg . nodeName), |
179 |
QffNormal) |
180 |
, (FieldDefinition "sinst_cnt" "Sinst" QFTNumber |
181 |
"Number of instances with this node as secondary", |
182 |
FieldConfig (\cfg -> |
183 |
rsNormal . length . snd . getNodeInstances cfg . nodeName), |
184 |
QffNormal) |
185 |
, (FieldDefinition "pinst_list" "PriInstances" QFTOther |
186 |
"List of instances with this node as primary", |
187 |
FieldConfig (\cfg -> rsNormal . niceSort . map instName . fst . |
188 |
getNodeInstances cfg . nodeName), QffNormal) |
189 |
, (FieldDefinition "sinst_list" "SecInstances" QFTOther |
190 |
"List of instances with this node as secondary", |
191 |
FieldConfig (\cfg -> rsNormal . niceSort . map instName . snd . |
192 |
getNodeInstances cfg . nodeName), QffNormal) |
193 |
, (FieldDefinition "role" "Role" QFTText nodeRoleDoc, |
194 |
FieldConfig ((rsNormal .) . getNodeRole), QffNormal) |
195 |
, (FieldDefinition "powered" "Powered" QFTBool |
196 |
"Whether node is thought to be powered on", |
197 |
FieldConfig getNodePower, QffNormal) |
198 |
-- FIXME: the two fields below are incomplete in Python, part of the |
199 |
-- non-implemented node resource model; they are declared just for |
200 |
-- parity, but are not functional |
201 |
, (FieldDefinition "hv_state" "HypervisorState" QFTOther "Hypervisor state", |
202 |
FieldSimple (const rsUnavail), QffNormal) |
203 |
, (FieldDefinition "disk_state" "DiskState" QFTOther "Disk state", |
204 |
FieldSimple (const rsUnavail), QffNormal) |
205 |
] ++ |
206 |
map nodeLiveFieldBuilder nodeLiveFieldsDefs ++ |
207 |
map buildNdParamField allNDParamFields ++ |
208 |
timeStampFields ++ |
209 |
uuidFields "Node" ++ |
210 |
serialFields "Node" ++ |
211 |
tagsFields |
212 |
|
213 |
-- | The node fields map. |
214 |
fieldsMap :: FieldMap Node Runtime |
215 |
fieldsMap = |
216 |
Map.fromList $ map (\v@(f, _, _) -> (fdefName f, v)) nodeFields |
217 |
|
218 |
-- | Scan the list of results produced by executeRpcCall and log all the RPC |
219 |
-- errors. |
220 |
logRpcErrors :: [(Node, Runtime)] -> IO () |
221 |
logRpcErrors allElems = |
222 |
let logOneRpcErr (_, Right _) = return () |
223 |
logOneRpcErr (_, Left err) = |
224 |
logError $ "Error in the RPC HTTP reply: " ++ show err |
225 |
in mapM_ logOneRpcErr allElems |
226 |
|
227 |
-- | Collect live data from RPC query if enabled. |
228 |
-- |
229 |
-- FIXME: Check which fields we actually need and possibly send empty |
230 |
-- hvs\/vgs if no info from hypervisor\/volume group respectively is |
231 |
-- required |
232 |
collectLiveData:: Bool -> ConfigData -> [Node] -> IO [(Node, Runtime)] |
233 |
collectLiveData False _ nodes = |
234 |
return $ zip nodes (repeat $ Left (RpcResultError "Live data disabled")) |
235 |
collectLiveData True cfg nodes = do |
236 |
let vgs = maybeToList . clusterVolumeGroupName $ configCluster cfg |
237 |
hvs = [getDefaultHypervisor cfg] |
238 |
step n (bn, gn, em) = |
239 |
let ndp' = getNodeNdParams cfg n |
240 |
in case ndp' of |
241 |
Just ndp -> (bn, n : gn, |
242 |
(nodeName n, ndpExclusiveStorage ndp) : em) |
243 |
Nothing -> (n : bn, gn, em) |
244 |
(bnodes, gnodes, emap) = foldr step ([], [], []) nodes |
245 |
rpcres <- executeRpcCall gnodes (RpcCallNodeInfo vgs hvs (Map.fromList emap)) |
246 |
logRpcErrors rpcres |
247 |
-- FIXME: The order of nodes in the result could be different from the input |
248 |
return $ zip bnodes (repeat $ Left (RpcResultError "Broken configuration")) |
249 |
++ rpcres |