From 22b16dd08f6699c1d253bde00c7581ce60d10b28 Mon Sep 17 00:00:00 2001
From: Agata Murawska <agatamurawska@google.com>
Date: Thu, 21 Jun 2012 14:11:02 +0200
Subject: [PATCH] Machine readable hcheck functionality
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Machine readable output for hcheck.

Signed-off-by: Agata Murawska <agatamurawska@google.com>
Reviewed-by: RenΓ© Nussbaumer <rn@google.com>
---
 htools/Ganeti/HTools/Program/Hcheck.hs | 37 ++++++++++++++++++++++++--
 1 file changed, 35 insertions(+), 2 deletions(-)

diff --git a/htools/Ganeti/HTools/Program/Hcheck.hs b/htools/Ganeti/HTools/Program/Hcheck.hs
index 7b7df8c15..6ce561fbd 100644
--- a/htools/Ganeti/HTools/Program/Hcheck.hs
+++ b/htools/Ganeti/HTools/Program/Hcheck.hs
@@ -76,15 +76,29 @@ options =
 data Phase = Initial
            | Rebalanced
 
+-- | Level of presented statistics.
+data Level = GroupLvl
+           | ClusterLvl
+
 -- | Prefix for machine readable names
 htcPrefix :: String
 htcPrefix = "HCHECK"
 
+-- | Phase-specific prefix for machine readable version.
+phasePrefix :: Phase -> String
+phasePrefix Initial = "INIT"
+phasePrefix Rebalanced = "FINAL"
+
 -- | Description of phases for human readable version.
 phaseDescription :: Phase -> String
 phaseDescription Initial = "initially"
 phaseDescription Rebalanced = "after rebalancing"
 
+-- | Level-specific prefix for machine readable version.
+levelPrefix :: Level -> String
+levelPrefix GroupLvl = "GROUP"
+levelPrefix ClusterLvl = "CLUSTER"
+
 -- | Data showed both per group and per cluster.
 commonData :: [(String, String)]
 commonData =[ ("N1_FAIL", "Nodes not N+1 happy")
@@ -101,9 +115,21 @@ groupData = commonData ++ [("SCORE", "Group score")]
 clusterData :: [(String, String)]
 clusterData = commonData ++ [("NEED_REBALANCE", "Cluster is not healthy")]
 
+-- | Format a list of key, value as a shell fragment.
+printKeysHTC :: [(String, String)] -> IO ()
+printKeysHTC = printKeys htcPrefix
+
 -- | Print all the statistics on a group level.
 printGroupStats :: Int -> Bool -> Phase -> Gdx -> [Int] -> Double -> IO ()
-printGroupStats _ True _ _ _ _  = return ()
+printGroupStats _ True phase gidx stats score = do
+  let printstats = map (printf "%d") stats ++ [printf "%.8f" score] :: [String]
+      printkeys = map (printf "%s_%s_%d_%s"
+                                  (phasePrefix phase)
+                                  (levelPrefix GroupLvl)
+                                  gidx)
+                       (map fst groupData) :: [String]
+  printKeysHTC (zip printkeys printstats)
+
 printGroupStats verbose False phase gidx stats score = do
   let printstats = map (printf "%d") stats ++ [printf "%.8f" score] :: [String]
 
@@ -115,8 +141,15 @@ printGroupStats verbose False phase gidx stats score = do
 
 -- | Print all the statistics on a cluster (global) level.
 printClusterStats :: Int -> Bool -> Phase -> [Int] -> IO (Bool)
-printClusterStats _ True _ stats = do
+printClusterStats _ True phase stats = do
   let needrebal = sum stats
+      printstats = map (printf "%d") $ stats ++ [needrebal]
+                 :: [String]
+      printkeys = map (printf "%s_%s_%s"
+                              (phasePrefix phase)
+                              (levelPrefix ClusterLvl))
+                      (map fst clusterData) :: [String]
+  printKeysHTC (zip printkeys printstats)
   return $ needrebal > 0
 
 printClusterStats verbose False phase stats = do
-- 
GitLab