Statistics
| Branch: | Tag: | Revision:

root / src / Ganeti / Hypervisor / Xen.hs @ 32be18fc

History | View | Annotate | Download (3.5 kB)

1 45ee8676 Michele Tartara
{-| Module to access the information provided by the Xen hypervisor.
2 45ee8676 Michele Tartara
3 45ee8676 Michele Tartara
-}
4 45ee8676 Michele Tartara
{-
5 45ee8676 Michele Tartara
6 45ee8676 Michele Tartara
Copyright (C) 2013 Google Inc.
7 45ee8676 Michele Tartara
8 45ee8676 Michele Tartara
This program is free software; you can redistribute it and/or modify
9 45ee8676 Michele Tartara
it under the terms of the GNU General Public License as published by
10 45ee8676 Michele Tartara
the Free Software Foundation; either version 2 of the License, or
11 45ee8676 Michele Tartara
(at your option) any later version.
12 45ee8676 Michele Tartara
13 45ee8676 Michele Tartara
This program is distributed in the hope that it will be useful, but
14 45ee8676 Michele Tartara
WITHOUT ANY WARRANTY; without even the implied warranty of
15 45ee8676 Michele Tartara
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16 45ee8676 Michele Tartara
General Public License for more details.
17 45ee8676 Michele Tartara
18 45ee8676 Michele Tartara
You should have received a copy of the GNU General Public License
19 45ee8676 Michele Tartara
along with this program; if not, write to the Free Software
20 45ee8676 Michele Tartara
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 45ee8676 Michele Tartara
02110-1301, USA.
22 45ee8676 Michele Tartara
23 45ee8676 Michele Tartara
-}
24 45ee8676 Michele Tartara
module Ganeti.Hypervisor.Xen
25 45ee8676 Michele Tartara
  ( getDomainsInfo
26 45ee8676 Michele Tartara
  , getInferredDomInfo
27 45ee8676 Michele Tartara
  , getUptimeInfo
28 45ee8676 Michele Tartara
  --Data types to be re-exported from here
29 45ee8676 Michele Tartara
  , Domain(..)
30 45ee8676 Michele Tartara
  , UptimeInfo(..)
31 45ee8676 Michele Tartara
  ) where
32 45ee8676 Michele Tartara
33 45ee8676 Michele Tartara
import qualified Control.Exception as E
34 45ee8676 Michele Tartara
import Data.Attoparsec.Text as A
35 45ee8676 Michele Tartara
import qualified Data.Map as Map
36 45ee8676 Michele Tartara
import Data.Text (pack)
37 45ee8676 Michele Tartara
import System.Process
38 45ee8676 Michele Tartara
39 45ee8676 Michele Tartara
import qualified Ganeti.BasicTypes as BT
40 45ee8676 Michele Tartara
import qualified Ganeti.Constants as C
41 45ee8676 Michele Tartara
import Ganeti.Hypervisor.Xen.Types
42 45ee8676 Michele Tartara
import Ganeti.Hypervisor.Xen.XmParser
43 eebc8ab2 Michele Tartara
import Ganeti.Logging
44 45ee8676 Michele Tartara
import Ganeti.Utils
45 45ee8676 Michele Tartara
46 45ee8676 Michele Tartara
47 45ee8676 Michele Tartara
-- | Get information about the current Xen domains as a map where the domain
48 45ee8676 Michele Tartara
-- name is the key. This only includes the information made available by Xen
49 45ee8676 Michele Tartara
-- itself.
50 eebc8ab2 Michele Tartara
getDomainsInfo :: IO (BT.Result (Map.Map String Domain))
51 45ee8676 Michele Tartara
getDomainsInfo = do
52 45ee8676 Michele Tartara
  contents <-
53 eebc8ab2 Michele Tartara
        (E.try $ readProcess C.xenCmdXm ["list", "--long"] "")
54 eebc8ab2 Michele Tartara
          :: IO (Either IOError String)
55 eebc8ab2 Michele Tartara
  return $
56 eebc8ab2 Michele Tartara
    either (BT.Bad . show) (
57 eebc8ab2 Michele Tartara
      \c ->
58 eebc8ab2 Michele Tartara
        case A.parseOnly xmListParser $ pack c of
59 eebc8ab2 Michele Tartara
          Left msg -> BT.Bad msg
60 eebc8ab2 Michele Tartara
          Right dom -> BT.Ok dom
61 eebc8ab2 Michele Tartara
      ) contents
62 45ee8676 Michele Tartara
63 45ee8676 Michele Tartara
-- | Given a domain and a map containing information about multiple domains,
64 45ee8676 Michele Tartara
-- infer additional information about that domain (specifically, whether it is
65 45ee8676 Michele Tartara
-- hung).
66 45ee8676 Michele Tartara
inferDomInfos :: Map.Map String Domain -> Domain -> Domain
67 45ee8676 Michele Tartara
inferDomInfos domMap dom1 =
68 45ee8676 Michele Tartara
  case Map.lookup (domName dom1) domMap of
69 45ee8676 Michele Tartara
    Just dom2 ->
70 45ee8676 Michele Tartara
      dom1 { domIsHung = Just $ domCpuTime dom1 == domCpuTime dom2 }
71 45ee8676 Michele Tartara
    Nothing -> dom1 { domIsHung = Nothing }
72 45ee8676 Michele Tartara
73 45ee8676 Michele Tartara
-- | Get information about the current Xen domains as a map where the domain
74 45ee8676 Michele Tartara
-- name is the key. This includes information made available by Xen itself as
75 45ee8676 Michele Tartara
-- well as further information that can be inferred by querying Xen multiple
76 45ee8676 Michele Tartara
-- times and comparing the results.
77 eebc8ab2 Michele Tartara
getInferredDomInfo :: IO (BT.Result (Map.Map String Domain))
78 45ee8676 Michele Tartara
getInferredDomInfo = do
79 45ee8676 Michele Tartara
  domMap1 <- getDomainsInfo
80 45ee8676 Michele Tartara
  domMap2 <- getDomainsInfo
81 eebc8ab2 Michele Tartara
  case (domMap1, domMap2) of
82 eebc8ab2 Michele Tartara
    (BT.Bad m1, BT.Bad m2) -> return . BT.Bad $ m1 ++ "\n" ++ m2
83 eebc8ab2 Michele Tartara
    (BT.Bad m, BT.Ok d) -> do
84 eebc8ab2 Michele Tartara
      logWarning $ "Unable to retrieve domains info the first time" ++ m
85 eebc8ab2 Michele Tartara
      return $ BT.Ok d
86 eebc8ab2 Michele Tartara
    (BT.Ok d, BT.Bad m) -> do
87 eebc8ab2 Michele Tartara
      logWarning $ "Unable to retrieve domains info the second time" ++ m
88 eebc8ab2 Michele Tartara
      return $ BT.Ok d
89 eebc8ab2 Michele Tartara
    (BT.Ok d1, BT.Ok d2) -> return . BT.Ok $ fmap (inferDomInfos d2) d1
90 45ee8676 Michele Tartara
91 45ee8676 Michele Tartara
-- | Get information about the uptime of domains, as a map where the domain ID
92 45ee8676 Michele Tartara
-- is the key.
93 45ee8676 Michele Tartara
getUptimeInfo :: IO (Map.Map Int UptimeInfo)
94 45ee8676 Michele Tartara
getUptimeInfo = do
95 45ee8676 Michele Tartara
  contents <-
96 45ee8676 Michele Tartara
    ((E.try $ readProcess C.xenCmdXm ["uptime"] "")
97 45ee8676 Michele Tartara
      :: IO (Either IOError String)) >>=
98 45ee8676 Michele Tartara
      exitIfBad "running command" . either (BT.Bad . show) BT.Ok
99 45ee8676 Michele Tartara
  case A.parseOnly xmUptimeParser $ pack contents of
100 45ee8676 Michele Tartara
    Left msg -> exitErr msg
101 45ee8676 Michele Tartara
    Right uInfo -> return uInfo