-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsysmon_alarm_daemon.py
331 lines (298 loc) · 13.2 KB
/
sysmon_alarm_daemon.py
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import telegram, os, socket, string, sys, psutil, time
"""
[1]감시 대상 서버 라벨 설정
"""
serverName = 'TEST'
title = '[' + serverName + ' 서버]\n'
"""
[2]Telegram bot token value
"""
sigong_token = 'YOUR_TOKEN'
sigong = 'YOUR_CHAT_ID'
bot = telegram.Bot(token = sigong_token)
"""
[3]텔레그램 메세지 전송
"""
def send(chat):
bot.sendMessage(sigong, chat, parse_mode='HTML')
"""
[4]리소스 사용량 체크
"""
def getLoadAverage(): # LoadAverage 측정
return psutil.getloadavg()[0]
def getCpuUsage(): # CPU 사용률 측정
cpu = 0
for x in range(4):
cpu = psutil.cpu_percent(interval=1, percpu=False)
return cpu
def getSwapUsage(): # swap 메모리 측정
return round(psutil.swap_memory()[3])
def getMemUsage(): # 메모리 사용률 측정
return round(psutil.virtual_memory()[2])
def getRootdisk(): # 최상위 디렉토리(/) 사용량 측정
return psutil.disk_usage('/')[3]
def getHomedisk(): # home 디렉토리 사용량 측정
return round(psutil.disk_usage('/home')[3])
"""
[5]각 함수를 호출하여 현재 리소스 사용량을 가져온다.
"""
current_load_Average = getLoadAverage()
current_cpu_Usage = getCpuUsage()
current_swap_Usage = getSwapUsage()
current_mem_Usage = getMemUsage()
current_root_Disk = getRootdisk()
current_home_Disk = getHomedisk()
"""
[6]리소스 임계치
"""
normal_root_disk = 90
normal_home_disk = 4
normal_loadAverage_first = 3.0
normal_loadAverage_second = 5.0
normal_loadAverage_third = 10.0
normal_swapUsage = 10
normal_cpuLimit_first = 20
normal_cpuLimit_second = 50
normal_cpuLimit_third = 70
normal_cpuLimit_fourth = 80
normal_cpuLimit_fifth = 90
normal_memLimit_first = 60
normal_memLimit_second = 80
normal_memLimit_third = 90
"""
[7]데몬 생성 함수
"""
def daemon():
try:
pid = os.fork()
if pid > 0:
print('PID: %d' % pid)
sys.exit()
except OSError as error:
print('Unable to fork. Error: %d (%s)' % (error.errno, error.strerror))
sys.exit()
sys_chk()
"""
[8]리소스 사용량이 정의된 임계치보다 높으면 "[경고]" 메시지를 전송하고, 임계치 이하면 "[복구]" 메시지를 전송한다.
"""
def sys_chk():
"new session create"
os.setsid()
os.open("/dev/null", os.O_RDWR)
os.dup(0)
os.dup(0)
home_alert = True
root_alert = True
load_alert_first = True
load_alert_second = True
load_alert_third = True
cpu_first_alert = True
cpu_second_alert = True
cpu_third_alert = True
cpu_fourth_alert = True
cpu_fifth_alert = True
swap_alert = True
mem_first_alert = True
mem_second_alert = True
mem_third_alert = True
while True:
msg = title
try:
current_home_Disk = getHomedisk()
if normal_home_disk < current_home_Disk:
if home_alert == True:
home_alert = False
msg += '/home 임계치: ' + str(normal_home_disk) + '%\n'
msg += '[경고] /home 사용량 : ' + str(current_home_Disk) + '%\n'
send(msg)
elif normal_home_disk >= current_home_Disk:
if home_alert == False:
home_alert = True
msg += '[복구] 현재 /home 사용량 : ' + str(current_home_Disk) + '%\n'
send(msg)
except:
pass
try: # / 디스크 경고 block
current_root_Disk = getRootdisk()
if normal_root_disk < current_root_Disk:
if root_alert == True:
root_alert = False
msg += ' / 임계치: ' + str(normal_root_disk) + '%\n'
msg += '[경고] / 사용량 : ' + str(current_root_Disk) + '%\n'
send(msg)
elif normal_root_disk >= current_root_Disk:
if root_alert == False:
root_alert = True
msg += '[복구] 현재 / 사용량 : ' + str(current_root_Disk) + '%\n'
send(msg)
except:
pass
try: # first LoadAverage alert block
current_load_Average = getLoadAverage()
if normal_loadAverage_first < current_load_Average:
if load_alert_first == True:
load_alert_first = False
msg += 'LoadAverage 임계치: ' + str(normal_loadAverage_first) + '\n'
msg += '[경고] LoadAverage : ' + str(current_load_Average) + '\n'
send(msg)
elif normal_loadAverage_first >= current_load_Average:
if load_alert_first == False:
load_alert_first = True
msg += '[복구] 현재 LoadAverage : ' + str(current_load_Average) + '\n'
send(msg)
except:
pass
try: # second LoadAverage alert block
current_load_Average = getLoadAverage()
if normal_loadAverage_second < current_load_Average:
if load_alert_second == True:
load_alert_second = False
msg += 'LoadAverage 임계치: ' + str(normal_loadAverage_second) + '\n'
msg += '[경고] LoadAverage : ' + str(current_load_Average) + '\n'
send(msg)
elif normal_loadAverage_second >= current_load_Average:
if load_alert_second == False:
load_alert_second = True
except:
pass
try: # third LoadAverage alert block
current_load_Average = getLoadAverage()
if normal_loadAverage_third < current_load_Average:
if load_alert_third == True:
load_alert_third = False
msg += 'LoadAverage 임계치: ' + str(normal_loadAverage_third) + '\n'
msg += '[경고] LoadAverage : ' + str(current_load_Average) + '\n'
send(msg)
elif normal_loadAverage_third >= current_load_Average:
if load_alert_third == False:
load_alert_third = True
except:
pass
try: # swap 경고 block
current_swap_Usage = getSwapUsage()
if normal_swapUsage < current_swap_Usage:
if swap_alert == True:
swal_alert = False
msg += 'SWAP 임계치: ' + str(swap_alert_list) + '%\n'
msg += '[경고] SWAP 사용량 : ' + str(current_swap_Usage) + '%\n'
send(msg)
elif normal_swapUsage >= current_swap_Usage:
if swap_alert == False:
swap_alert = True
msg += '[복구] 현재 SWAP 사용량 : ' + str(current_swap_Usage) + '%\n'
send(msg)
except:
pass
try: # first CPU 경고 block
current_cpu_Usage = getCpuUsage()
if normal_cpuLimit_first < current_cpu_Usage:
if cpu_first_alert == True:
cpu_first_alert = False
msg += 'CPU 임계치: ' + str(normal_cpuLimit_first) + '%\n'
msg += '[경고] CPU 사용량 : ' + str(current_cpu_Usage) + '%\n'
send(msg)
elif normal_cpuLimit_first >= current_cpu_Usage:
if cpu_first_alert == False:
cpu_first_alert = True
msg += '[복구] 현재 CPU 사용량 : ' + str(current_cpu_Usage) + '%\n'
send(msg)
except:
pass
try: # second CPU 경고 block
current_cpu_Usage = getCpuUsage()
if normal_cpuLimit_second < current_cpu_Usage:
if cpu_second_alert == True:
cpu_second_alert = False
msg += 'CPU 임계치: ' + str(normal_cpuLimit_second) + '%\n'
msg += '[경고] CPU 사용량 : ' + str(current_cpu_Usage) + '%\n'
send(msg)
elif normal_cpuLimit_second >= current_cpu_Usage:
if cpu_second_alert == False:
cpu_second_alert = True
except:
pass
try: # third CPU 경고 block
current_cpu_Usage = getCpuUsage()
if normal_cpuLimit_third < current_cpu_Usage:
if cpu_third_alert == True:
cpu_third_alert = False
msg += 'CPU 임계치: ' + str(normal_cpuLimit_third) + '%\n'
msg += '[경고] CPU 사용량 : ' + str(current_cpu_Usage) + '%\n'
send(msg)
elif normal_cpuLimit_third >= current_cpu_Usage:
if cpu_third_alert == False:
cpu_third_alert = True
except:
pass
try: # fourth CPU 경고 block
current_cpu_Usage = getCpuUsage()
if normal_cpuLimit_fourth < current_cpu_Usage:
if cpu_fourth_alert == True:
cpu_fourth_alert = False
msg += 'CPU 임계치: ' + str(normal_cpuLimit_fourth) + '%\n'
msg += '[경고] CPU 사용량 : ' + str(current_cpu_Usage) + '%\n'
send(msg)
elif normal_cpuLimit_fourth >= current_cpu_Usage:
if cpu_fourth_alert == False:
cpu_fourth_alert = True
except:
pass
try: # fifth CPU 경고 block
current_cpu_Usage = getCpuUsage()
if normal_cpuLimit_fifth < current_cpu_Usage:
if cpu_fifth_alert == True:
cpu_fifth_alert = False
msg += 'CPU 임계치: ' + str(normal_cpuLimit_fifth) + '%\n'
msg += '[경고] CPU 사용량 : ' + str(current_cpu_Usage) + '%\n'
send(msg)
elif normal_cpuLimit_fifth >= current_cpu_Usage:
if cpu_fifth_alert == False:
cpu_fifth_alert = True
except:
pass
try: # first memory rate alert block
current_mem_Usage = getMemUsage()
if normal_memLimit_first < current_mem_Usage:
if mem_first_alert == True:
mem_first_alert = False
msg += 'Memory 임계치: ' + str(normal_memLimit_first) + '%\n'
msg += '[경고] Memory 사용량 : ' + str(current_mem_Usage) + '%\n'
send(msg)
elif normal_memLimit_first >= current_mem_Usage:
if mem_first_alert == False:
mem_first_alert = True
msg += '[복구] 현재 Memory 사용량 : ' + str(current_mem_Usage) + '%\n'
send(msg)
except:
pass
try: # second memory rate alert block
current_mem_Usage = getMemUsage()
if normal_memLimit_second < current_mem_Usage:
if mem_second_alert == True:
mem_second_alert = False
msg += 'Memory 임계치: ' + str(normal_memLimit_second) + '%\n'
msg += '[경고] Memory 사용량 : ' + str(current_mem_Usage) + '%\n'
send(msg)
elif normal_memLimit_second >= current_mem_Usage:
if mem_second_alert == False:
mem_second_alert = True
except:
pass
try: # third memory rate alert block
current_mem_Usage = getMemUsage()
if normal_memLimit_third < current_mem_Usage:
if mem_third_alert == True:
mem_third_alert = False
msg += 'Memory 임계치: ' + str(normal_memLimit_third) + '%\n'
msg += '[경고] Memory 사용량 : ' + str(current_mem_Usage) + '%\n'
send(msg)
elif normal_memLimit_third >= current_mem_Usage:
if mem_third_alert == False:
mem_third_alert = True
except:
pass
time.sleep(3)
if __name__ == '__main__':
daemon()