module Ganeti.HTools.Program.Hcheck
( main
, options
, arguments
) where
import Control.Monad
import Data.List (transpose)
import System.Exit
import Text.Printf (printf)
import qualified Ganeti.HTools.Container as Container
import qualified Ganeti.HTools.Cluster as Cluster
import qualified Ganeti.HTools.Group as Group
import qualified Ganeti.HTools.Node as Node
import qualified Ganeti.HTools.Instance as Instance
import qualified Ganeti.HTools.Program.Hbal as Hbal
import Ganeti.Common
import Ganeti.HTools.CLI
import Ganeti.HTools.ExtLoader
import Ganeti.HTools.Loader
import Ganeti.HTools.Types
import Ganeti.Utils
options :: IO [OptType]
options = do
luxi <- oLuxiSocket
return
[ oDataFile
, oDiskMoves
, oDynuFile
, oEvacMode
, oExInst
, oExTags
, oIAllocSrc
, oInstMoves
, luxi
, oMachineReadable
, oMaxCpu
, oMaxSolLength
, oMinDisk
, oMinGain
, oMinGainLim
, oMinScore
, oNoSimulation
, oOfflineNode
, oQuiet
, oRapiMaster
, oSelInst
, oVerbose
]
arguments :: [ArgCompletion]
arguments = []
data Phase = Initial
| Rebalanced
data Level = GroupLvl String
| ClusterLvl
type GroupInfo = (Gdx, (Node.List, Instance.List))
type GroupStats = ((Group.Group, Double), [Int])
htcPrefix :: String
htcPrefix = "HCHECK"
commonData :: [(String, String)]
commonData =[ ("N1_FAIL", "Nodes not N+1 happy")
, ("CONFLICT_TAGS", "Nodes with conflicting instances")
, ("OFFLINE_PRI", "Instances having the primary node offline")
, ("OFFLINE_SEC", "Instances having a secondary node offline")
]
groupData :: [(String, String)]
groupData = commonData ++ [("SCORE", "Group score")]
clusterData :: [(String, String)]
clusterData = commonData ++
[ ("NEED_REBALANCE", "Cluster is not healthy") ]
phasePrefix :: Phase -> String
phasePrefix Initial = "INIT"
phasePrefix Rebalanced = "FINAL"
levelPrefix :: Level -> String
levelPrefix GroupLvl {} = "GROUP"
levelPrefix ClusterLvl = "CLUSTER"
keysData :: Level -> [String]
keysData GroupLvl {} = map fst groupData
keysData ClusterLvl = map fst clusterData
phaseDescr :: Phase -> String
phaseDescr Initial = "initially"
phaseDescr Rebalanced = "after rebalancing"
descrData :: Level -> [String]
descrData GroupLvl {} = map snd groupData
descrData ClusterLvl = map snd clusterData
phaseLevelDescr :: Phase -> Level -> String
phaseLevelDescr phase (GroupLvl name) =
printf "Statistics for group %s %s\n" name $ phaseDescr phase
phaseLevelDescr phase ClusterLvl =
printf "Cluster statistics %s\n" $ phaseDescr phase
printKeysHTC :: [(String, String)] -> IO ()
printKeysHTC = printKeys htcPrefix
printBool :: Bool
-> Bool
-> String
printBool True True = "1"
printBool True False = "0"
printBool False b = show b
printGroupsMappings :: Group.List -> IO ()
printGroupsMappings gl = do
let extract_vals g = (printf "GROUP_UUID_%d" $ Group.idx g :: String,
Group.uuid g)
printpairs = map extract_vals (Container.elems gl)
printKeysHTC printpairs
prepareKey :: Level -> Phase -> String -> String
prepareKey level@ClusterLvl phase suffix =
printf "%s_%s_%s" (phasePrefix phase) (levelPrefix level) suffix
prepareKey level@(GroupLvl idx) phase suffix =
printf "%s_%s_%s_%s" (phasePrefix phase) (levelPrefix level) idx suffix
printStats :: Int
-> Bool
-> Level
-> Phase
-> [String]
-> IO ()
printStats _ True level phase values = do
let keys = map (prepareKey level phase) (keysData level)
printKeysHTC $ zip keys values
printStats verbose False level phase values = do
let prefix = phaseLevelDescr phase level
descr = descrData level
unless (verbose < 1) $ do
putStrLn ""
putStr prefix
mapM_ (uncurry (printf " %s: %s\n")) (zip descr values)
extractGroupData :: Bool -> Group.Group -> String
extractGroupData True grp = show $ Group.idx grp
extractGroupData False grp = Group.name grp
prepareGroupValues :: [Int] -> Double -> [String]
prepareGroupValues stats score =
map show stats ++ [printf "%.8f" score]
prepareClusterValues :: Bool -> [Int] -> [Bool] -> [String]
prepareClusterValues machineread stats bstats =
map show stats ++ map (printBool machineread) bstats
printGroupStats :: Int -> Bool -> Phase -> GroupStats -> IO ()
printGroupStats verbose machineread phase ((grp, score), stats) = do
let values = prepareGroupValues stats score
extradata = extractGroupData machineread grp
printStats verbose machineread (GroupLvl extradata) phase values
printClusterStats :: Int -> Bool -> Phase -> [Int] -> Bool -> IO ()
printClusterStats verbose machineread phase stats needhbal = do
let values = prepareClusterValues machineread stats [needhbal]
printStats verbose machineread ClusterLvl phase values
clusterNeedsRebalance :: [Int] -> Bool
clusterNeedsRebalance stats = sum stats > 0
perGroupChecks :: Group.List -> GroupInfo -> GroupStats
perGroupChecks gl (gidx, (nl, il)) =
let grp = Container.find gidx gl
offnl = filter Node.offline (Container.elems nl)
n1violated = length . fst $ Cluster.computeBadItems nl il
conflicttags = length $ filter (>0)
(map Node.conflictingPrimaries (Container.elems nl))
offline_pri = sum . map length $ map Node.pList offnl
offline_sec = length $ map Node.sList offnl
score = Cluster.compCV nl
groupstats = [ n1violated
, conflicttags
, offline_pri
, offline_sec
]
in ((grp, score), groupstats)
executeSimulation :: Options -> Cluster.Table -> Double
-> Gdx -> Node.List -> Instance.List
-> IO GroupInfo
executeSimulation opts ini_tbl min_cv gidx nl il = do
let imlen = maximum . map (length . Instance.alias) $ Container.elems il
nmlen = maximum . map (length . Node.alias) $ Container.elems nl
(fin_tbl, _) <- Hbal.iterateDepth False ini_tbl
(optMaxLength opts)
(optDiskMoves opts)
(optInstMoves opts)
False
nmlen imlen [] min_cv
(optMinGainLim opts) (optMinGain opts)
(optEvacMode opts)
let (Cluster.Table fin_nl fin_il _ _) = fin_tbl
return (gidx, (fin_nl, fin_il))
maybeSimulateGroupRebalance :: Options -> GroupInfo -> IO GroupInfo
maybeSimulateGroupRebalance opts (gidx, (nl, il)) = do
let ini_cv = Cluster.compCV nl
ini_tbl = Cluster.Table nl il ini_cv []
min_cv = optMinScore opts
if ini_cv < min_cv
then return (gidx, (nl, il))
else executeSimulation opts ini_tbl min_cv gidx nl il
maybeSimulateRebalance :: Bool
-> Options
-> [GroupInfo]
-> IO [GroupInfo]
maybeSimulateRebalance True opts cluster =
mapM (maybeSimulateGroupRebalance opts) cluster
maybeSimulateRebalance False _ cluster = return cluster
printFinalHTC :: Bool -> IO ()
printFinalHTC = printFinal htcPrefix
main :: Options -> [String] -> IO ()
main opts args = do
unless (null args) $ exitErr "This program doesn't take any arguments."
let verbose = optVerbose opts
machineread = optMachineReadable opts
nosimulation = optNoSimulation opts
(ClusterData gl fixed_nl ilf _ _) <- loadExternalData opts
nlf <- setNodeStatus opts fixed_nl
let splitcluster = Cluster.splitCluster nlf ilf
when machineread $ printGroupsMappings gl
let groupsstats = map (perGroupChecks gl) splitcluster
clusterstats = map sum . transpose . map snd $ groupsstats
needrebalance = clusterNeedsRebalance clusterstats
unless (verbose < 1 || machineread) .
putStrLn $ if nosimulation
then "Running in no-simulation mode."
else if needrebalance
then "Cluster needs rebalancing."
else "No need to rebalance cluster, no problems found."
mapM_ (printGroupStats verbose machineread Initial) groupsstats
printClusterStats verbose machineread Initial clusterstats needrebalance
let exitOK = nosimulation || not needrebalance
simulate = not nosimulation && needrebalance
rebalancedcluster <- maybeSimulateRebalance simulate opts splitcluster
when (simulate || machineread) $ do
let newgroupstats = map (perGroupChecks gl) rebalancedcluster
newclusterstats = map sum . transpose . map snd $ newgroupstats
newneedrebalance = clusterNeedsRebalance clusterstats
mapM_ (printGroupStats verbose machineread Rebalanced) newgroupstats
printClusterStats verbose machineread Rebalanced newclusterstats
newneedrebalance
printFinalHTC machineread
unless exitOK . exitWith $ ExitFailure 1