Statistics
| Branch: | Tag: | Revision:

root / htools / Ganeti / DataCollectors / Drbd.hs @ 332b1340

History | View | Annotate | Download (2.4 kB)

1 332b1340 Michele Tartara
{-| DRBD data collector.
2 332b1340 Michele Tartara
3 332b1340 Michele Tartara
-}
4 332b1340 Michele Tartara
5 332b1340 Michele Tartara
{-
6 332b1340 Michele Tartara
7 332b1340 Michele Tartara
Copyright (C) 2012 Google Inc.
8 332b1340 Michele Tartara
9 332b1340 Michele Tartara
This program is free software; you can redistribute it and/or modify
10 332b1340 Michele Tartara
it under the terms of the GNU General Public License as published by
11 332b1340 Michele Tartara
the Free Software Foundation; either version 2 of the License, or
12 332b1340 Michele Tartara
(at your option) any later version.
13 332b1340 Michele Tartara
14 332b1340 Michele Tartara
This program is distributed in the hope that it will be useful, but
15 332b1340 Michele Tartara
WITHOUT ANY WARRANTY; without even the implied warranty of
16 332b1340 Michele Tartara
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17 332b1340 Michele Tartara
General Public License for more details.
18 332b1340 Michele Tartara
19 332b1340 Michele Tartara
You should have received a copy of the GNU General Public License
20 332b1340 Michele Tartara
along with this program; if not, write to the Free Software
21 332b1340 Michele Tartara
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
22 332b1340 Michele Tartara
02110-1301, USA.
23 332b1340 Michele Tartara
24 332b1340 Michele Tartara
-}
25 332b1340 Michele Tartara
26 332b1340 Michele Tartara
module Ganeti.DataCollectors.Drbd
27 332b1340 Michele Tartara
  ( main
28 332b1340 Michele Tartara
  , options
29 332b1340 Michele Tartara
  , arguments
30 332b1340 Michele Tartara
  ) where
31 332b1340 Michele Tartara
32 332b1340 Michele Tartara
33 332b1340 Michele Tartara
import qualified Control.Exception as E
34 332b1340 Michele Tartara
import Data.Attoparsec.Text.Lazy as A
35 332b1340 Michele Tartara
import Data.Text.Lazy (pack, unpack)
36 332b1340 Michele Tartara
import Text.JSON
37 332b1340 Michele Tartara
38 332b1340 Michele Tartara
import qualified Ganeti.BasicTypes as BT
39 332b1340 Michele Tartara
import qualified Ganeti.Constants as C
40 332b1340 Michele Tartara
import Ganeti.Block.Drbd.Parser(drbdStatusParser)
41 332b1340 Michele Tartara
import Ganeti.Common
42 332b1340 Michele Tartara
import Ganeti.HTools.CLI
43 332b1340 Michele Tartara
import Ganeti.Utils
44 332b1340 Michele Tartara
45 332b1340 Michele Tartara
46 332b1340 Michele Tartara
-- | The default path of the DRBD status file.
47 332b1340 Michele Tartara
-- It is hardcoded because it is not likely to change.
48 332b1340 Michele Tartara
defaultFile :: FilePath
49 332b1340 Michele Tartara
defaultFile = C.drbdStatusFile
50 332b1340 Michele Tartara
51 332b1340 Michele Tartara
-- | The default setting for the maximum amount of not parsed character to
52 332b1340 Michele Tartara
-- print in case of error.
53 332b1340 Michele Tartara
-- It is set to use most of the screen estate on a standard 80x25 terminal.
54 332b1340 Michele Tartara
-- TODO: add the possibility to set this with a command line parameter.
55 332b1340 Michele Tartara
defaultCharNum :: Int
56 332b1340 Michele Tartara
defaultCharNum = 80*20
57 332b1340 Michele Tartara
58 332b1340 Michele Tartara
options :: IO [OptType]
59 332b1340 Michele Tartara
options = return []
60 332b1340 Michele Tartara
61 332b1340 Michele Tartara
-- | The list of arguments supported by the program.
62 332b1340 Michele Tartara
arguments :: [ArgCompletion]
63 332b1340 Michele Tartara
arguments = [ArgCompletion OptComplFile 0 (Just 1)]
64 332b1340 Michele Tartara
65 332b1340 Michele Tartara
66 332b1340 Michele Tartara
-- * Command line options
67 332b1340 Michele Tartara
68 332b1340 Michele Tartara
-- | Main function.
69 332b1340 Michele Tartara
main :: Options -> [String] -> IO ()
70 332b1340 Michele Tartara
main _ args = do
71 332b1340 Michele Tartara
  contents <-
72 332b1340 Michele Tartara
    ((E.try . readFile $ getInputPath args) :: IO (Either IOError String)) >>=
73 332b1340 Michele Tartara
      exitIfBad "Error reading from file" . either (BT.Bad . show) BT.Ok
74 332b1340 Michele Tartara
  output <-
75 332b1340 Michele Tartara
    case A.parse drbdStatusParser $ pack contents of
76 332b1340 Michele Tartara
      A.Fail unparsedText contexts errorMessage -> exitErr $
77 332b1340 Michele Tartara
        show (Prelude.take defaultCharNum $ unpack unparsedText) ++ "\n"
78 332b1340 Michele Tartara
          ++ show contexts ++ "\n" ++ errorMessage
79 332b1340 Michele Tartara
      A.Done _ drbdStatus -> return $ encode drbdStatus
80 332b1340 Michele Tartara
  putStrLn output
81 332b1340 Michele Tartara
  where getInputPath a =
82 332b1340 Michele Tartara
          if null a
83 332b1340 Michele Tartara
            then defaultFile
84 332b1340 Michele Tartara
            else head a