module Ganeti.DataCollectors.Diskstats
( main
, options
, arguments
, dcName
, dcVersion
, dcFormatVersion
, dcCategory
, dcKind
, dcReport
) where
import qualified Control.Exception as E
import Control.Monad
import Data.Attoparsec.Text.Lazy as A
import Data.Maybe
import Data.Text.Lazy (pack, unpack)
import qualified Text.JSON as J
import qualified Ganeti.BasicTypes as BT
import qualified Ganeti.Constants as C
import Ganeti.Storage.Diskstats.Parser(diskstatsParser)
import Ganeti.Common
import Ganeti.DataCollectors.CLI
import Ganeti.DataCollectors.Types
import Ganeti.Utils
defaultFile :: FilePath
defaultFile = C.diskstatsFile
defaultCharNum :: Int
defaultCharNum = 80*20
dcName :: String
dcName = "diskstats"
dcVersion :: DCVersion
dcVersion = DCVerBuiltin
dcFormatVersion :: Int
dcFormatVersion = 1
dcCategory :: Maybe DCCategory
dcCategory = Just DCStorage
dcKind :: DCKind
dcKind = DCKPerf
dcReport :: IO DCReport
dcReport = buildDCReport defaultFile
options :: IO [OptType]
options =
return
[ oInputFile
]
arguments :: [ArgCompletion]
arguments = [ArgCompletion OptComplFile 0 (Just 0)]
buildJsonReport :: FilePath -> IO J.JSValue
buildJsonReport inputFile = do
contents <-
((E.try $ readFile inputFile) :: IO (Either IOError String)) >>=
exitIfBad "reading from file" . either (BT.Bad . show) BT.Ok
diskstatsData <-
case A.parse diskstatsParser $ pack contents of
A.Fail unparsedText contexts errorMessage -> exitErr $
show (Prelude.take defaultCharNum $ unpack unparsedText) ++ "\n"
++ show contexts ++ "\n" ++ errorMessage
A.Done _ diskstatsD -> return diskstatsD
return $ J.showJSON diskstatsData
buildDCReport :: FilePath -> IO DCReport
buildDCReport inputFile =
buildJsonReport inputFile >>=
buildReport dcName dcVersion dcFormatVersion dcCategory dcKind
main :: Options -> [String] -> IO ()
main opts args = do
let inputFile = fromMaybe defaultFile $ optInputFile opts
unless (null args) . exitErr $ "This program takes exactly zero" ++
" arguments, got '" ++ unwords args ++ "'"
report <- buildDCReport inputFile
putStrLn $ J.encode report