-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtraefik_es_logs.py
68 lines (60 loc) · 1.65 KB
/
traefik_es_logs.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
#!/usr/bin/python3
import datetime
from elasticsearch import Elasticsearch
import os
'''
export apache like logs from elasticsearch traefik data
Angelos Karageorgiou angelos@unix.gr
Disclaimer: No GPT was used ;-)
'''
#Please edit
ELASTIC_USER="elastic"
ELASTIC_PASSWORD=os.getenv('ELASTIC_PASSWORD')
ELASTIC_HOST=os.getenv('ELASTIC_HOST')
# make this tighter or face timeouts
INDEX_PATTERN="*"
SCROLL="1m" #increase if above is too inclusive
DAYS_BEHIND=90
today = datetime.datetime.now()
diffdate = datetime.timedelta(days = DAYS_BEHIND)
past = (today - diffdate).strftime('%Y-%m-%dT%H:%M:%S.%fZ')[:-3]
# Thank you mdme kibana
query= {
"bool": {
"must": [],
"filter": [
{
"bool": {
"should": [
{
"match": {
"kubernetes.labels.app_kubernetes_io/name": "traefik"
}
}
],
"minimum_should_match": 1
}
},
{
"range": {
"@timestamp": {
"format": "strict_date_optional_time",
"gte": past,
"lte": datetime.datetime.now().strftime('%Y-%m-%dT%H:%M:%S.%fZ')[:-3]
}
}
}
],
}
}
es = Elasticsearch(ELASTIC_HOST, http_auth=(ELASTIC_USER, ELASTIC_PASSWORD))
res = es.search(index=INDEX_PATTERN, query=query, size=10000, scroll=SCROLL)
scroll_id=res['_scroll_id']
while scroll_id:
for hit in res['hits']['hits']:
print(hit['_source']['message'])
res = es.scroll(
scroll_id = scroll_id,
scroll = SCROLL # time value for search, does it even matter?
)
scroll_id=res['_scroll_id']