Statistics
| Branch: | Tag: | Revision:

root / htools / Ganeti / DataCollectors / Drbd.hs @ 638e0a6f

History | View | Annotate | Download (2.6 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 55abd2c7 Iustin Pop
import Ganeti.DataCollectors.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
-- * Command line options
66 332b1340 Michele Tartara
67 332b1340 Michele Tartara
-- | Main function.
68 332b1340 Michele Tartara
main :: Options -> [String] -> IO ()
69 332b1340 Michele Tartara
main _ args = do
70 638e0a6f Iustin Pop
  proc_drbd <- case args of
71 638e0a6f Iustin Pop
                 [ ] -> return defaultFile
72 638e0a6f Iustin Pop
                 [x] -> return x
73 638e0a6f Iustin Pop
                 _   -> exitErr $ "This program takes only one argument," ++
74 638e0a6f Iustin Pop
                                  " got '" ++ unwords args ++ "'"
75 332b1340 Michele Tartara
  contents <-
76 638e0a6f Iustin Pop
    ((E.try $ readFile proc_drbd) :: IO (Either IOError String)) >>=
77 638e0a6f Iustin Pop
      exitIfBad "reading from file" . either (BT.Bad . show) BT.Ok
78 332b1340 Michele Tartara
  output <-
79 332b1340 Michele Tartara
    case A.parse drbdStatusParser $ pack contents of
80 332b1340 Michele Tartara
      A.Fail unparsedText contexts errorMessage -> exitErr $
81 332b1340 Michele Tartara
        show (Prelude.take defaultCharNum $ unpack unparsedText) ++ "\n"
82 332b1340 Michele Tartara
          ++ show contexts ++ "\n" ++ errorMessage
83 332b1340 Michele Tartara
      A.Done _ drbdStatus -> return $ encode drbdStatus
84 332b1340 Michele Tartara
  putStrLn output