Skip to content
Snippets Groups Projects
Commit 93128620 authored by jurgenhaas's avatar jurgenhaas
Browse files

ansible-inventories/arocom#2937 Fix purge script to work with Python 3

parent 50b4621c
Branches
No related tags found
No related merge requests found
#!/usr/bin/env python #!/usr/bin/env python3
''' '''
Purge ElasticSearch Data Purge ElasticSearch Data
...@@ -10,7 +10,7 @@ import json ...@@ -10,7 +10,7 @@ import json
import pycurl import pycurl
from datetime import date from datetime import date
from dateutil.relativedelta import relativedelta from dateutil.relativedelta import relativedelta
from StringIO import StringIO from io import BytesIO
try: try:
# python 3 # python 3
from urllib.parse import urlencode from urllib.parse import urlencode
...@@ -28,7 +28,7 @@ args = parser.parse_args() ...@@ -28,7 +28,7 @@ args = parser.parse_args()
def getIndex(): def getIndex():
buffer = StringIO() buffer = BytesIO()
c = pycurl.Curl() c = pycurl.Curl()
c.setopt(c.URL, 'http://%s:%s@localhost:9200/_cat/indices?v&pretty&s=index&format=json' % (args.username, args.password)) c.setopt(c.URL, 'http://%s:%s@localhost:9200/_cat/indices?v&pretty&s=index&format=json' % (args.username, args.password))
c.setopt(c.HTTPHEADER, ['Content-Type: application/json']) c.setopt(c.HTTPHEADER, ['Content-Type: application/json'])
...@@ -44,7 +44,7 @@ def purgeSingle(cat, day): ...@@ -44,7 +44,7 @@ def purgeSingle(cat, day):
post_data = {"query":{"bool":{"filter":{"range":{"@timestamp":{"lte":day.strftime('%Y-%m-%d')}}}}}} post_data = {"query":{"bool":{"filter":{"range":{"@timestamp":{"lte":day.strftime('%Y-%m-%d')}}}}}}
postfields = json.dumps(post_data) postfields = json.dumps(post_data)
print(' Postfields: ' + postfields) print(' Postfields: ' + postfields)
buffer = StringIO() buffer = BytesIO()
c = pycurl.Curl() c = pycurl.Curl()
c.setopt(c.URL, 'http://%s:%s@localhost:9200/%s/_delete_by_query' % (args.username, args.password, args.index)) c.setopt(c.URL, 'http://%s:%s@localhost:9200/%s/_delete_by_query' % (args.username, args.password, args.index))
c.setopt(c.HTTPHEADER, ['Content-Type: application/json']) c.setopt(c.HTTPHEADER, ['Content-Type: application/json'])
...@@ -52,9 +52,9 @@ def purgeSingle(cat, day): ...@@ -52,9 +52,9 @@ def purgeSingle(cat, day):
c.setopt(c.POSTFIELDS, postfields) c.setopt(c.POSTFIELDS, postfields)
c.perform() c.perform()
c.close() c.close()
print(' Delete: ' + buffer.getvalue()) print(' Delete: ' + buffer.getvalue().decode('utf8'))
buffer = StringIO() buffer = BytesIO()
c = pycurl.Curl() c = pycurl.Curl()
c.setopt(c.URL, 'http://%s:%s@localhost:9200/%s/_forcemerge?only_expunge_deletes=true' % (args.username, args.password, args.index)) c.setopt(c.URL, 'http://%s:%s@localhost:9200/%s/_forcemerge?only_expunge_deletes=true' % (args.username, args.password, args.index))
c.setopt(c.HTTPHEADER, ['Content-Type: application/json']) c.setopt(c.HTTPHEADER, ['Content-Type: application/json'])
...@@ -62,7 +62,7 @@ def purgeSingle(cat, day): ...@@ -62,7 +62,7 @@ def purgeSingle(cat, day):
c.setopt(c.POSTFIELDS, '') c.setopt(c.POSTFIELDS, '')
c.perform() c.perform()
c.close() c.close()
print(' Purge: ' + buffer.getvalue()) print(' Purge: ' + buffer.getvalue().decode('utf8'))
return return
print('Index not found') print('Index not found')
...@@ -74,7 +74,7 @@ def purgeDaily(cat, day): ...@@ -74,7 +74,7 @@ def purgeDaily(cat, day):
for index in cat: for index in cat:
if index['index'].find(prefix) == 0 and index['index'] < range: if index['index'].find(prefix) == 0 and index['index'] < range:
print(' Index: ' + index['index']) print(' Index: ' + index['index'])
buffer = StringIO() buffer = BytesIO()
c = pycurl.Curl() c = pycurl.Curl()
c.setopt(c.URL, 'http://%s:%s@localhost:9200/%s' % (args.username, args.password, index['index'])) c.setopt(c.URL, 'http://%s:%s@localhost:9200/%s' % (args.username, args.password, index['index']))
c.setopt(c.HTTPHEADER, ['Content-Type: application/json']) c.setopt(c.HTTPHEADER, ['Content-Type: application/json'])
...@@ -82,7 +82,7 @@ def purgeDaily(cat, day): ...@@ -82,7 +82,7 @@ def purgeDaily(cat, day):
c.setopt(c.CUSTOMREQUEST, 'DELETE') c.setopt(c.CUSTOMREQUEST, 'DELETE')
c.perform() c.perform()
c.close() c.close()
print(' Delete: ' + buffer.getvalue()) print(' Delete: ' + buffer.getvalue().decode('utf8'))
cat = getIndex() cat = getIndex()
......
...@@ -11,6 +11,7 @@ ...@@ -11,6 +11,7 @@
"b_end", "b_end",
"c_ip", "c_ip",
"status_code", "status_code",
"sni",
"http_request" "http_request"
], ],
"sort": [ "sort": [
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment