root / src / Ganeti / DataCollectors / Diskstats.hs @ d6a7518a
History | View | Annotate | Download (3.8 kB)
1 | 1a99d6a2 | Michele Tartara | {-| @/proc/diskstats@ data collector. |
---|---|---|---|
2 | 1a99d6a2 | Michele Tartara | |
3 | 1a99d6a2 | Michele Tartara | -} |
4 | 1a99d6a2 | Michele Tartara | |
5 | 1a99d6a2 | Michele Tartara | {- |
6 | 1a99d6a2 | Michele Tartara | |
7 | 1a99d6a2 | Michele Tartara | Copyright (C) 2013 Google Inc. |
8 | 1a99d6a2 | Michele Tartara | |
9 | 1a99d6a2 | Michele Tartara | This program is free software; you can redistribute it and/or modify |
10 | 1a99d6a2 | Michele Tartara | it under the terms of the GNU General Public License as published by |
11 | 1a99d6a2 | Michele Tartara | the Free Software Foundation; either version 2 of the License, or |
12 | 1a99d6a2 | Michele Tartara | (at your option) any later version. |
13 | 1a99d6a2 | Michele Tartara | |
14 | 1a99d6a2 | Michele Tartara | This program is distributed in the hope that it will be useful, but |
15 | 1a99d6a2 | Michele Tartara | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | 1a99d6a2 | Michele Tartara | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
17 | 1a99d6a2 | Michele Tartara | General Public License for more details. |
18 | 1a99d6a2 | Michele Tartara | |
19 | 1a99d6a2 | Michele Tartara | You should have received a copy of the GNU General Public License |
20 | 1a99d6a2 | Michele Tartara | along with this program; if not, write to the Free Software |
21 | 1a99d6a2 | Michele Tartara | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
22 | 1a99d6a2 | Michele Tartara | 02110-1301, USA. |
23 | 1a99d6a2 | Michele Tartara | |
24 | 1a99d6a2 | Michele Tartara | -} |
25 | 1a99d6a2 | Michele Tartara | |
26 | 1a99d6a2 | Michele Tartara | module Ganeti.DataCollectors.Diskstats |
27 | 1a99d6a2 | Michele Tartara | ( main |
28 | 1a99d6a2 | Michele Tartara | , options |
29 | 1a99d6a2 | Michele Tartara | , arguments |
30 | 1a99d6a2 | Michele Tartara | , dcName |
31 | 1a99d6a2 | Michele Tartara | , dcVersion |
32 | 1a99d6a2 | Michele Tartara | , dcFormatVersion |
33 | 1a99d6a2 | Michele Tartara | , dcCategory |
34 | 1a99d6a2 | Michele Tartara | , dcKind |
35 | 1a99d6a2 | Michele Tartara | , dcReport |
36 | 1a99d6a2 | Michele Tartara | ) where |
37 | 1a99d6a2 | Michele Tartara | |
38 | 1a99d6a2 | Michele Tartara | |
39 | 1a99d6a2 | Michele Tartara | import qualified Control.Exception as E |
40 | 1a99d6a2 | Michele Tartara | import Control.Monad |
41 | 1a99d6a2 | Michele Tartara | import Data.Attoparsec.Text.Lazy as A |
42 | 1a99d6a2 | Michele Tartara | import Data.Maybe |
43 | 1a99d6a2 | Michele Tartara | import Data.Text.Lazy (pack, unpack) |
44 | 1a99d6a2 | Michele Tartara | import qualified Text.JSON as J |
45 | 1a99d6a2 | Michele Tartara | |
46 | 1a99d6a2 | Michele Tartara | import qualified Ganeti.BasicTypes as BT |
47 | 1a99d6a2 | Michele Tartara | import qualified Ganeti.Constants as C |
48 | c5f6cba2 | Helga Velroyen | import Ganeti.Storage.Diskstats.Parser(diskstatsParser) |
49 | 1a99d6a2 | Michele Tartara | import Ganeti.Common |
50 | 1a99d6a2 | Michele Tartara | import Ganeti.DataCollectors.CLI |
51 | 1a99d6a2 | Michele Tartara | import Ganeti.DataCollectors.Types |
52 | 1a99d6a2 | Michele Tartara | import Ganeti.Utils |
53 | 1a99d6a2 | Michele Tartara | |
54 | 1a99d6a2 | Michele Tartara | |
55 | 1a99d6a2 | Michele Tartara | -- | The default path of the diskstats status file. |
56 | 1a99d6a2 | Michele Tartara | -- It is hardcoded because it is not likely to change. |
57 | 1a99d6a2 | Michele Tartara | defaultFile :: FilePath |
58 | 1a99d6a2 | Michele Tartara | defaultFile = C.diskstatsFile |
59 | 1a99d6a2 | Michele Tartara | |
60 | 1a99d6a2 | Michele Tartara | -- | The default setting for the maximum amount of not parsed character to |
61 | 1a99d6a2 | Michele Tartara | -- print in case of error. |
62 | 1a99d6a2 | Michele Tartara | -- It is set to use most of the screen estate on a standard 80x25 terminal. |
63 | 1a99d6a2 | Michele Tartara | -- TODO: add the possibility to set this with a command line parameter. |
64 | 1a99d6a2 | Michele Tartara | defaultCharNum :: Int |
65 | 1a99d6a2 | Michele Tartara | defaultCharNum = 80*20 |
66 | 1a99d6a2 | Michele Tartara | |
67 | 1a99d6a2 | Michele Tartara | -- | The name of this data collector. |
68 | 1a99d6a2 | Michele Tartara | dcName :: String |
69 | 1a99d6a2 | Michele Tartara | dcName = "diskstats" |
70 | 1a99d6a2 | Michele Tartara | |
71 | 1a99d6a2 | Michele Tartara | -- | The version of this data collector. |
72 | 1a99d6a2 | Michele Tartara | dcVersion :: DCVersion |
73 | 1a99d6a2 | Michele Tartara | dcVersion = DCVerBuiltin |
74 | 1a99d6a2 | Michele Tartara | |
75 | 1a99d6a2 | Michele Tartara | -- | The version number for the data format of this data collector. |
76 | 1a99d6a2 | Michele Tartara | dcFormatVersion :: Int |
77 | 1a99d6a2 | Michele Tartara | dcFormatVersion = 1 |
78 | 1a99d6a2 | Michele Tartara | |
79 | 1a99d6a2 | Michele Tartara | -- | The category of this data collector. |
80 | 1a99d6a2 | Michele Tartara | dcCategory :: Maybe DCCategory |
81 | 1a99d6a2 | Michele Tartara | dcCategory = Just DCStorage |
82 | 1a99d6a2 | Michele Tartara | |
83 | 1a99d6a2 | Michele Tartara | -- | The kind of this data collector. |
84 | 1a99d6a2 | Michele Tartara | dcKind :: DCKind |
85 | 1a99d6a2 | Michele Tartara | dcKind = DCKPerf |
86 | 1a99d6a2 | Michele Tartara | |
87 | 1a99d6a2 | Michele Tartara | -- | The data exported by the data collector, taken from the default location. |
88 | 1a99d6a2 | Michele Tartara | dcReport :: IO DCReport |
89 | 1a99d6a2 | Michele Tartara | dcReport = buildDCReport defaultFile |
90 | 1a99d6a2 | Michele Tartara | |
91 | 1a99d6a2 | Michele Tartara | -- * Command line options |
92 | 1a99d6a2 | Michele Tartara | |
93 | 1a99d6a2 | Michele Tartara | options :: IO [OptType] |
94 | 1a99d6a2 | Michele Tartara | options = |
95 | 1a99d6a2 | Michele Tartara | return |
96 | 1a99d6a2 | Michele Tartara | [ oInputFile |
97 | 1a99d6a2 | Michele Tartara | ] |
98 | 1a99d6a2 | Michele Tartara | |
99 | 1a99d6a2 | Michele Tartara | -- | The list of arguments supported by the program. |
100 | 1a99d6a2 | Michele Tartara | arguments :: [ArgCompletion] |
101 | 1a99d6a2 | Michele Tartara | arguments = [ArgCompletion OptComplFile 0 (Just 0)] |
102 | 1a99d6a2 | Michele Tartara | |
103 | 1a99d6a2 | Michele Tartara | -- | This function computes the JSON representation of the diskstats status. |
104 | 1a99d6a2 | Michele Tartara | buildJsonReport :: FilePath -> IO J.JSValue |
105 | 1a99d6a2 | Michele Tartara | buildJsonReport inputFile = do |
106 | 1a99d6a2 | Michele Tartara | contents <- |
107 | 1a99d6a2 | Michele Tartara | ((E.try $ readFile inputFile) :: IO (Either IOError String)) >>= |
108 | 1a99d6a2 | Michele Tartara | exitIfBad "reading from file" . either (BT.Bad . show) BT.Ok |
109 | 1a99d6a2 | Michele Tartara | diskstatsData <- |
110 | 1a99d6a2 | Michele Tartara | case A.parse diskstatsParser $ pack contents of |
111 | 1a99d6a2 | Michele Tartara | A.Fail unparsedText contexts errorMessage -> exitErr $ |
112 | 1a99d6a2 | Michele Tartara | show (Prelude.take defaultCharNum $ unpack unparsedText) ++ "\n" |
113 | 1a99d6a2 | Michele Tartara | ++ show contexts ++ "\n" ++ errorMessage |
114 | 1a99d6a2 | Michele Tartara | A.Done _ diskstatsD -> return diskstatsD |
115 | 1a99d6a2 | Michele Tartara | return $ J.showJSON diskstatsData |
116 | 1a99d6a2 | Michele Tartara | |
117 | 1a99d6a2 | Michele Tartara | -- | This function computes the DCReport for the diskstats status. |
118 | 1a99d6a2 | Michele Tartara | buildDCReport :: FilePath -> IO DCReport |
119 | 1a99d6a2 | Michele Tartara | buildDCReport inputFile = |
120 | 1a99d6a2 | Michele Tartara | buildJsonReport inputFile >>= |
121 | 1a99d6a2 | Michele Tartara | buildReport dcName dcVersion dcFormatVersion dcCategory dcKind |
122 | 1a99d6a2 | Michele Tartara | |
123 | 1a99d6a2 | Michele Tartara | -- | Main function. |
124 | 1a99d6a2 | Michele Tartara | main :: Options -> [String] -> IO () |
125 | 1a99d6a2 | Michele Tartara | main opts args = do |
126 | 1a99d6a2 | Michele Tartara | let inputFile = fromMaybe defaultFile $ optInputFile opts |
127 | 1a99d6a2 | Michele Tartara | unless (null args) . exitErr $ "This program takes exactly zero" ++ |
128 | 1a99d6a2 | Michele Tartara | " arguments, got '" ++ unwords args ++ "'" |
129 | 1a99d6a2 | Michele Tartara | report <- buildDCReport inputFile |
130 | 1a99d6a2 | Michele Tartara | putStrLn $ J.encode report |