root / src / Ganeti / Cpu / LoadParser.hs @ 40960882
History | View | Annotate | Download (2.2 kB)
1 | f6d4b52d | Spyros Trigazis | {-# LANGUAGE OverloadedStrings #-} |
---|---|---|---|
2 | f6d4b52d | Spyros Trigazis | {-| /proc/stat file parser |
3 | f6d4b52d | Spyros Trigazis | |
4 | f6d4b52d | Spyros Trigazis | This module holds the definition of the parser that extracts information |
5 | f6d4b52d | Spyros Trigazis | about the CPU load of the system from the @/proc/stat@ file. |
6 | f6d4b52d | Spyros Trigazis | |
7 | f6d4b52d | Spyros Trigazis | -} |
8 | f6d4b52d | Spyros Trigazis | {- |
9 | f6d4b52d | Spyros Trigazis | |
10 | f6d4b52d | Spyros Trigazis | Copyright (C) 2013 Google Inc. |
11 | f6d4b52d | Spyros Trigazis | |
12 | f6d4b52d | Spyros Trigazis | This program is free software; you can redistribute it and/or modify |
13 | f6d4b52d | Spyros Trigazis | it under the terms of the GNU General Public License as published by |
14 | f6d4b52d | Spyros Trigazis | the Free Software Foundation; either version 2 of the License, or |
15 | f6d4b52d | Spyros Trigazis | (at your option) any later version. |
16 | f6d4b52d | Spyros Trigazis | |
17 | f6d4b52d | Spyros Trigazis | This program is distributed in the hope that it will be useful, but |
18 | f6d4b52d | Spyros Trigazis | WITHOUT ANY WARRANTY; without even the implied warranty of |
19 | f6d4b52d | Spyros Trigazis | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
20 | f6d4b52d | Spyros Trigazis | General Public License for more details. |
21 | f6d4b52d | Spyros Trigazis | |
22 | f6d4b52d | Spyros Trigazis | You should have received a copy of the GNU General Public License |
23 | f6d4b52d | Spyros Trigazis | along with this program; if not, write to the Free Software |
24 | f6d4b52d | Spyros Trigazis | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
25 | f6d4b52d | Spyros Trigazis | 02110-1301, USA. |
26 | f6d4b52d | Spyros Trigazis | |
27 | f6d4b52d | Spyros Trigazis | -} |
28 | f6d4b52d | Spyros Trigazis | module Ganeti.Cpu.LoadParser (cpustatParser) where |
29 | f6d4b52d | Spyros Trigazis | |
30 | f6d4b52d | Spyros Trigazis | import Control.Applicative ((<*>), (<*), (*>), (<$>), (<|>)) |
31 | f6d4b52d | Spyros Trigazis | import qualified Data.Attoparsec.Text as A |
32 | f6d4b52d | Spyros Trigazis | import qualified Data.Attoparsec.Combinator as AC |
33 | f6d4b52d | Spyros Trigazis | import Data.Attoparsec.Text (Parser) |
34 | f6d4b52d | Spyros Trigazis | |
35 | f6d4b52d | Spyros Trigazis | import Ganeti.Parsers |
36 | f6d4b52d | Spyros Trigazis | import Ganeti.Cpu.Types |
37 | f6d4b52d | Spyros Trigazis | |
38 | f6d4b52d | Spyros Trigazis | -- * Parser implementation |
39 | f6d4b52d | Spyros Trigazis | |
40 | f6d4b52d | Spyros Trigazis | -- | The parser for one line of the CPU status file. |
41 | f6d4b52d | Spyros Trigazis | oneCPUstatParser :: Parser CPUstat |
42 | f6d4b52d | Spyros Trigazis | oneCPUstatParser = |
43 | f6d4b52d | Spyros Trigazis | let nameP = stringP |
44 | f6d4b52d | Spyros Trigazis | userP = numberP |
45 | f6d4b52d | Spyros Trigazis | niceP = numberP |
46 | f6d4b52d | Spyros Trigazis | systemP = numberP |
47 | f6d4b52d | Spyros Trigazis | idleP = numberP |
48 | f6d4b52d | Spyros Trigazis | iowaitP = numberP |
49 | f6d4b52d | Spyros Trigazis | irqP = numberP |
50 | f6d4b52d | Spyros Trigazis | softirqP = numberP |
51 | f6d4b52d | Spyros Trigazis | stealP = numberP |
52 | f6d4b52d | Spyros Trigazis | guestP = numberP |
53 | f6d4b52d | Spyros Trigazis | guest_niceP = numberP |
54 | f6d4b52d | Spyros Trigazis | in |
55 | f6d4b52d | Spyros Trigazis | CPUstat <$> nameP <*> userP <*> niceP <*> systemP <*> idleP <*> iowaitP |
56 | f6d4b52d | Spyros Trigazis | <*> irqP <*> softirqP <*> stealP <*> guestP <*> guest_niceP |
57 | f6d4b52d | Spyros Trigazis | <* A.endOfLine |
58 | f6d4b52d | Spyros Trigazis | |
59 | f6d4b52d | Spyros Trigazis | -- | When this is satisfied all the lines containing information about |
60 | f6d4b52d | Spyros Trigazis | -- the CPU load are parsed. |
61 | f6d4b52d | Spyros Trigazis | intrFound :: Parser () |
62 | f6d4b52d | Spyros Trigazis | intrFound = (A.string "intr" *> return ()) |
63 | f6d4b52d | Spyros Trigazis | <|> (A.string "page" *> return ()) |
64 | f6d4b52d | Spyros Trigazis | <|> (A.string "swap" *> return ()) |
65 | f6d4b52d | Spyros Trigazis | |
66 | f6d4b52d | Spyros Trigazis | -- | The parser for the fragment of CPU status file containing |
67 | f6d4b52d | Spyros Trigazis | -- information about the CPU load. |
68 | f6d4b52d | Spyros Trigazis | cpustatParser :: Parser [CPUstat] |
69 | f6d4b52d | Spyros Trigazis | cpustatParser = oneCPUstatParser `AC.manyTill` intrFound |