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