Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (9.6 kB)

1 046fe3f5 Iustin Pop
{-| Implementation of the Ganeti Query2 node queries.
2 046fe3f5 Iustin Pop
3 046fe3f5 Iustin Pop
 -}
4 046fe3f5 Iustin Pop
5 046fe3f5 Iustin Pop
{-
6 046fe3f5 Iustin Pop
7 36162faf Iustin Pop
Copyright (C) 2012, 2013 Google Inc.
8 046fe3f5 Iustin Pop
9 046fe3f5 Iustin Pop
This program is free software; you can redistribute it and/or modify
10 046fe3f5 Iustin Pop
it under the terms of the GNU General Public License as published by
11 046fe3f5 Iustin Pop
the Free Software Foundation; either version 2 of the License, or
12 046fe3f5 Iustin Pop
(at your option) any later version.
13 046fe3f5 Iustin Pop
14 046fe3f5 Iustin Pop
This program is distributed in the hope that it will be useful, but
15 046fe3f5 Iustin Pop
WITHOUT ANY WARRANTY; without even the implied warranty of
16 046fe3f5 Iustin Pop
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17 046fe3f5 Iustin Pop
General Public License for more details.
18 046fe3f5 Iustin Pop
19 046fe3f5 Iustin Pop
You should have received a copy of the GNU General Public License
20 046fe3f5 Iustin Pop
along with this program; if not, write to the Free Software
21 046fe3f5 Iustin Pop
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
22 046fe3f5 Iustin Pop
02110-1301, USA.
23 046fe3f5 Iustin Pop
24 046fe3f5 Iustin Pop
-}
25 046fe3f5 Iustin Pop
26 046fe3f5 Iustin Pop
module Ganeti.Query.Node
27 36162faf Iustin Pop
  ( Runtime
28 36162faf Iustin Pop
  , fieldsMap
29 36162faf Iustin Pop
  , collectLiveData
30 046fe3f5 Iustin Pop
  ) where
31 046fe3f5 Iustin Pop
32 046fe3f5 Iustin Pop
import Control.Applicative
33 046fe3f5 Iustin Pop
import Data.List
34 64b0309a Dimitris Aragiorgis
import Data.Maybe
35 046fe3f5 Iustin Pop
import qualified Data.Map as Map
36 cca4e6fe Agata Murawska
import qualified Text.JSON as J
37 046fe3f5 Iustin Pop
38 046fe3f5 Iustin Pop
import Ganeti.Config
39 046fe3f5 Iustin Pop
import Ganeti.Objects
40 cca4e6fe Agata Murawska
import Ganeti.JSON
41 7f0fd838 Agata Murawska
import Ganeti.Rpc
42 4cab6703 Iustin Pop
import Ganeti.Query.Language
43 046fe3f5 Iustin Pop
import Ganeti.Query.Common
44 046fe3f5 Iustin Pop
import Ganeti.Query.Types
45 c81b97f2 Iustin Pop
import Ganeti.Utils (niceSort)
46 046fe3f5 Iustin Pop
47 36162faf Iustin Pop
-- | Runtime is the resulting type for NodeInfo call.
48 36162faf Iustin Pop
type Runtime = Either RpcError RpcResultNodeInfo
49 046fe3f5 Iustin Pop
50 cca4e6fe Agata Murawska
-- | List of node live fields.
51 046fe3f5 Iustin Pop
nodeLiveFieldsDefs :: [(FieldName, FieldTitle, FieldType, String, FieldDoc)]
52 046fe3f5 Iustin Pop
nodeLiveFieldsDefs =
53 046fe3f5 Iustin Pop
  [ ("bootid", "BootID", QFTText, "bootid",
54 046fe3f5 Iustin Pop
     "Random UUID renewed for each system reboot, can be used\
55 046fe3f5 Iustin Pop
     \ for detecting reboots by tracking changes")
56 046fe3f5 Iustin Pop
  , ("cnodes", "CNodes", QFTNumber, "cpu_nodes",
57 046fe3f5 Iustin Pop
     "Number of NUMA domains on node (if exported by hypervisor)")
58 046fe3f5 Iustin Pop
  , ("csockets", "CSockets", QFTNumber, "cpu_sockets",
59 046fe3f5 Iustin Pop
     "Number of physical CPU sockets (if exported by hypervisor)")
60 046fe3f5 Iustin Pop
  , ("ctotal", "CTotal", QFTNumber, "cpu_total",
61 046fe3f5 Iustin Pop
     "Number of logical processors")
62 046fe3f5 Iustin Pop
  , ("dfree", "DFree", QFTUnit, "vg_free",
63 046fe3f5 Iustin Pop
     "Available disk space in volume group")
64 046fe3f5 Iustin Pop
  , ("dtotal", "DTotal", QFTUnit, "vg_size",
65 046fe3f5 Iustin Pop
     "Total disk space in volume group used for instance disk allocation")
66 046fe3f5 Iustin Pop
  , ("mfree", "MFree", QFTUnit, "memory_free",
67 046fe3f5 Iustin Pop
     "Memory available for instance allocations")
68 046fe3f5 Iustin Pop
  , ("mnode", "MNode", QFTUnit, "memory_dom0",
69 046fe3f5 Iustin Pop
     "Amount of memory used by node (dom0 for Xen)")
70 046fe3f5 Iustin Pop
  , ("mtotal", "MTotal", QFTUnit, "memory_total",
71 046fe3f5 Iustin Pop
     "Total amount of memory of physical machine")
72 046fe3f5 Iustin Pop
  ]
73 046fe3f5 Iustin Pop
74 cca4e6fe Agata Murawska
-- | Map each name to a function that extracts that value from
75 cca4e6fe Agata Murawska
-- the RPC result.
76 edb3f937 Iustin Pop
nodeLiveFieldExtract :: FieldName -> RpcResultNodeInfo -> J.JSValue
77 cca4e6fe Agata Murawska
nodeLiveFieldExtract "bootid" res =
78 edb3f937 Iustin Pop
  J.showJSON $ rpcResNodeInfoBootId res
79 edb3f937 Iustin Pop
nodeLiveFieldExtract "cnodes" res =
80 edb3f937 Iustin Pop
  jsonHead (rpcResNodeInfoHvInfo res) hvInfoCpuNodes
81 edb3f937 Iustin Pop
nodeLiveFieldExtract "csockets" res =
82 edb3f937 Iustin Pop
  jsonHead (rpcResNodeInfoHvInfo res) hvInfoCpuSockets
83 edb3f937 Iustin Pop
nodeLiveFieldExtract "ctotal" res =
84 edb3f937 Iustin Pop
  jsonHead (rpcResNodeInfoHvInfo res) hvInfoCpuTotal
85 edb3f937 Iustin Pop
nodeLiveFieldExtract "dfree" res =
86 318853ab Iustin Pop
  getMaybeJsonHead (rpcResNodeInfoVgInfo res) vgInfoVgFree
87 edb3f937 Iustin Pop
nodeLiveFieldExtract "dtotal" res =
88 318853ab Iustin Pop
  getMaybeJsonHead (rpcResNodeInfoVgInfo res) vgInfoVgSize
89 edb3f937 Iustin Pop
nodeLiveFieldExtract "mfree" res =
90 edb3f937 Iustin Pop
  jsonHead (rpcResNodeInfoHvInfo res) hvInfoMemoryFree
91 edb3f937 Iustin Pop
nodeLiveFieldExtract "mnode" res =
92 edb3f937 Iustin Pop
  jsonHead (rpcResNodeInfoHvInfo res) hvInfoMemoryDom0
93 edb3f937 Iustin Pop
nodeLiveFieldExtract "mtotal" res =
94 edb3f937 Iustin Pop
  jsonHead (rpcResNodeInfoHvInfo res) hvInfoMemoryTotal
95 cca4e6fe Agata Murawska
nodeLiveFieldExtract _ _ = J.JSNull
96 cca4e6fe Agata Murawska
97 cca4e6fe Agata Murawska
-- | Helper for extracting field from RPC result.
98 36162faf Iustin Pop
nodeLiveRpcCall :: FieldName -> Runtime -> Node -> ResultEntry
99 cca4e6fe Agata Murawska
nodeLiveRpcCall fname (Right res) _ =
100 edb3f937 Iustin Pop
  case nodeLiveFieldExtract fname res of
101 edb3f937 Iustin Pop
    J.JSNull -> rsNoData
102 edb3f937 Iustin Pop
    x -> rsNormal x
103 cca4e6fe Agata Murawska
nodeLiveRpcCall _ (Left err) _ =
104 cca4e6fe Agata Murawska
    ResultEntry (rpcErrorToStatus err) Nothing
105 cca4e6fe Agata Murawska
106 046fe3f5 Iustin Pop
-- | Builder for node live fields.
107 046fe3f5 Iustin Pop
nodeLiveFieldBuilder :: (FieldName, FieldTitle, FieldType, String, FieldDoc)
108 36162faf Iustin Pop
                     -> FieldData Node Runtime
109 046fe3f5 Iustin Pop
nodeLiveFieldBuilder (fname, ftitle, ftype, _, fdoc) =
110 cca4e6fe Agata Murawska
  ( FieldDefinition fname ftitle ftype fdoc
111 f94a9680 Iustin Pop
  , FieldRuntime $ nodeLiveRpcCall fname
112 f94a9680 Iustin Pop
  , QffNormal)
113 046fe3f5 Iustin Pop
114 2412bdea Iustin Pop
-- | The docstring for the node role. Note that we use 'reverse' in
115 046fe3f5 Iustin Pop
-- order to keep the same order as Python.
116 046fe3f5 Iustin Pop
nodeRoleDoc :: String
117 046fe3f5 Iustin Pop
nodeRoleDoc =
118 046fe3f5 Iustin Pop
  "Node role; " ++
119 5b11f8db Iustin Pop
  intercalate ", "
120 5b11f8db Iustin Pop
   (map (\role ->
121 046fe3f5 Iustin Pop
          "\"" ++ nodeRoleToRaw role ++ "\" for " ++ roleDescription role)
122 046fe3f5 Iustin Pop
   (reverse [minBound..maxBound]))
123 046fe3f5 Iustin Pop
124 5227de56 Iustin Pop
-- | Get node powered status.
125 5227de56 Iustin Pop
getNodePower :: ConfigData -> Node -> ResultEntry
126 5227de56 Iustin Pop
getNodePower cfg node =
127 5227de56 Iustin Pop
  case getNodeNdParams cfg node of
128 5227de56 Iustin Pop
    Nothing -> rsNoData
129 5227de56 Iustin Pop
    Just ndp -> if null (ndpOobProgram ndp)
130 5227de56 Iustin Pop
                  then rsUnavail
131 5227de56 Iustin Pop
                  else rsNormal (nodePowered node)
132 5227de56 Iustin Pop
133 046fe3f5 Iustin Pop
-- | List of all node fields.
134 36162faf Iustin Pop
nodeFields :: FieldList Node Runtime
135 046fe3f5 Iustin Pop
nodeFields =
136 046fe3f5 Iustin Pop
  [ (FieldDefinition "drained" "Drained" QFTBool "Whether node is drained",
137 f94a9680 Iustin Pop
     FieldSimple (rsNormal . nodeDrained), QffNormal)
138 046fe3f5 Iustin Pop
  , (FieldDefinition "master_candidate" "MasterC" QFTBool
139 046fe3f5 Iustin Pop
       "Whether node is a master candidate",
140 f94a9680 Iustin Pop
     FieldSimple (rsNormal . nodeMasterCandidate), QffNormal)
141 046fe3f5 Iustin Pop
  , (FieldDefinition "master_capable" "MasterCapable" QFTBool
142 046fe3f5 Iustin Pop
       "Whether node can become a master candidate",
143 f94a9680 Iustin Pop
     FieldSimple (rsNormal . nodeMasterCapable), QffNormal)
144 046fe3f5 Iustin Pop
  , (FieldDefinition "name" "Node" QFTText "Node name",
145 91c1a265 Iustin Pop
     FieldSimple (rsNormal . nodeName), QffHostname)
146 046fe3f5 Iustin Pop
  , (FieldDefinition "offline" "Offline" QFTBool
147 046fe3f5 Iustin Pop
       "Whether node is marked offline",
148 f94a9680 Iustin Pop
     FieldSimple (rsNormal . nodeOffline), QffNormal)
149 046fe3f5 Iustin Pop
  , (FieldDefinition "vm_capable" "VMCapable" QFTBool
150 046fe3f5 Iustin Pop
       "Whether node can host instances",
151 f94a9680 Iustin Pop
     FieldSimple (rsNormal . nodeVmCapable), QffNormal)
152 046fe3f5 Iustin Pop
  , (FieldDefinition "pip" "PrimaryIP" QFTText "Primary IP address",
153 f94a9680 Iustin Pop
     FieldSimple (rsNormal . nodePrimaryIp), QffNormal)
154 046fe3f5 Iustin Pop
  , (FieldDefinition "sip" "SecondaryIP" QFTText "Secondary IP address",
155 f94a9680 Iustin Pop
     FieldSimple (rsNormal . nodeSecondaryIp), QffNormal)
156 046fe3f5 Iustin Pop
  , (FieldDefinition "master" "IsMaster" QFTBool "Whether node is master",
157 046fe3f5 Iustin Pop
     FieldConfig (\cfg node ->
158 046fe3f5 Iustin Pop
                    rsNormal (nodeName node ==
159 f94a9680 Iustin Pop
                              clusterMasterNode (configCluster cfg))),
160 f94a9680 Iustin Pop
     QffNormal)
161 046fe3f5 Iustin Pop
  , (FieldDefinition "group" "Group" QFTText "Node group",
162 046fe3f5 Iustin Pop
     FieldConfig (\cfg node ->
163 a64cc96b Helga Velroyen
                    rsMaybeNoData (groupName <$> getGroupOfNode cfg node)),
164 f94a9680 Iustin Pop
     QffNormal)
165 046fe3f5 Iustin Pop
  , (FieldDefinition "group.uuid" "GroupUUID" QFTText "UUID of node group",
166 f94a9680 Iustin Pop
     FieldSimple (rsNormal . nodeGroup), QffNormal)
167 046fe3f5 Iustin Pop
  ,  (FieldDefinition "ndparams" "NodeParameters" QFTOther
168 046fe3f5 Iustin Pop
        "Merged node parameters",
169 a64cc96b Helga Velroyen
      FieldConfig ((rsMaybeNoData .) . getNodeNdParams), QffNormal)
170 046fe3f5 Iustin Pop
  , (FieldDefinition "custom_ndparams" "CustomNodeParameters" QFTOther
171 046fe3f5 Iustin Pop
                       "Custom node parameters",
172 f94a9680 Iustin Pop
     FieldSimple (rsNormal . nodeNdparams), QffNormal)
173 046fe3f5 Iustin Pop
  -- FIXME: the below could be generalised a bit, like in Python
174 046fe3f5 Iustin Pop
  , (FieldDefinition "pinst_cnt" "Pinst" QFTNumber
175 046fe3f5 Iustin Pop
       "Number of instances with this node as primary",
176 046fe3f5 Iustin Pop
     FieldConfig (\cfg ->
177 f94a9680 Iustin Pop
                    rsNormal . length . fst . getNodeInstances cfg . nodeName),
178 f94a9680 Iustin Pop
     QffNormal)
179 046fe3f5 Iustin Pop
  , (FieldDefinition "sinst_cnt" "Sinst" QFTNumber
180 046fe3f5 Iustin Pop
       "Number of instances with this node as secondary",
181 046fe3f5 Iustin Pop
     FieldConfig (\cfg ->
182 f94a9680 Iustin Pop
                    rsNormal . length . snd . getNodeInstances cfg . nodeName),
183 f94a9680 Iustin Pop
     QffNormal)
184 b9bdc10e Iustin Pop
  , (FieldDefinition "pinst_list" "PriInstances" QFTOther
185 046fe3f5 Iustin Pop
       "List of instances with this node as primary",
186 c81b97f2 Iustin Pop
     FieldConfig (\cfg -> rsNormal . niceSort . map instName . fst .
187 f94a9680 Iustin Pop
                          getNodeInstances cfg . nodeName), QffNormal)
188 b9bdc10e Iustin Pop
  , (FieldDefinition "sinst_list" "SecInstances" QFTOther
189 046fe3f5 Iustin Pop
       "List of instances with this node as secondary",
190 c81b97f2 Iustin Pop
     FieldConfig (\cfg -> rsNormal . niceSort . map instName . snd .
191 f94a9680 Iustin Pop
                          getNodeInstances cfg . nodeName), QffNormal)
192 046fe3f5 Iustin Pop
  , (FieldDefinition "role" "Role" QFTText nodeRoleDoc,
193 f94a9680 Iustin Pop
     FieldConfig ((rsNormal .) . getNodeRole), QffNormal)
194 046fe3f5 Iustin Pop
  , (FieldDefinition "powered" "Powered" QFTBool
195 046fe3f5 Iustin Pop
       "Whether node is thought to be powered on",
196 f94a9680 Iustin Pop
     FieldConfig getNodePower, QffNormal)
197 046fe3f5 Iustin Pop
  -- FIXME: the two fields below are incomplete in Python, part of the
198 046fe3f5 Iustin Pop
  -- non-implemented node resource model; they are declared just for
199 046fe3f5 Iustin Pop
  -- parity, but are not functional
200 046fe3f5 Iustin Pop
  , (FieldDefinition "hv_state" "HypervisorState" QFTOther "Hypervisor state",
201 82953e9a Iustin Pop
     FieldSimple (const rsUnavail), QffNormal)
202 046fe3f5 Iustin Pop
  , (FieldDefinition "disk_state" "DiskState" QFTOther "Disk state",
203 82953e9a Iustin Pop
     FieldSimple (const rsUnavail), QffNormal)
204 046fe3f5 Iustin Pop
  ] ++
205 046fe3f5 Iustin Pop
  map nodeLiveFieldBuilder nodeLiveFieldsDefs ++
206 046fe3f5 Iustin Pop
  map buildNdParamField allNDParamFields ++
207 046fe3f5 Iustin Pop
  timeStampFields ++
208 046fe3f5 Iustin Pop
  uuidFields "Node" ++
209 046fe3f5 Iustin Pop
  serialFields "Node" ++
210 046fe3f5 Iustin Pop
  tagsFields
211 046fe3f5 Iustin Pop
212 046fe3f5 Iustin Pop
-- | The node fields map.
213 36162faf Iustin Pop
fieldsMap :: FieldMap Node Runtime
214 36162faf Iustin Pop
fieldsMap =
215 f94a9680 Iustin Pop
  Map.fromList $ map (\v@(f, _, _) -> (fdefName f, v)) nodeFields
216 1ba01ff7 Iustin Pop
217 1ba01ff7 Iustin Pop
-- | Collect live data from RPC query if enabled.
218 1ba01ff7 Iustin Pop
--
219 1ba01ff7 Iustin Pop
-- FIXME: Check which fields we actually need and possibly send empty
220 36162faf Iustin Pop
-- hvs\/vgs if no info from hypervisor\/volume group respectively is
221 1ba01ff7 Iustin Pop
-- required
222 36162faf Iustin Pop
collectLiveData:: Bool -> ConfigData -> [Node] -> IO [(Node, Runtime)]
223 36162faf Iustin Pop
collectLiveData False _ nodes =
224 1ba01ff7 Iustin Pop
  return $ zip nodes (repeat $ Left (RpcResultError "Live data disabled"))
225 36162faf Iustin Pop
collectLiveData True cfg nodes = do
226 64b0309a Dimitris Aragiorgis
  let vgs = maybeToList . clusterVolumeGroupName $ configCluster cfg
227 1ba01ff7 Iustin Pop
      hvs = [getDefaultHypervisor cfg]
228 319322a7 Bernardo Dal Seno
      step n (bn, gn, em) =
229 319322a7 Bernardo Dal Seno
        let ndp' = getNodeNdParams cfg n
230 319322a7 Bernardo Dal Seno
        in case ndp' of
231 319322a7 Bernardo Dal Seno
             Just ndp -> (bn, n : gn,
232 319322a7 Bernardo Dal Seno
                          (nodeName n, ndpExclusiveStorage ndp) : em)
233 319322a7 Bernardo Dal Seno
             Nothing -> (n : bn, gn, em)
234 319322a7 Bernardo Dal Seno
      (bnodes, gnodes, emap) = foldr step ([], [], []) nodes
235 319322a7 Bernardo Dal Seno
  rpcres <- executeRpcCall gnodes (RpcCallNodeInfo vgs hvs (Map.fromList emap))
236 319322a7 Bernardo Dal Seno
  -- FIXME: The order of nodes in the result could be different from the input
237 319322a7 Bernardo Dal Seno
  return $ zip bnodes (repeat $ Left (RpcResultError "Broken configuration"))
238 319322a7 Bernardo Dal Seno
           ++ rpcres