root / htools / Ganeti / HTools / Program / Hinfo.hs @ 707cd3d7
History | View | Annotate | Download (5.6 kB)
1 |
{-| Cluster information printer. |
---|---|
2 |
|
3 |
-} |
4 |
|
5 |
{- |
6 |
|
7 |
Copyright (C) 2012 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.HTools.Program.Hinfo |
27 |
( main |
28 |
, options |
29 |
, arguments |
30 |
) where |
31 |
|
32 |
import Control.Monad |
33 |
import Data.List |
34 |
import System.IO |
35 |
|
36 |
import Text.Printf (printf) |
37 |
|
38 |
import qualified Ganeti.HTools.Container as Container |
39 |
import qualified Ganeti.HTools.Cluster as Cluster |
40 |
import qualified Ganeti.HTools.Node as Node |
41 |
import qualified Ganeti.HTools.Group as Group |
42 |
import qualified Ganeti.HTools.Instance as Instance |
43 |
|
44 |
import Ganeti.Common |
45 |
import Ganeti.HTools.CLI |
46 |
import Ganeti.HTools.ExtLoader |
47 |
import Ganeti.HTools.Loader |
48 |
import Ganeti.Utils |
49 |
|
50 |
-- | Options list and functions. |
51 |
options :: [OptType] |
52 |
options = |
53 |
[ oPrintNodes |
54 |
, oPrintInsts |
55 |
, oDataFile |
56 |
, oRapiMaster |
57 |
, oLuxiSocket |
58 |
, oIAllocSrc |
59 |
, oVerbose |
60 |
, oQuiet |
61 |
, oOfflineNode |
62 |
] |
63 |
|
64 |
-- | The list of arguments supported by the program. |
65 |
arguments :: [ArgCompletion] |
66 |
arguments = [] |
67 |
|
68 |
-- | Group information data-type. |
69 |
data GroupInfo = GroupInfo { giName :: String |
70 |
, giNodeCount :: Int |
71 |
, giInstCount :: Int |
72 |
, giBadNodes :: Int |
73 |
, giBadInsts :: Int |
74 |
, giN1Status :: Bool |
75 |
, giScore :: Double |
76 |
} |
77 |
|
78 |
-- | Node group statistics. |
79 |
calcGroupInfo :: Group.Group |
80 |
-> Node.List |
81 |
-> Instance.List |
82 |
-> GroupInfo |
83 |
calcGroupInfo g nl il = |
84 |
let nl_size = Container.size nl |
85 |
il_size = Container.size il |
86 |
(bad_nodes, bad_instances) = Cluster.computeBadItems nl il |
87 |
bn_size = length bad_nodes |
88 |
bi_size = length bad_instances |
89 |
n1h = bn_size == 0 |
90 |
score = Cluster.compCV nl |
91 |
in GroupInfo (Group.name g) nl_size il_size bn_size bi_size n1h score |
92 |
|
93 |
-- | Helper to format one group row result. |
94 |
groupRowFormatHelper :: GroupInfo -> [String] |
95 |
groupRowFormatHelper gi = |
96 |
[ giName gi |
97 |
, printf "%d" $ giNodeCount gi |
98 |
, printf "%d" $ giInstCount gi |
99 |
, printf "%d" $ giBadNodes gi |
100 |
, printf "%d" $ giBadInsts gi |
101 |
, show $ giN1Status gi |
102 |
, printf "%.8f" $ giScore gi |
103 |
] |
104 |
|
105 |
-- | Print node group information. |
106 |
showGroupInfo :: Int -> Group.List -> Node.List -> Instance.List -> IO () |
107 |
showGroupInfo verbose gl nl il = do |
108 |
let cgrs = map (\(gdx, (gnl, gil)) -> |
109 |
calcGroupInfo (Container.find gdx gl) gnl gil) $ |
110 |
Cluster.splitCluster nl il |
111 |
cn1h = all giN1Status cgrs |
112 |
grs = map groupRowFormatHelper cgrs |
113 |
header = ["Group", "Nodes", "Instances", "Bad_Nodes", "Bad_Instances", |
114 |
"N+1", "Score"] |
115 |
|
116 |
when (verbose > 1) $ |
117 |
printf "Node group information:\n%s" |
118 |
(printTable " " header grs [False, True, True, True, True, |
119 |
False, True]) |
120 |
|
121 |
printf "Cluster is N+1 %s\n" $ if cn1h then "happy" else "unhappy" |
122 |
|
123 |
-- | Gather and print split instances. |
124 |
splitInstancesInfo :: Int -> Node.List -> Instance.List -> IO () |
125 |
splitInstancesInfo verbose nl il = do |
126 |
let split_insts = Cluster.findSplitInstances nl il |
127 |
if null split_insts |
128 |
then |
129 |
when (verbose > 1) $ |
130 |
putStrLn "No split instances found"::IO () |
131 |
else do |
132 |
putStrLn "Found instances belonging to multiple node groups:" |
133 |
mapM_ (\i -> hPutStrLn stderr $ " " ++ Instance.name i) split_insts |
134 |
|
135 |
-- | Print common (interesting) information. |
136 |
commonInfo :: Int -> Group.List -> Node.List -> Instance.List -> IO () |
137 |
commonInfo verbose gl nl il = do |
138 |
when (Container.null il && verbose > 1) $ |
139 |
printf "Cluster is empty.\n"::IO () |
140 |
|
141 |
let nl_size = Container.size nl |
142 |
il_size = Container.size il |
143 |
gl_size = Container.size gl |
144 |
printf "Loaded %d %s, %d %s, %d %s\n" |
145 |
nl_size (plural nl_size "node" "nodes") |
146 |
il_size (plural il_size "instance" "instances") |
147 |
gl_size (plural gl_size "node group" "node groups")::IO () |
148 |
|
149 |
let csf = commonSuffix nl il |
150 |
when (not (null csf) && verbose > 2) $ |
151 |
printf "Note: Stripping common suffix of '%s' from names\n" csf |
152 |
|
153 |
-- | Main function. |
154 |
main :: Options -> [String] -> IO () |
155 |
main opts args = do |
156 |
unless (null args) $ exitErr "This program doesn't take any arguments." |
157 |
|
158 |
let verbose = optVerbose opts |
159 |
shownodes = optShowNodes opts |
160 |
showinsts = optShowInsts opts |
161 |
|
162 |
(ClusterData gl fixed_nl ilf ctags ipol) <- loadExternalData opts |
163 |
|
164 |
putStrLn $ "Loaded cluster tags: " ++ intercalate "," ctags |
165 |
|
166 |
when (verbose > 2) . |
167 |
putStrLn $ "Loaded cluster ipolicy: " ++ show ipol |
168 |
|
169 |
nlf <- setNodeStatus opts fixed_nl |
170 |
|
171 |
commonInfo verbose gl nlf ilf |
172 |
|
173 |
splitInstancesInfo verbose nlf ilf |
174 |
|
175 |
showGroupInfo verbose gl nlf ilf |
176 |
|
177 |
maybePrintInsts showinsts "Instances" (Cluster.printInsts nlf ilf) |
178 |
|
179 |
maybePrintNodes shownodes "Cluster" (Cluster.printNodes nlf) |
180 |
|
181 |
printf "Cluster coefficients:\n%s" (Cluster.printStats " " nlf)::IO () |
182 |
printf "Cluster score: %.8f\n" (Cluster.compCV nlf) |