Statistics
| Branch: | Tag: | Revision:

root / htools / Ganeti / DataCollectors / Drbd.hs @ 55abd2c7

History | View | Annotate | Download (2.4 kB)

1
{-| DRBD data collector.
2

    
3
-}
4

    
5
{-
6

    
7
Copyright (C) 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.DataCollectors.Drbd
27
  ( main
28
  , options
29
  , arguments
30
  ) where
31

    
32

    
33
import qualified Control.Exception as E
34
import Data.Attoparsec.Text.Lazy as A
35
import Data.Text.Lazy (pack, unpack)
36
import Text.JSON
37

    
38
import qualified Ganeti.BasicTypes as BT
39
import qualified Ganeti.Constants as C
40
import Ganeti.Block.Drbd.Parser(drbdStatusParser)
41
import Ganeti.Common
42
import Ganeti.DataCollectors.CLI
43
import Ganeti.Utils
44

    
45

    
46
-- | The default path of the DRBD status file.
47
-- It is hardcoded because it is not likely to change.
48
defaultFile :: FilePath
49
defaultFile = C.drbdStatusFile
50

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

    
58
options :: IO [OptType]
59
options = return []
60

    
61
-- | The list of arguments supported by the program.
62
arguments :: [ArgCompletion]
63
arguments = [ArgCompletion OptComplFile 0 (Just 1)]
64

    
65

    
66
-- * Command line options
67

    
68
-- | Main function.
69
main :: Options -> [String] -> IO ()
70
main _ args = do
71
  contents <-
72
    ((E.try . readFile $ getInputPath args) :: IO (Either IOError String)) >>=
73
      exitIfBad "Error reading from file" . either (BT.Bad . show) BT.Ok
74
  output <-
75
    case A.parse drbdStatusParser $ pack contents of
76
      A.Fail unparsedText contexts errorMessage -> exitErr $
77
        show (Prelude.take defaultCharNum $ unpack unparsedText) ++ "\n"
78
          ++ show contexts ++ "\n" ++ errorMessage
79
      A.Done _ drbdStatus -> return $ encode drbdStatus
80
  putStrLn output
81
  where getInputPath a =
82
          if null a
83
            then defaultFile
84
            else head a