Statistics
| Branch: | Tag: | Revision:

root / src / Ganeti / DataCollectors / Drbd.hs @ a895fa19

History | View | Annotate | Download (4.3 kB)

1
{-| DRBD data collector.
2

    
3
-}
4

    
5
{-
6

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

    
34

    
35
import qualified Control.Exception as E
36
import Control.Monad
37
import Data.Attoparsec.Text.Lazy as A
38
import Data.Maybe
39
import Data.Text.Lazy (pack, unpack)
40
import Network.BSD (getHostName)
41
import qualified Text.JSON as J
42

    
43
import qualified Ganeti.BasicTypes as BT
44
import qualified Ganeti.Constants as C
45
import Ganeti.Block.Drbd.Parser(drbdStatusParser)
46
import Ganeti.Block.Drbd.Types(DrbdInstMinor)
47
import Ganeti.Common
48
import Ganeti.Confd.Client
49
import Ganeti.Confd.Types
50
import Ganeti.DataCollectors.CLI
51
import Ganeti.DataCollectors.Types
52
import Ganeti.Utils
53

    
54

    
55
-- | The default path of the DRBD status file.
56
-- It is hardcoded because it is not likely to change.
57
defaultFile :: FilePath
58
defaultFile = C.drbdStatusFile
59

    
60
-- | The default setting for the maximum amount of not parsed character to
61
-- print in case of error.
62
-- It is set to use most of the screen estate on a standard 80x25 terminal.
63
-- TODO: add the possibility to set this with a command line parameter.
64
defaultCharNum :: Int
65
defaultCharNum = 80*20
66

    
67
-- | The name of this data collector.
68
dcName :: String
69
dcName = "drbd"
70

    
71
-- | The version of this data collector.
72
dcVersion :: DCVersion
73
dcVersion = DCVerBuiltin
74

    
75
-- | The version number for the data format of this data collector.
76
dcFormatVersion :: Int
77
dcFormatVersion = 1
78

    
79
-- * Command line options
80

    
81
options :: IO [OptType]
82
options =
83
  return
84
    [ oDrbdStatus
85
    , oDrbdPairing
86
    ]
87

    
88
-- | The list of arguments supported by the program.
89
arguments :: [ArgCompletion]
90
arguments = [ArgCompletion OptComplFile 0 (Just 0)]
91

    
92
-- | Get information about the pairing of DRBD minors and Ganeti instances
93
-- on the current node. The information is taken from the Confd client
94
-- or, if a filename is specified, from a JSON encoded file (for testing
95
-- purposes).
96
getPairingInfo :: Maybe String -> IO (BT.Result [DrbdInstMinor])
97
getPairingInfo Nothing = do
98
  curNode <- getHostName
99
  client <- getConfdClient Nothing Nothing
100
  reply <- query client ReqNodeDrbd $ PlainQuery curNode
101
  return $
102
    case fmap (J.readJSONs . confdReplyAnswer) reply of
103
      Just (J.Ok instMinor) -> BT.Ok instMinor
104
      Just (J.Error msg) -> BT.Bad msg
105
      Nothing -> BT.Bad "No answer from the Confd server"
106
getPairingInfo (Just filename) = do
107
  content <- readFile filename
108
  return $
109
    case J.decode content of
110
      J.Ok instMinor -> BT.Ok instMinor
111
      J.Error msg -> BT.Bad msg
112

    
113
-- | This function builds a report with the DRBD status.
114
buildDRBDReport :: FilePath -> Maybe FilePath -> IO DCReport
115
buildDRBDReport statusFile pairingFile = do
116
  contents <-
117
    ((E.try $ readFile statusFile) :: IO (Either IOError String)) >>=
118
      exitIfBad "reading from file" . either (BT.Bad . show) BT.Ok
119
  pairingResult <- getPairingInfo pairingFile
120
  pairing <- exitIfBad "Can't get pairing info" pairingResult
121
  jsonData <-
122
    case A.parse (drbdStatusParser pairing) $ pack contents of
123
      A.Fail unparsedText contexts errorMessage -> exitErr $
124
        show (Prelude.take defaultCharNum $ unpack unparsedText) ++ "\n"
125
          ++ show contexts ++ "\n" ++ errorMessage
126
      A.Done _ drbdStatus -> return $ J.showJSON drbdStatus
127
  buildReport dcName dcVersion dcFormatVersion jsonData
128

    
129
-- | Main function.
130
main :: Options -> [String] -> IO ()
131
main opts args = do
132
  let statusFile = fromMaybe defaultFile $ optDrbdStatus opts
133
      pairingFile = optDrbdPairing opts
134
  unless (null args) . exitErr $ "This program takes exactly zero" ++
135
                                  " arguments, got '" ++ unwords args ++ "'"
136
  report <- buildDRBDReport statusFile pairingFile
137
  putStrLn $ J.encode report