Statistics
| Branch: | Tag: | Revision:

root / src / Ganeti / DataCollectors / Drbd.hs @ 54c7dff7

History | View | Annotate | Download (4.6 kB)

1 332b1340 Michele Tartara
{-| DRBD data collector.
2 332b1340 Michele Tartara
3 332b1340 Michele Tartara
-}
4 332b1340 Michele Tartara
5 332b1340 Michele Tartara
{-
6 332b1340 Michele Tartara
7 ddceb4c5 Michele Tartara
Copyright (C) 2012, 2013 Google Inc.
8 332b1340 Michele Tartara
9 332b1340 Michele Tartara
This program is free software; you can redistribute it and/or modify
10 332b1340 Michele Tartara
it under the terms of the GNU General Public License as published by
11 332b1340 Michele Tartara
the Free Software Foundation; either version 2 of the License, or
12 332b1340 Michele Tartara
(at your option) any later version.
13 332b1340 Michele Tartara
14 332b1340 Michele Tartara
This program is distributed in the hope that it will be useful, but
15 332b1340 Michele Tartara
WITHOUT ANY WARRANTY; without even the implied warranty of
16 332b1340 Michele Tartara
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17 332b1340 Michele Tartara
General Public License for more details.
18 332b1340 Michele Tartara
19 332b1340 Michele Tartara
You should have received a copy of the GNU General Public License
20 332b1340 Michele Tartara
along with this program; if not, write to the Free Software
21 332b1340 Michele Tartara
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
22 332b1340 Michele Tartara
02110-1301, USA.
23 332b1340 Michele Tartara
24 332b1340 Michele Tartara
-}
25 332b1340 Michele Tartara
26 332b1340 Michele Tartara
module Ganeti.DataCollectors.Drbd
27 332b1340 Michele Tartara
  ( main
28 332b1340 Michele Tartara
  , options
29 332b1340 Michele Tartara
  , arguments
30 ddceb4c5 Michele Tartara
  , dcName
31 a895fa19 Michele Tartara
  , dcVersion
32 834dc290 Michele Tartara
  , dcFormatVersion
33 f0e4b2a4 Michele Tartara
  , dcCategory
34 54c7dff7 Michele Tartara
  , dcKind
35 332b1340 Michele Tartara
  ) where
36 332b1340 Michele Tartara
37 332b1340 Michele Tartara
38 332b1340 Michele Tartara
import qualified Control.Exception as E
39 d78970ba Michele Tartara
import Control.Monad
40 332b1340 Michele Tartara
import Data.Attoparsec.Text.Lazy as A
41 d78970ba Michele Tartara
import Data.Maybe
42 332b1340 Michele Tartara
import Data.Text.Lazy (pack, unpack)
43 d78970ba Michele Tartara
import Network.BSD (getHostName)
44 d78970ba Michele Tartara
import qualified Text.JSON as J
45 332b1340 Michele Tartara
46 332b1340 Michele Tartara
import qualified Ganeti.BasicTypes as BT
47 332b1340 Michele Tartara
import qualified Ganeti.Constants as C
48 332b1340 Michele Tartara
import Ganeti.Block.Drbd.Parser(drbdStatusParser)
49 d78970ba Michele Tartara
import Ganeti.Block.Drbd.Types(DrbdInstMinor)
50 332b1340 Michele Tartara
import Ganeti.Common
51 d78970ba Michele Tartara
import Ganeti.Confd.Client
52 d78970ba Michele Tartara
import Ganeti.Confd.Types
53 55abd2c7 Iustin Pop
import Ganeti.DataCollectors.CLI
54 e71c47d3 Michele Tartara
import Ganeti.DataCollectors.Types
55 332b1340 Michele Tartara
import Ganeti.Utils
56 332b1340 Michele Tartara
57 332b1340 Michele Tartara
58 332b1340 Michele Tartara
-- | The default path of the DRBD status file.
59 332b1340 Michele Tartara
-- It is hardcoded because it is not likely to change.
60 332b1340 Michele Tartara
defaultFile :: FilePath
61 332b1340 Michele Tartara
defaultFile = C.drbdStatusFile
62 332b1340 Michele Tartara
63 332b1340 Michele Tartara
-- | The default setting for the maximum amount of not parsed character to
64 332b1340 Michele Tartara
-- print in case of error.
65 332b1340 Michele Tartara
-- It is set to use most of the screen estate on a standard 80x25 terminal.
66 332b1340 Michele Tartara
-- TODO: add the possibility to set this with a command line parameter.
67 332b1340 Michele Tartara
defaultCharNum :: Int
68 332b1340 Michele Tartara
defaultCharNum = 80*20
69 332b1340 Michele Tartara
70 e71c47d3 Michele Tartara
-- | The name of this data collector.
71 e71c47d3 Michele Tartara
dcName :: String
72 e71c47d3 Michele Tartara
dcName = "drbd"
73 e71c47d3 Michele Tartara
74 a895fa19 Michele Tartara
-- | The version of this data collector.
75 a895fa19 Michele Tartara
dcVersion :: DCVersion
76 a895fa19 Michele Tartara
dcVersion = DCVerBuiltin
77 a895fa19 Michele Tartara
78 e71c47d3 Michele Tartara
-- | The version number for the data format of this data collector.
79 e71c47d3 Michele Tartara
dcFormatVersion :: Int
80 e71c47d3 Michele Tartara
dcFormatVersion = 1
81 e71c47d3 Michele Tartara
82 f0e4b2a4 Michele Tartara
-- | The category of this data collector.
83 f0e4b2a4 Michele Tartara
dcCategory :: Maybe DCCategory
84 f0e4b2a4 Michele Tartara
dcCategory = Just DCStorage
85 f0e4b2a4 Michele Tartara
86 54c7dff7 Michele Tartara
-- | The kind of this data collector.
87 54c7dff7 Michele Tartara
dcKind :: DCKind
88 54c7dff7 Michele Tartara
dcKind = DCKStatus
89 54c7dff7 Michele Tartara
90 7a171e3f Michele Tartara
-- * Command line options
91 7a171e3f Michele Tartara
92 332b1340 Michele Tartara
options :: IO [OptType]
93 d78970ba Michele Tartara
options =
94 d78970ba Michele Tartara
  return
95 d78970ba Michele Tartara
    [ oDrbdStatus
96 d78970ba Michele Tartara
    , oDrbdPairing
97 d78970ba Michele Tartara
    ]
98 332b1340 Michele Tartara
99 332b1340 Michele Tartara
-- | The list of arguments supported by the program.
100 332b1340 Michele Tartara
arguments :: [ArgCompletion]
101 d78970ba Michele Tartara
arguments = [ArgCompletion OptComplFile 0 (Just 0)]
102 332b1340 Michele Tartara
103 d78970ba Michele Tartara
-- | Get information about the pairing of DRBD minors and Ganeti instances
104 d78970ba Michele Tartara
-- on the current node. The information is taken from the Confd client
105 d78970ba Michele Tartara
-- or, if a filename is specified, from a JSON encoded file (for testing
106 d78970ba Michele Tartara
-- purposes).
107 d78970ba Michele Tartara
getPairingInfo :: Maybe String -> IO (BT.Result [DrbdInstMinor])
108 d78970ba Michele Tartara
getPairingInfo Nothing = do
109 d78970ba Michele Tartara
  curNode <- getHostName
110 7dc27988 Michele Tartara
  client <- getConfdClient Nothing Nothing
111 d78970ba Michele Tartara
  reply <- query client ReqNodeDrbd $ PlainQuery curNode
112 d78970ba Michele Tartara
  return $
113 d78970ba Michele Tartara
    case fmap (J.readJSONs . confdReplyAnswer) reply of
114 d78970ba Michele Tartara
      Just (J.Ok instMinor) -> BT.Ok instMinor
115 d78970ba Michele Tartara
      Just (J.Error msg) -> BT.Bad msg
116 d78970ba Michele Tartara
      Nothing -> BT.Bad "No answer from the Confd server"
117 d78970ba Michele Tartara
getPairingInfo (Just filename) = do
118 d78970ba Michele Tartara
  content <- readFile filename
119 d78970ba Michele Tartara
  return $
120 d78970ba Michele Tartara
    case J.decode content of
121 d78970ba Michele Tartara
      J.Ok instMinor -> BT.Ok instMinor
122 d78970ba Michele Tartara
      J.Error msg -> BT.Bad msg
123 d78970ba Michele Tartara
124 e71c47d3 Michele Tartara
-- | This function builds a report with the DRBD status.
125 e71c47d3 Michele Tartara
buildDRBDReport :: FilePath -> Maybe FilePath -> IO DCReport
126 e71c47d3 Michele Tartara
buildDRBDReport statusFile pairingFile = do
127 332b1340 Michele Tartara
  contents <-
128 e71c47d3 Michele Tartara
    ((E.try $ readFile statusFile) :: IO (Either IOError String)) >>=
129 638e0a6f Iustin Pop
      exitIfBad "reading from file" . either (BT.Bad . show) BT.Ok
130 e71c47d3 Michele Tartara
  pairingResult <- getPairingInfo pairingFile
131 d78970ba Michele Tartara
  pairing <- exitIfBad "Can't get pairing info" pairingResult
132 e71c47d3 Michele Tartara
  jsonData <-
133 d78970ba Michele Tartara
    case A.parse (drbdStatusParser pairing) $ pack contents of
134 332b1340 Michele Tartara
      A.Fail unparsedText contexts errorMessage -> exitErr $
135 332b1340 Michele Tartara
        show (Prelude.take defaultCharNum $ unpack unparsedText) ++ "\n"
136 332b1340 Michele Tartara
          ++ show contexts ++ "\n" ++ errorMessage
137 e71c47d3 Michele Tartara
      A.Done _ drbdStatus -> return $ J.showJSON drbdStatus
138 54c7dff7 Michele Tartara
  buildReport dcName dcVersion dcFormatVersion dcCategory dcKind jsonData
139 e71c47d3 Michele Tartara
140 e71c47d3 Michele Tartara
-- | Main function.
141 e71c47d3 Michele Tartara
main :: Options -> [String] -> IO ()
142 e71c47d3 Michele Tartara
main opts args = do
143 e71c47d3 Michele Tartara
  let statusFile = fromMaybe defaultFile $ optDrbdStatus opts
144 e71c47d3 Michele Tartara
      pairingFile = optDrbdPairing opts
145 e71c47d3 Michele Tartara
  unless (null args) . exitErr $ "This program takes exactly zero" ++
146 e71c47d3 Michele Tartara
                                  " arguments, got '" ++ unwords args ++ "'"
147 e71c47d3 Michele Tartara
  report <- buildDRBDReport statusFile pairingFile
148 e71c47d3 Michele Tartara
  putStrLn $ J.encode report