root / htools / Ganeti / HTools / Program / Hscan.hs @ 22278fa7
History | View | Annotate | Download (4.9 kB)
1 |
{-| Scan clusters via RAPI or LUXI and write state data files. |
---|---|
2 |
|
3 |
-} |
4 |
|
5 |
{- |
6 |
|
7 |
Copyright (C) 2009, 2010, 2011, 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.Hscan |
27 |
( main |
28 |
, options |
29 |
, arguments |
30 |
) where |
31 |
|
32 |
import Control.Monad |
33 |
import Data.Maybe (isJust, fromJust, fromMaybe) |
34 |
import System.Exit |
35 |
import System.IO |
36 |
import System.FilePath |
37 |
|
38 |
import Text.Printf (printf) |
39 |
|
40 |
import qualified Ganeti.HTools.Container as Container |
41 |
import qualified Ganeti.HTools.Cluster as Cluster |
42 |
import qualified Ganeti.HTools.Node as Node |
43 |
import qualified Ganeti.HTools.Instance as Instance |
44 |
import qualified Ganeti.HTools.Rapi as Rapi |
45 |
import qualified Ganeti.HTools.Luxi as Luxi |
46 |
import qualified Ganeti.Path as Path |
47 |
import Ganeti.HTools.Loader (checkData, mergeData, ClusterData(..)) |
48 |
import Ganeti.HTools.Text (serializeCluster) |
49 |
|
50 |
import Ganeti.Common |
51 |
import Ganeti.HTools.CLI |
52 |
import Ganeti.HTools.Types |
53 |
|
54 |
-- | Options list and functions. |
55 |
options :: [OptType] |
56 |
options = |
57 |
[ oPrintNodes |
58 |
, oOutputDir |
59 |
, oLuxiSocket |
60 |
, oVerbose |
61 |
, oNoHeaders |
62 |
] |
63 |
|
64 |
-- | The list of arguments supported by the program. |
65 |
arguments :: [ArgCompletion] |
66 |
arguments = [ArgCompletion OptComplHost 0 Nothing] |
67 |
|
68 |
-- | Return a one-line summary of cluster state. |
69 |
printCluster :: Node.List -> Instance.List |
70 |
-> String |
71 |
printCluster nl il = |
72 |
let (bad_nodes, bad_instances) = Cluster.computeBadItems nl il |
73 |
ccv = Cluster.compCV nl |
74 |
nodes = Container.elems nl |
75 |
insts = Container.elems il |
76 |
t_ram = sum . map Node.tMem $ nodes |
77 |
t_dsk = sum . map Node.tDsk $ nodes |
78 |
f_ram = sum . map Node.fMem $ nodes |
79 |
f_dsk = sum . map Node.fDsk $ nodes |
80 |
in printf "%5d %5d %5d %5d %6.0f %6d %6.0f %6d %.8f" |
81 |
(length nodes) (length insts) |
82 |
(length bad_nodes) (length bad_instances) |
83 |
t_ram f_ram (t_dsk / 1024) (f_dsk `div` 1024) ccv |
84 |
|
85 |
-- | Replace slashes with underscore for saving to filesystem. |
86 |
fixSlash :: String -> String |
87 |
fixSlash = map (\x -> if x == '/' then '_' else x) |
88 |
|
89 |
-- | Generates serialized data from loader input. |
90 |
processData :: ClusterData -> Result ClusterData |
91 |
processData input_data = do |
92 |
cdata@(ClusterData _ nl il _ _) <- mergeData [] [] [] [] input_data |
93 |
let (_, fix_nl) = checkData nl il |
94 |
return cdata { cdNodes = fix_nl } |
95 |
|
96 |
-- | Writes cluster data out. |
97 |
writeData :: Int |
98 |
-> String |
99 |
-> Options |
100 |
-> Result ClusterData |
101 |
-> IO Bool |
102 |
writeData _ name _ (Bad err) = |
103 |
printf "\nError for %s: failed to load data. Details:\n%s\n" name err >> |
104 |
return False |
105 |
|
106 |
writeData nlen name opts (Ok cdata) = do |
107 |
let fixdata = processData cdata |
108 |
case fixdata of |
109 |
Bad err -> printf "\nError for %s: failed to process data. Details:\n%s\n" |
110 |
name err >> return False |
111 |
Ok processed -> writeDataInner nlen name opts cdata processed |
112 |
|
113 |
-- | Inner function for writing cluster data to disk. |
114 |
writeDataInner :: Int |
115 |
-> String |
116 |
-> Options |
117 |
-> ClusterData |
118 |
-> ClusterData |
119 |
-> IO Bool |
120 |
writeDataInner nlen name opts cdata fixdata = do |
121 |
let (ClusterData _ nl il _ _) = fixdata |
122 |
printf "%-*s " nlen name :: IO () |
123 |
hFlush stdout |
124 |
let shownodes = optShowNodes opts |
125 |
odir = optOutPath opts |
126 |
oname = odir </> fixSlash name |
127 |
putStrLn $ printCluster nl il |
128 |
hFlush stdout |
129 |
when (isJust shownodes) . |
130 |
putStr $ Cluster.printNodes nl (fromJust shownodes) |
131 |
writeFile (oname <.> "data") (serializeCluster cdata) |
132 |
return True |
133 |
|
134 |
-- | Main function. |
135 |
main :: Options -> [String] -> IO () |
136 |
main opts clusters = do |
137 |
let local = "LOCAL" |
138 |
|
139 |
let nlen = if null clusters |
140 |
then length local |
141 |
else maximum . map length $ clusters |
142 |
|
143 |
unless (optNoHeaders opts) $ |
144 |
printf "%-*s %5s %5s %5s %5s %6s %6s %6s %6s %10s\n" nlen |
145 |
"Name" "Nodes" "Inst" "BNode" "BInst" "t_mem" "f_mem" |
146 |
"t_disk" "f_disk" "Score" |
147 |
|
148 |
when (null clusters) $ do |
149 |
let lsock = fromMaybe Path.defaultLuxiSocket (optLuxi opts) |
150 |
let name = local |
151 |
input_data <- Luxi.loadData lsock |
152 |
result <- writeData nlen name opts input_data |
153 |
unless result . exitWith $ ExitFailure 2 |
154 |
|
155 |
results <- mapM (\name -> Rapi.loadData name >>= writeData nlen name opts) |
156 |
clusters |
157 |
unless (all id results) $ exitWith (ExitFailure 2) |