-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
371 lines (329 loc) · 11.3 KB
/
main.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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
"""
Tool for extending a standard Kubernetes manifest file into a manifest with logging capabilities.
It does so by injecting the log analysis components into the input file.
"""
import argparse
import json
import yaml
from elasticsearch import Elasticsearch
from yrca import yrca_process_logs
from utils import (
load_manifests,
build_merged_text,
is_valid_k8s_namespace,
is_valid_timeout,
)
DEFAULT_NAMESPACE = "log-enabled"
def parse_options():
"""
Parse command line options.
"""
parser = argparse.ArgumentParser(description="PROGETTO")
parser.add_argument(
"-i",
"--inject",
metavar="input_file",
help="Inject log analysis components into the specified input file",
)
parser.add_argument(
"-o",
"--output",
help="Specify an output file pathname. \
If not specified, the output will be printed to stdout.",
)
parser.add_argument(
"-t",
"--timeout",
default="15s",
help="Specify a custom Envoy timeout. Defaults to 15s",
)
parser.add_argument(
"-n",
"--namespace",
default="",
help="Specify a suffix for the namespace (resulting in log-enabled-suffix)",
)
parser.add_argument(
"--no-header",
action="store_true",
help="Do not add the Istio and ELK Stack components to the output file \
(useful for adding deployments to an already running main deployment file)",
)
parser.add_argument(
"-c",
"--connect",
help="Connect to the specified Elasticsearch instance",
type=str,
)
parser.add_argument(
"-p", "--port", default=9200, help="Elasticsearch port", type=int
)
parser.add_argument(
"--pod",
help="Dumps the logs of the specified pod (to a file, if used with -o). \
Cannot be used with yRCA log output format.",
type=str,
)
parser.add_argument(
"--dump-all",
action="store_true",
help="Dump all the logs in the Elasticsearch instance (to a file, if used with -o). \
Cannot be used with yRCA log output format.",
)
parser.add_argument(
"-f",
"--format",
choices=["yrca", "gelf", "syslog"],
default="yrca",
help="Specify the format of the logs to be processed. Defaults to yrca.",
)
return parser.parse_args()
def inject(yaml_file, timeout="15s", no_header=False, custom_namespace_suffix=""):
"""
Inject log analysis components into a YAML file.
"""
final_namespace = DEFAULT_NAMESPACE
if custom_namespace_suffix:
final_namespace = DEFAULT_NAMESPACE + "-" + custom_namespace_suffix
if not is_valid_k8s_namespace(final_namespace):
print(
f"Error: The namespace {final_namespace} is not valid. \
Please specify a valid namespace name."
)
return None
if not is_valid_timeout(timeout):
print(
f"Error: The timeout {timeout} is not valid. Please specify a valid timeout."
)
return None
try:
manifests = load_manifests(
[
yaml_file,
"manifest/namespace_manifest.yaml",
"manifest/istio_manifest.yaml",
"manifest/elk_manifest.yaml",
]
)
except FileNotFoundError:
print(
"Error: A necessary file has not been found. \
Ensure the file paths are correct and try again."
)
return None
virtual_services = []
docs_text = []
namespace_template = yaml.safe_load(manifests["manifest/namespace_manifest.yaml"])
namespace_template["metadata"]["name"] = final_namespace
docs_text.append(yaml.dump(namespace_template, default_flow_style=False))
for doc in yaml.safe_load_all(manifests[yaml_file]):
if doc:
doc = replace_namespace(doc, final_namespace)
if doc["kind"].lower() == "service":
service_name = doc["metadata"]["name"]
virtual_service = create_virtual_service(
service_name, timeout, final_namespace
)
virtual_services.append(virtual_service)
docs_text.append(yaml.dump(doc, default_flow_style=False))
virtual_services_text = [
yaml.dump(vs, default_flow_style=False) for vs in virtual_services
]
if no_header:
merged_text = build_merged_text(docs_text + virtual_services_text)
else:
merged_text = build_merged_text(
[
manifests["manifest/istio_manifest.yaml"],
manifests["manifest/elk_manifest.yaml"],
]
+ docs_text
+ virtual_services_text
)
return merged_text
def replace_namespace(yaml_section, namespace):
"""
Replace or set the namespace in the YAML section.
"""
if "metadata" not in yaml_section:
yaml_section["metadata"] = {}
yaml_section["metadata"]["namespace"] = namespace
return yaml_section
def create_virtual_service(service_name, timeout, namespace):
"""
Create an Istio VirtualService configuration for a given service.
"""
return {
"apiVersion": "networking.istio.io/v1alpha3",
"kind": "VirtualService",
"metadata": {"name": service_name, "namespace": namespace},
"spec": {
"hosts": [service_name],
"http": [
{"route": [{"destination": {"host": service_name}}], "timeout": timeout}
],
},
}
def retrieve_logs(es, query, scroll_time="1m", size=10000):
"""
Retrieve logs from Elasticsearch based on the provided query.
"""
logs = []
try:
response = es.search(
index="logstash-*",
query=query,
scroll=scroll_time,
size=size,
sort="@timestamp:asc",
)
while len(response["hits"]["hits"]):
logs.extend(response["hits"]["hits"])
response = es.scroll(scroll_id=response["_scroll_id"], scroll=scroll_time)
es.clear_scroll(scroll_id=response["_scroll_id"])
except Exception as e:
print(
"Error while retrieving logs from Elasticsearch instance. The error is specified below."
)
print(e)
return []
return logs
def connect_elasticsearch(
es_host="localhost",
port=9200,
dump_all=False,
output_format="yrca",
pod=None,
custom_namespace_suffix="",
):
"""
Connect to an Elasticsearch instance and retrieve logs based on the provided query.
"""
if output_format == "yrca" and (dump_all or pod):
print(
"The yRCA format (the default) only requires the dump of Envoy proxies, \
so it cannot be used with --dump-all or --pod. Specify a custom format with -f."
)
return None
if dump_all and pod:
print("Cannot use --dump-all and --pod together.")
return None
final_namespace = DEFAULT_NAMESPACE
if custom_namespace_suffix:
final_namespace = DEFAULT_NAMESPACE + "-" + custom_namespace_suffix
if not is_valid_k8s_namespace(final_namespace):
print(
f"Error: The namespace {final_namespace} is not valid. \
Please specify a valid namespace name."
)
return None
es = Elasticsearch([{"host": es_host, "port": port, "scheme": "http"}])
envoy_proxy_query = {
"bool": {
"must": [
{"term": {"kubernetes.namespace.keyword": final_namespace}},
{"match": {"kubernetes.container.name": "istio-proxy"}},
{"match": {"message": "start_time"}},
]
}
}
dump_all_query = {
"bool": {
"must": [
{"term": {"kubernetes.namespace.keyword": final_namespace}},
],
"must_not": [{"match": {"kubernetes.container.name": "istio-proxy"}}],
}
}
pod_query = {
"bool": {
"must": [
{"term": {"kubernetes.namespace.keyword": final_namespace}},
{"match": {"kubernetes.pod.name.keyword": pod}},
],
"must_not": [{"match": {"kubernetes.container.name": "istio-proxy"}}],
}
}
logs = []
if output_format == "yrca":
response = retrieve_logs(es, envoy_proxy_query)
yrca_logs = []
for hit in response:
pod_name = hit["_source"]["kubernetes"]["pod"]["name"]
log_message = hit["_source"]["message"]
formatted_log = f"{pod_name} {log_message}"
yrca_logs.append(formatted_log)
return yrca_process_logs(yrca_logs)
if dump_all:
query = dump_all_query
elif pod:
query = pod_query
else:
query = envoy_proxy_query
response = retrieve_logs(es, query)
if output_format == "gelf":
for hit in response:
version = "1.1"
host = hit["_source"]["host"]["name"]
short_message = hit["_source"]["message"]
full_message = hit["_source"]["message"]
timestamp = hit["_source"]["@timestamp"]
namespace_name = hit["_source"]["kubernetes"]["namespace"]
pod_name = hit["_source"]["kubernetes"]["pod"]["name"]
container_name = hit["_source"]["kubernetes"]["container"]["name"]
level = "INFO"
formatted_log = f'{{"version": "{version}", "host": "{host}",\
"short_message": "{short_message}", "full_message": "{full_message}", \
"timestamp": "{timestamp}", "level": "{level}", "_namespace_name": \
"{namespace_name}", "_pod_name": "{pod_name}", "_container_name": \
"{container_name}"}}'
logs.append(formatted_log)
elif output_format == "syslog":
for hit in response:
priority = "<134>"
timestamp = hit["_source"]["@timestamp"]
hostname = hit["_source"]["host"]["name"]
app_name = hit["_source"]["kubernetes"]["container"]["name"]
procid = hit["_source"]["kubernetes"]["pod"]["name"]
msg = hit["_source"]["message"]
formatted_log = (
f"{priority}{timestamp} {hostname} {app_name} {procid} {msg}"
)
logs.append(formatted_log)
return logs
def main():
"""
Main function to execute the script.
"""
args = parse_options()
if args.inject and args.connect:
print("Cannot use both -i and -c together.")
return
output = None
if args.inject:
output = inject(args.inject, args.timeout, args.no_header, args.namespace)
if args.connect:
output = connect_elasticsearch(
args.connect,
args.port,
args.dump_all,
args.format,
args.pod,
args.namespace,
)
if not output:
return
if args.output:
with open(args.output, "w", encoding="utf-8") as stream:
if isinstance(output, list):
stream.write("\n".join(d for d in output))
else:
stream.write(output)
print(f"Output file written to {args.output}")
else:
if isinstance(output, list):
print("\n".join(json.dumps(d) for d in output))
else:
print(output)
if __name__ == "__main__":
main()