Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (4.5 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
  , dcCategory
34
  ) where
35

    
36

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

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

    
56

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

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

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

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

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

    
81
-- | The category of this data collector.
82
dcCategory :: Maybe DCCategory
83
dcCategory = Just DCStorage
84

    
85
-- * Command line options
86

    
87
options :: IO [OptType]
88
options =
89
  return
90
    [ oDrbdStatus
91
    , oDrbdPairing
92
    ]
93

    
94
-- | The list of arguments supported by the program.
95
arguments :: [ArgCompletion]
96
arguments = [ArgCompletion OptComplFile 0 (Just 0)]
97

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

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

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