Allow overriding the field list in -p
[ganeti-local] / Ganeti / HTools / Cluster.hs
index d13f6c8..9a94d0b 100644 (file)
@@ -125,27 +125,11 @@ computeBadItems nl il =
   in
     (bad_nodes, bad_instances)
 
+-- | Zero-initializer for the CStats type
 emptyCStats :: CStats
-emptyCStats = CStats { csFmem = 0
-                     , csFdsk = 0
-                     , csAmem = 0
-                     , csAdsk = 0
-                     , csAcpu = 0
-                     , csMmem = 0
-                     , csMdsk = 0
-                     , csMcpu = 0
-                     , csImem = 0
-                     , csIdsk = 0
-                     , csIcpu = 0
-                     , csTmem = 0
-                     , csTdsk = 0
-                     , csTcpu = 0
-                     , csXmem = 0
-                     , csNmem = 0
-                     , csScore = 0
-                     , csNinst = 0
-                     }
+emptyCStats = CStats 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
 
+-- | Update stats with data from a new node
 updateCStats :: CStats -> Node.Node -> CStats
 updateCStats cs node =
     let CStats { csFmem = x_fmem, csFdsk = x_fdsk,
@@ -211,23 +195,30 @@ compDetailedCV nl =
         (offline, nodes) = partition Node.offline all_nodes
         mem_l = map Node.pMem nodes
         dsk_l = map Node.pDsk nodes
+        -- metric: memory covariance
         mem_cv = varianceCoeff mem_l
+        -- metric: disk covariance
         dsk_cv = varianceCoeff dsk_l
         n1_l = length $ filter Node.failN1 nodes
+        -- metric: ratio of failN1 nodes
         n1_score = fromIntegral n1_l /
                    fromIntegral (length nodes)::Double
         res_l = map Node.pRem nodes
+        -- metric: reserved memory covariance
         res_cv = varianceCoeff res_l
         offline_inst = sum . map (\n -> (length . Node.pList $ n) +
                                         (length . Node.sList $ n)) $ offline
         online_inst = sum . map (\n -> (length . Node.pList $ n) +
                                        (length . Node.sList $ n)) $ nodes
+        -- metric: ratio of instances on offline nodes
         off_score = if offline_inst == 0
                     then 0::Double
                     else fromIntegral offline_inst /
                          fromIntegral (offline_inst + online_inst)::Double
         cpu_l = map Node.pCpu nodes
+        -- metric: covariance of vcpu/pcpu ratio
         cpu_cv = varianceCoeff cpu_l
+        -- metrics: covariance of cpu, memory, disk and network load
         (c_load, m_load, d_load, n_load) = unzip4 $
             map (\n ->
                      let DynUtil c1 m1 d1 n1 = Node.utilLoad n
@@ -565,8 +556,7 @@ tryReloc _ _ _ reqn _  = fail $ "Unsupported number of relocation \
 -- | Given the original and final nodes, computes the relocation description.
 computeMoves :: Instance.Instance -- ^ The instance to be moved
              -> String -- ^ The instance name
-             -> String -- ^ Original primary
-             -> String -- ^ Original secondary
+             -> IMove  -- ^ The move being performed
              -> String -- ^ New primary
              -> String -- ^ New secondary
              -> (String, [String])
@@ -574,27 +564,13 @@ computeMoves :: Instance.Instance -- ^ The instance to be moved
                 -- either @/f/@ for failover or @/r:name/@ for replace
                 -- secondary, while the command list holds gnt-instance
                 -- commands (without that prefix), e.g \"@failover instance1@\"
-computeMoves i inam a b c d
-    -- same primary
-    | c == a =
-        if d == b
-        then {- Same sec??! -} ("-", [])
-        else {- Change of secondary -}
-            (printf "r:%s" d, [rep d])
-    -- failover and ...
-    | c == b =
-        if d == a
-        then {- that's all -} ("f", [mig])
-        else (printf "f r:%s" d, [mig, rep d])
-    -- ... and keep primary as secondary
-    | d == a =
-        (printf "r:%s f" c, [rep c, mig])
-    -- ... keep same secondary
-    | d == b =
-        (printf "f r:%s f" c, [mig, rep c, mig])
-    -- nothing in common -
-    | otherwise =
-        (printf "r:%s f r:%s" c d, [rep c, mig, rep d])
+computeMoves i inam mv c d =
+    case mv of
+      Failover -> ("f", [mig])
+      FailoverAndReplace _ -> (printf "f r:%s" d, [mig, rep d])
+      ReplaceSecondary _ -> (printf "r:%s" d, [rep d])
+      ReplaceAndFailover _ -> (printf "r:%s f" c, [rep c, mig])
+      ReplacePrimary _ -> (printf "f r:%s f" c, [mig, rep c, mig])
     where morf = if Instance.running i then "migrate" else "failover"
           mig = printf "%s -f %s" morf inam::String
           rep n = printf "replace-disks -n %s %s" n inam
@@ -611,14 +587,14 @@ printSolutionLine :: Node.List     -- ^ The node list
 printSolutionLine nl il nmlen imlen plc pos =
     let
         pmlen = (2*nmlen + 1)
-        (i, p, s, _, c) = plc
+        (i, p, s, mv, c) = plc
         inst = Container.find i il
         inam = Instance.name inst
         npri = Container.nameOf nl p
         nsec = Container.nameOf nl s
         opri = Container.nameOf nl $ Instance.pNode inst
         osec = Container.nameOf nl $ Instance.sNode inst
-        (moves, cmds) =  computeMoves inst inam opri osec npri nsec
+        (moves, cmds) =  computeMoves inst inam mv npri nsec
         ostr = printf "%s:%s" opri osec::String
         nstr = printf "%s:%s" npri nsec::String
     in
@@ -683,30 +659,39 @@ printSolution nl il sol =
       unzip $ zipWith (printSolutionLine nl il nmlen imlen) sol [1..]
 
 -- | Print the node list.
-printNodes :: Node.List -> String
-printNodes nl =
-    let snl = sortBy (compare `on` Node.idx) (Container.elems nl)
-        header = ["F", "Name"
-                 , "t_mem", "n_mem", "i_mem", "x_mem", "f_mem", "r_mem"
-                 , "t_dsk", "f_dsk", "pcpu", "vcpu", "pri",  "sec"
-                 , "p_fmem", "p_fdsk", "r_cpu"
-                 , "lCpu", "lMem", "lDsk", "lNet" ]
-        isnum = False:False:repeat True
+printNodes :: Node.List -> [String] -> String
+printNodes nl fs =
+    let fields = if null fs
+                 then Node.defaultFields
+                 else fs
+        snl = sortBy (compare `on` Node.idx) (Container.elems nl)
+        (header, isnum) = unzip $ map Node.showHeader fields
     in unlines . map ((:) ' ' .  intercalate " ") $
-       formatTable (header:map Node.list snl) isnum
+       formatTable (header:map (Node.list fields) snl) isnum
 
 -- | Print the instance list.
 printInsts :: Node.List -> Instance.List -> String
 printInsts nl il =
     let sil = sortBy (compare `on` Instance.idx) (Container.elems il)
-        helper inst = [ (Instance.name inst)
-                      , (Container.nameOf nl (Instance.pNode inst))
+        helper inst = [ if Instance.running inst then "R" else " "
+                      , Instance.name inst
+                      , Container.nameOf nl (Instance.pNode inst)
                       , (let sdx = Instance.sNode inst
                          in if sdx == Node.noSecondary
                             then  ""
-                            else Container.nameOf nl sdx) ]
-        header = ["Name", "Pri_node", "Sec_node"]
-        isnum = repeat False
+                            else Container.nameOf nl sdx)
+                      , printf "%3d" $ Instance.vcpus inst
+                      , printf "%5d" $ Instance.mem inst
+                      , printf "%5d" $ Instance.dsk inst `div` 1024
+                      , printf "%5.3f" lC
+                      , printf "%5.3f" lM
+                      , printf "%5.3f" lD
+                      , printf "%5.3f" lN
+                      ]
+            where DynUtil lC lM lD lN = Instance.util inst
+        header = [ "F", "Name", "Pri_node", "Sec_node", "vcpu", "mem"
+                 , "dsk", "lCpu", "lMem", "lDsk", "lNet" ]
+        isnum = False:False:False:False:repeat True
     in unlines . map ((:) ' ' . intercalate " ") $
        formatTable (header:map helper sil) isnum