module Network.Metrics.Ganglia (
Slope(..)
, MetricType(..)
, Metric(..)
, defaultMetric
, open
, emit
, test
, I.Handle
, I.close
) where
import Control.Concurrent (threadDelay)
import Control.Monad (unless, liftM)
import Data.Binary.Put
import Data.Bits ((.&.))
import Data.Char (toLower)
import Data.Data (Data, Typeable)
import Data.Default (Default, def)
import Data.Int (Int32)
import Data.Word (Word32)
import Network.Socket
import System.Random (randomRIO)
import qualified Data.ByteString as B
import qualified Data.ByteString.Char8 as BS
import qualified Network.Metrics.Internal as I
data Slope = Zero | Positive | Negative | Both | Unspecified
deriving (Data, Typeable, Show, Eq, Enum)
data MetricType = String | Int8 | UInt8 | Int16 | UInt16 | Int32 | UInt32 | Float | Double
deriving (Data, Typeable, Eq, Show)
data Metric = Metric
{ name :: BS.ByteString
, type' :: MetricType
, units :: BS.ByteString
, value :: BS.ByteString
, host :: BS.ByteString
, spoof :: BS.ByteString
, group :: BS.ByteString
, slope :: Slope
, tmax :: Word32
, dmax :: Word32
} deriving (Show)
instance Default Metric where
def = defaultMetric
defaultMetric :: Metric
defaultMetric = Metric
{ name = "magical_metric"
, type' = UInt16
, units = ""
, value = "0"
, host = ""
, spoof = ""
, group = ""
, slope = Both
, tmax = 60
, dmax = 0
}
open :: String -> String -> IO I.Handle
open = I.open Datagram
emit :: Metric -> I.Handle -> IO I.Handle
emit metric handle@(I.Handle sock addr) = do
sIsConnected sock >>= \b -> unless b $ connect sock addr
_ <- push putMetaData handle
_ <- push putMetric handle
return handle
where
push fn = I.emit . runPut $ fn metric
bufferSize :: Integer
bufferSize = 1500
putMetaData :: Metric -> Put
putMetaData metric@Metric{..} = do
putHeader 128 metric
putType type'
putString name
putString units
putEnum slope
putUInt tmax
putUInt dmax
putGroup group
putMetric :: Metric -> Put
putMetric metric@Metric{..} = do
putHeader 133 metric
putString "%s"
putString value
putHeader :: Int32 -> Metric -> Put
putHeader code Metric{..} = do
putInt code
putString host
putString name
putString spoof
putGroup :: BS.ByteString -> Put
putGroup group | BS.null group = putInt 0
| otherwise = do
putInt 1
putString "GROUP"
putString group
putInt :: Int32 -> Put
putInt = putWord32be . fromIntegral
putUInt :: Word32 -> Put
putUInt = putWord32be
putEnum :: Enum a => a -> Put
putEnum = putInt . fromIntegral . fromEnum
putString :: BS.ByteString -> Put
putString bstr = do
putInt $ fromIntegral len
putByteString bstr
case fromIntegral len .&. 3 of
0 -> return ()
m -> putByteString $ B.replicate (4 m) 0
where
len = BS.length bstr
putType :: MetricType -> Put
putType = putString . BS.pack . map toLower . show
oneSecond :: Int
oneSecond = 1000000
variance :: (Int, Int)
variance = (0, 1000)
sample :: [a] -> IO a
sample xs = liftM (xs !!) (randomRIO (0, length xs 1))
test :: String -> String -> IO a
test host port = open host port >>= loop
where
loop h = do
r <- randomRIO variance :: IO Int
n <- sample ["magic", "candy", "unicorns"]
f <- emit defaultMetric { name = n, value = BS.pack $ show r } h
threadDelay oneSecond
loop f