Files
iDRAC_Info/data/scripts/09-Log_Viewer.py
2025-12-19 20:23:59 +09:00

105 lines
3.4 KiB
Python

import os
import subprocess
import time
import logging
from dotenv import load_dotenv
from concurrent.futures import ThreadPoolExecutor
# .env 파일 로드
load_dotenv()
# 로깅 설정
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s [INFO] root: %(message)s',
datefmt='%Y-%m-%d %H:%M:%S'
)
logger = logging.getLogger(__name__)
# 사용자 이름 및 비밀번호 설정
IDRAC_USER = os.getenv("IDRAC_USER")
IDRAC_PASS = os.getenv("IDRAC_PASS")
# IP 파일 유효성 검사 함수
def validate_ip_file(ip_file_path):
if not os.path.isfile(ip_file_path):
raise FileNotFoundError(f"IP 파일 '{ip_file_path}' 이(가) 존재하지 않습니다.")
return ip_file_path
# 정보 저장 디렉터리 설정
OUTPUT_DIR = "idrac_info"
os.makedirs(OUTPUT_DIR, exist_ok=True)
# iDRAC 정보를 가져오는 함수
def fetch_idrac_info(idrac_ip):
try:
logger.info(f"{idrac_ip}에 대한 샷시 정보 가져오는 중...")
getsysinfo_command = [
"racadm", "-r", idrac_ip,
"-u", IDRAC_USER,
"-p", IDRAC_PASS,
"getsysinfo"
]
getsysinfo_result = subprocess.run(getsysinfo_command, capture_output=True, text=True)
getsysinfo = getsysinfo_result.stdout
svc_tag = ""
for line in getsysinfo.splitlines():
if "SVC Tag" in line:
svc_tag = line.split('=')[1].strip()
break
if not svc_tag:
logger.error(f"IP: {idrac_ip}에 대한 SVC Tag 가져오기 실패")
return
logger.info(f"{idrac_ip}에 대한 서버 Log 가져오는 중...")
viewerlog_command = [
"racadm", "-r", idrac_ip,
"-u", IDRAC_USER,
"-p", IDRAC_PASS,
"getsel"
]
viewerlog_result = subprocess.run(viewerlog_command, capture_output=True, text=True)
viewerlog = viewerlog_result.stdout
output_file = os.path.join(OUTPUT_DIR, f"{svc_tag}.txt")
with open(output_file, 'w', encoding='utf-8') as f:
f.write(f"Dell Server Log Viewer (SVC Tag: {svc_tag})\n\n")
f.write("------------------------------------------Log Viewer------------------------------------------\n")
f.write(f"1. SVC Tag : {svc_tag}\n")
f.write(f"{viewerlog}\n")
logger.info(f"{idrac_ip}에 대한 로그 정보가 {output_file}에 저장되었습니다.")
except Exception as e:
logger.error(f"설정 중 오류 발생: {e}")
# 소요 시간 계산 함수
def calculate_elapsed_time(start_time):
elapsed_time = time.time() - start_time
hours, remainder = divmod(elapsed_time, 3600)
minutes, seconds = divmod(remainder, 60)
logger.info(f"전체 작업 소요 시간: {int(hours)} 시간, {int(minutes)} 분, {int(seconds)} 초.")
# 메인 함수
if __name__ == "__main__":
import sys
if len(sys.argv) != 2:
logger.error(f"Usage: {sys.argv[0]} <ip_file>")
sys.exit(1)
ip_file_path = validate_ip_file(sys.argv[1])
with open(ip_file_path, 'r') as ip_file:
ip_addresses = [line.strip() for line in ip_file if line.strip()]
start_time = time.time()
# 병렬 처리를 위해 ThreadPoolExecutor 사용
max_workers = 100 # 병렬 작업 수 설정
with ThreadPoolExecutor(max_workers=max_workers) as executor:
executor.map(fetch_idrac_info, ip_addresses)
calculate_elapsed_time(start_time)