Statistics
| Branch: | Tag: | Revision:

root / src / Ganeti / DataCollectors / Lv.hs @ f22b987a

History | View | Annotate | Download (4 kB)

1 1a9d864c Michele Tartara
{-| Logical Volumes data collector.
2 1a9d864c Michele Tartara
3 1a9d864c Michele Tartara
-}
4 1a9d864c Michele Tartara
5 1a9d864c Michele Tartara
{-
6 1a9d864c Michele Tartara
7 1a9d864c Michele Tartara
Copyright (C) 2013 Google Inc.
8 1a9d864c Michele Tartara
9 1a9d864c Michele Tartara
This program is free software; you can redistribute it and/or modify
10 1a9d864c Michele Tartara
it under the terms of the GNU General Public License as published by
11 1a9d864c Michele Tartara
the Free Software Foundation; either version 2 of the License, or
12 1a9d864c Michele Tartara
(at your option) any later version.
13 1a9d864c Michele Tartara
14 1a9d864c Michele Tartara
This program is distributed in the hope that it will be useful, but
15 1a9d864c Michele Tartara
WITHOUT ANY WARRANTY; without even the implied warranty of
16 1a9d864c Michele Tartara
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17 1a9d864c Michele Tartara
General Public License for more details.
18 1a9d864c Michele Tartara
19 1a9d864c Michele Tartara
You should have received a copy of the GNU General Public License
20 1a9d864c Michele Tartara
along with this program; if not, write to the Free Software
21 1a9d864c Michele Tartara
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
22 1a9d864c Michele Tartara
02110-1301, USA.
23 1a9d864c Michele Tartara
24 1a9d864c Michele Tartara
-}
25 1a9d864c Michele Tartara
26 1a9d864c Michele Tartara
module Ganeti.DataCollectors.Lv
27 1a9d864c Michele Tartara
  ( main
28 1a9d864c Michele Tartara
  , options
29 1a9d864c Michele Tartara
  , arguments
30 1a9d864c Michele Tartara
  , dcName
31 1a9d864c Michele Tartara
  , dcVersion
32 1a9d864c Michele Tartara
  , dcFormatVersion
33 1a9d864c Michele Tartara
  , dcCategory
34 1a9d864c Michele Tartara
  , dcKind
35 1a9d864c Michele Tartara
  , dcReport
36 1a9d864c Michele Tartara
  ) where
37 1a9d864c Michele Tartara
38 1a9d864c Michele Tartara
39 1a9d864c Michele Tartara
import qualified Control.Exception as E
40 1a9d864c Michele Tartara
import Control.Monad
41 1a9d864c Michele Tartara
import Data.Attoparsec.Text.Lazy as A
42 1a9d864c Michele Tartara
import Data.Text.Lazy (pack, unpack)
43 1a9d864c Michele Tartara
import System.Process
44 1a9d864c Michele Tartara
import qualified Text.JSON as J
45 1a9d864c Michele Tartara
46 1a9d864c Michele Tartara
import qualified Ganeti.BasicTypes as BT
47 1a9d864c Michele Tartara
import Ganeti.Common
48 1a9d864c Michele Tartara
import Ganeti.DataCollectors.CLI
49 1a9d864c Michele Tartara
import Ganeti.DataCollectors.Types
50 1a9d864c Michele Tartara
import Ganeti.Storage.Lvm.LVParser
51 f22b987a Michele Tartara
import Ganeti.Storage.Lvm.Types
52 1a9d864c Michele Tartara
import Ganeti.Utils
53 1a9d864c Michele Tartara
54 1a9d864c Michele Tartara
55 1a9d864c Michele Tartara
-- | The default setting for the maximum amount of not parsed character to
56 1a9d864c Michele Tartara
-- print in case of error.
57 1a9d864c Michele Tartara
-- It is set to use most of the screen estate on a standard 80x25 terminal.
58 1a9d864c Michele Tartara
-- TODO: add the possibility to set this with a command line parameter.
59 1a9d864c Michele Tartara
defaultCharNum :: Int
60 1a9d864c Michele Tartara
defaultCharNum = 80*20
61 1a9d864c Michele Tartara
62 1a9d864c Michele Tartara
-- | The name of this data collector.
63 1a9d864c Michele Tartara
dcName :: String
64 1a9d864c Michele Tartara
dcName = "lv"
65 1a9d864c Michele Tartara
66 1a9d864c Michele Tartara
-- | The version of this data collector.
67 1a9d864c Michele Tartara
dcVersion :: DCVersion
68 1a9d864c Michele Tartara
dcVersion = DCVerBuiltin
69 1a9d864c Michele Tartara
70 1a9d864c Michele Tartara
-- | The version number for the data format of this data collector.
71 1a9d864c Michele Tartara
dcFormatVersion :: Int
72 1a9d864c Michele Tartara
dcFormatVersion = 1
73 1a9d864c Michele Tartara
74 1a9d864c Michele Tartara
-- | The category of this data collector.
75 1a9d864c Michele Tartara
dcCategory :: Maybe DCCategory
76 1a9d864c Michele Tartara
dcCategory = Just DCStorage
77 1a9d864c Michele Tartara
78 1a9d864c Michele Tartara
-- | The kind of this data collector.
79 1a9d864c Michele Tartara
dcKind :: DCKind
80 1a9d864c Michele Tartara
dcKind = DCKPerf
81 1a9d864c Michele Tartara
82 1a9d864c Michele Tartara
-- | The data exported by the data collector, taken from the default location.
83 1a9d864c Michele Tartara
dcReport :: IO DCReport
84 1a9d864c Michele Tartara
dcReport = buildDCReport Nothing
85 1a9d864c Michele Tartara
86 1a9d864c Michele Tartara
-- * Command line options
87 1a9d864c Michele Tartara
88 1a9d864c Michele Tartara
options :: IO [OptType]
89 1a9d864c Michele Tartara
options =
90 1a9d864c Michele Tartara
  return
91 1a9d864c Michele Tartara
    [ oInputFile
92 1a9d864c Michele Tartara
    ]
93 1a9d864c Michele Tartara
94 1a9d864c Michele Tartara
-- | The list of arguments supported by the program.
95 1a9d864c Michele Tartara
arguments :: [ArgCompletion]
96 1a9d864c Michele Tartara
arguments = [ArgCompletion OptComplFile 0 (Just 0)]
97 1a9d864c Michele Tartara
98 f22b987a Michele Tartara
-- | Get information about logical volumes from file (if specified) or
99 f22b987a Michele Tartara
-- by actually running the command to get it from a live cluster.
100 f22b987a Michele Tartara
getLvInfo :: Maybe FilePath -> IO [LVInfo]
101 f22b987a Michele Tartara
getLvInfo inputFile = do
102 f5d84060 Michele Tartara
  let cmd = lvCommand
103 f5d84060 Michele Tartara
      params = lvParams
104 1a9d864c Michele Tartara
      fromLvs =
105 1a9d864c Michele Tartara
        ((E.try $ readProcess cmd params "") :: IO (Either IOError String)) >>=
106 1a9d864c Michele Tartara
          exitIfBad "running command" . either (BT.Bad . show) BT.Ok
107 1a9d864c Michele Tartara
  contents <-
108 1a9d864c Michele Tartara
    maybe fromLvs (\fn -> ((E.try $ readFile fn) :: IO (Either IOError String))
109 1a9d864c Michele Tartara
      >>= exitIfBad "reading from file" . either (BT.Bad . show) BT.Ok)
110 1a9d864c Michele Tartara
      inputFile
111 f22b987a Michele Tartara
  case A.parse lvParser $ pack contents of
112 f22b987a Michele Tartara
    A.Fail unparsedText contexts errorMessage -> exitErr $
113 f22b987a Michele Tartara
      show (Prelude.take defaultCharNum $ unpack unparsedText) ++ "\n"
114 f22b987a Michele Tartara
        ++ show contexts ++ "\n" ++ errorMessage
115 f22b987a Michele Tartara
    A.Done _ lvinfoD -> return lvinfoD
116 f22b987a Michele Tartara
117 f22b987a Michele Tartara
-- | This function computes the JSON representation of the LV status
118 f22b987a Michele Tartara
buildJsonReport :: Maybe FilePath -> IO J.JSValue
119 f22b987a Michele Tartara
buildJsonReport inputFile = do
120 f22b987a Michele Tartara
  lvInfo <- getLvInfo inputFile
121 1a9d864c Michele Tartara
  return $ J.showJSON lvInfo
122 1a9d864c Michele Tartara
123 1a9d864c Michele Tartara
-- | This function computes the DCReport for the logical volumes.
124 1a9d864c Michele Tartara
buildDCReport :: Maybe FilePath -> IO DCReport
125 1a9d864c Michele Tartara
buildDCReport inputFile =
126 1a9d864c Michele Tartara
  buildJsonReport inputFile >>=
127 1a9d864c Michele Tartara
    buildReport dcName dcVersion dcFormatVersion dcCategory dcKind
128 1a9d864c Michele Tartara
129 1a9d864c Michele Tartara
-- | Main function.
130 1a9d864c Michele Tartara
main :: Options -> [String] -> IO ()
131 1a9d864c Michele Tartara
main opts args = do
132 1a9d864c Michele Tartara
  unless (null args) . exitErr $ "This program takes exactly zero" ++
133 1a9d864c Michele Tartara
                                 " arguments, got '" ++ unwords args ++ "'"
134 1a9d864c Michele Tartara
  report <- buildDCReport $ optInputFile opts
135 1a9d864c Michele Tartara
136 1a9d864c Michele Tartara
  putStrLn $ J.encode report