-
Notifications
You must be signed in to change notification settings - Fork 0
/
indexManager.py
executable file
·226 lines (201 loc) · 10.4 KB
/
indexManager.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
#!/usr/bin/python
import requests
import json
import os
import re
import datetime
import sys
OKGREEN = '\033[92m'
WARNING = '\033[93m'
FAIL = '\033[91m'
ENDC = '\033[0m'
print("""
.___ .___ _____
| | ____ __| _/____ ___ ___ / \ _____ ____ _____ ____ ___________
| |/ \ / __ |/ __ \\\\ \/ / / \ / \\\\__ \ / \\\\__ \ / ___\_/ __ \_ __ \\
| | | \/ /_/ \ ___/ > < / Y \/ __ \| | \/ __ \_/ /_/ > ___/| | \/
|___|___| /\____ |\___ >__/\_ \ \____|__ (____ /___| (____ /\___ / \___ >__|
\/ \/ \/ \/ \/ \/ \/ \//_____/ \/
@ Iker Loidi""")
def isCompatible(index):
if re.search('\d{4}-[0-9]{1,2}', index) is not None or re.search('[0-9]{1,2}-\d{4}', index) is not None:
return True
else:
return False
def getPattern(index):
try:
if re.search('\d{4}-[0-9]{1,2}', index) is not None:
return index.replace(re.search('\d{4}-[0-9]{1,2}', index).group(0),"#")
#print("[+] Es del mes {} del ano {}".format(re.search('\d{4}-\d{2}', index).group(0).split("-")[1],re.search('\d{4}-\d{2}', index).group(0).split("-")[0]))
if re.search('[0-9]{1,2}-\d{4}', index) is not None:
#print("[+] Es del mes {} del ano {}".format(re.search('\d{2}-\d{4}', index).group(0).split("-")[0],re.search('\d{2}-\d{4}', index).group(0).split("-")[1]))
return str(index.replace(re.search('[0-9]{1,2}-\d{4}', index).group(0),"#"))
except:
print("[-] Error al intentar identificar la fecha de {}".format(index))
return None
def listIndices():
n = 0
array = list()
print("[+] Buscando indices...\n")
lista_json = json.loads(requests.get("http://localhost:9200/_cat/indices?format=json&pretty=true").text)
for i,indice_linea in enumerate(lista_json):
# Mirar si el indice contiene un patron de fecha-mes o mes-fecha
if isCompatible(indice_linea['index']):
compatible = " [COMPATIBLE] "
# Mirar si actualmente tiene un fichero de conf
pattern = getPattern(indice_linea['index'])
if os.path.exists("./config/{}".format(pattern)):
existe = " ** Con conf **"
color = OKGREEN
else:
existe = " ** Sin conf **"
color = WARNING
n += 1
#print(color+str(n)+": "+str(pattern)+" -> "+indice_linea['index']+existe+ENDC)
array.append(color+str(pattern)+existe+ENDC)
else:
compatible = " [NO COMPATIBLE] "
color = FAIL
#print(color+str(i)+": "+str(indice_linea['index']+existe+compatible+ENDC))
for element in list(set(array)):
print(element)
print("")
def createConfig(index,tsClose,tsRem):
data = {}
data['index'] = index
data['tsClose'] = tsClose
data['tsRem'] = tsRem
# Generando fichero
f = open("./config/"+index, "w")
f.write(json.dumps(data))
f.close()
print("[+] Fichero generado para el indice "+index)
def showConfig(index):
try:
f = open("./config/"+index,"r")
json_data = json.loads(f.read())
print("[+] Indice: {}\n[+] Se cerrara a los {} meses\n[+] Se borrara a los {} meses".format(json_data['index'],json_data['tsClose'],json_data['tsRem']))
except:
print("[-] Error al leer el fichero de configuracion")
def deleteConfig(index):
try:
os.remove("./config/"+index)
print("[+] Configuracion del indice {} borrado".format(index))
except:
print("[-] Error al eliminar la configuracion del indice {}".format(index))
def help():
print("""
[+] Comandos:
- list
- show [nombre_indice]
[Muestra la configuracion de este indice]
- execute [nombre_indice]
[Ejecuta la configuracion para este indice]
- create [nombre_indice] [meses_cerrar] [meses_borrar]
[Crea un fichero de configuracion para este indice (Si existe lo sobrescribe)]
- delete [nombre_indice]
[Elimina la configuracion de un indice]
- exit
""")
def getMes(year,month):
return int(year)*12+int(month)
def executeAll():
try:
for file in os.listdir("./config"):
executeConfig(file)
print("[+] Ejecutando configuracion para {}".format(file))
except:
print("[-] Error al ejecutar ficheros de configuracion")
def executeConfig(index):
try:
f = open("./config/"+index,"r")
json_data = json.loads(f.read())
print("[+] Indice: {}\n[+] Se cerrara a los {} meses\n[+] Se borrara a los {} meses".format(json_data['index'],json_data['tsClose'],json_data['tsRem']))
tsClose=int(json_data['tsClose'])
tsRem=int(json_data['tsRem'])
print("[+] Ejecutando la configuracion del indice patron {}".format(index))
lista_json = json.loads(requests.get("http://localhost:9200/_cat/indices?format=json&pretty=true").text)
for i,indice_linea in enumerate(lista_json):
if isCompatible(indice_linea['index']) and getPattern(indice_linea['index']) == index:
print("[+] Encontrado el indice: {}".format(indice_linea['index']))
try:
if re.search('\d{4}-[0-9]{1,2}', indice_linea['index']) is not None:
print("[+] Es del mes {} del ano {}".format(re.search('\d{4}-[0-9]{1,2}', indice_linea['index']).group(0).split("-")[1],re.search('\d{4}-[0-9]{1,2}', indice_linea['index']).group(0).split("-")[0]))
indexMes = getMes(re.search('\d{4}-[0-9]{1,2}', indice_linea['index']).group(0).split("-")[0],re.search('\d{4}-[0-9]{1,2}', indice_linea['index']).group(0).split("-")[1])
today = datetime.datetime.today()
nowMes = getMes(today.year,today.month)
print("[*] Hoy es el mes {} y el indice es del mes {}. Hay {} meses de diferencia".format(nowMes,indexMes,nowMes-indexMes))
if int(nowMes-indexMes) > int(json_data['tsClose']):
print(WARNING+"[+] Cerrando indice "+indice_linea['index']+ENDC)
print(requests.post("http://localhost:9200/{}/_close".format(str(indice_linea['index']))))
if int(nowMes-indexMes) > int(json_data['tsRem']):
print(FAIL+"[+] Borrando indice "+indice_linea['index']+ENDC)
# ultima comprobacion
if not "*" in str(indice_linea['index']) and str(indice_linea['index']) != "":
print(requests.delete("http://localhost:9200/{}".format(str(indice_linea['index']))))
else:
print("[-] Abortado..")
if re.search('[0-9]{1,2}-\d{4}', indice_linea['index']) is not None:
print("[+] Es del mes {} del ano {}".format(re.search('[0-9]{1,2}-\d{4}', indice_linea['index']).group(0).split("-")[0],re.search('[0-9]{1,2}-\d{4}', indice_linea['index']).group(0).split("-")[1]))
indexMes = getMes(re.search('[0-9]{1,2}-\d{4}', indice_linea['index']).group(0).split("-")[1],re.search('[0-9]{1,2}-\d{4}', indice_linea['index']).group(0).split("-")[0])
today = datetime.datetime.today()
nowMes = getMes(int(today.year),int(today.month))
print("[*] Hoy es el mes {} y el indice es del mes {}. Hay {} meses de diferencia".format(nowMes,indexMes,nowMes-indexMes))
if int(nowMes-indexMes) > int(json_data['tsClose']):
print(WARNING+"[+] Cerrando indice "+indice_linea['index']+ENDC)
print(requests.post("http://localhost:9200/{}/_close".format(str(indice_linea['index']))))
if int(nowMes-indexMes) > int(json_data['tsRem']):
print(FAIL+"[+] Borrando indice "+indice_linea['index']+ENDC)
if not "*" in str(indice_linea['index']) and str(indice_linea['index']) != "":
print(requests.delete("http://localhost:9200/{}".format(str(indice_linea['index']))))
else:
print("[-] Abortado..")
except Exception as p:
print(p)
print("[-] Error al intentar identificar la fecha de {}".format(indice_linea['index']))
except:
print("[-] Error al abrir el fichero de configuracion {}".format(index))
def checkConfigDir():
if not os.path.exists("config/"):
try:
os.mkdir("config")
except:
print("[-] No se ha podido crear el directorio de configuraciones")
sys.exit(-1)
checkConfigDir()
if len(sys.argv) > 1:
if sys.argv[1] == "execute":
executeAll()
sys.exit(0)
else:
print("\n[-] Usar \"{} execute\" para ejecutar todos las configuraciones".format(sys.argv[0]))
sys.exit(-1)
listIndices()
while(True):
option = raw_input("> ")
if "LIST" in option.upper():
listIndices()
if "EXIT" in option.upper():
exit(0)
if "HELP" in option.upper():
help()
if "CREATE" in option.upper():
if len(option.split(" ")) != 4:
print("[-] Usar: create [nombre_indice] [meses_cerrar] [meses_borrar]")
else:
createConfig(option.split(" ")[1],option.split(" ")[2],option.split(" ")[3])
if "SHOW" in option.upper():
if len(option.split(" ")) != 2:
print("[-] Usar: show [nombre_indice]")
else:
showConfig(option.split(" ")[1])
if "DELETE" in option.upper():
if len(option.split(" ")) != 2:
print("[-] Usar: delete [nombre_indice]")
else:
deleteConfig(option.split(" ")[1])
if "EXECUTE" in option.upper():
if len(option.split(" ")) != 2:
print("[-] Usar: execute [nombre_indice]")
else:
executeConfig(option.split(" ")[1])