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