Statistics
| Branch: | Tag: | Revision:

root / src / Ganeti / DataCollectors / Lv.hs @ 1a9d864c

History | View | Annotate | Download (3.7 kB)

1
{-| Logical Volumes data collector.
2

    
3
-}
4

    
5
{-
6

    
7
Copyright (C) 2013 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.DataCollectors.Lv
27
  ( main
28
  , options
29
  , arguments
30
  , dcName
31
  , dcVersion
32
  , dcFormatVersion
33
  , dcCategory
34
  , dcKind
35
  , dcReport
36
  ) where
37

    
38

    
39
import qualified Control.Exception as E
40
import Control.Monad
41
import Data.Attoparsec.Text.Lazy as A
42
import Data.Text.Lazy (pack, unpack)
43
import System.Process
44
import qualified Text.JSON as J
45

    
46
import qualified Ganeti.BasicTypes as BT
47
import Ganeti.Common
48
import Ganeti.DataCollectors.CLI
49
import Ganeti.DataCollectors.Types
50
import Ganeti.Storage.Lvm.LVParser
51
import Ganeti.Utils
52

    
53

    
54
-- | The default setting for the maximum amount of not parsed character to
55
-- print in case of error.
56
-- It is set to use most of the screen estate on a standard 80x25 terminal.
57
-- TODO: add the possibility to set this with a command line parameter.
58
defaultCharNum :: Int
59
defaultCharNum = 80*20
60

    
61
-- | The name of this data collector.
62
dcName :: String
63
dcName = "lv"
64

    
65
-- | The version of this data collector.
66
dcVersion :: DCVersion
67
dcVersion = DCVerBuiltin
68

    
69
-- | The version number for the data format of this data collector.
70
dcFormatVersion :: Int
71
dcFormatVersion = 1
72

    
73
-- | The category of this data collector.
74
dcCategory :: Maybe DCCategory
75
dcCategory = Just DCStorage
76

    
77
-- | The kind of this data collector.
78
dcKind :: DCKind
79
dcKind = DCKPerf
80

    
81
-- | The data exported by the data collector, taken from the default location.
82
dcReport :: IO DCReport
83
dcReport = buildDCReport Nothing
84

    
85
-- * Command line options
86

    
87
options :: IO [OptType]
88
options =
89
  return
90
    [ oInputFile
91
    ]
92

    
93
-- | The list of arguments supported by the program.
94
arguments :: [ArgCompletion]
95
arguments = [ArgCompletion OptComplFile 0 (Just 0)]
96

    
97
-- | This function computes the JSON representation of the LV status
98
buildJsonReport :: Maybe FilePath -> IO J.JSValue
99
buildJsonReport inputFile = do
100
  let (cmd:params) = lvCommand
101
      fromLvs =
102
        ((E.try $ readProcess cmd params "") :: IO (Either IOError String)) >>=
103
          exitIfBad "running command" . either (BT.Bad . show) BT.Ok
104
  contents <-
105
    maybe fromLvs (\fn -> ((E.try $ readFile fn) :: IO (Either IOError String))
106
      >>= exitIfBad "reading from file" . either (BT.Bad . show) BT.Ok)
107
      inputFile
108
  lvInfo <-
109
    case A.parse lvParser $ pack contents of
110
      A.Fail unparsedText contexts errorMessage -> exitErr $
111
        show (Prelude.take defaultCharNum $ unpack unparsedText) ++ "\n"
112
          ++ show contexts ++ "\n" ++ errorMessage
113
      A.Done _ lvinfoD -> return lvinfoD
114
  return $ J.showJSON lvInfo
115

    
116
-- | This function computes the DCReport for the logical volumes.
117
buildDCReport :: Maybe FilePath -> IO DCReport
118
buildDCReport inputFile =
119
  buildJsonReport inputFile >>=
120
    buildReport dcName dcVersion dcFormatVersion dcCategory dcKind
121

    
122
-- | Main function.
123
main :: Options -> [String] -> IO ()
124
main opts args = do
125
  unless (null args) . exitErr $ "This program takes exactly zero" ++
126
                                 " arguments, got '" ++ unwords args ++ "'"
127
  report <- buildDCReport $ optInputFile opts
128

    
129
  putStrLn $ J.encode report