-
-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathmonitoring.hs
209 lines (154 loc) · 5.47 KB
/
monitoring.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
{-# LANGUAGE NumericUnderscores, TypeApplications #-}
import qualified Network.Socket as S
import Network.Socket.ByteString (recv, sendAll)
import Control.Exception.Safe
import qualified Data.ByteString as BS
import qualified Data.ByteString.Char8
import System.Environment
import System.Exit
import System.IO
import System.Signal
import Control.Concurrent
import Control.Concurrent.Async
import Control.Concurrent.STM
import qualified Data.Sequence as Seq
import Data.Ratio ((%))
import Control.Monad (forever, when)
import Data.Foldable (asum, for_, find)
import Data.Maybe (mapMaybe)
-- An event report represents the result of a single action.
data EventReport = Success | Failure
deriving Eq
-- The system status is an overview of whether, in general, actions tend to be succeeding or failing.
data SystemStatus = Okay | Alarm
deriving Eq
main =
do
hSetBuffering stdout LineBuffering
args <- getArgs
case args of
["aggregate-reports"] -> aggregateReportsMain
["send-demo-reports"] -> sendDemoReportsMain
_ -> die "Invalid args"
aggregateReportsMain =
withServerSocket $ \serverSocket ->
do
putStrLn "The monitoring server has started."
reportQueue <- atomically newTQueue
alarmQueue <- atomically newTQueue
foldr1 race_
[ receiveReports serverSocket reportQueue
, analyzeReports reportQueue alarmQueue
, sendAlarms alarmQueue
, waitForTerminationSignal
]
putStrLn "The monitoring server is stopping."
waitForTerminationSignal =
do
terminate <- atomically (newTVar False)
installHandler sigTERM $ \_signal ->
atomically (writeTVar terminate True)
atomically (readTVar terminate >>= check)
--- Message format ---
encodeReport r =
case r of
Failure -> '0'
Success -> '1'
decodeReport c =
find (\r -> encodeReport r == c) [Failure, Success]
--- Receiving event reports ---
serverAddress = S.SockAddrUnix "\0haskell-phrasebook/monitoring"
openSocket = S.socket S.AF_UNIX S.Stream S.defaultProtocol
withServerSocket action =
bracket openSocket S.close $ \serverSocket ->
do
S.bind serverSocket serverAddress
S.listen serverSocket S.maxListenQueue
action serverSocket
receiveReports serverSocket reportQueue =
forever $
mask $ \unmask ->
do
(clientSocket, _clientAddr) <- S.accept serverSocket
forkFinally
(unmask (receiveReports' clientSocket reportQueue))
(\_ -> S.close clientSocket)
receiveReports' clientSocket reportQueue = continue
where
continue =
do
receivedBytes <- recv clientSocket 1024
case BS.length receivedBytes of
0 -> return ()
_ ->
do
receiveReports'' receivedBytes reportQueue
continue
receiveReports'' receivedBytes reportQueue =
for_ @[] (Data.ByteString.Char8.unpack receivedBytes) $ \c ->
for_ @Maybe (decodeReport c) $ \r -> do
putStrLn (case r of Success -> "1 (success)"
Failure -> "0 (failure)")
atomically (writeTQueue reportQueue r)
--- Analysis of system status changes using event reports ---
reportWindowSize = 10
okayThreshold = 80 % 100
alarmThreshold = 50 % 100
analysis reports
| Seq.length reports < reportWindowSize = Nothing
| successRate <= alarmThreshold = Just Alarm
| successRate >= okayThreshold = Just Okay
| otherwise = Nothing
where
successes = Seq.filter (== Success) reports
successRate = Seq.length successes % Seq.length reports
analyzeReports reportQueue alarmQueue = continue Nothing Seq.empty
where
continue status reports =
do
newReport <- atomically (readTQueue reportQueue)
let reports' = Seq.take reportWindowSize
(newReport Seq.<| reports)
let status' = asum [analysis reports', status]
for_ @Maybe status' $ \s ->
when (status /= status') $
atomically (writeTQueue alarmQueue s)
continue status' reports'
--- Sending alerts about system status changes ---
sendAlarms alarmQueue =
forever $
do
a <- atomically (readTQueue alarmQueue)
case a of
Alarm -> putStrLn "Alarm! System is in a degraded state."
Okay -> putStrLn "System status is normal."
--- Client that sends event reports to an aggregation service ---
sendDemoReportsMain =
do
reportQueue <- atomically newTQueue
foldr1 race_
[ generateReports reportQueue
, sendReports reportQueue
]
--- A fixed schedule of event reports for demonstration purposes ---
demoReports = mapMaybe decodeReport
"1111111111111010011000001000000100011101111110111111"
-- successes -- -- failures -- -- successes --
generateReports reportQueue =
for_ demoReports $ \r ->
do
atomically (writeTQueue reportQueue r)
threadDelay 100_000
--- Sending reports to the server ---
withClientSocket action =
bracket openSocket S.close $ \clientSocket ->
do
S.connect clientSocket serverAddress
action clientSocket
sendReports reportQueue =
withClientSocket $ \clientSocket ->
forever $
do
r <- atomically (readTQueue reportQueue)
sendAll clientSocket
(Data.ByteString.Char8.pack [encodeReport r])