#!/bin/bash

##Item Format: local_bdf  rc_bdf  vidid  nvmectrl
SSDFILE="ssd_info"
TMPFILE="/tmp/randinfo"


#loquat vendor id and device id array
declare -a list6920
list6920=("1c5f003e" "1c1b0757")

declare -a list6530
list6530=("1c5f000e" )

declare -a list7940
list7940=("1c5f003f" "1f9f1333")

declare -a list7a40
list7a40=("1c5f0027")

# find the product nickname by the Vendorid Deviceid

checkenv()
{

PYTHON=`which python3 2>/dev/null`
if [ X"$PYTHON" == X ]; then
      PYTHON3=`which python3 2>/dev/null`
      if [ X"$PYTHON3" == X ]; then
              echo " The python3 should be installed "
              exit
      fi
     
fi

NVMECLI=`which nvme 2>/dev/null`
if [ X"$NVMECLI" == X ]; then
     echo " The nvmecli tool should be installed "
     exit
fi

ID=`id -u`
if [ $ID -ne 0 ];then
     echo " This program must be run as root" 
     exit
fi

}

scan_nvme()
{
echo "">${SSDFILE}
lspci -D |grep Non-Vo |while read line
do
#set -x
        local_bdf=`echo $line[0]|awk '{print $1}'`
        vidid=` lspci -nmm -s \$local_bdf|tr -d '\"'| awk '{print $3$4}'`
        path=`find /sys/devices -name nvme |grep \$local_bdf `
        nvmectrl=`ls ${path}`
        rc_bdf=`echo $path| awk -F/ '{print $(NF-2)}'`
        if [ "X${vidid:0:4}" == "X1c5f" ] || [ "X${vidid:0:4}" == "X1b18" ] || [ "X${vidid}" == "X1c1b0757" ] || [ "X${vidid}" == "X1f9f1333" ] || [ "X${vidid}" == "X1e491a48" ]; then
        item="${local_bdf}|${rc_bdf}|${vidid}|${nvmectrl}|"
        echo $item>>$SSDFILE
        fi
done
}

lookup_nvme()
{
list=$1
for item in `cat ${OUTPUT}/${SSDFILE}`
do
        echo $item
done
}

cpos_log()
{
find /var/log/ -mtime -14 |grep -E "boot|kern|message|dmesg|syslog" > $TMPFILE

cat $TMPFILE |while read line
do
 cp $line ./
done

}

cpcrash_log()
{
find /var/crash/ -mtime -14 -a -type f  |grep dmesg > $TMPFILE
cat $TMPFILE|while read line
do
     #echo $line
     dir=`echo $line|awk -F/ '{print $4_$5}'`
     cp $line $dir
done

}
collect_oslog()
{
cpos_log
cpcrash_log
dmesg -T >dmesg.now
}


collect_pciinfo()
{
#set -x
lspci | grep -i Non-Volatile  > ./devices.log
lspci -t > ./pcie-tree.log
lspci > ./lspci.log
lspci -vvv > ./lspci-vvv.log
cat ${OUTPUT}/${SSDFILE} |while read line
do
     if [ X$line == X ]; then
	continue
     fi
     local_bdf=`echo $line | awk -F\| '{print $1}'`
     #echo $local_bdf
     lspci -s $local_bdf -vvvv -xxxx > ./lspci_vx_${local_bdf}.log
     rc_bdf=`echo $line|awk -F\| '{print $2}'`
     #echo $rc_bdf
     lspci -s $rc_bdf -vvvv -xxxx > ./lspci_vx_${local_bdf}_rc.log
     
done

}

collect_osinfo()
{
cat /proc/interrupts | grep nvme > ./nvme.interrupts
cat /etc/sysconfig/irqbalance > ./irqbalance.config
cat /sys/module/nvme/version > ./dirver.VersionUse
modinfo nvme > ./dirver.VersionBuild
uname -a > ./uname_all.info
cat /proc/cmdline > ./cmdline.info
cp /boot/config-`uname -r`* ./
dmidecode -t1 > ./system.info
free -g > ./memory.GB
dmidecode -t processor > ./processor.info
cat /proc/cpuinfo > ./proc_cpu.info
cpupower frequency-info > ./cpupower.frequency
cpupower monitor > ./cpupower.power
date > ./os.date
cat /etc/*release > ./os.release
dmidecode -t bios > ./bios.info
uptime > ./system.uptime
mount >./mount_option
lsblk  >>./blockdevice.list
ls -la /sys/block >>./blockdevice.list
strings /etc/localtime >./timezone
cat /sys/module/nvme_core/parameters/admin_timeout > ./nvme_admin_timeout
cat /sys/module/nvme_core/parameters/io_timeout > ./nvme_io_timeout
cat /sys/module/nvme_core/parameters/streams > ./nvme_streams

}

collect_nvmeinfo()
{
cat ${OUTPUT}/${SSDFILE} |while read line
do
     if [ X$line == X ]; then
        continue
     fi

     ctrl=`echo $line | awk -F\| '{print $4}'`
     ctrlp="/dev/${ctrl}"
     ns="/dev/${ctrl}n1"
     echo $ctrl $ctrlp $ns
     $NVMECLI list >./nvme_list
     $NVMECLI id-ctrl $ctrlp -v >./${ctrl}_id_ctrl
     $NVMECLI id-ns $ns -v >./${ctrl}_id_ns
     $NVMECLI smart-log $ns >./${ctrl}_smart_log
     $NVMECLI error-log $ns >./${ctrl}_error_log
     $NVMECLI fw-log $ns >./${ctrl}_fw_log
     $NVMECLI memblaze smart-log-add $ns >./${ctrl}_smartadd_log
     $NVMECLI get-feature $ctrlp -f 0x2 >./${ctrl}_powermode_log
done
}

collect_debuglog()
{
SSDLIST=$1
cpu=`which lscpu 2>/dev/null |wc -l`
if [ $cpu -eq 1 ];then
     CPU=`lscpu |head -n 1|awk '{print $2}'`
else 
     CPU="x86_64"
fi
if [ X${SSDLIST} == X ];then
cat ${OUTPUT}/${SSDFILE} |while read line
do
     if [ X$line == X ]; then
        continue
     fi
     ctrl=`echo $line | awk -F\| '{print $4}'`
     ctrlp="/dev/${ctrl}"
     cd $BASE
     ${BASE}/LogDumper_${CPU} -d $ctrlp --log all

done
else 
array=(${SSDLIST//,/ })
for line in ${array[@]}
do
     if [ X$line == X ]; then
        continue
     fi
#set -x
     ctrl=`ls -l /sys/block/${line}|awk -F/ '{print $(NF-1)}' `
     ctrlp="/dev/${ctrl}"
     cd $BASE
     ${BASE}/LogDumper_${CPU} -d $ctrlp --log all  
#set +x
done
fi

wait

}

archive_log()
{
per=`date +%F%H%M%S` 
cd ${BASE}
find ./FaLog -name *.zip |xargs -I{} cp  {} ./output
tar -zcvf output_log_${per}.tar.gz ./output
rm -rf output
rm -rf FaLog

}
