Commit fecade06 authored by Linshizhi's avatar Linshizhi

Add address to MonitorItem.

parent 191dd3a8
{-# LANGUAGE LambdaCase #-}
module Lib
( someFunc,
checkServerConn
......@@ -19,21 +21,23 @@ webHook = "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=2f209b18-4e9b-48
type WebHook = String
type Msg = String
type ServerAddr = String
data MonitorItem = MI {
url :: String,
status :: Bool,
lasttime :: UTCTime }
lasttime :: UTCTime,
server :: String }
hour :: NominalDiffTime
hour = 3600
defaultMI :: WebHook -> MonitorItem
defaultMI wh = MI wh True (UTCTime (fromOrdinalDate 0 0) 0)
defaultMI :: WebHook -> ServerAddr -> MonitorItem
defaultMI wh addr = MI wh True (UTCTime (fromOrdinalDate 0 0) 0) addr
monitorUrls :: [MonitorItem]
monitorUrls = [
defaultMI "http://69.234.216.199:5001/ppt2newdraft"
defaultMI "http://69.234.216.199:5001/ppt2newdraft" "69.234.216.199"
]
cmdFactory :: WebHook -> Msg -> String
......@@ -48,11 +52,13 @@ cmdFactory wh msg =
"}" ++
"}'"
checkServerConn :: IO Bool
checkServerConn = system "ping -q -c 1 -w 10 69.234.216.199"
>>= \code -> case code of
ExitSuccess -> return True
_ -> return False
checkServerConn :: ServerAddr -> IO Bool
checkServerConn addr = system ("ping -q -c 1 -w " ++ addr)
>>= \case {
ExitSuccess -> return True;
_ -> return False
}
onFailure :: MonitorItem -> IO MonitorItem
onFailure mi = do
......@@ -72,14 +78,14 @@ onFailure mi = do
doNotify :: IO MonitorItem
doNotify = do
connected <- checkServerConn
connected <- checkServerConn $ server mi
let cmd = if connected
then cmdFactory webHook ("Fail to access api " ++ url mi)
else cmdFactory webHook "Server (69.234.216.199) down"
current <- getCurrentTime
-- FIXME: system may failed need to check it's return code
system cmd >> return (MI (url mi) False current)
system cmd >> return (MI (url mi) False current (server mi))
onSuccess :: MonitorItem -> IO MonitorItem
onSuccess mi = do
......@@ -87,7 +93,7 @@ onSuccess mi = do
if not $ status mi
then do let cmd = cmdFactory webHook ("Api has recovered: " ++ url mi)
system cmd >> return (MI (url mi) True current)
system cmd >> return (MI (url mi) True current (server mi))
else return mi
checkUrl :: MonitorItem -> IO MonitorItem
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment