-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathservice_port_socket_alive_chk_daemon.py
244 lines (222 loc) · 8.93 KB
/
service_port_socket_alive_chk_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
import telegram
from telegram.ext import Updater, CommandHandler, MessageHandler, Filters
import requests, sys, time, daemon, os, socket, string, sys
from requests.exceptions import Timeout
"""
서버 라벨 설정(서버 또는 호스트의 이름)
"""
serverName = 'service Port'
title = '[' + serverName + ']\n'
"""
Telegram bot의 token value 설정
"""
sigong_token = 'Your_token'
sigong = 'Your Chat ID'
bot = telegram.Bot(token = sigong_token)
"""
메시지 전송 설정
"""
def send(chat):
bot.sendMessage(sigong, chat, parse_mode='HTML')
"""
감시 대상 URL, port
"""
host = ['10.10.10.10','10.10.10.11','10.10.10.12','10.10.10.13','10.10.10.14','10.10.10.15','10.10.10.16','10.10.10.17','10.10.10.18','10.10.10.19']
port = 80
"""
소켓을 생성하고 주어진 host/port로 연결을 연결을 시도한다.
연결에 성공하면 True, 실패하면 False
"""
def Creat_Socket_Connect_Host_Port(host, port):
try:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.settimeout(5);
s.connect((host, port))
return True
except (socket.gaierror, socket.error, socket.timeout) as e:
return False
"""
데몬 생성 함수
"""
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()
GET_URL_STATUS_CHK()
"""
URL 상태 체크 함수
"""
def GET_URL_STATUS_CHK():
os.setsid()
os.open("/dev/null", os.O_RDWR)
os.dup(0)
os.dup(0)
status_of_url1 = True # 소켓 상태 기본 값 = True
status_of_url2 = True
status_of_url3 = True
status_of_url4 = True
status_of_url5 = True
status_of_url6 = True
status_of_url7 = True
status_of_url8 = True
status_of_url9 = True
status_of_url10 = True
while True:
try:
msg = title
current_url_1_status = Creat_Socket_Connect_Host_Port(host[0], port)
if current_url_1_status == True:
if status_of_url1 == True:
status_of_url1 = False
msg += host[0] + ": Jeus Service is available at %d port\r\n" % port
send(msg)
elif current_url_1_status == False:
if status_of_url1 == False:
status_of_url1 = True
msg += host[0] + " Jeus socket timeout at %d port\r\n" % port
send(msg)
except:
pass
try:
msg = title
current_url_2_status = Creat_Socket_Connect_Host_Port(host[1], port)
if current_url_2_status == True:
if status_of_url2 == True:
status_of_url2 = False
msg += host[1] + ": Jeus Service is available at %d port\r\n" % port
send(msg)
elif current_url_2_status == False:
if status_of_url2 == False:
status_of_url2 = True
msg += host[1] + " Jeus socket timeout at %d port\r\n" % port
send(msg)
except:
pass
try:
msg = title
current_url_3_status = Creat_Socket_Connect_Host_Port(host[2], port)
if current_url_3_status == True:
if status_of_url3 == True:
status_of_url3 = False
msg += host[2] + ": Jeus Service is available at %d port\r\n" % port
send(msg)
elif current_url_3_status == False:
if status_of_url3 == False:
status_of_url3 = True
msg += host[2] + " Jeus socket timeout at %d port\r\n" % port
send(msg)
except:
pass
try:
msg = title
current_url_4_status = Creat_Socket_Connect_Host_Port(host[3], port)
if current_url_4_status == True:
if status_of_url4 == True:
status_of_url4 = False
msg += host[3] + ": Jeus Service is available at %d port\r\n" % port
send(msg)
elif current_url_4_status == False:
if status_of_url4 == False:
status_of_url4 = True
msg += host[3] + " Jeus socket timeout at %d port\r\n" % port
send(msg)
except:
pass
try:
msg = title
current_url_5_status = Creat_Socket_Connect_Host_Port(host[4], port)
if current_url_5_status == True:
if status_of_url5 == True:
status_of_url5 = False
msg += host[4] + ": Jeus Service is available at %d port\r\n" % port
send(msg)
elif current_url_5_status == False:
if status_of_url5 == False:
status_of_url5 = True
msg += host[4] + " Jeus socket timeout at %d port\r\n" % port
send(msg)
except:
pass
try:
msg = title
current_url_6_status = Creat_Socket_Connect_Host_Port(host[5], port)
if current_url_6_status == True:
if status_of_url6 == True:
status_of_url6 = False
msg += host[5] + ": Jeus Service is available at %d port\r\n" % port
send(msg)
elif current_url_6_status == False:
if status_of_url6 == False:
status_of_url6 = True
msg += host[5] + " Jeus socket timeout at %d port\r\n" % port
send(msg)
except:
pass
try:
msg = title
current_url_7_status = Creat_Socket_Connect_Host_Port(host[6], port)
if current_url_7_status == True:
if status_of_url7 == True:
status_of_url7 = False
msg += host[6] + ": Jeus Service is available at %d port\r\n" % port
send(msg)
elif current_url_7_status == False:
if status_of_url7 == False:
status_of_url7 = True
msg += host[6] + " Jeus socket timeout at %d port\r\n" % port
send(msg)
except:
pass
try:
msg = title
current_url_8_status = Creat_Socket_Connect_Host_Port(host[7], port)
if current_url_8_status == True:
if status_of_url8 == True:
status_of_url8 = False
msg += host[7] + ": Jeus Service is available at %d port\r\n" % port
send(msg)
elif current_url_8_status == False:
if status_of_url8 == False:
status_of_url8 = True
msg += host[7] + " Jeus socket timeout at %d port\r\n" % port
send(msg)
except:
pass
try:
msg = title
current_url_9_status = Creat_Socket_Connect_Host_Port(host[8], port)
if current_url_9_status == True:
if status_of_url9 == True:
status_of_url9 = False
msg += host[8] + ": Jeus Service is available at %d port\r\n" % port
send(msg)
elif current_url_9_status == False:
if status_of_url9 == False:
status_of_url9 = True
msg += host[8] + " Jeus socket timeout at %d port\r\n" % port
send(msg)
except:
pass
try:
msg = title
current_url_10_status = Creat_Socket_Connect_Host_Port(host[9], port)
if current_url_10_status == True:
if status_of_url10 == True:
status_of_url10 = False
msg += host[9] + ": Jeus Service is available at %d port\r\n" % port
send(msg)
elif current_url_10_status == False:
if status_of_url10 == False:
status_of_url10 = True
msg += host[9] + " Jeus socket timeout at %d port\r\n" % port
send(msg)
except:
pass
time.sleep(5)
if __name__ == '__main__':
daemon()