aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
-rw-r--r--Makefile6
-rw-r--r--src/Graylog/Gelf.hs9
-rw-r--r--src/Graylog/Types.hs27
-rw-r--r--src/Graylog/UDP.hs40
-rw-r--r--test/Test/Graylog/UDP.hs18
5 files changed, 68 insertions, 32 deletions
diff --git a/Makefile b/Makefile
index f577035..79e6937 100644
--- a/Makefile
+++ b/Makefile
@@ -1,2 +1,8 @@
1build:
2 stack build
3
4repl:
5 stack repl
6
1graylog: 7graylog:
2 docker run -t -p 9000:9000 -p 12201:12201 -p 12201:12201/udp graylog2/allinone 8 docker run -t -p 9000:9000 -p 12201:12201 -p 12201:12201/udp graylog2/allinone
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
65simpleGelf
66 :: Text -- ^ Hostname
67 -> Text -- ^ Short message
68 -> GELF
69simpleGelf 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
3module Graylog.Types 4module Graylog.Types
@@ -14,6 +15,7 @@ module Graylog.Types
14 , closeGraylog 15 , closeGraylog
15 ) where 16 ) where
16 17
18import Data.List
17import Data.Text (Text) 19import Data.Text (Text)
18import qualified Data.Text as T 20import qualified Data.Text as T
19import Network.BSD 21import Network.BSD
@@ -34,19 +36,18 @@ data Graylog
34defaultChunkSize :: ChunkSize 36defaultChunkSize :: ChunkSize
35defaultChunkSize = 8192 37defaultChunkSize = 8192
36 38
37openGraylog :: HostName -> ServiceName -> ChunkSize -> IO (Either String Graylog) 39openGraylog
38openGraylog host port chksize = do 40 :: HostName -> ServiceName -> ChunkSize -> IO (Either String Graylog)
39 infos <- getAddrInfo Nothing (Just host) (Just port) 41openGraylog 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
49closeGraylog :: Graylog -> IO () 52closeGraylog :: Graylog -> IO ()
50closeGraylog Graylog{..} = close _graylogSocket 53closeGraylog 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
7import Data.Aeson 7import Data.Aeson
8import qualified Data.ByteString.Lazy as LBS 8{-import Data.ByteString.Builder-}
9import Data.Word 9{-import qualified Data.ByteString.Lazy as LBS-}
10{-import Data.Word-}
10import Network.Socket.ByteString.Lazy 11import Network.Socket.ByteString.Lazy
11import System.Random 12{-import System.Random-}
12 13
13import Graylog.Gelf as Export 14import Graylog.Gelf as Export
14import Graylog.Types as Export 15import Graylog.Types as Export
15 16
16sendLog :: Graylog -> GELF -> IO () 17sendLog :: Graylog -> GELF -> IO ()
17sendLog glog msg = mapM_ (send $ _graylogSocket glog) cks 18sendLog 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
22chunky :: Graylog -> LBS.ByteString -> IO [LBS.ByteString] 25{-sendLog :: Graylog -> GELF -> IO ()-}
23chunky 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-}
diff --git a/test/Test/Graylog/UDP.hs b/test/Test/Graylog/UDP.hs
index 67da6fe..d62f11a 100644
--- a/test/Test/Graylog/UDP.hs
+++ b/test/Test/Graylog/UDP.hs
@@ -1,13 +1,23 @@
1{-# LANGUAGE OverloadedStrings #-}
2
1module Test.Graylog.UDP where 3module Test.Graylog.UDP where
2 4
3import Test.Tasty 5import Test.Tasty
4import Test.Tasty.HUnit 6import Test.Tasty.HUnit
5 7
8import Graylog.UDP
9
6tests :: TestTree 10tests :: TestTree
7tests = testGroup "Test.Graylog.UDP" 11tests = testGroup "Test.Graylog.UDP"
8 [ testCase "Validation: Something" case_validateSomething 12 {-[ testCase "Send: Sample" case_validateSomething-}
13 [ testCase "Send sample message." case_sendSample
9 ] 14 ]
10 15
11case_validateSomething :: IO () 16case_sendSample :: IO ()
12case_validateSomething = return () 17case_sendSample = do
13 18 eglog <- openGraylog "192.168.99.100" "12201" defaultChunkSize
19 case eglog of
20 Left e -> assertFailure e
21 Right g -> sendLog g sample
22 where
23 sample = simpleGelf "localhost" "hello world!"