root / src / Ganeti / DataCollectors / InstStatus.hs @ d4de2ea8
History | View | Annotate | Download (3.7 kB)
1 | d7e9323b | Michele Tartara | {-| Instance status data collector. |
---|---|---|---|
2 | d7e9323b | Michele Tartara | |
3 | d7e9323b | Michele Tartara | -} |
4 | d7e9323b | Michele Tartara | |
5 | d7e9323b | Michele Tartara | {- |
6 | d7e9323b | Michele Tartara | |
7 | d7e9323b | Michele Tartara | Copyright (C) 2013 Google Inc. |
8 | d7e9323b | Michele Tartara | |
9 | d7e9323b | Michele Tartara | This program is free software; you can redistribute it and/or modify |
10 | d7e9323b | Michele Tartara | it under the terms of the GNU General Public License as published by |
11 | d7e9323b | Michele Tartara | the Free Software Foundation; either version 2 of the License, or |
12 | d7e9323b | Michele Tartara | (at your option) any later version. |
13 | d7e9323b | Michele Tartara | |
14 | d7e9323b | Michele Tartara | This program is distributed in the hope that it will be useful, but |
15 | d7e9323b | Michele Tartara | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | d7e9323b | Michele Tartara | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
17 | d7e9323b | Michele Tartara | General Public License for more details. |
18 | d7e9323b | Michele Tartara | |
19 | d7e9323b | Michele Tartara | You should have received a copy of the GNU General Public License |
20 | d7e9323b | Michele Tartara | along with this program; if not, write to the Free Software |
21 | d7e9323b | Michele Tartara | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
22 | d7e9323b | Michele Tartara | 02110-1301, USA. |
23 | d7e9323b | Michele Tartara | |
24 | d7e9323b | Michele Tartara | -} |
25 | d7e9323b | Michele Tartara | |
26 | d7e9323b | Michele Tartara | module Ganeti.DataCollectors.InstStatus |
27 | d7e9323b | Michele Tartara | ( main |
28 | d7e9323b | Michele Tartara | , options |
29 | d7e9323b | Michele Tartara | , arguments |
30 | d7e9323b | Michele Tartara | ) where |
31 | d7e9323b | Michele Tartara | |
32 | d7e9323b | Michele Tartara | |
33 | d7e9323b | Michele Tartara | import Data.Maybe |
34 | d7e9323b | Michele Tartara | import qualified Data.Map as Map |
35 | d7e9323b | Michele Tartara | import Network.BSD (getHostName) |
36 | d7e9323b | Michele Tartara | import qualified Text.JSON as J |
37 | d7e9323b | Michele Tartara | |
38 | d7e9323b | Michele Tartara | import qualified Ganeti.BasicTypes as BT |
39 | d7e9323b | Michele Tartara | import Ganeti.Confd.Client |
40 | d7e9323b | Michele Tartara | import Ganeti.Confd.Types |
41 | d7e9323b | Michele Tartara | import Ganeti.Common |
42 | d7e9323b | Michele Tartara | import Ganeti.DataCollectors.CLI |
43 | d7e9323b | Michele Tartara | import Ganeti.DataCollectors.InstStatusTypes |
44 | d7e9323b | Michele Tartara | import Ganeti.Hypervisor.Xen |
45 | d4de2ea8 | Michele Tartara | import Ganeti.Hypervisor.Xen.Types |
46 | d7e9323b | Michele Tartara | import Ganeti.Objects |
47 | d7e9323b | Michele Tartara | import Ganeti.Utils |
48 | d7e9323b | Michele Tartara | |
49 | d7e9323b | Michele Tartara | -- * Command line options |
50 | d7e9323b | Michele Tartara | |
51 | d7e9323b | Michele Tartara | options :: IO [OptType] |
52 | d7e9323b | Michele Tartara | options = return |
53 | d7e9323b | Michele Tartara | [ oConfdAddr |
54 | d7e9323b | Michele Tartara | , oConfdPort |
55 | d7e9323b | Michele Tartara | ] |
56 | d7e9323b | Michele Tartara | |
57 | d7e9323b | Michele Tartara | -- | The list of arguments supported by the program. |
58 | d7e9323b | Michele Tartara | arguments :: [ArgCompletion] |
59 | d7e9323b | Michele Tartara | arguments = [] |
60 | d7e9323b | Michele Tartara | |
61 | d7e9323b | Michele Tartara | -- | Get the list of instances ([primary], [secondary]) on the given node. |
62 | d7e9323b | Michele Tartara | -- Implemented as a function, even if used a single time, to specify in a |
63 | d7e9323b | Michele Tartara | -- convenient and elegant way the return data type, required in order to |
64 | d7e9323b | Michele Tartara | -- prevent incurring in the monomorphism restriction. |
65 | d7e9323b | Michele Tartara | -- The server address and the server port parameters are mainly intended |
66 | d7e9323b | Michele Tartara | -- for testing purposes. If they are Nothing, the default values will be used. |
67 | d7e9323b | Michele Tartara | getInstances |
68 | d7e9323b | Michele Tartara | :: String |
69 | d7e9323b | Michele Tartara | -> Maybe String |
70 | d7e9323b | Michele Tartara | -> Maybe Int |
71 | d7e9323b | Michele Tartara | -> IO (BT.Result ([Ganeti.Objects.Instance], [Ganeti.Objects.Instance])) |
72 | d7e9323b | Michele Tartara | getInstances node srvAddr srvPort = do |
73 | d7e9323b | Michele Tartara | client <- getConfdClient srvAddr srvPort |
74 | d7e9323b | Michele Tartara | reply <- query client ReqNodeInstances $ PlainQuery node |
75 | d7e9323b | Michele Tartara | return $ |
76 | d7e9323b | Michele Tartara | case fmap (J.readJSON . confdReplyAnswer) reply of |
77 | d7e9323b | Michele Tartara | Just (J.Ok instances) -> BT.Ok instances |
78 | d7e9323b | Michele Tartara | Just (J.Error msg) -> BT.Bad msg |
79 | d7e9323b | Michele Tartara | Nothing -> BT.Bad "No answer from the Confd server" |
80 | d7e9323b | Michele Tartara | |
81 | d7e9323b | Michele Tartara | -- Builds the status of an instance using runtime information about the Xen |
82 | d7e9323b | Michele Tartara | -- Domains, their uptime information and the static information provided by |
83 | d7e9323b | Michele Tartara | -- the ConfD server. |
84 | d7e9323b | Michele Tartara | buildStatus :: Map.Map String Domain -> Map.Map Int UptimeInfo -> Instance |
85 | d7e9323b | Michele Tartara | -> IO InstStatus |
86 | d7e9323b | Michele Tartara | buildStatus domains uptimes inst = do |
87 | d7e9323b | Michele Tartara | let name = instName inst |
88 | d7e9323b | Michele Tartara | currDomain = Map.lookup name domains |
89 | d7e9323b | Michele Tartara | idNum = fmap domId currDomain |
90 | d7e9323b | Michele Tartara | currUInfo = idNum >>= (`Map.lookup` uptimes) |
91 | d7e9323b | Michele Tartara | uptime = fmap uInfoUptime currUInfo |
92 | d7e9323b | Michele Tartara | adminState = instAdminState inst |
93 | d4de2ea8 | Michele Tartara | actualState = |
94 | d4de2ea8 | Michele Tartara | if adminState == AdminDown && isNothing currDomain |
95 | d4de2ea8 | Michele Tartara | then ActualShutdown |
96 | d4de2ea8 | Michele Tartara | else case currDomain of |
97 | d4de2ea8 | Michele Tartara | (Just dom@(Domain _ _ _ _ (Just isHung))) -> |
98 | d4de2ea8 | Michele Tartara | if isHung |
99 | d4de2ea8 | Michele Tartara | then ActualHung |
100 | d4de2ea8 | Michele Tartara | else domState dom |
101 | d4de2ea8 | Michele Tartara | _ -> ActualUnknown |
102 | d7e9323b | Michele Tartara | return $ |
103 | d7e9323b | Michele Tartara | InstStatus |
104 | d7e9323b | Michele Tartara | name |
105 | d7e9323b | Michele Tartara | (instUuid inst) |
106 | d7e9323b | Michele Tartara | adminState |
107 | d4de2ea8 | Michele Tartara | actualState |
108 | d7e9323b | Michele Tartara | uptime |
109 | d7e9323b | Michele Tartara | (instMtime inst) |
110 | d7e9323b | Michele Tartara | |
111 | d7e9323b | Michele Tartara | -- | Main function. |
112 | d7e9323b | Michele Tartara | main :: Options -> [String] -> IO () |
113 | d7e9323b | Michele Tartara | main opts _ = do |
114 | d7e9323b | Michele Tartara | curNode <- getHostName |
115 | d7e9323b | Michele Tartara | let node = fromMaybe curNode $ optNode opts |
116 | d7e9323b | Michele Tartara | answer <- getInstances node (optConfdAddr opts) (optConfdPort opts) |
117 | d7e9323b | Michele Tartara | inst <- exitIfBad "Can't get instance info from ConfD" answer |
118 | d7e9323b | Michele Tartara | domains <- getInferredDomInfo |
119 | d7e9323b | Michele Tartara | uptimes <- getUptimeInfo |
120 | d7e9323b | Michele Tartara | let primaryInst = fst inst |
121 | d7e9323b | Michele Tartara | iStatus <- mapM (buildStatus domains uptimes) primaryInst |
122 | d7e9323b | Michele Tartara | putStrLn $ J.encode iStatus |