def es_getSrvResponse(ip: str): try: es = elasticsearch.Elasticsearch([{'host': ip, 'port': 9200}]) if not es.ping(): print("es_api: " + ip + " ne repond pas") except elasticsearch.ConnectionError: print("es_api: " + ip + " ne repond pas") exitProgram()
def es_add_ilm_policy(ip: str): es = elasticsearch.Elasticsearch([{'host': ip, 'port': 9200}]) try: roll = es.ilm.put_lifecycle("pyloggen_rollov", es_ilm_settings) print("es_api: Ilm : Regle pyloggen_rollov ajoutée") except elasticsearch.ConnectionError: print("es_api: Ilm impossible d'ajouter la regle") exitProgram()
def extract_a_specified_file_from_zip_archive(archive_path_file_name:str, fileName_to_extract:str, dest_extraction_path:str): with ZipFile(archive_path_file_name, mode="r") as zip_ref: try: zip_ref.extract(fileName_to_extract, dest_extraction_path) zip_ref.close() except: print("File handler: probleme d'extraction de l'archive: " + archive_path_file_name) exitProgram()
def file_exist_check_ifNot_exit(file_with_path:str): try: status = os.path.exists(file_with_path) if not status: print("File handler: Check file exist: " + file_with_path + " fichier non trouve") exitProgram() except OSError: print("File handler: Check file exist: " + file_with_path + " fichier non trouve") exitProgram()
def read_file(filename:str): try: with open(filename, "r") as fileToRead: line = fileToRead.readline() except IOError: print("file_handler: impossible d'ouvrir le fichier pour la lecture: " + filename) exitProgram() return line
def es_check_existing_template(ip: str): try: es = elasticsearch.Elasticsearch([{'host': ip, 'port': 9200}]) if not es.indices.exists_template(index_template_name): print("es_api: Template: " + index_template_name + " non trouve.") es_create_new_template(ip) except elasticsearch.ElasticsearchException: print("es_api: Template impossible de savoir si le template existe") exitProgram()
def es_check_ilm_policy_exist(ip: str): es = elasticsearch.Elasticsearch([{'host': ip, 'port': 9200}]) try: roll = es.ilm.get_lifecycle() x = re.search("pyloggen_rollov", str(roll)) if not x: es_add_ilm_policy(ip) except elasticsearch.ConnectionError: print("es_api: Ilm impossible savoir si la regle existe") exitProgram()
def es_create_new_pipeline(ip: str): try: es = elasticsearch.Elasticsearch([{'host': ip, 'port': 9200}]) es.ingest.put_pipeline(user_agent_pipeline_name, pipeline_user_agent) if es.ingest.get_pipeline(user_agent_pipeline_name): print("es_api: Pipeline cree => " + user_agent_pipeline_name) except elasticsearch.ElasticsearchException: print( "es_api: Pipeline : il y eu un problème a la creation du pipeline") exitProgram()
def es_create_new_template(ip: str): index_template_settings = es_setShardReplicaNumber(ip) try: es = elasticsearch.Elasticsearch([{'host': ip, 'port': 9200}]) es.indices.put_template(index_template_name, index_template_settings) if es.indices.exists_template(index_template_name): print("es_api: Template cree => " + index_template_name) except elasticsearch.ElasticsearchException: #print("es_Api: " + ip + " status code : " + str(res.status_code)) print("es_api: Il y eu un roblème a la creation du template") exitProgram()
def es_getSrvColorStatus(ip: str): try: es = elasticsearch.Elasticsearch([{'host': ip, 'port': 9200}]) cHealth = es.cluster.health() if cHealth["status"] == "green" or cHealth["status"] == "yellow": print("es_api: Cluster_color: " + str(cHealth["status"])) else: print("es_api: Attention Cluster_color: " + str(cHealth["status"])) exitProgram() except elasticsearch.ElasticsearchException: print("es_api : " + ip + " ne repond pas") exitProgram()
def es_getNodeNumber(ip: str): try: es = elasticsearch.Elasticsearch([{'host': ip, 'port': 9200}]) es_cluster_nodeNumber = es.cluster.health()["number_of_nodes"] print("es_api: Cluster_number_of_nodes : " + str(es_cluster_nodeNumber)) except elasticsearch.ElasticsearchException: print( "es_api: Cluster_Number_of_nodes : impossible de determiner le nombre de noeud" ) exitProgram() return es_cluster_nodeNumber
def es_create_new_index(ip: str): index_name = es_get_index_name_datenow() try: es = elasticsearch.Elasticsearch([{'host': ip, 'port': 9200}]) if es.indices.exists(index_name): print("es_api: Index => " + index_name + " présent") else: es.indices.create(index_name) es_add_alias(ip, index_name) print("es_api: Index => " + index_name + " créé") except elasticsearch.ElasticsearchException: print("es_api: Probleme à la créationde l'index") exitProgram()
def es_add_alias(ip: str, index_name: str): try: es = elasticsearch.Elasticsearch([{'host': ip, 'port': 9200}]) es.indices.put_alias(index_name, "pyloggen") aliasList = es.cat.aliases("pyloggen") aliasListArray = aliasList.splitlines() stringOfALiases = "" for alias in aliasListArray: if stringOfALiases == "": stringOfALiases = stringOfALiases + alias else: stringOfALiases = stringOfALiases + ", " + alias print("es_api: Alias créé") print("es_api: Aiste des alias: " + stringOfALiases) except elasticsearch.ElasticsearchException: print("es_api: Probleme à la création de l'alias") exitProgram()
def map_ua_csv2array(my_path_file_name: str): try: with open(my_path_file_name, "r") as fileToRead: line = fileToRead.readline() ctn = 1 myArray = [] while line: line = fileToRead.readline() try: myArray.append(line) ctn += 1 except BufferError: print("error: " + BufferError) print("Bdd handler: " + str(ctn) + " lignes comptée depuis le fichier : " + my_path_file_name) return myArray except IOError: print("Bdd hendler: impossible de mapper daas le tableau le fichier " + my_path_file_name) exitProgram()
def check_sizeFileNotNull(path_and_file_name:str): if not os.path.isfile(path_and_file_name) and not os.path.getsize(path_and_file_name) > 0: print("File handler: le fichier : " + path_and_file_name + " presente une taille de : " + os.path.getsize(path_and_file_name)) exitProgram()