Statistics
| Branch: | Tag: | Revision:

root / src / Ganeti / DataCollectors / Drbd.hs @ 834dc290

History | View | Annotate | Download (4.4 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
  , dcFormatVersion
33
  ) where
34

    
35

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

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

    
55

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

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

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

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

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

    
80
-- * Command line options
81

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

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

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

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

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