Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (4.2 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 Control.Monad
35
import Data.Attoparsec.Text.Lazy as A
36
import Data.Maybe
37
import Data.Text.Lazy (pack, unpack)
38
import Network.BSD (getHostName)
39
import qualified Text.JSON as J
40

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

    
52

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

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

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

    
69
-- | The version number for the data format of this data collector.
70
dcFormatVersion :: Int
71
dcFormatVersion = 1
72

    
73
options :: IO [OptType]
74
options =
75
  return
76
    [ oDrbdStatus
77
    , oDrbdPairing
78
    ]
79

    
80
-- | The list of arguments supported by the program.
81
arguments :: [ArgCompletion]
82
arguments = [ArgCompletion OptComplFile 0 (Just 0)]
83

    
84
-- * Command line options
85

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

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

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