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