diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/Graylog/Types.hs | 5 | ||||
-rw-r--r-- | src/Graylog/UDP.hs | 54 |
2 files changed, 35 insertions, 24 deletions
diff --git a/src/Graylog/Types.hs b/src/Graylog/Types.hs index 8fad8cc..5c32a8b 100644 --- a/src/Graylog/Types.hs +++ b/src/Graylog/Types.hs | |||
@@ -8,6 +8,7 @@ module Graylog.Types | |||
8 | , _graylogAddress | 8 | , _graylogAddress |
9 | , _graylogSocket | 9 | , _graylogSocket |
10 | , _graylogHostName | 10 | , _graylogHostName |
11 | , _graylogChunkSize | ||
11 | , ChunkSize | 12 | , ChunkSize |
12 | 13 | ||
13 | , defaultChunkSize | 14 | , defaultChunkSize |
@@ -38,7 +39,9 @@ defaultChunkSize = 8192 | |||
38 | 39 | ||
39 | openGraylog | 40 | openGraylog |
40 | :: HostName -> ServiceName -> ChunkSize -> IO (Either String Graylog) | 41 | :: HostName -> ServiceName -> ChunkSize -> IO (Either String Graylog) |
41 | openGraylog h p cksize = getAddrInfo Nothing (Just h) (Just p) >>= \case | 42 | openGraylog h p cksize |
43 | | cksize < 1024 = return $ Left "ChunkSize must be at least 1024." | ||
44 | | otherwise = getAddrInfo Nothing (Just h) (Just p) >>= \case | ||
42 | [] -> return $ Left "No address info found." | 45 | [] -> return $ Left "No address info found." |
43 | infos -> | 46 | infos -> |
44 | case find (\i -> addrSocketType i == Datagram) infos of | 47 | case find (\i -> addrSocketType i == Datagram) infos of |
diff --git a/src/Graylog/UDP.hs b/src/Graylog/UDP.hs index c925d7e..cd7fe5a 100644 --- a/src/Graylog/UDP.hs +++ b/src/Graylog/UDP.hs | |||
@@ -5,37 +5,45 @@ module Graylog.UDP | |||
5 | ) where | 5 | ) where |
6 | 6 | ||
7 | import Data.Aeson | 7 | import Data.Aeson |
8 | {-import Data.ByteString.Builder-} | 8 | import Data.ByteString.Builder |
9 | {-import qualified Data.ByteString.Lazy as LBS-} | 9 | import qualified Data.ByteString.Lazy as LBS |
10 | {-import Data.Word-} | 10 | import Data.Monoid |
11 | import Data.Word | ||
11 | import Network.Socket.ByteString.Lazy | 12 | import Network.Socket.ByteString.Lazy |
12 | {-import System.Random-} | 13 | import System.Random |
13 | 14 | ||
14 | import Graylog.Gelf as Export | 15 | import Graylog.Gelf as Export |
15 | import Graylog.Types as Export | 16 | import Graylog.Types as Export |
16 | 17 | ||
17 | sendLog :: Graylog -> GELF -> IO () | 18 | sendLog :: Graylog -> GELF -> IO () |
18 | sendLog glog msg = do | 19 | sendLog glog msg = do |
19 | _ <- send (_graylogSocket glog) raw | 20 | cks <- chunky glog raw |
20 | print raw | 21 | mapM_ (send $ _graylogSocket glog) cks |
21 | return () | ||
22 | where | 22 | where |
23 | raw = encode msg | 23 | raw = encode msg |
24 | 24 | ||
25 | {-sendLog :: Graylog -> GELF -> IO ()-} | 25 | chunky :: Graylog -> LBS.ByteString -> IO [LBS.ByteString] |
26 | {-sendLog glog msg = do-} | 26 | chunky glog raw = do |
27 | {-cks <- chunky glog raw-} | 27 | groupId <- randomIO |
28 | {-mapM_ (send $ _graylogSocket glog) cks-} | 28 | let groups = divide totalNum raw |
29 | {-where-} | 29 | return $ append groupId groups seqNums |
30 | {-raw = encode msg-} | 30 | where |
31 | magic = word8 0x1e <> word8 0x0f | ||
32 | seqNums = [0..] :: [Word8] | ||
33 | totalNum = if excess > 0 then count + 1 else count | ||
34 | (count, excess) = quotRem (LBS.length raw) gsize | ||
35 | hlen = 12 | ||
36 | gsize = (fromIntegral (_graylogChunkSize glog)) - hlen | ||
37 | |||
38 | divide 0 dat = [dat] | ||
39 | divide num dat = let (pre, post) = LBS.splitAt gsize dat | ||
40 | in pre : divide (num - 1) post | ||
31 | 41 | ||
32 | {-chunky :: Graylog -> LBS.ByteString -> IO [LBS.ByteString]-} | 42 | append _ [] _ = [] |
33 | {-chunky glog raw = do-} | 43 | append _ _ [] = error "the impossible has happened." |
34 | {-groupId <- randomIO-} | 44 | append gid (g:gs) (s:ss) = (toLazyByteString |
35 | {-splitAt gsize-} | 45 | $ magic |
36 | {-where-} | 46 | <> word64BE gid |
37 | {-magic = word8 0x1e <> word8 0x0f-} | 47 | <> word8 s |
38 | {-seqNum = undefined-} | 48 | <> word8 (fromIntegral totalNum) |
39 | {-(count, excess) = quotRem (LBS.length raw) gzie-} | 49 | <> lazyByteString g) : append gid gs ss |
40 | {-hlen = 12-} | ||
41 | {-gsize = (fromIntegral (_graylogChunkSize glog)) - hlen-} | ||