LVParser.hs 3.63 KB
Newer Older
Michele Tartara's avatar
Michele Tartara committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
{-# LANGUAGE OverloadedStrings #-}
{-| Logical Volumer information parser

This module holds the definition of the parser that extracts status
information about the logical volumes (LVs) of the system from the output of the
@lvs@ command.

-}
{-

Copyright (C) 2013 Google Inc.

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301, USA.

-}
module Ganeti.Storage.Lvm.LVParser (lvParser, lvCommand) where

import Control.Applicative ((<*>), (*>), (<*), (<$>), Applicative, )
import qualified Data.Attoparsec.Text as A
import qualified Data.Attoparsec.Combinator as AC
import Data.Attoparsec.Text (Parser)
import Data.Text (unpack)

import Ganeti.Storage.Lvm.Types


-- | The separator of the fields returned by @lvs@
lvsSeparator :: Char
lvsSeparator = ';'

-- * Utility functions

-- | Our own space-skipping function, because A.skipSpace also skips
-- newline characters. It skips ZERO or more spaces, so it does not
-- fail if there are no spaces.
skipSpaces :: Parser ()
skipSpaces = A.skipWhile A.isHorizontalSpace

-- | A parser recognizing a number of bytes, represented as a number preceeded
-- by a separator and followed by the "B" character.
bytesP :: Parser Int
bytesP = A.char lvsSeparator *> A.decimal <* A.char 'B'

-- | A parser recognizing a number discarding the preceeding separator
intP :: Parser Int
intP = A.char lvsSeparator *> A.signed A.decimal

-- | A parser recognizing a string starting with and closed by a separator (both
-- are discarded)
stringP :: Parser String
stringP =
  A.char lvsSeparator *> fmap unpack (A.takeWhile (`notElem`
    [ lvsSeparator
    , '\n']
    ))

-- * Parser implementation

-- | The command providing the data, in the format the parser expects
lvCommand :: [String]
lvCommand =
  [ "lvs"
  , "--noheadings"
  , "--units B"
  , "-- separator ;"
  , "-o lv_uuid,lv_name,lv_attr,lv_major,lv_minor,lv_kernel_major\
    \,lv_kernel_minor,lv_size,seg_count,lv_tags,modules,vg_uuid,vg_name,segtype\
    \,seg_start,seg_start_pe,seg_size,seg_tags,seg_pe_ranges,devices"
  ]

-- | The parser for one line of the diskstatus file.
oneLvParser :: Parser LVInfo
oneLvParser =
  let uuidP = skipSpaces *> fmap unpack (A.takeWhile (/= lvsSeparator))
      nameP = stringP
      attrP = stringP
      majorP = intP
      minorP = intP
      kernelMajorP = intP
      kernelMinorP = intP
      sizeP = bytesP
      segCountP = intP
      tagsP = stringP
      modulesP = stringP
      vgUuidP = stringP
      vgNameP = stringP
      segtypeP = stringP
      segStartP = bytesP
      segStartPeP = intP
      segSizeP = bytesP
      segTagsP = stringP
      segPeRangesP = stringP
      devicesP = stringP
    in
      LVInfo
        <$> uuidP <*> nameP <*> attrP <*> majorP <*> minorP <*> kernelMajorP
        <*> kernelMinorP <*> sizeP <*> segCountP <*> tagsP <*> modulesP
        <*> vgUuidP <*> vgNameP <*> segtypeP <*> segStartP <*> segStartPeP
        <*> segSizeP <*> segTagsP <*> segPeRangesP <*> devicesP <* A.endOfLine

-- | The parser for a whole diskstatus file.
lvParser :: Parser [LVInfo]
lvParser = oneLvParser `AC.manyTill` A.endOfInput