]>
Commit | Line | Data |
---|---|---|
b23afe7b | 1 | {-# LANGUAGE LambdaCase #-} |
2ff46fce A |
2 | {-# LANGUAGE RecordWildCards #-} |
3 | ||
4 | module Graylog.Types | |
5 | ( Graylog | |
6 | , _graylogHost | |
7 | , _graylogPort | |
8 | , _graylogAddress | |
c91dbdc0 A |
9 | , _graylogSocket |
10 | , _graylogHostName | |
f82a8dfc | 11 | , _graylogChunkSize |
c91dbdc0 | 12 | , ChunkSize |
2ff46fce | 13 | |
c91dbdc0 | 14 | , defaultChunkSize |
2ff46fce A |
15 | , openGraylog |
16 | , closeGraylog | |
17 | ) where | |
18 | ||
b23afe7b | 19 | import Data.List |
2ff46fce A |
20 | import Data.Text (Text) |
21 | import qualified Data.Text as T | |
22 | import Network.BSD | |
23 | import Network.Socket | |
24 | ||
d9a5d441 | 25 | -- | The maximum size of each datagram when using UDP transit methods. |
c91dbdc0 A |
26 | type ChunkSize = Word |
27 | ||
d9a5d441 A |
28 | -- | Handle for a socket connected to Graylog. In some cases this socket |
29 | -- is UDP and will not have a maintained session. | |
2ff46fce A |
30 | data Graylog |
31 | = Graylog | |
c91dbdc0 A |
32 | { _graylogHost :: String |
33 | , _graylogPort :: String | |
34 | , _graylogAddress :: AddrInfo | |
35 | , _graylogSocket :: Socket | |
36 | , _graylogHostName :: Text | |
37 | , _graylogChunkSize :: ChunkSize | |
2ff46fce A |
38 | } |
39 | ||
c91dbdc0 A |
40 | defaultChunkSize :: ChunkSize |
41 | defaultChunkSize = 8192 | |
42 | ||
b23afe7b | 43 | openGraylog |
d9a5d441 A |
44 | :: HostName -- ^ The host on which graylog is running. |
45 | -> ServiceName -- ^ The port on which graylog is running. | |
46 | -> ChunkSize -- ^ The maximum size of each UDP datagram. | |
47 | -> IO (Either String Graylog) | |
f82a8dfc A |
48 | openGraylog h p cksize |
49 | | cksize < 1024 = return $ Left "ChunkSize must be at least 1024." | |
50 | | otherwise = getAddrInfo Nothing (Just h) (Just p) >>= \case | |
b23afe7b A |
51 | [] -> return $ Left "No address info found." |
52 | infos -> | |
53 | case find (\i -> addrSocketType i == Datagram) infos of | |
54 | Nothing -> return $ Left "No datagram info found for address." | |
55 | Just i -> do | |
56 | sock <- socket (addrFamily i) Datagram defaultProtocol | |
57 | connect sock (addrAddress i) | |
58 | hostname <- getHostName | |
59 | return $ Right $ Graylog h p i sock (T.pack hostname) cksize | |
2ff46fce A |
60 | |
61 | closeGraylog :: Graylog -> IO () | |
62 | closeGraylog Graylog{..} = close _graylogSocket |