7 Copyright (C) 2009 Google Inc.
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.
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.
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
26 module Main (main) where
30 import Data.Maybe (isJust, fromJust, fromMaybe)
34 import System.Console.GetOpt
35 import qualified System
37 import Text.Printf (printf, hPrintf)
39 import qualified Ganeti.HTools.Container as Container
40 import qualified Ganeti.HTools.Cluster as Cluster
41 import qualified Ganeti.HTools.Node as Node
42 import qualified Ganeti.HTools.CLI as CLI
44 import Ganeti.HTools.Utils
46 -- | Command line options structure.
47 data Options = Options
48 { optShowNodes :: Bool -- ^ Whether to show node status
49 , optShowCmds :: Maybe FilePath -- ^ Whether to show the command list
50 , optOneline :: Bool -- ^ Switch output to a single line
51 , optNodef :: FilePath -- ^ Path to the nodes file
52 , optNodeSet :: Bool -- ^ The nodes have been set by options
53 , optInstf :: FilePath -- ^ Path to the instances file
54 , optInstSet :: Bool -- ^ The insts have been set by options
55 , optMaxLength :: Int -- ^ Stop after this many steps
56 , optMaster :: String -- ^ Collect data from RAPI
57 , optLuxi :: Maybe FilePath -- ^ Collect data from Luxi
58 , optVerbose :: Int -- ^ Verbosity level
59 , optOffline :: [String] -- ^ Names of offline nodes
60 , optMinScore :: Cluster.Score -- ^ The minimum score we aim for
61 , optMcpu :: Double -- ^ Max cpu ratio for nodes
62 , optMdsk :: Double -- ^ Max disk usage ratio for nodes
63 , optShowVer :: Bool -- ^ Just show the program version
64 , optShowHelp :: Bool -- ^ Just show the help
67 instance CLI.CLIOptions Options where
68 showVersion = optShowVer
69 showHelp = optShowHelp
71 instance CLI.EToolOptions Options where
76 masterName = optMaster
78 silent a = optVerbose a == 0
80 -- | Default values for the command line options.
81 defaultOptions :: Options
82 defaultOptions = Options
83 { optShowNodes = False
84 , optShowCmds = Nothing
88 , optInstf = "instances"
102 -- | Options list and functions
103 options :: [OptDescr (Options -> Options)]
105 [ Option ['p'] ["print-nodes"]
106 (NoArg (\ opts -> opts { optShowNodes = True }))
107 "print the final node list"
108 , Option ['C'] ["print-commands"]
109 (OptArg ((\ f opts -> opts { optShowCmds = Just f }) . fromMaybe "-")
111 "print the ganeti command list for reaching the solution,\
112 \if an argument is passed then write the commands to a file named\
114 , Option ['o'] ["oneline"]
115 (NoArg (\ opts -> opts { optOneline = True }))
116 "print the ganeti command list for reaching the solution"
117 , Option ['n'] ["nodes"]
118 (ReqArg (\ f opts -> opts { optNodef = f, optNodeSet = True }) "FILE")
120 , Option ['i'] ["instances"]
121 (ReqArg (\ f opts -> opts { optInstf = f, optInstSet = True }) "FILE")
122 "the instance list FILE"
123 , Option ['m'] ["master"]
124 (ReqArg (\ m opts -> opts { optMaster = m }) "ADDRESS")
125 "collect data via RAPI at the given ADDRESS"
126 , Option ['L'] ["luxi"]
127 (OptArg ((\ f opts -> opts { optLuxi = Just f }) .
128 fromMaybe CLI.defaultLuxiSocket) "SOCKET")
129 "collect data via Luxi, optionally using the given SOCKET path"
130 , Option ['l'] ["max-length"]
131 (ReqArg (\ i opts -> opts { optMaxLength = read i::Int }) "N")
132 "cap the solution at this many moves (useful for very unbalanced \
134 , Option ['v'] ["verbose"]
135 (NoArg (\ opts -> opts { optVerbose = optVerbose opts + 1 }))
136 "increase the verbosity level"
137 , Option ['q'] ["quiet"]
138 (NoArg (\ opts -> opts { optVerbose = optVerbose opts - 1 }))
139 "decrease the verbosity level"
140 , Option ['O'] ["offline"]
141 (ReqArg (\ n opts -> opts { optOffline = n:optOffline opts }) "NODE")
142 " set node as offline"
143 , Option ['e'] ["min-score"]
144 (ReqArg (\ e opts -> opts { optMinScore = read e }) "EPSILON")
145 " mininum score to aim for"
146 , Option [] ["max-cpu"]
147 (ReqArg (\ n opts -> opts { optMcpu = read n }) "RATIO")
148 "maximum virtual-to-physical cpu ratio for nodes"
149 , Option [] ["min-disk"]
150 (ReqArg (\ n opts -> opts { optMdsk = read n }) "RATIO")
151 "minimum free disk space for nodes (between 0 and 1)"
152 , Option ['V'] ["version"]
153 (NoArg (\ opts -> opts { optShowVer = True}))
154 "show the version of the program"
155 , Option ['h'] ["help"]
156 (NoArg (\ opts -> opts { optShowHelp = True}))
160 {- | Start computing the solution at the given depth and recurse until
161 we find a valid solution or we exceed the maximum depth.
164 iterateDepth :: Cluster.Table -- ^ The starting table
165 -> Int -- ^ Remaining length
166 -> Int -- ^ Max node name len
167 -> Int -- ^ Max instance name len
168 -> [[String]] -- ^ Current command list
169 -> Bool -- ^ Wheter to be silent
170 -> Cluster.Score -- ^ Score at which to stop
171 -> IO (Cluster.Table, [[String]]) -- ^ The resulting table and
173 iterateDepth ini_tbl max_rounds nmlen imlen
174 cmd_strs oneline min_score =
175 let Cluster.Table ini_nl ini_il ini_cv ini_plc = ini_tbl
176 all_inst = Container.elems ini_il
177 node_idx = map Node.idx . filter (not . Node.offline) $
178 Container.elems ini_nl
179 fin_tbl = Cluster.checkMove node_idx ini_tbl all_inst
180 (Cluster.Table _ _ fin_cv fin_plc) = fin_tbl
181 ini_plc_len = length ini_plc
182 fin_plc_len = length fin_plc
183 allowed_next = (max_rounds < 0 || length fin_plc < max_rounds)
187 (sol_line, cmds) = Cluster.printSolutionLine ini_nl ini_il
188 nmlen imlen (head fin_plc) fin_plc_len
189 upd_cmd_strs = cmds:cmd_strs
190 unless (oneline || fin_plc_len == ini_plc_len) $ do
193 (if fin_cv < ini_cv then -- this round made success, try deeper
194 if allowed_next && fin_cv > min_score
195 then iterateDepth fin_tbl max_rounds
196 nmlen imlen upd_cmd_strs oneline min_score
197 -- don't go deeper, but return the better solution
198 else return (fin_tbl, upd_cmd_strs)
200 return (ini_tbl, cmd_strs))
202 -- | Formats the solution for the oneline display
203 formatOneline :: Double -> Int -> Double -> String
204 formatOneline ini_cv plc_len fin_cv =
205 printf "%.8f %d %.8f %8.3f" ini_cv plc_len fin_cv
206 (if fin_cv == 0 then 1 else ini_cv / fin_cv)
211 cmd_args <- System.getArgs
212 (opts, args) <- CLI.parseOpts cmd_args "hbal" options defaultOptions
214 unless (null args) $ do
215 hPutStrLn stderr "Error: this program doesn't take any arguments."
216 exitWith $ ExitFailure 1
218 let oneline = optOneline opts
219 verbose = optVerbose opts
221 (fixed_nl, il, csf) <- CLI.loadExternalData opts
223 let offline_names = optOffline opts
224 all_nodes = Container.elems fixed_nl
225 all_names = map Node.name all_nodes
226 offline_wrong = filter (flip notElem all_names) offline_names
227 offline_indices = map Node.idx $
228 filter (\n -> elem (Node.name n) offline_names)
233 when (length offline_wrong > 0) $ do
234 hPrintf stderr "Wrong node name(s) set as offline: %s\n"
235 (commaJoin offline_wrong)
236 exitWith $ ExitFailure 1
238 let nm = Container.map (\n -> if elem (Node.idx n) offline_indices
239 then Node.setOffline n True
241 nl = Container.map (flip Node.setMdsk m_dsk . flip Node.setMcpu m_cpu)
244 when (Container.size il == 0) $ do
245 (if oneline then putStrLn $ formatOneline 0 0 0
246 else printf "Cluster is empty, exiting.\n")
249 unless oneline $ printf "Loaded %d nodes, %d instances\n"
253 when (length csf > 0 && not oneline && verbose > 1) $
254 printf "Note: Stripping common suffix of '%s' from names\n" csf
256 let (bad_nodes, bad_instances) = Cluster.computeBadItems nl il
257 unless (oneline || verbose == 0) $ printf
258 "Initial check done: %d bad nodes, %d bad instances.\n"
259 (length bad_nodes) (length bad_instances)
261 when (length bad_nodes > 0) $
262 putStrLn "Cluster is not N+1 happy, continuing but no guarantee \
263 \that the cluster will end N+1 happy."
265 when (optShowNodes opts) $
267 putStrLn "Initial cluster status:"
268 putStrLn $ Cluster.printNodes nl
270 let ini_cv = Cluster.compCV nl
271 ini_tbl = Cluster.Table nl il ini_cv []
272 min_cv = optMinScore opts
274 when (ini_cv < min_cv) $ do
276 putStrLn $ formatOneline ini_cv 0 ini_cv
277 else printf "Cluster is already well balanced (initial score %.6g,\n\
278 \minimum score %.6g).\nNothing to do, exiting\n"
282 unless oneline (if verbose > 2 then
283 printf "Initial coefficients: overall %.8f, %s\n"
284 ini_cv (Cluster.printStats nl)
286 printf "Initial score: %.8f\n" ini_cv)
288 unless oneline $ putStrLn "Trying to minimize the CV..."
289 let imlen = Container.maxNameLen il
290 nmlen = Container.maxNameLen nl
292 (fin_tbl, cmd_strs) <- iterateDepth ini_tbl (optMaxLength opts)
293 nmlen imlen [] oneline min_cv
294 let (Cluster.Table fin_nl _ fin_cv fin_plc) = fin_tbl
295 ord_plc = reverse fin_plc
296 sol_msg = if null fin_plc
297 then printf "No solution found\n"
299 then printf "Final coefficients: overall %.8f, %s\n"
300 fin_cv (Cluster.printStats fin_nl)
301 else printf "Cluster score improved from %.8f to %.8f\n"
305 unless oneline $ putStr sol_msg
307 unless (oneline || verbose == 0) $
308 printf "Solution length=%d\n" (length ord_plc)
310 let cmd_data = Cluster.formatCmds . reverse $ cmd_strs
312 when (isJust $ optShowCmds opts) $
314 let out_path = fromJust $ optShowCmds opts
316 (if out_path == "-" then
317 printf "Commands to run to reach the above solution:\n%s"
318 (unlines . map (" " ++) .
319 filter (/= "check") .
322 writeFile out_path (CLI.shTemplate ++ cmd_data)
323 printf "The commands have been written to file '%s'\n" out_path)
325 when (optShowNodes opts) $
327 let ini_cs = Cluster.totalResources nl
328 fin_cs = Cluster.totalResources fin_nl
330 putStrLn "Final cluster status:"
331 putStrLn $ Cluster.printNodes fin_nl
334 printf "Original: mem=%d disk=%d\n"
335 (Cluster.cs_fmem ini_cs) (Cluster.cs_fdsk ini_cs)
336 printf "Final: mem=%d disk=%d\n"
337 (Cluster.cs_fmem fin_cs) (Cluster.cs_fdsk fin_cs)
339 putStrLn $ formatOneline ini_cv (length ord_plc) fin_cv