diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/Graylog/Gelf.hs | 9 | ||||
-rw-r--r-- | src/Graylog/Types.hs | 27 | ||||
-rw-r--r-- | src/Graylog/UDP.hs | 40 |
3 files changed, 48 insertions, 28 deletions
diff --git a/src/Graylog/Gelf.hs b/src/Graylog/Gelf.hs index 32b9321..cd68e05 100644 --- a/src/Graylog/Gelf.hs +++ b/src/Graylog/Gelf.hs | |||
@@ -59,3 +59,12 @@ instance ToJSON SyslogLevel where | |||
59 | toJSON Notice = Number 5 | 59 | toJSON Notice = Number 5 |
60 | toJSON Informational = Number 6 | 60 | toJSON Informational = Number 6 |
61 | toJSON Debug = Number 7 | 61 | toJSON Debug = Number 7 |
62 | |||
63 | -- | ||
64 | |||
65 | simpleGelf | ||
66 | :: Text -- ^ Hostname | ||
67 | -> Text -- ^ Short message | ||
68 | -> GELF | ||
69 | simpleGelf host short = | ||
70 | GELF Version1x1 host short Nothing Nothing Nothing Nothing Nothing | ||
diff --git a/src/Graylog/Types.hs b/src/Graylog/Types.hs index bfee38a..8fad8cc 100644 --- a/src/Graylog/Types.hs +++ b/src/Graylog/Types.hs | |||
@@ -1,3 +1,4 @@ | |||
1 | {-# LANGUAGE LambdaCase #-} | ||
1 | {-# LANGUAGE RecordWildCards #-} | 2 | {-# LANGUAGE RecordWildCards #-} |
2 | 3 | ||
3 | module Graylog.Types | 4 | module Graylog.Types |
@@ -14,6 +15,7 @@ module Graylog.Types | |||
14 | , closeGraylog | 15 | , closeGraylog |
15 | ) where | 16 | ) where |
16 | 17 | ||
18 | import Data.List | ||
17 | import Data.Text (Text) | 19 | import Data.Text (Text) |
18 | import qualified Data.Text as T | 20 | import qualified Data.Text as T |
19 | import Network.BSD | 21 | import Network.BSD |
@@ -34,19 +36,18 @@ data Graylog | |||
34 | defaultChunkSize :: ChunkSize | 36 | defaultChunkSize :: ChunkSize |
35 | defaultChunkSize = 8192 | 37 | defaultChunkSize = 8192 |
36 | 38 | ||
37 | openGraylog :: HostName -> ServiceName -> ChunkSize -> IO (Either String Graylog) | 39 | openGraylog |
38 | openGraylog host port chksize = do | 40 | :: HostName -> ServiceName -> ChunkSize -> IO (Either String Graylog) |
39 | infos <- getAddrInfo Nothing (Just host) (Just port) | 41 | openGraylog h p cksize = getAddrInfo Nothing (Just h) (Just p) >>= \case |
40 | case infos of | 42 | [] -> return $ Left "No address info found." |
41 | [] -> return $ Left "No address info found." | 43 | infos -> |
42 | [info] -> do | 44 | case find (\i -> addrSocketType i == Datagram) infos of |
43 | sock <- socket (addrFamily info) Datagram defaultProtocol | 45 | Nothing -> return $ Left "No datagram info found for address." |
44 | connect sock (addrAddress info) | 46 | Just i -> do |
45 | hostname <- getHostName | 47 | sock <- socket (addrFamily i) Datagram defaultProtocol |
46 | return $ Right $ Graylog host port info sock (T.pack hostname) chksize | 48 | connect sock (addrAddress i) |
47 | _ -> return $ Left "Too many address infos found." | 49 | hostname <- getHostName |
50 | return $ Right $ Graylog h p i sock (T.pack hostname) cksize | ||
48 | 51 | ||
49 | closeGraylog :: Graylog -> IO () | 52 | closeGraylog :: Graylog -> IO () |
50 | closeGraylog Graylog{..} = close _graylogSocket | 53 | closeGraylog Graylog{..} = close _graylogSocket |
51 | |||
52 | |||
diff --git a/src/Graylog/UDP.hs b/src/Graylog/UDP.hs index 656cbc8..c925d7e 100644 --- a/src/Graylog/UDP.hs +++ b/src/Graylog/UDP.hs | |||
@@ -5,27 +5,37 @@ module Graylog.UDP | |||
5 | ) where | 5 | ) where |
6 | 6 | ||
7 | import Data.Aeson | 7 | import Data.Aeson |
8 | import qualified Data.ByteString.Lazy as LBS | 8 | {-import Data.ByteString.Builder-} |
9 | import Data.Word | 9 | {-import qualified Data.ByteString.Lazy as LBS-} |
10 | {-import Data.Word-} | ||
10 | import Network.Socket.ByteString.Lazy | 11 | import Network.Socket.ByteString.Lazy |
11 | import System.Random | 12 | {-import System.Random-} |
12 | 13 | ||
13 | import Graylog.Gelf as Export | 14 | import Graylog.Gelf as Export |
14 | import Graylog.Types as Export | 15 | import Graylog.Types as Export |
15 | 16 | ||
16 | sendLog :: Graylog -> GELF -> IO () | 17 | sendLog :: Graylog -> GELF -> IO () |
17 | sendLog glog msg = mapM_ (send $ _graylogSocket glog) cks | 18 | sendLog glog msg = do |
19 | _ <- send (_graylogSocket glog) raw | ||
20 | print raw | ||
21 | return () | ||
18 | where | 22 | where |
19 | raw = encode msg | 23 | raw = encode msg |
20 | cks = chunky glog raw | ||
21 | 24 | ||
22 | chunky :: Graylog -> LBS.ByteString -> IO [LBS.ByteString] | 25 | {-sendLog :: Graylog -> GELF -> IO ()-} |
23 | chunky glog raw = do | 26 | {-sendLog glog msg = do-} |
24 | groupId <- randomIO | 27 | {-cks <- chunky glog raw-} |
25 | splitAt gsize | 28 | {-mapM_ (send $ _graylogSocket glog) cks-} |
26 | where | 29 | {-where-} |
27 | magic = undefined | 30 | {-raw = encode msg-} |
28 | seq = undefined | 31 | |
29 | total = undefined | 32 | {-chunky :: Graylog -> LBS.ByteString -> IO [LBS.ByteString]-} |
30 | hlen = 12 | 33 | {-chunky glog raw = do-} |
31 | gsize = (fromIntegral (_graylogChunkSize glog)) - hlen | 34 | {-groupId <- randomIO-} |
35 | {-splitAt gsize-} | ||
36 | {-where-} | ||
37 | {-magic = word8 0x1e <> word8 0x0f-} | ||
38 | {-seqNum = undefined-} | ||
39 | {-(count, excess) = quotRem (LBS.length raw) gzie-} | ||
40 | {-hlen = 12-} | ||
41 | {-gsize = (fromIntegral (_graylogChunkSize glog)) - hlen-} | ||