@@ -140,12 +140,17 @@ def send_error(self, msg):
140140 Returns:
141141 None
142142 """
143- _msg = "[%s][%s] %s" % (self .name , self .cur_account , msg )
144- print (_msg )
143+
145144 if self .cfg ["sns_arn" ] != "" :
146- sns_region = self .cfg ["sns_arn" ].split (":" )[4 ]
145+ cur_account = self .cfg ["sns_arn" ].split (":" )[4 ]
146+ _msg = "[%s][%s] %s" % (self .name , self .cur_account , msg )
147+ print (_msg )
148+ sns_region = self .cfg ["sns_arn" ].split (":" )[3 ]
147149 sns = boto3 .client ("sns" , region_name = sns_region )
148150 sns .publish (TopicArn = self .cfg ["sns_arn" ], Message = _msg )
151+ else :
152+ _msg = "No SNS topic provided. Just printing..."
153+ print (_msg )
149154
150155 def delete_index (self , index_name ):
151156 """ES DELETE specific index
@@ -176,24 +181,27 @@ def lambda_handler(event, context):
176181 None
177182 """
178183 es = ES_Cleanup (event , context )
179- # Index cutoff definition, remove older than this date
180- earliest_to_keep = datetime .date .today () - datetime .timedelta (
181- days = int (es .cfg ["delete_after" ]))
182- for index in es .get_indices ():
183- if index ["index" ] == ".kibana" :
184- # ignore .kibana index
185- print ("Found .kibana index - ignoring" )
186- continue
187-
188- idx_name = '-' .join (word for word in index ["index" ].split ("-" )[:- 1 ])
189- idx_date = index ["index" ].split ("-" )[- 1 ]
190- print ("Found index: %s - %s" % (idx_name , idx_date ))
191- if idx_name in es .cfg ["index" ] or "all" in es .cfg ["index" ]:
192-
193- if idx_date <= earliest_to_keep .strftime (es .cfg ["index_format" ]):
194- print ("Deleting index: %s" % index ["index" ])
195- es .delete_index (index ["index" ])
196-
184+ try :
185+ # Index cutoff definition, remove older than this date
186+ earliest_to_keep = datetime .date .today () - datetime .timedelta (
187+ days = int (es .cfg ["delete_after" ]))
188+ for index in es .get_indices ():
189+ if index ["index" ] == ".kibana" :
190+ # ignore .kibana index
191+ print ("Found .kibana index - ignoring" )
192+ continue
193+
194+ idx_name = '-' .join (word for word in index ["index" ].split ("-" )[:- 1 ])
195+ idx_date = index ["index" ].split ("-" )[- 1 ]
196+ print ("Found index: %s - %s" % (idx_name , idx_date ))
197+ if idx_name in es .cfg ["index" ] or "all" in es .cfg ["index" ]:
198+
199+ if idx_date <= earliest_to_keep .strftime (es .cfg ["index_format" ]):
200+ print ("Deleting index: %s" % index ["index" ])
201+ es .delete_index (index ["index" ])
202+ except Exception as e :
203+ print (str (e ))
204+ es .send_error (str (e ))
197205
198206if __name__ == '__main__' :
199207 event = {
0 commit comments