Revision e71c47d3 src/Ganeti/DataCollectors/Drbd.hs

b/src/Ganeti/DataCollectors/Drbd.hs
46 46
import Ganeti.Confd.Client
47 47
import Ganeti.Confd.Types
48 48
import Ganeti.DataCollectors.CLI
49
import Ganeti.DataCollectors.Types
49 50
import Ganeti.Utils
50 51

  
51 52

  
......
61 62
defaultCharNum :: Int
62 63
defaultCharNum = 80*20
63 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

  
64 73
options :: IO [OptType]
65 74
options =
66 75
  return
......
95 104
      J.Ok instMinor -> BT.Ok instMinor
96 105
      J.Error msg -> BT.Bad msg
97 106

  
98
-- | Main function.
99
main :: Options -> [String] -> IO ()
100
main opts args = do
101
  let proc_drbd = fromMaybe defaultFile $ optDrbdStatus opts
102
      instMinor = optDrbdPairing opts
103
  unless (null args) . exitErr $ "This program takes exactly zero" ++
104
                                  " arguments, got '" ++ unwords args ++ "'"
107
-- | This function builds a report with the DRBD status.
108
buildDRBDReport :: FilePath -> Maybe FilePath -> IO DCReport
109
buildDRBDReport statusFile pairingFile = do
105 110
  contents <-
106
    ((E.try $ readFile proc_drbd) :: IO (Either IOError String)) >>=
111
    ((E.try $ readFile statusFile) :: IO (Either IOError String)) >>=
107 112
      exitIfBad "reading from file" . either (BT.Bad . show) BT.Ok
108
  pairingResult <- getPairingInfo instMinor
113
  pairingResult <- getPairingInfo pairingFile
109 114
  pairing <- exitIfBad "Can't get pairing info" pairingResult
110
  output <-
115
  jsonData <-
111 116
    case A.parse (drbdStatusParser pairing) $ pack contents of
112 117
      A.Fail unparsedText contexts errorMessage -> exitErr $
113 118
        show (Prelude.take defaultCharNum $ unpack unparsedText) ++ "\n"
114 119
          ++ show contexts ++ "\n" ++ errorMessage
115
      A.Done _ drbdStatus -> return $ J.encode drbdStatus
116
  putStrLn output
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

Also available in: Unified diff