#!/bin/bash

# 사용자 이름 및 비밀번호 설정
IDRAC_USER="root"
IDRAC_PASS="calvin"

# IP 주소 파일 경로 인자 받기
if [ -z "$1" ]; then
    echo "Usage: $0 <ip_file>"
    exit 1
fi

IP_FILE=$1

if [ ! -f "$IP_FILE" ]; then
    echo "IP file $IP_FILE does not exist."
    exit 1
fi

# 정보 저장 디렉터리 설정
OUTPUT_DIR="idrac_info"
mkdir -p $OUTPUT_DIR

# iDRAC 정보를 가져오는 함수 정의
fetch_idrac_info() {
    local IDRAC_IP=$(cat $IP_FILE)

    # 모든 hwinventory 저장
    local getsysinfo=$(racadm -r $IDRAC_IP -u $IDRAC_USER -p $IDRAC_PASS getsysinfo)
    local swinventory=$(racadm -r $IDRAC_IP -u $IDRAC_USER -p $IDRAC_PASS swinventory)
    local hwinventory=$(racadm -r $IDRAC_IP -u $IDRAC_USER -p $IDRAC_PASS hwinventory)

    #서비스 태그 가져오기
    local SVC_TAG=$(echo "$getsysinfo" | grep -i "SVC Tag" | awk -F '=' '{print $2}' | tr -d '[:space:]')

    #iDRAC MAC 확인
    local idrac_mac=$(echo "$getsysinfo" | grep -i "MAC Address             = " | awk -F '=' '{print $2}' | tr -d '[:space:]')

    #NIC.Integrated MAC 확인
    local Integrated_1=$(echo "$getsysinfo" | grep -i "NIC.Integrated.1-1-1" | grep -o -E "([0-9A-Fa-f]{2}:){5}[0-9A-Fa-f]{2}")
    local Integrated_2=$(echo "$getsysinfo" | grep -i "NIC.Integrated.1-2-1" | grep -o -E "([0-9A-Fa-f]{2}:){5}[0-9A-Fa-f]{2}")
    local Integrated_3=$(echo "$getsysinfo" | grep -i "NIC.Integrated.1-3-1" | grep -o -E "([0-9A-Fa-f]{2}:){5}[0-9A-Fa-f]{2}")
    local Integrated_4=$(echo "$getsysinfo" | grep -i "NIC.Integrated.1-4-1" | grep -o -E "([0-9A-Fa-f]{2}:){5}[0-9A-Fa-f]{2}")

    #NIC.Slot.31 MAC 확인
    #local NICslot31_1=$(echo "$swinventory" | awk '/FQDD = NIC.Slot.31-1-1/ {print x; next} {x=$0}' | tail -1 | grep -o -E "([0-9A-Fa-f]{2}:){5}[0-9A-Fa-f]{2}")
    #local NICslot31_2=$(echo "$swinventory" | awk '/FQDD = NIC.Slot.31-2-1/ {print x; next} {x=$0}' | tail -1 | grep -o -E "([0-9A-Fa-f]{2}:){5}[0-9A-Fa-f]{2}")

    #추가 NIC MAC 확인
    local InfiniBand_Slot_31=$(echo "$swinventory" | awk '/FQDD = InfiniBand.Slot.31-1/ {print x; next} {x=$0}' | tail -1 | grep -o -E "([0-9A-Fa-f]{2}:){5}[0-9A-Fa-f]{2}")
    local InfiniBand_Slot_32=$(echo "$swinventory" | awk '/FQDD = InfiniBand.Slot.32-1/ {print x; next} {x=$0}' | tail -1 | grep -o -E "([0-9A-Fa-f]{2}:){5}[0-9A-Fa-f]{2}")
    local InfiniBand_Slot_33=$(echo "$swinventory" | awk '/FQDD = InfiniBand.Slot.33-1/ {print x; next} {x=$0}' | tail -1 | grep -o -E "([0-9A-Fa-f]{2}:){5}[0-9A-Fa-f]{2}")
    local InfiniBand_Slot_34=$(echo "$swinventory" | awk '/FQDD = InfiniBand.Slot.34-1/ {print x; next} {x=$0}' | tail -1 | grep -o -E "([0-9A-Fa-f]{2}:){5}[0-9A-Fa-f]{2}")
    local InfiniBand_Slot_35=$(echo "$swinventory" | awk '/FQDD = InfiniBand.Slot.35-1/ {print x; next} {x=$0}' | tail -1 | grep -o -E "([0-9A-Fa-f]{2}:){5}[0-9A-Fa-f]{2}")
    local InfiniBand_Slot_36=$(echo "$swinventory" | awk '/FQDD = InfiniBand.Slot.36-1/ {print x; next} {x=$0}' | tail -1 | grep -o -E "([0-9A-Fa-f]{2}:){5}[0-9A-Fa-f]{2}")
    local InfiniBand_Slot_37=$(echo "$swinventory" | awk '/FQDD = InfiniBand.Slot.37-1/ {print x; next} {x=$0}' | tail -1 | grep -o -E "([0-9A-Fa-f]{2}:){5}[0-9A-Fa-f]{2}")
    local InfiniBand_Slot_38=$(echo "$swinventory" | awk '/FQDD = InfiniBand.Slot.38-1/ {print x; next} {x=$0}' | tail -1 | grep -o -E "([0-9A-Fa-f]{2}:){5}[0-9A-Fa-f]{2}")
    local InfiniBand_Slot_39=$(echo "$swinventory" | awk '/FQDD = InfiniBand.Slot.39-1/ {print x; next} {x=$0}' | tail -1 | grep -o -E "([0-9A-Fa-f]{2}:){5}[0-9A-Fa-f]{2}")
    local InfiniBand_Slot_40=$(echo "$swinventory" | awk '/FQDD = InfiniBand.Slot.40-1/ {print x; next} {x=$0}' | tail -1 | grep -o -E "([0-9A-Fa-f]{2}:){5}[0-9A-Fa-f]{2}")


    #OnBoard MAC 확인
    local Onboard_1=$(echo "$getsysinfo" | grep -P "NIC.Embedded.1-1-1" | grep -o -E "([0-9A-Fa-f]{2}:){5}[0-9A-Fa-f]{2}")
    local Onboard_2=$(echo "$getsysinfo" | grep -P "NIC.Embedded.2-1-1" | grep -o -E "([0-9A-Fa-f]{2}:){5}[0-9A-Fa-f]{2}")

    #파트 벤더 확인
    local memory=$(echo "$hwinventory" | grep -A5 "DIMM" | grep "Manufacturer" | awk -F '=' '{print $2}' | awk '{$1=$1};1' | sort | uniq | cut -c1)
    local ssd=$(echo "$hwinventory" | grep -A3 "Disk.Bay" | grep "Manufacturer" | awk -F '=' '{print $2}' | awk '{$1=$1};1' | sort | uniq)

    # 서비스 태그가 존재하는지 확인
    if [ -z "$SVC_TAG" ]; then
        echo "Failed to retrieve SVC Tag for IP: $IDRAC_IP"
        return
    fi

    local OUTPUT_FILE="$OUTPUT_DIR/$SVC_TAG.txt"
    # 정보저장
    echo "$SVC_TAG" >> "$OUTPUT_FILE" 
    echo "$Integrated_1" >> "$OUTPUT_FILE"
    echo "$Integrated_2" >> "$OUTPUT_FILE"
    echo "$Integrated_3" >> "$OUTPUT_FILE"
    echo "$Integrated_4" >> "$OUTPUT_FILE"
    echo "$Onboard_1" >> "$OUTPUT_FILE"
    echo "$Onboard_2" >> "$OUTPUT_FILE"
    echo "$InfiniBand_Slot_38" >> "$OUTPUT_FILE"
    echo "$InfiniBand_Slot_39" >> "$OUTPUT_FILE"
    echo "$InfiniBand_Slot_37" >> "$OUTPUT_FILE"
    echo "$InfiniBand_Slot_36" >> "$OUTPUT_FILE"
    echo "$InfiniBand_Slot_32" >> "$OUTPUT_FILE"
    echo "$InfiniBand_Slot_33" >> "$OUTPUT_FILE"
    echo "$InfiniBand_Slot_34" >> "$OUTPUT_FILE"
    echo "$InfiniBand_Slot_35" >> "$OUTPUT_FILE"
    echo "$InfiniBand_Slot_31" >> "$OUTPUT_FILE"
    echo "$InfiniBand_Slot_40" >> "$OUTPUT_FILE"        
    echo "$idrac_mac" >> "$OUTPUT_FILE"
    echo "$memory" >> "$OUTPUT_FILE"
    echo "$ssd" >> "$OUTPUT_FILE"
    #임시파일 제거
    rm -f $IP_FILE
}

export -f fetch_idrac_info
export IDRAC_USER
export IDRAC_PASS
export OUTPUT_DIR

# 시작 시간 기록
START_TIME=$(date +%s)

# IP 목록 파일을 읽어 병렬로 작업 수행
fetch_idrac_info

# 종료 시간 기록
END_TIME=$(date +%s)

# 소요 시간 계산
ELAPSED_TIME=$(($END_TIME - $START_TIME))
ELAPSED_HOURS=$(($ELAPSED_TIME / 3600))
ELAPSED_MINUTES=$((($ELAPSED_TIME % 3600) / 60))
ELAPSED_SECONDS=$(($ELAPSED_TIME % 60))

echo "정보 수집 완료."
echo "수집 완료 시간: $ELAPSED_HOURS 시간, $ELAPSED_MINUTES 분, $ELAPSED_SECONDS 초."
