diff --git a/Telk-Alert-Suite/Telk-Alert/modules/Telk_Alert_Class.py b/Telk-Alert-Suite/Telk-Alert/modules/Telk_Alert_Class.py index a06116f..358cf03 100644 --- a/Telk-Alert-Suite/Telk-Alert/modules/Telk_Alert_Class.py +++ b/Telk-Alert-Suite/Telk-Alert/modules/Telk_Alert_Class.py @@ -1,4 +1,4 @@ -from os import path +from sys import exit from threading import Thread from libPyElk import libPyElk from libPyLog import libPyLog @@ -53,42 +53,42 @@ def startTelkAlert(self): Method that starts the Telk-Alert application. """ try: - if path.exists(self.__constants.PATH_FILE_CONFIGURATION): - data_configuration = self.__utils.readYamlFile(self.__constants.PATH_FILE_CONFIGURATION) - if data_configuration["use_http_authentication"] == True: - conn_es = self.__elasticsearch.createConnectionToElasticSearch(data_configuration, path_key_file = self.__constants.PATH_KEY_FILE) - else: - conn_es = self.__elasticsearch.createConnectionToElasticSearch(data_configuration) - if not conn_es == None: - self.__logger.generateApplicationLog("Telk-Alert v3.2", 1, "__start", use_stream_handler = True) - self.__logger.generateApplicationLog("@2022 Tekium. All rights reserved.", 1, "__start", use_stream_handler = True) - self.__logger.generateApplicationLog("Author: Erick Rodriguez", 1, "__start", use_stream_handler = True) - self.__logger.generateApplicationLog("Email: erodriguez@tekium.mx, erickrr.tbd93@gmail.com", 1, "__start", use_stream_handler = True) - self.__logger.generateApplicationLog("License: GPLv3", 1, "__start", use_stream_handler = True) - self.__logger.generateApplicationLog("Telk-Alert started", 1, "__start", use_stream_handler = True) - self.__logger.generateApplicationLog("Established connection with: " + data_configuration['es_host'] + ':' + str(data_configuration['es_port']), 1, "__start" , use_stream_handler = True) - self.__logger.generateApplicationLog("Elasticsearch Cluster Name: " + conn_es.info()["cluster_name"], 1, "__start", use_stream_handler = True) - self.__logger.generateApplicationLog("Elasticsearch Version: " + conn_es.info()["version"]["number"], 1, "__start", use_stream_handler = True) - path_alert_rules_folder = self.__constants.PATH_BASE_TELK_ALERT + '/' + data_configuration["name_folder_rules"] - list_all_alert_rules = self.__utils.getListOfAllYamlFilesInFolder(path_alert_rules_folder) - if list_all_alert_rules: - self.__logger.generateApplicationLog(str(len(list_all_alert_rules)) + " alert rules in: " + path_alert_rules_folder, 1, "__start", use_stream_handler = True) - for alert_rule in list_all_alert_rules: - self.__logger.generateApplicationLog(alert_rule[:-5] + " loaded", 1, "__start", use_stream_handler = True) - data_alert_rule = self.__utils.readYamlFile(path_alert_rules_folder + '/' + alert_rule) - Thread(name = alert_rule[:-5], target = self.__startAlertRule, args = (conn_es, data_alert_rule, )).start() - else: - self.__logger.generateApplicationLog("No alert rules found in: " + path_alert_rules_folder, 1, "__start", use_stream_handler = True) + data_configuration = self.__utils.readYamlFile(self.__constants.PATH_FILE_CONFIGURATION) + if data_configuration["use_http_authentication"] == True: + conn_es = self.__elasticsearch.createConnectionToElasticSearch(data_configuration, path_key_file = self.__constants.PATH_KEY_FILE) else: - self.__logger.generateApplicationLog("Configuration file not found", 3, "Configuration", use_stream_handler = True) + conn_es = self.__elasticsearch.createConnectionToElasticSearch(data_configuration) + if not conn_es == None: + self.__logger.generateApplicationLog("Telk-Alert v3.2", 1, "__start", use_stream_handler = True) + self.__logger.generateApplicationLog("@2022 Tekium. All rights reserved.", 1, "__start", use_stream_handler = True) + self.__logger.generateApplicationLog("Author: Erick Rodriguez", 1, "__start", use_stream_handler = True) + self.__logger.generateApplicationLog("Email: erodriguez@tekium.mx, erickrr.tbd93@gmail.com", 1, "__start", use_stream_handler = True) + self.__logger.generateApplicationLog("License: GPLv3", 1, "__start", use_stream_handler = True) + self.__logger.generateApplicationLog("Telk-Alert started", 1, "__start", use_stream_handler = True) + self.__logger.generateApplicationLog("Established connection with: " + data_configuration['es_host'] + ':' + str(data_configuration['es_port']), 1, "__connection" , use_stream_handler = True) + self.__logger.generateApplicationLog("Elasticsearch Cluster Name: " + conn_es.info()["cluster_name"], 1, "__connection", use_stream_handler = True) + self.__logger.generateApplicationLog("Elasticsearch Version: " + conn_es.info()["version"]["number"], 1, "__connection", use_stream_handler = True) + path_alert_rules_folder = self.__constants.PATH_BASE_TELK_ALERT + '/' + data_configuration["name_folder_rules"] + list_all_alert_rules = self.__utils.getListOfAllYamlFilesInFolder(path_alert_rules_folder) + if list_all_alert_rules: + self.__logger.generateApplicationLog(str(len(list_all_alert_rules)) + " alert rules in: " + path_alert_rules_folder, 1, "__readAlertRules", use_stream_handler = True) + for alert_rule in list_all_alert_rules: + self.__logger.generateApplicationLog(alert_rule[:-5] + " loaded", 1, "__alertRule", use_stream_handler = True) + data_alert_rule = self.__utils.readYamlFile(path_alert_rules_folder + '/' + alert_rule) + Thread(name = alert_rule[:-5], target = self.__startAlertRule, args = (conn_es, data_alert_rule, )).start() + else: + self.__logger.generateApplicationLog("No alert rules found in: " + path_alert_rules_folder, 1, "__readAlertRules", use_stream_handler = True) except KeyError as exception: self.__logger.generateApplicationLog("Key Error: " + str(exception), 3, "__start", use_stream_handler = True, use_file_handler = True, name_file_log = self.__constants.NAME_FILE_LOG, user = self.__constants.USER, group = self.__constants.GROUP) + exit(1) except (OSError, IOError, FileNotFoundError) as exception: self.__logger.generateApplicationLog("Error to found, open or read a file or directory. For more information, see the logs.", 3, "__start", use_stream_handler = True) self.__logger.generateApplicationLog(exception, 3, "__start", use_file_handler = True, name_file_log = self.__constants.NAME_FILE_LOG, user = self.__constants.USER, group = self.__constants.GROUP) - except (self.__elasticsearch.exceptions.AuthenticationException, self.__elasticsearch.exceptions.ConnectionError, self.__elasticsearch.exceptions.AuthorizationException, self.__elasticsearch.exceptions.RequestError) as exception: + exit(1) + except (self.__elasticsearch.exceptions.AuthenticationException, self.__elasticsearch.exceptions.ConnectionError, self.__elasticsearch.exceptions.AuthorizationException, self.__elasticsearch.exceptions.RequestError, self.__elasticsearch.exceptions.ConnectionTimeout) as exception: self.__logger.generateApplicationLog("Error connecting with ElasticSearch. For more information, see the logs.", 3, "__connection", use_stream_handler = True) - self.__logger.generateApplicationLog(exception, 3, "__start", use_file_handler = True, name_file_log = self.__constants.NAME_FILE_LOG, user = self.__constants.USER, group = self.__constants.GROUP) + self.__logger.generateApplicationLog(exception, 3, "__connection", use_file_handler = True, name_file_log = self.__constants.NAME_FILE_LOG, user = self.__constants.USER, group = self.__constants.GROUP) + exit(1) def __startAlertRule(self, conn_es, data_alert_rule): @@ -149,9 +149,11 @@ def __startAlertRule(self, conn_es, data_alert_rule): sleep(time_search_in_seconds) except KeyError as exception: self.__logger.generateApplicationLog("Key Error: " + str(exception), 3, "__" + data_alert_rule["alert_rule_name"], use_stream_handler = True, use_file_handler = True, name_file_log = self.__constants.NAME_FILE_LOG, user = self.__constants.USER, group = self.__constants.GROUP) - except (self.__elasticsearch.exceptions.AuthenticationException, self.__elasticsearch.exceptions.ConnectionError, self.__elasticsearch.exceptions.AuthorizationException, self.__elasticsearch.exceptions.RequestError) as exception: + exit(1) + except (self.__elasticsearch.exceptions.AuthenticationException, self.__elasticsearch.exceptions.ConnectionError, self.__elasticsearch.exceptions.AuthorizationException, self.__elasticsearch.exceptions.RequestError, self.__elasticsearch.exceptions.ConnectionTimeout) as exception: self.__logger.generateApplicationLog("Error performing an action in ElasticSearch. For more information, see the logs.", 3, "__" + data_alert_rule["alert_rule_name"], use_stream_handler = True) - self.__logger.generateApplicationLog(exception, 3, "__" + data_alert_rule["alert_rule_name"], use_file_handler = True, name_file_log = self.__constants.NAME_FILE_LOG, user = self.__constants.USER, group = self.__constants.GROUP) + self.__logger.generateApplicationLog(exception, 3, "__" + data_alert_rule["alert_rule_name"], use_file_handler = True, name_file_log = self.__constants.NAME_FILE_LOG, user = self.__constants.USER, group = self.__constants.GROUP) + exit(1) def __sendMultipleAlertRule(self, result_search, data_alert_rule, telegram_bot_token, telegram_chat_id): @@ -172,6 +174,7 @@ def __sendMultipleAlertRule(self, result_search, data_alert_rule, telegram_bot_t self.__createLogByTelegramCode(response_status_code, data_alert_rule["alert_rule_name"]) except KeyError as exception: self.__logger.generateApplicationLog("Key Error: " + str(exception), 3, "__" + data_alert_rule["alert_rule_name"], use_stream_handler = True, use_file_handler = True, name_file_log = self.__constants.NAME_FILE_LOG, user = self.__constants.USER, group = self.__constants.GROUP) + exit(1) def __sendOnlyAlertRule(self, result_search, data_alert_rule, telegram_bot_token, telegram_chat_id, total_events): @@ -195,6 +198,7 @@ def __sendOnlyAlertRule(self, result_search, data_alert_rule, telegram_bot_token self.__createLogByTelegramCode(response_status_code, data_alert_rule["alert_rule_name"]) except KeyError as exception: self.__logger.generateApplicationLog("Key Error: " + str(exception), 3, "__" + data_alert_rule["alert_rule_name"], use_stream_handler = True, use_file_handler = True, name_file_log = self.__constants.NAME_FILE_LOG, user = self.__constants.USER, group = self.__constants.GROUP) + exit(1) def __createLogByTelegramCode(self, response_status_code, alert_rule_name):