-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstandalone-es-service-port-check.py
More file actions
193 lines (161 loc) · 6.19 KB
/
Copy pathstandalone-es-service-port-check.py
File metadata and controls
193 lines (161 loc) · 6.19 KB
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
import os
import json
import argparse
from dotenv import load_dotenv
import os
from threading import Thread
import requests
import logging
import warnings
import socket
from elasticsearch import Elasticsearch
import pytz
import time, sys
import socket
from flask import Flask, render_template
from datetime import datetime, timedelta
import pandas as pd
import dotenv
warnings.filterwarnings("ignore")
load_dotenv()
# 로깅 설정
logging.basicConfig(
level=logging.INFO,
format="[%(asctime)s] [%(name)s] [%(levelname)s] %(message)s",
handlers=[logging.StreamHandler(sys.stdout)],
)
logger = logging.getLogger("Service-Port-Validation-Script")
es_service_infra = {
"test": {
"elasticsearch": [
{"Nodes #1": "localhost:9200"},
{"Nodes #2": "localhost:9201"},
{"Nodes #3": "localhost:9202"}
],
"logstash": [
{"Nodes #2": "localhost:5043"},
{"Nodes #2": "localhost:5044"},
{"Nodes #2": "localhost:5045"},
{"Nodes #2": "localhost:5046"},
{"Nodes #2": "localhost:5047"},
{"Nodes #2": "localhost:5048"}
],
"kibana": [
{"Nodes #3": "http://localhost:5601"}
],
"kafka": [
{"Data #1": "localhost:9092"},
{"Data #2": "localhost:9092"},
{"Data #3": "localhost:9092"}
],
"zookeeper": [
{"Data #1": "localhost:2181"},
{"Data #2": "localhost:2181"},
{"Data #3": "localhost:2181"}
],
"kafka_connect": [
{"Data #1": "http://localhost:8083"},
{"Data #2": "http://localhost:8083"},
{"Data #3": "http://localhost:8083"}
],
"spark_cluster": [
{"Data #1": "http://localhost:8480"}
]
},
"test1": {
"elasticsearch": [
{"Nodes #1": "localhost:9200"},
{"Nodes #2": "localhost:9201"},
{"Nodes #3": "localhost:9202"}
]
}
}
es_service_infra_chk = {
"Source_Host": [],
"Direction" : [],
"Env" : [],
"Target_Host": [],
"Service": [],
"Ports": [],
"Access": [],
"Description": []
}
def is_port_open(host, port):
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.settimeout(1) # Stop waiting after 1 second
return s.connect_ex((host, port)) == 0
def get_access_api(env, s_name, info, service_url) -> str:
logger.info(f"{env} - {info} - {service_url}")
try:
response = requests.get(url=service_url, headers=None, verify=False, timeout=10)
if response.status_code == 200:
es_service_infra_chk.get("Access").append("Ok")
return "Ok"
except Exception as e:
logger.error(f"get_access_api : {e}")
es_service_infra_chk.get("Access").append("Not reachable")
return e
def work() -> None:
try:
logger.info(f"Port checking")
for k in es_service_infra.keys():
for s_name in es_service_infra.get(k).keys():
for es_nodes_json in es_service_infra.get(k).get(s_name):
""" Update basic info """
es_service_infra_chk.get("Source_Host").append(socket.gethostname())
es_service_infra_chk.get("Direction").append("-->")
es_service_infra_chk.get("Env").append(k.upper())
es_service_infra_chk.get("Service").append(s_name.capitalize())
""""""
for info, value in es_nodes_json.items():
es_service_infra_chk.get("Ports").append(value.rsplit(":", 1)[-1])
''' http reqeust for testing'''
if s_name in ["kibana", "kafka_connect", "spark_cluster"]:
es_service_infra_chk.get("Target_Host").append(value.split(":")[1].replace("//", ""))
# es_service_infra_chk.get("Ports").append(value.split(":")[2])
logger.info(get_access_api(k, s_name, info, value))
else:
# es_service_infra_chk.get("Ports").append(value.split(":")[1])
es_service_infra_chk.get("Target_Host").append(value.split(":")[0].replace("//", ""))
''' Tcp socket for testing'''
if is_port_open(value.split(":")[0], int(value.split(":")[1])):
es_service_infra_chk.get("Access").append("Ok")
else:
es_service_infra_chk.get("Access").append("Not reachable")
es_service_infra_chk.get("Description").append(info)
print("\n")
logger.info(json.dumps(es_service_infra_chk, indent=2))
print("\n")
df = pd.DataFrame(es_service_infra_chk)
print(df)
# except (KeyboardInterrupt, SystemExit) as e:
except Exception as e:
logger.error(f"work func : {e}")
pass
if __name__ == "__main__":
"""
Ingnore ssl pip - pip install numpy==1.26.4 --trusted-host pypi.org --trusted-host files.pythonhosted.org
Running this service allows us to check and delete old ES indices
python ./standalone-es-services-port-check.py
"""
parser = argparse.ArgumentParser(
description="Running this service allows us to check the service ports using this script"
)
global gloabal_default_timezone
gloabal_default_timezone = pytz.timezone("US/Eastern")
# --
# Only One process we can use due to 'Global Interpreter Lock'
# 'Multiprocessing' is that we can use for running with multiple process
# --
try:
T = []
th1 = Thread(target=work, args=())
th1.daemon = True
th1.start()
T.append(th1)
for t in T:
while t.is_alive():
t.join(0.5)
# work(target_server)
except Exception as e:
logger.error(e)