forked from JulieChv/Analyse_Reseaux
ajout de doc
This commit is contained in:
127
Parseurs_logs_Switch/src/main.py
Normal file
127
Parseurs_logs_Switch/src/main.py
Normal file
@@ -0,0 +1,127 @@
|
||||
import sys
|
||||
import os
|
||||
import time
|
||||
import scripts.json_hpe as json_hpe
|
||||
from pathlib import Path
|
||||
from scripts.export_schema_infra import main as schema_infra
|
||||
from scripts.export_rapport_uplink import main as rapport_uplink
|
||||
from scripts.export_rapport_stack import main as rapport_stack
|
||||
from scripts.export_rapport_interfaces import main as rapport_interfaces
|
||||
|
||||
sys.path.append(os.path.dirname(__file__))
|
||||
|
||||
def verify_if_file_exists(name):
|
||||
base, ext = os.path.splitext(name)
|
||||
counter = 1
|
||||
new_name = name
|
||||
while os.path.exists(new_name):
|
||||
new_name = f"{base}_{counter}{ext}"
|
||||
counter += 1
|
||||
return new_name
|
||||
|
||||
def main():
|
||||
"""Fonction principale du programme d'analyse de switches"""
|
||||
if len(sys.argv) < 3:
|
||||
print("Usage: python3 src/main.py <switch_type>[hpe|...] <input_file> [-o <output_file>] [-u] [-d] [-s] [-i]")
|
||||
print("Options:")
|
||||
print(" <switch_type> Type of switch to process (hpe)")
|
||||
print(" -o <output_file> Specify output JSON file name (optional)")
|
||||
print(" -d Generate infrastructure schema (optional)")
|
||||
print(" -u Generate uplink report (optional)")
|
||||
print(" -s Generate stack report (optional)")
|
||||
print(" -i Generate interface report (optional)")
|
||||
sys.exit(1)
|
||||
|
||||
switch_type = sys.argv[1].lower()
|
||||
input_data = sys.argv[2]
|
||||
input_path = "src/input/"
|
||||
output_path = "src/output/"
|
||||
os.makedirs(output_path, exist_ok=True)
|
||||
|
||||
if "-o" in sys.argv:
|
||||
o_index = sys.argv.index("-o")
|
||||
if o_index + 1 < len(sys.argv):
|
||||
output_file1_json = f"{output_path}{switch_type}_saved_{sys.argv[o_index + 1]}.json"
|
||||
output_file2_json = f"{output_path}{switch_type}_current_{sys.argv[o_index + 1]}.json"
|
||||
else:
|
||||
print("Erreur: nom de fichier de sortie manquant après '-o'.")
|
||||
sys.exit(1)
|
||||
else:
|
||||
timestamp = time.strftime("%Y%m%d")
|
||||
output_file1_json = f"{output_path}{switch_type}_current_{timestamp}.json"
|
||||
output_file2_json = f"{output_path}{switch_type}_saved_{timestamp}.json"
|
||||
output_file1_json = verify_if_file_exists(output_file1_json)
|
||||
output_file2_json = verify_if_file_exists(output_file2_json)
|
||||
|
||||
if switch_type == "hpe":
|
||||
json_hpe.generate_json_hpe(input_data, output_file1_json, output_file2_json)
|
||||
else:
|
||||
print("Erreur: type de switch inconnu. Utilisez 'hpe'.")
|
||||
sys.exit(1)
|
||||
|
||||
if "-d" in sys.argv:
|
||||
print(f"\nGénération du schéma d'infrastructure...")
|
||||
if "-o" in sys.argv:
|
||||
o_index = sys.argv.index("-o")
|
||||
if o_index + 1 < len(sys.argv):
|
||||
output_file_html = os.path.join(f"{output_path}schema_infra_{switch_type}_{sys.argv[o_index + 1]}.html")
|
||||
else:
|
||||
print("Erreur: nom de fichier de sortie manquant après '-o'.")
|
||||
sys.exit(1)
|
||||
else:
|
||||
timestamp = time.strftime("%Y%m%d")
|
||||
output_file_html = f"{output_path}schema_infra_{switch_type}_{timestamp}.html"
|
||||
output_file_html = verify_if_file_exists(output_file_html)
|
||||
html_file = schema_infra(output_path, output_file_html)
|
||||
print(f"✓ Processus terminé. Fichiers générés:\n - JSON: {output_file1_json} & {output_file2_json}\n - HTML: {html_file}")
|
||||
|
||||
if "-u" in sys.argv:
|
||||
print(f"\nGénération du rapport d'uplink...")
|
||||
if "-o" in sys.argv:
|
||||
o_index = sys.argv.index("-o")
|
||||
if o_index + 1 < len(sys.argv):
|
||||
output_file_excel = os.path.join(f"{output_path}rapport_uplink_{switch_type}_{sys.argv[o_index + 1]}.xlsx")
|
||||
else:
|
||||
print("Erreur: nom de fichier de sortie manquant après '-o'.")
|
||||
sys.exit(1)
|
||||
else:
|
||||
timestamp = time.strftime("%Y%m%d")
|
||||
output_file_excel = f"{output_path}rapport_uplink_{switch_type}_{timestamp}.xlsx"
|
||||
output_file_excel = verify_if_file_exists(output_file_excel)
|
||||
excel_file = rapport_uplink(output_path, output_file_excel)
|
||||
print(f"✓ Processus terminé. Fichiers générés:\n - JSON: {output_file1_json} & {output_file2_json}\n - Excel: {excel_file}")
|
||||
|
||||
if "-s" in sys.argv:
|
||||
print(f"\nGénération du rapport de stack...")
|
||||
if "-o" in sys.argv:
|
||||
o_index = sys.argv.index("-o")
|
||||
if o_index + 1 < len(sys.argv):
|
||||
output_file_excel = os.path.join(f"{output_path}rapport_stack_{switch_type}_{sys.argv[o_index + 1]}.xlsx")
|
||||
else:
|
||||
print("Erreur: nom de fichier de sortie manquant après '-o'.")
|
||||
sys.exit(1)
|
||||
else:
|
||||
timestamp = time.strftime("%Y%m%d")
|
||||
output_file_excel = f"{output_path}rapport_stack_{switch_type}_{timestamp}.xlsx"
|
||||
output_file_excel = verify_if_file_exists(output_file_excel)
|
||||
excel_file = rapport_stack(output_path, output_file_excel)
|
||||
print(f"✓ Processus terminé. Fichiers générés:\n - JSON: {output_file1_json} & {output_file2_json}\n - Excel: {excel_file}")
|
||||
|
||||
if "-i" in sys.argv:
|
||||
print(f"\nGénération du rapport d'interfaces...")
|
||||
if "-o" in sys.argv:
|
||||
o_index = sys.argv.index("-o")
|
||||
if o_index + 1 < len(sys.argv):
|
||||
output_file_excel = os.path.join(f"{output_path}rapport_interfaces_{switch_type}_{sys.argv[o_index + 1]}.xlsx")
|
||||
else:
|
||||
print("Erreur: nom de fichier de sortie manquant après '-o'.")
|
||||
sys.exit(1)
|
||||
else:
|
||||
timestamp = time.strftime("%Y%m%d")
|
||||
output_file_excel = f"{output_path}rapport_interfaces_{switch_type}_{timestamp}.xlsx"
|
||||
output_file_excel = verify_if_file_exists(output_file_excel)
|
||||
excel_file = rapport_interfaces(output_path, output_file_excel)
|
||||
print(f"✓ Processus terminé. Fichiers générés:\n - JSON: {output_file1_json} & {output_file2_json}\n - Excel: {excel_file}")
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -181,7 +181,7 @@ class ParserMixin:
|
||||
"software-version": f"Version {self.config['version'].version if self.config['version'].version else None} Release {self.config['version'].release if self.config['version'].release else None}"
|
||||
})
|
||||
|
||||
# Conversion current configuration/saved configuration TODO
|
||||
# Conversion current configuration/saved configuration
|
||||
# Users
|
||||
if self.config[config_type].local_users:
|
||||
oc_users = []
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
Parser pour configuration switch HPE vers un format json normalisé OpenConfig YANG
|
||||
Extrait les différentes sections de la configuration HPE et les convertit en objets Python, puis exporte le tout au format JSON OpenConfig.
|
||||
Extrait les différentes sections de logs et les convertit en objets Python, puis exporte le tout au format JSON OpenConfig.
|
||||
"""
|
||||
|
||||
import json
|
||||
@@ -14,7 +14,7 @@ class HpeParser(ParserMixin):
|
||||
"""Parser pour fichier XML HPE"""
|
||||
|
||||
def __init__(self, file_path: str):
|
||||
"""Initialise le parser avec le chemin du fichier de configuration HPE"""
|
||||
"""Initialise le parser avec le chemin du fichier de logs HPE"""
|
||||
self.file = file_path
|
||||
self.root = None
|
||||
self.raw_data = None
|
||||
@@ -1505,33 +1505,4 @@ def generate_json_hpe(input_file: str, output_file1: str, output_file2: str):
|
||||
|
||||
except Exception as e:
|
||||
print(f"✗ Erreur: {e}")
|
||||
return
|
||||
|
||||
# if __name__ == "__main__":
|
||||
# import sys
|
||||
|
||||
# if len(sys.argv) != 2:
|
||||
# print("Usage: python json_hpe.py <fichier_config>")
|
||||
# print("Exemple: python json_hpe.py config_hpe")
|
||||
# sys.exit(1)
|
||||
|
||||
# file = sys.argv[1]
|
||||
|
||||
# if not Path(file).exists():
|
||||
# print(f"✗ Erreur: Le fichier '{file}' n'existe pas")
|
||||
# sys.exit(1)
|
||||
|
||||
# try:
|
||||
# parser = HpeParser(file)
|
||||
# parser.parse_all()
|
||||
# parser.print_summary()
|
||||
|
||||
# input_path = Path(file)
|
||||
# output_file = 'output_Hpe.json'
|
||||
# parser.export_to_json(output_file)
|
||||
|
||||
# print(f"\n✓ Conversion terminée! Vérifiez le fichier: {output_file}")
|
||||
|
||||
# except Exception as e:
|
||||
# print(f"✗ Erreur: {e}")
|
||||
# sys.exit(1)
|
||||
return
|
||||
Reference in New Issue
Block a user