From b23afe7b9089b3177d9aee994464f72e52efbd2a Mon Sep 17 00:00:00 2001 From: AndrewRademacher Date: Sat, 27 Feb 2016 21:54:20 -0600 Subject: Successful test of un-chunked message. --- src/Graylog/Gelf.hs | 9 +++++++++ src/Graylog/Types.hs | 27 ++++++++++++++------------- src/Graylog/UDP.hs | 40 +++++++++++++++++++++++++--------------- 3 files changed, 48 insertions(+), 28 deletions(-) (limited to 'src') 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 toJSON Notice = Number 5 toJSON Informational = Number 6 toJSON Debug = Number 7 + +-- + +simpleGelf + :: Text -- ^ Hostname + -> Text -- ^ Short message + -> GELF +simpleGelf host short = + 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 @@ +{-# LANGUAGE LambdaCase #-} {-# LANGUAGE RecordWildCards #-} module Graylog.Types @@ -14,6 +15,7 @@ module Graylog.Types , closeGraylog ) where +import Data.List import Data.Text (Text) import qualified Data.Text as T import Network.BSD @@ -34,19 +36,18 @@ data Graylog defaultChunkSize :: ChunkSize defaultChunkSize = 8192 -openGraylog :: HostName -> ServiceName -> ChunkSize -> IO (Either String Graylog) -openGraylog host port chksize = do - infos <- getAddrInfo Nothing (Just host) (Just port) - case infos of - [] -> return $ Left "No address info found." - [info] -> do - sock <- socket (addrFamily info) Datagram defaultProtocol - connect sock (addrAddress info) - hostname <- getHostName - return $ Right $ Graylog host port info sock (T.pack hostname) chksize - _ -> return $ Left "Too many address infos found." +openGraylog + :: HostName -> ServiceName -> ChunkSize -> IO (Either String Graylog) +openGraylog h p cksize = getAddrInfo Nothing (Just h) (Just p) >>= \case + [] -> return $ Left "No address info found." + infos -> + case find (\i -> addrSocketType i == Datagram) infos of + Nothing -> return $ Left "No datagram info found for address." + Just i -> do + sock <- socket (addrFamily i) Datagram defaultProtocol + connect sock (addrAddress i) + hostname <- getHostName + return $ Right $ Graylog h p i sock (T.pack hostname) cksize closeGraylog :: Graylog -> IO () closeGraylog Graylog{..} = close _graylogSocket - - 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 ) where import Data.Aeson -import qualified Data.ByteString.Lazy as LBS -import Data.Word +{-import Data.ByteString.Builder-} +{-import qualified Data.ByteString.Lazy as LBS-} +{-import Data.Word-} import Network.Socket.ByteString.Lazy -import System.Random +{-import System.Random-} import Graylog.Gelf as Export import Graylog.Types as Export sendLog :: Graylog -> GELF -> IO () -sendLog glog msg = mapM_ (send $ _graylogSocket glog) cks +sendLog glog msg = do + _ <- send (_graylogSocket glog) raw + print raw + return () where raw = encode msg - cks = chunky glog raw -chunky :: Graylog -> LBS.ByteString -> IO [LBS.ByteString] -chunky glog raw = do - groupId <- randomIO - splitAt gsize - where - magic = undefined - seq = undefined - total = undefined - hlen = 12 - gsize = (fromIntegral (_graylogChunkSize glog)) - hlen +{-sendLog :: Graylog -> GELF -> IO ()-} +{-sendLog glog msg = do-} + {-cks <- chunky glog raw-} + {-mapM_ (send $ _graylogSocket glog) cks-} + {-where-} + {-raw = encode msg-} + +{-chunky :: Graylog -> LBS.ByteString -> IO [LBS.ByteString]-} +{-chunky glog raw = do-} + {-groupId <- randomIO-} + {-splitAt gsize-} + {-where-} + {-magic = word8 0x1e <> word8 0x0f-} + {-seqNum = undefined-} + {-(count, excess) = quotRem (LBS.length raw) gzie-} + {-hlen = 12-} + {-gsize = (fromIntegral (_graylogChunkSize glog)) - hlen-} -- cgit v1.2.3