#!/bin/bash
# set -x

g_sas_cmd=
raid_type=
ssd_manufacture=
CMD_LSPCI="get_cache lspci"
CMD_LSSCSI="get_cache lsscsi"
CMD_ARCCONF="get_cache arcconf"
CMD_SMARTCTL="get_cache smartctl"
CMD_SSACLI="get_cache ssacli"
SASTOOL_PATH="/opt/op/"
CMD_SAS2IRCU="get_cache ${SASTOOL_PATH}sas2ircu"
CMD_SAS3IRCU="get_cache ${SASTOOL_PATH}sas3ircu"
CMD_MEGACLI="get_cache /opt/MegaRAID/MegaCli/MegaCli64"
# CMD_STORCLI="/opt/MegaRAID/storcli/storcli64"

function wait_other() {
    local cmd=$1
    for i in {0..30};do
        ps -e|grep -q $cmd
        if [[ $? -ne 0 ]];then
            return 0
        fi
        sleep 1
    done 
    error_quit "other '$cmd' still running"
}

function get_cache() {
    local cmd=$@
    local pname=$(basename $(echo "$1"|awk '{print $1}'))
    local cache_name="monitor_"`echo $*|tr ' ' '_'|tr '/' '_'|sed 's/[:,]//g'`
    local cache_file=/dev/shm/$cache_name
    local cache_time='0'
    local code=0

    wait_other $pname

    if [[ -f $cache_file ]];then
        cache_time=`stat -c %Z $cache_file`
    fi
    if [[ $(($(date +%s) - $cache_time)) -gt 300 ]];then
        $cmd > $cache_file
        code=$?
    fi
    cat $cache_file

    return $code
}


function error_quit() {
    echo "$1"
    exit 1
}

# detect vm
function detect_vm() {
    if grep 'flags' /proc/cpuinfo | grep -q 'hypervisor'; then
    # if [ $? -eq 0 ]; then
        error_quit "Not Support VM"
    fi
}

function check_cmd_exist() {
    local cmd=$1
    type "$cmd" > /dev/null 2>&1 || {
        echo >&2 "require command [$cmd] but it's not installed. Aborting."
        exit 1
    }
}

function check_manufacturer() {
    local CHECK_COMMAND
    CHECK_COMMAND=$(dmidecode -t1)
    manufacturer=$(get_value "$(grep "Manufacturer" <<< "$CHECK_COMMAND")" ":")
    echo -n "$manufacturer"
}

function collect_sata_ssd_info() {
    detect_vm
    check_board
    case $raid_type in
        LSI_HBA)
            lsihba_sata_ssd_collect "$@"
            ;;
        LSI_RAID)
            lsi_sata_ssd_collect "$@"
            ;;
        PMC_HBA)
            pmchba_sata_smart_collect "$@"
            ;;
        PMC_RAID)
            pmc_sata_ssd_collect "$@"
            ;;
        DELL_RAID)
            lsi_sata_ssd_collect "$@"
            ;;
        HP_RAID)
            hp_sata_ssd_collect "$@"
            ;;
        *)
            exit 0;
            ;;
    esac
}


function join_string() {
    local arr=("${!2}")
    local result
    result=$(printf "$1%s" "${arr[@]}")
    result=${result:1}
    echo -n "$result"
}

function get_value() {
    local ori_str="$1"
    local trim_cmd="awk"
    if [ $# -gt 1 ] && [ "$2" != " " ]; then
        trim_cmd="$trim_cmd -F$2"
    fi

    local result
    result=$($trim_cmd '{
        gsub(/^[ \t]+/, "", $2);
        gsub(/[ \t]+$/, "", $2);
        print $2}' <<< "$ori_str")
    echo -n "$result"
}

function check_board() {
    check_cmd_exist $CMD_LSPCI

    local lspci_result
    lspci_result=$($CMD_LSPCI)
    case $lspci_result in
        *"LSI"*)
            local result
            result=$(grep "LSI" <<< "$lspci_result")
            case $result in
                *"005f"*)
                    exit
                    ;;
                *"2308"* | *"2008"* )
                    g_sas_cmd=$CMD_SAS2IRCU
                    raid_type="LSI_HBA"
                    ;;
                *"3008"* )
                    g_sas_cmd=$CMD_SAS3IRCU
                    raid_type="LSI_HBA"
                    ;;
                *"2208"* | *"3108"* | *"3316"* )
                    local manu
                    manu=$(check_manufacturer)
                    if [ "$manu" == "Dell Inc." ];then
                        raid_type="DELL_RAID"
                    else
                        raid_type="LSI_RAID"
                    fi
                    ;;
                *)
                    exit 1
                    ;;
            esac
            ;;
        *"Adaptec"*)
            local result
            result=$(grep -i "adaptec" <<< "$lspci_result")
            case $result in
                *"SCSI"*)
                    raid_type="PMC_HBA"
                    ;;
                *"RAID"*)
                    raid_type="PMC_RAID"
                    ;;
            esac
            ;;
        *"RAID bus controller: Hewlett-Packard"*)
            raid_type="HP_RAID"
            ;;
        *)
            exit
            ;;
    esac
}

function lsihba_sata_ssd_collect() {
    check_cmd_exist $CMD_SMARTCTL
    check_cmd_exist $g_sas_cmd

    local flags=$1
    local ssd_result=()
    local value
    local i=0
    local lsscsi_result
    lsscsi_result=$($CMD_LSSCSI -g)
    local ALL_DISKS
    ALL_DISKS=$(grep "ATA" <<< "$lsscsi_result" | awk '{print $NF}' | xargs | sed 's/ /|/g')
    local PHYSICAL_DISK_NUM
    PHYSICAL_DISK_NUM=$(grep -c "ATA" <<< "$lsscsi_result")

    for((j=1; j<="$PHYSICAL_DISK_NUM"; j++)); do
        local disk_label
        disk_label=$(echo "$ALL_DISKS" | awk -F"|" '{print $'$j'}')
        local disk_smart
        disk_smart=$($CMD_SMARTCTL -a "$disk_label")
        local disk_rotation_rate
        disk_rotation_rate=$(get_value "$(grep "Rotation Rate:" <<< "$disk_smart")" ":")
        if [ "$disk_rotation_rate" == "Solid State Device" ];then
            serial_number=$(get_value "$(grep "Serial Number:" <<< "$disk_smart")" ":")
            device_info=$($g_sas_cmd 0 display | grep -B 10 "$serial_number")
            Enclosure=$(get_value "$(grep "Enclosure #" <<< "$device_info")" ":")
            Slot=$(get_value "$(grep "Slot #" <<< "$device_info")" ":")
            lsi_check_ssd_man "$disk_smart"

            case "$ssd_manufacture" in
                intel)
                    case $flags in
                        end_error)
                            value=$(echo "$disk_smart" | grep "184 End-to-End_Error" | awk '{print $4}')
                            if [ -z "$value" ];then
                                continue
                            else
                                ssd_result[i]='{"value": '$((10#$value))', "tags":"raid_type='$raid_type',ssd_manu='$ssd_manufacture',Enclosure_Device_ID='$Enclosure',Slot_Number='$Slot'"}'
                                ((i++))
                            fi
                            ;;
                        uncorrect)
                            value=$(echo "$disk_smart" | grep "187 Reported_Uncorrect" | awk '{print $NF}')
                            if [ -z "$value" ];then
                                continue
                            else
                                ssd_result[i]='{"value": '$((10#$value))', "tags":"raid_type='$raid_type',ssd_manu='$ssd_manufacture',Enclosure_Device_ID='$Enclosure',Slot_Number='$Slot'"}'
                                ((i++))
                            fi
                            ;;
                        space)
                            value=$(echo "$disk_smart" | grep "232 Available_Reservd_Space" | awk '{print $4}')
                            if [ -z "$value" ];then
                                continue
                            else
                                ssd_result[i]='{"value": '$((10#$value))', "tags":"raid_type='$raid_type',ssd_manu='$ssd_manufacture',Enclosure_Device_ID='$Enclosure',Slot_Number='$Slot'"}'
                                ((i++))
                            fi
                            ;;
                        *)
                            continue
                            ;;
                    esac
                    ;;
                samsung)
                    case $flags in
                        reallocated)
                            value=$(echo "$disk_smart" | grep "5 Reallocated_Sector_Ct" | awk '{print $4}')
                            if [ -z "$value" ];then
                                continue
                            else
                                ssd_result[i]='{"value": '$((10#$value))', "tags":"raid_type='$raid_type',ssd_manu='$ssd_manufacture',Enclosure_Device_ID='$Enclosure',Slot_Number='$Slot'"}'
                                ((i++))
                            fi
                            ;;
                        count)
                            value=$(echo "$disk_smart" | grep "177 Wear" | awk '{print $4}')
                            if [ -z "$value" ];then
                                continue
                            else
                                ssd_result[i]='{"value": '$((10#$value))', "tags":"raid_type='$raid_type',ssd_manu='$ssd_manufacture',Enclosure_Device_ID='$Enclosure',Slot_Number='$Slot'"}'
                                ((i++))
                            fi
                            ;;
                        used_rsvd)
                            value=$(echo "$disk_smart" | grep "179 Used" | awk '{print $4}')
                            if [ -z "$value" ];then
                                continue
                            else
                                ssd_result[i]='{"value": '$((10#$value))', "tags":"raid_type='$raid_type',ssd_manu='$ssd_manufacture',Enclosure_Device_ID='$Enclosure',Slot_Number='$Slot'"}'
                                ((i++))
                            fi
                            ;;
                        unused_rsvd)
                            value=$(echo "$disk_smart" | grep "180 Unused" | awk '{print $4}')
                            if [ -z "$value" ];then
                                continue
                            else
                                ssd_result[i]='{"value": '$((10#$value))', "tags":"raid_type='$raid_type',ssd_manu='$ssd_manufacture',Enclosure_Device_ID='$Enclosure',Slot_Number='$Slot'"}'
                                ((i++))
                            fi
                            ;;
                        runtime)
                            value=$(echo "$disk_smart" | grep "183 Runtime_Bad_Block" | awk '{print $4}')
                            if [ -z "$value" ];then
                                continue
                            else
                                ssd_result[i]='{"value": '$((10#$value))', "tags":"raid_type='$raid_type',ssd_manu='$ssd_manufacture',Enclosure_Device_ID='$Enclosure',Slot_Number='$Slot'"}'
                                ((i++))
                            fi
                            ;;
                        end_error)
                            value=$(echo "$disk_smart" | grep "184 End-to-End_Error" | awk '{print $4}')
                            if [ -z "$value" ];then
                                continue
                            else
                                ssd_result[i]='{"value": '$((10#$value))', "tags":"raid_type='$raid_type',ssd_manu='$ssd_manufacture',Enclosure_Device_ID='$Enclosure',Slot_Number='$Slot'"}'
                                ((i++))
                            fi
                            ;;
                        uncorrect)
                            value=$(echo "$disk_smart" | grep "187 Reported" | awk '{print $NF}')
                            if [ -z "$value" ];then
                                continue
                            else
                                ssd_result[i]='{"value": '$((10#$value))', "tags":"raid_type='$raid_type',ssd_manu='$ssd_manufacture',Enclosure_Device_ID='$Enclosure',Slot_Number='$Slot'"}'
                                ((i++))
                            fi
                            ;;
                        *)
                            continue
                            ;;
                    esac
                    ;;
                micron)
                    case $flags in
                        error_rate)
                            value=$(echo "$disk_smart" | grep "1 Raw_Read_Error_Rate" | awk '{print $4}')
                            if [ -z "$value" ];then
                                continue
                            else
                                ssd_result[i]='{"value": '$((10#$value))', "tags":"raid_type='$raid_type',ssd_manu='$ssd_manufacture',Enclosure_Device_ID='$Enclosure',Slot_Number='$Slot'"}'
                                ((i++))
                            fi
                            ;;
                        reallocated) # Deprecated
                            value=$(echo "$disk_smart" | grep "Reallocated_Sector_Ct" | awk '{print $4}')
                            if [ -z "$value" ];then
                                continue
                            else
                                ssd_result[i]='{"value": '$((10#$value))', "tags":"raid_type='$raid_type',ssd_manu='$ssd_manufacture',Enclosure_Device_ID='$Enclosure',Slot_Number='$Slot'"}'
                                ((i++))
                            fi
                            ;;
                        reserved_block)
                            value=$(echo "$disk_smart" | grep -E "170 Unknown_|170 Reserved" | awk '{print $4}')
                            if [ -z "$value" ];then
                                continue
                            else
                                ssd_result[i]='{"value": '$((10#$value))', "tags":"raid_type='$raid_type',ssd_manu='$ssd_manufacture',Enclosure_Device_ID='$Enclosure',Slot_Number='$Slot'"}'
                                ((i++))
                            fi
                            ;;
                        erase) # Deprecated
                            value=$(echo "$disk_smart" | grep "Erase_Fail_Count" | awk '{print $4}')
                            if [ -z "$value" ];then
                                continue
                            else
                                ssd_result[i]='{"value": '$((10#$value))', "tags":"raid_type='$raid_type',ssd_manu='$ssd_manufacture',Enclosure_Device_ID='$Enclosure',Slot_Number='$Slot'"}'
                                ((i++))
                            fi
                            ;;
                        lifetime)
                            value=$(echo "$disk_smart" | grep -E "202 Unknown_|202 Percent_Lifetime" | awk '{print $4}')
                            if [ -z "$value" ];then
                                continue
                            else
                                ssd_result[i]='{"value": '$((10#$value))', "tags":"raid_type='$raid_type',ssd_manu='$ssd_manufacture',Enclosure_Device_ID='$Enclosure',Slot_Number='$Slot'"}'
                                ((i++))
                            fi
                            ;;
                        uncorrect)
                            value=$(echo "$disk_smart" | grep "187 Reported" | awk '{print $NF}')
                            if [ -z "$value" ];then
                                continue
                            else
                                ssd_result[i]='{"value": '$((10#$value))', "tags":"raid_type='$raid_type',ssd_manu='$ssd_manufacture',Enclosure_Device_ID='$Enclosure',Slot_Number='$Slot'"}'
                                ((i++))
                            fi
                            ;;
                        *)
                            continue
                            ;;
                    esac
                    ;;
                sandisk)
                    case $flags in
                        reallocated)
                            value=$(echo "$disk_smart" | grep "5 Reallocated_Sector_Ct" | awk '{print $4}')
                            if [ -z "$value" ];then
                                continue
                            else
                                ssd_result[i]='{"value": '$((10#$value))', "tags":"raid_type='$raid_type',ssd_manu='$ssd_manufacture',Enclosure_Device_ID='$Enclosure',Slot_Number='$Slot'"}'
                                ((i++))
                            fi
                            ;;
                        lifetime)
                            value=$(echo "$disk_smart" | grep "13 Read" | awk '{print $NF}')
                            if [ -z "$value" ];then
                                continue
                            else
                                ssd_result[i]='{"value": '$((10#$value))', "tags":"raid_type='$raid_type',ssd_manu='$ssd_manufacture',Enclosure_Device_ID='$Enclosure',Slot_Number='$Slot'"}'
                                ((i++))
                            fi
                            ;;
                        temperature_celsius) # Deprecated
                            value=$(echo "$disk_smart" | grep "Temperature_Celsius" | awk '{print $4}')
                            if [ -z "$value" ];then
                                continue
                            else
                                ssd_result[i]='{"value": '$((10#$value))', "tags":"raid_type='$raid_type',ssd_manu='$ssd_manufacture',Enclosure_Device_ID='$Enclosure',Slot_Number='$Slot'"}'
                                ((i++))
                            fi
                            ;;
                        reallocated_event)
                            value=$(echo "$disk_smart" | grep "196 Reallocated" | awk '{print $4}')
                            if [ -z "$value" ];then
                                continue
                            else
                                ssd_result[i]='{"value": '$((10#$value))', "tags":"raid_type='$raid_type',ssd_manu='$ssd_manufacture',Enclosure_Device_ID='$Enclosure',Slot_Number='$Slot'"}'
                                ((i++))
                            fi
                            ;;
                        drive)
                            value=$(echo "$disk_smart" | grep "245 Unknown" | awk '{print $4}')
                            if [ -z "$value" ];then
                                continue
                            else
                                ssd_result[i]='{"value": '$((10#$value))', "tags":"raid_type='$raid_type',ssd_manu='$ssd_manufacture',Enclosure_Device_ID='$Enclosure',Slot_Number='$Slot'"}'
                                ((i++))
                            fi
                            ;;
                        *)
                            continue
                            ;;
                    esac
                    ;;
                *)
                    exit
                    ;;
            esac
        fi
    done

    local ss_col
    ss_col=$(join_string "," ssd_result[@])
    if [ ${#ssd_result[@]} -eq 0 ];then
        exit
    fi
    
    echo -n "[$ss_col]"
}

function lsi_sata_ssd_collect() {
    check_cmd_exist $CMD_SMARTCTL

    local flags=$1
    local ssd_result=()
    local value
    local i=0
    local slots
    slots=$(get_slots)
    
    for slot in "${slots[@]}"; do
        CMD_RESULT=$($CMD_MEGACLI -PDList -a "$slot")
        PHYSICAL_DISK_NUM=$($CMD_MEGACLI -pdGetNum -a "$slot" | grep "Number of Physical Drives on Adapter $slot:" | awk -F":" '{print $2}' | sed 's/ //g')
        Enclosure_Device_ID=$(grep "Enclosure Device ID:" <<< "$CMD_RESULT" | awk -F":" '{print $2}' | xargs | sed 's/ /|/g')
        Slot_Number=$(grep "Slot Number:" <<< "$CMD_RESULT" | awk -F":" '{print $2}' | xargs | sed 's/ /|/g')
        Device_ID=$(grep "Device Id" <<< "$CMD_RESULT" | awk -F":" '{print $2}' | xargs | sed 's/ /|/g')

        for((j=1; j<="$PHYSICAL_DISK_NUM"; j++)); do
            local ID
            ID=$(echo "$Device_ID" | awk -F"|" '{print $'$j'}')
            local disk_smart
            disk_smart=$($CMD_SMARTCTL -a -d sat+megaraid,"$ID" /dev/$(lsblk -d -n -r|head -n1|awk '{print $1}'))
            local disk_rotation_rate
            disk_rotation_rate=$(get_value "$(grep "Rotation Rate:" <<< "$disk_smart")" ":")
            if [ "$disk_rotation_rate" == "Solid State Device" ];then
                lsi_check_ssd_man "$disk_smart"

                case "$ssd_manufacture" in
                    intel)
                        case $flags in
                            end_error)
                                value=$(echo "$disk_smart" | grep "184 End-to-End_Error" | awk '{print $4}')
                                if [ -z "$value" ];then
                                    continue
                                else
                                    jEnclosure_Device_ID=$(echo "$Enclosure_Device_ID" | awk -F"|" '{print $'$j'}')
                                    jSlot_Number=$(echo "$Slot_Number" | awk -F"|" '{print $'$j'}')
                                    ssd_result[i]='{"value": '$((10#$value))', "tags":"raid_type='$raid_type',ssd_manu='$ssd_manufacture',Enclosure_Device_ID='$jEnclosure_Device_ID',Slot_Number='$jSlot_Number'"}'
                                    ((i++))
                                fi
                                ;;
                            uncorrect)
                                value=$(echo "$disk_smart" | grep "187 Reported_Uncorrect" | awk '{print $NF}')
                                if [ -z "$value" ];then
                                    continue
                                else
                                    jEnclosure_Device_ID=$(echo "$Enclosure_Device_ID" | awk -F"|" '{print $'$j'}')
                                    jSlot_Number=$(echo "$Slot_Number" | awk -F"|" '{print $'$j'}')
                                    ssd_result[i]='{"value": '$((10#$value))', "tags":"raid_type='$raid_type',ssd_manu='$ssd_manufacture',Enclosure_Device_ID='$jEnclosure_Device_ID',Slot_Number='$jSlot_Number'"}'
                                    ((i++))
                                fi
                                ;;
                            space)
                                value=$(echo "$disk_smart" | grep "232 Available_Reservd_Space" | awk '{print $4}')
                                if [ -z "$value" ];then
                                    continue
                                else
                                    jEnclosure_Device_ID=$(echo "$Enclosure_Device_ID" | awk -F"|" '{print $'$j'}')
                                    jSlot_Number=$(echo "$Slot_Number" | awk -F"|" '{print $'$j'}')
                                    ssd_result[i]='{"value": '$((10#$value))', "tags":"raid_type='$raid_type',ssd_manu='$ssd_manufacture',Enclosure_Device_ID='$jEnclosure_Device_ID',Slot_Number='$jSlot_Number'"}'
                                    ((i++))
                                fi
                                ;;
                            *)
                                continue
                                ;;
                        esac
                        ;;
                    samsung)
                        case $flags in
                            reallocated)
                                value=$(echo "$disk_smart" | grep "5 Reallocated_Sector_Ct" | awk '{print $4}')
                                if [ -z "$value" ];then
                                    continue
                                else
                                    jEnclosure_Device_ID=$(echo "$Enclosure_Device_ID" | awk -F"|" '{print $'$j'}')
                                    jSlot_Number=$(echo "$Slot_Number" | awk -F"|" '{print $'$j'}')
                                    ssd_result[i]='{"value": '$((10#$value))', "tags":"raid_type='$raid_type',ssd_manu='$ssd_manufacture',Enclosure_Device_ID='$jEnclosure_Device_ID',Slot_Number='$jSlot_Number'"}'
                                    ((i++))
                                fi
                                ;;
                            count)
                                value=$(echo "$disk_smart" | grep "177 Wear" | awk '{print $4}')
                                if [ -z "$value" ];then
                                    continue
                                else
                                    jEnclosure_Device_ID=$(echo "$Enclosure_Device_ID" | awk -F"|" '{print $'$j'}')
                                    jSlot_Number=$(echo "$Slot_Number" | awk -F"|" '{print $'$j'}')
                                    ssd_result[i]='{"value": '$((10#$value))', "tags":"raid_type='$raid_type',ssd_manu='$ssd_manufacture',Enclosure_Device_ID='$jEnclosure_Device_ID',Slot_Number='$jSlot_Number'"}'
                                    ((i++))
                                fi
                                ;;
                            used_rsvd)
                                value=$(echo "$disk_smart" | grep "179 Used" | awk '{print $4}')
                                if [ -z "$value" ];then
                                    continue
                                else
                                    jEnclosure_Device_ID=$(echo "$Enclosure_Device_ID" | awk -F"|" '{print $'$j'}')
                                    jSlot_Number=$(echo "$Slot_Number" | awk -F"|" '{print $'$j'}')
                                    ssd_result[i]='{"value": '$((10#$value))', "tags":"raid_type='$raid_type',ssd_manu='$ssd_manufacture',Enclosure_Device_ID='$jEnclosure_Device_ID',Slot_Number='$jSlot_Number'"}'
                                    ((i++))
                                fi
                                ;;
                            unused_rsvd)
                                value=$(echo "$disk_smart" | grep "180 Unused_" | awk '{print $4}')
                                if [ -z "$value" ];then
                                    continue
                                else
                                    jEnclosure_Device_ID=$(echo "$Enclosure_Device_ID" | awk -F"|" '{print $'$j'}')
                                    jSlot_Number=$(echo "$Slot_Number" | awk -F"|" '{print $'$j'}')
                                    ssd_result[i]='{"value": '$((10#$value))', "tags":"raid_type='$raid_type',ssd_manu='$ssd_manufacture',Enclosure_Device_ID='$jEnclosure_Device_ID',Slot_Number='$jSlot_Number'"}'
                                    ((i++))
                                fi
                                ;;
                            runtime)
                                value=$(echo "$disk_smart" | grep "183 Runtime_" | awk '{print $4}')
                                if [ -z "$value" ];then
                                    continue
                                else
                                    jEnclosure_Device_ID=$(echo "$Enclosure_Device_ID" | awk -F"|" '{print $'$j'}')
                                    jSlot_Number=$(echo "$Slot_Number" | awk -F"|" '{print $'$j'}')
                                    ssd_result[i]='{"value": '$((10#$value))', "tags":"raid_type='$raid_type',ssd_manu='$ssd_manufacture',Enclosure_Device_ID='$jEnclosure_Device_ID',Slot_Number='$jSlot_Number'"}'
                                    ((i++))
                                fi
                                ;;
                            end_error)
                                value=$(echo "$disk_smart" | grep "184 End-to-End_Error" | awk '{print $4}')
                                if [ -z "$value" ];then
                                    continue
                                else
                                    jEnclosure_Device_ID=$(echo "$Enclosure_Device_ID" | awk -F"|" '{print $'$j'}')
                                    jSlot_Number=$(echo "$Slot_Number" | awk -F"|" '{print $'$j'}')
                                    ssd_result[i]='{"value": '$((10#$value))', "tags":"raid_type='$raid_type',ssd_manu='$ssd_manufacture',Enclosure_Device_ID='$jEnclosure_Device_ID',Slot_Number='$jSlot_Number'"}'
                                    ((i++))
                                fi
                                ;;
                            uncorrect)
                                value=$(echo "$disk_smart" | grep "187 Reported" | awk '{print $NF}')
                                if [ -z "$value" ];then
                                    continue
                                else
                                    jEnclosure_Device_ID=$(echo "$Enclosure_Device_ID" | awk -F"|" '{print $'$j'}')
                                    jSlot_Number=$(echo "$Slot_Number" | awk -F"|" '{print $'$j'}')
                                    ssd_result[i]='{"value": '$((10#$value))', "tags":"raid_type='$raid_type',ssd_manu='$ssd_manufacture',Enclosure_Device_ID='$jEnclosure_Device_ID',Slot_Number='$jSlot_Number'"}'
                                    ((i++))
                                fi
                                ;;
                            *)
                                continue
                                ;;
                        esac
                        ;;
                    micron)
                        case $flags in
                            error_rate)
                                value=$(echo "$disk_smart" | grep "1 Raw_Read_Error_Rate" | awk '{print $4}')
                                if [ -z "$value" ];then
                                    continue
                                else
                                    jEnclosure_Device_ID=$(echo "$Enclosure_Device_ID" | awk -F"|" '{print $'$j'}')
                                    jSlot_Number=$(echo "$Slot_Number" | awk -F"|" '{print $'$j'}')
                                    ssd_result[i]='{"value": '$((10#$value))', "tags":"raid_type='$raid_type',ssd_manu='$ssd_manufacture',Enclosure_Device_ID='$jEnclosure_Device_ID',Slot_Number='$jSlot_Number'"}'
                                    ((i++))
                                fi
                                ;;
                            reallocated) # Deprecated
                                value=$(echo "$disk_smart" | grep "Reallocated_Sector_Ct" | awk '{print $4}')
                                if [ -z "$value" ];then
                                    continue
                                else
                                    jEnclosure_Device_ID=$(echo "$Enclosure_Device_ID" | awk -F"|" '{print $'$j'}')
                                    jSlot_Number=$(echo "$Slot_Number" | awk -F"|" '{print $'$j'}')
                                    ssd_result[i]='{"value": '$((10#$value))', "tags":"raid_type='$raid_type',ssd_manu='$ssd_manufacture',Enclosure_Device_ID='$jEnclosure_Device_ID',Slot_Number='$jSlot_Number'"}'
                                    ((i++))
                                fi
                                ;;
                            reserved_block)
                                value=$(echo "$disk_smart" | grep -E "170 Unknown_|170 Reserved" | awk '{print $4}')
                                if [ -z "$value" ];then
                                    continue
                                else
                                    jEnclosure_Device_ID=$(echo "$Enclosure_Device_ID" | awk -F"|" '{print $'$j'}')
                                    jSlot_Number=$(echo "$Slot_Number" | awk -F"|" '{print $'$j'}')
                                    ssd_result[i]='{"value": '$((10#$value))', "tags":"raid_type='$raid_type',ssd_manu='$ssd_manufacture',Enclosure_Device_ID='$jEnclosure_Device_ID',Slot_Number='$jSlot_Number'"}'
                                    ((i++))
                                fi
                                ;;
                            erase) # Deprecated
                                value=$(echo "$disk_smart" | grep "Erase_Fail_Count" | awk '{print $4}')
                                if [ -z "$value" ];then
                                    continue
                                else
                                    jEnclosure_Device_ID=$(echo "$Enclosure_Device_ID" | awk -F"|" '{print $'$j'}')
                                    jSlot_Number=$(echo "$Slot_Number" | awk -F"|" '{print $'$j'}')
                                    ssd_result[i]='{"value": '$((10#$value))', "tags":"raid_type='$raid_type',ssd_manu='$ssd_manufacture',Enclosure_Device_ID='$jEnclosure_Device_ID',Slot_Number='$jSlot_Number'"}'
                                    ((i++))
                                fi
                                ;;
                            lifetime)
                                value=$(echo "$disk_smart" | grep -E "202 Unknown_|202 Percent_Lifetime" | awk '{print $4}')
                                if [ -z "$value" ];then
                                    continue
                                else
                                    jEnclosure_Device_ID=$(echo "$Enclosure_Device_ID" | awk -F"|" '{print $'$j'}')
                                    jSlot_Number=$(echo "$Slot_Number" | awk -F"|" '{print $'$j'}')
                                    ssd_result[i]='{"value": '$((10#$value))', "tags":"raid_type='$raid_type',ssd_manu='$ssd_manufacture',Enclosure_Device_ID='$jEnclosure_Device_ID',Slot_Number='$jSlot_Number'"}'
                                    ((i++))
                                fi
                                ;;
                            uncorrect)
                                value=$(echo "$disk_smart" | grep "187 Reported" | awk '{print $NF}')
                                if [ -z "$value" ];then
                                    continue
                                else
                                    jEnclosure_Device_ID=$(echo "$Enclosure_Device_ID" | awk -F"|" '{print $'$j'}')
                                    jSlot_Number=$(echo "$Slot_Number" | awk -F"|" '{print $'$j'}')
                                    ssd_result[i]='{"value": '$((10#$value))', "tags":"raid_type='$raid_type',ssd_manu='$ssd_manufacture',Enclosure_Device_ID='$jEnclosure_Device_ID',Slot_Number='$jSlot_Number'"}'
                                    ((i++))
                                fi
                                ;;
                            *)
                                continue
                                ;;
                        esac
                        ;;
                    sandisk)
                        case $flags in
                            reallocated)
                                value=$(echo "$disk_smart" | grep "5 Reallocated_Sector_Ct" | awk '{print $4}')
                                if [ -z "$value" ];then
                                    continue
                                else
                                    jEnclosure_Device_ID=$(echo "$Enclosure_Device_ID" | awk -F"|" '{print $'$j'}')
                                    jSlot_Number=$(echo "$Slot_Number" | awk -F"|" '{print $'$j'}')
                                    ssd_result[i]='{"value": '$((10#$value))', "tags":"raid_type='$raid_type',ssd_manu='$ssd_manufacture',Enclosure_Device_ID='$jEnclosure_Device_ID',Slot_Number='$jSlot_Number'"}'
                                    ((i++))
                                fi
                                ;;
                            lifetime)
                                value=$(echo "$disk_smart" | grep "13 Read" | awk '{print $NF}')
                                if [ -z "$value" ];then
                                    continue
                                else
                                    jEnclosure_Device_ID=$(echo "$Enclosure_Device_ID" | awk -F"|" '{print $'$j'}')
                                    jSlot_Number=$(echo "$Slot_Number" | awk -F"|" '{print $'$j'}')
                                    ssd_result[i]='{"value": '$((10#$value))', "tags":"raid_type='$raid_type',ssd_manu='$ssd_manufacture',Enclosure_Device_ID='$jEnclosure_Device_ID',Slot_Number='$jSlot_Number'"}'
                                    ((i++))
                                fi
                                ;;
                            temperature_celsius) # Deprecated
                                value=$(echo "$disk_smart" | grep "Temperature_Celsius" | awk '{print $4}')
                                if [ -z "$value" ];then
                                    continue
                                else
                                    jEnclosure_Device_ID=$(echo "$Enclosure_Device_ID" | awk -F"|" '{print $'$j'}')
                                    jSlot_Number=$(echo "$Slot_Number" | awk -F"|" '{print $'$j'}')
                                    ssd_result[i]='{"value": '$((10#$value))', "tags":"raid_type='$raid_type',ssd_manu='$ssd_manufacture',Enclosure_Device_ID='$jEnclosure_Device_ID',Slot_Number='$jSlot_Number'"}'
                                    ((i++))
                                fi
                                ;;
                            reallocated_event)
                                value=$(echo "$disk_smart" | grep "196 Reallocated" | awk '{print $4}')
                                if [ -z "$value" ];then
                                    continue
                                else
                                    jEnclosure_Device_ID=$(echo "$Enclosure_Device_ID" | awk -F"|" '{print $'$j'}')
                                    jSlot_Number=$(echo "$Slot_Number" | awk -F"|" '{print $'$j'}')
                                    ssd_result[i]='{"value": '$((10#$value))', "tags":"raid_type='$raid_type',ssd_manu='$ssd_manufacture',Enclosure_Device_ID='$jEnclosure_Device_ID',Slot_Number='$jSlot_Number'"}'
                                    ((i++))
                                fi
                                ;;
                            drive)
                                value=$(echo "$disk_smart" | grep "245 Unknown" | awk '{print $4}')
                                if [ -z "$value" ];then
                                    continue
                                else
                                    jEnclosure_Device_ID=$(echo "$Enclosure_Device_ID" | awk -F"|" '{print $'$j'}')
                                    jSlot_Number=$(echo "$Slot_Number" | awk -F"|" '{print $'$j'}')
                                    ssd_result[i]='{"value": '$((10#$value))', "tags":"raid_type='$raid_type',ssd_manu='$ssd_manufacture',Enclosure_Device_ID='$jEnclosure_Device_ID',Slot_Number='$jSlot_Number'"}'
                                    ((i++))
                                fi
                                ;;
                            *)
                                continue
                                ;;
                        esac
                        ;;
                    *)
                        exit
                        ;;
                esac
            fi
        done
    done

    local ss_col
    ss_col=$(join_string "," ssd_result[@])
    if [ ${#ssd_result[@]} -eq 0 ];then
        exit
    fi
    
    echo -n "[$ss_col]"
}

function pmc_sata_ssd_collect() {
    check_cmd_exist $CMD_SMARTCTL

    local controllers
    controllers=$(get_controller)
    local ssd_result=()
    local flags=$1
    local i=0

    for controller in "${controllers[@]}"; do
        local r
        r=$($CMD_ARCCONF getconfig "$controller" PD NODISKINFO| sed -n -e '/Device #/,/^$/p')
        local Device_ID
        Device_ID=$(lsscsi -g |grep  "ATA" |awk '{print $1}' |awk -F":" '{print $3}' | xargs | sed 's/ /|/g')
        local PHYSICAL_DISK_NUM
        PHYSICAL_DISK_NUM=$(grep -c "Device is a Hard drive" <<< "$r")

        for ((j=1; j<="$PHYSICAL_DISK_NUM"; j++)); do
            local ID
            ID=$(echo "$Device_ID" | awk -F"|" '{print $'$j'}')
            local disk_smart
            disk_smart=$($CMD_SMARTCTL -a -d aacraid,0,0,"$ID" /dev/sda)
            local disk_rotation_rate
            disk_rotation_rate=$(get_value "$(grep "Rotation Rate:" <<< "$disk_smart")" ":")
            if [ "$disk_rotation_rate" == "Solid State Device" ];then
                lsi_check_ssd_man "$disk_smart"
                local get_sn
                get_sn=$(echo "$disk_smart" | grep "Serial Number" | awk '{print $3}')
                local find_location
                find_location=$(echo "$r" | grep -B 13 "$get_sn" | grep "Reported Location")
                local enclosure
                enclosure=$(echo "$find_location" | awk -F, '{print $1}' | awk '{print $NF}')
                local slot
                slot=$(echo "$find_location" | awk -F, '{print $2}' | sed 's/(.*)//g' | awk '{print $2}')

                case "$ssd_manufacture" in
                    intel)
                        case $flags in
                            end_error)
                                value=$(echo "$disk_smart" | grep "184 End-to-End_Error" | awk '{print $4}')
                                if [ -z "$value" ];then
                                    continue
                                else
                                    ssd_result[i]='{"value": '$((10#$value))', "tags":"raid_type='$raid_type',ssd_manu='$ssd_manufacture',controller='$controller',enclosure='$enclosure',slot='$slot'"}'
                                    ((i++))
                                fi
                                ;;
                            uncorrect)
                                value=$(echo "$disk_smart" | grep "187 Reported_Uncorrect" | awk '{print $NF}')
                                if [ -z "$value" ];then
                                    continue
                                else
                                    ssd_result[i]='{"value": '$((10#$value))', "tags":"raid_type='$raid_type',ssd_manu='$ssd_manufacture',controller='$controller',enclosure='$enclosure',slot='$slot'"}'
                                    ((i++))
                                fi
                                ;;
                            space)
                                value=$(echo "$disk_smart" | grep "232 Available_Reservd_Space" | awk '{print $4}')
                                if [ -z "$value" ];then
                                    continue
                                else
                                    ssd_result[i]='{"value": '$((10#$value))', "tags":"raid_type='$raid_type',ssd_manu='$ssd_manufacture',controller='$controller',enclosure='$enclosure',slot='$slot'"}'
                                    ((i++))
                                fi
                                ;;
                            *)
                                continue
                                ;;
                        esac
                        ;;
                    samsung)
                        case $flags in
                            reallocated)
                                value=$(echo "$disk_smart" | grep "5 Reallocated_Sector_Ct" | awk '{print $4}')
                                if [ -z "$value" ];then
                                    continue
                                else
                                    ssd_result[i]='{"value": '$((10#$value))', "tags":"raid_type='$raid_type',ssd_manu='$ssd_manufacture',controller='$controller',enclosure='$enclosure',slot='$slot'"}'
                                    ((i++))
                                fi
                                ;;
                            count)
                                value=$(echo "$disk_smart" | grep "177 Wear" | awk '{print $4}')
                                if [ -z "$value" ];then
                                    continue
                                else
                                    ssd_result[i]='{"value": '$((10#$value))', "tags":"raid_type='$raid_type',ssd_manu='$ssd_manufacture',controller='$controller',enclosure='$enclosure',slot='$slot'"}'
                                    ((i++))
                                fi
                                ;;
                            used_rsvd)
                                value=$(echo "$disk_smart" | grep "179 Used_" | awk '{print $4}')
                                if [ -z "$value" ];then
                                    continue
                                else
                                    ssd_result[i]='{"value": '$((10#$value))', "tags":"raid_type='$raid_type',ssd_manu='$ssd_manufacture',controller='$controller',enclosure='$enclosure',slot='$slot'"}'
                                    ((i++))
                                fi
                                ;;
                            unused_rsvd)
                                value=$(echo "$disk_smart" | grep "180 Unused_" | awk '{print $4}')
                                if [ -z "$value" ];then
                                    continue
                                else
                                    ssd_result[i]='{"value": '$((10#$value))', "tags":"raid_type='$raid_type',ssd_manu='$ssd_manufacture',controller='$controller',enclosure='$enclosure',slot='$slot'"}'
                                    ((i++))
                                fi
                                ;;
                            runtime)
                                value=$(echo "$disk_smart" | grep "183 Runtime_" | awk '{print $4}')
                                if [ -z "$value" ];then
                                    continue
                                else
                                    ssd_result[i]='{"value": '$((10#$value))', "tags":"raid_type='$raid_type',ssd_manu='$ssd_manufacture',controller='$controller',enclosure='$enclosure',slot='$slot'"}'
                                    ((i++))
                                fi
                                ;;
                            end_error)
                                value=$(echo "$disk_smart" | grep "184 End-to-End_Error" | awk '{print $4}')
                                if [ -z "$value" ];then
                                    continue
                                else
                                    ssd_result[i]='{"value": '$((10#$value))', "tags":"raid_type='$raid_type',ssd_manu='$ssd_manufacture',controller='$controller',enclosure='$enclosure',slot='$slot'"}'
                                    ((i++))
                                fi
                                ;;
                            uncorrect)
                                value=$(echo "$disk_smart" | grep "187 Reported" | awk '{print $NF}')
                                if [ -z "$value" ];then
                                    continue
                                else
                                    ssd_result[i]='{"value": '$((10#$value))', "tags":"raid_type='$raid_type',ssd_manu='$ssd_manufacture',controller='$controller',enclosure='$enclosure',slot='$slot'"}'
                                    ((i++))
                                fi
                                ;;
                            *)
                                continue
                                ;;
                        esac
                        ;;
                    micron)
                        case $flags in
                            error_rate)
                                value=$(echo "$disk_smart" | grep "1 Raw_Read_Error_Rate" | awk '{print $4}')
                                if [ -z "$value" ];then
                                    continue
                                else
                                    ssd_result[i]='{"value": '$((10#$value))', "tags":"raid_type='$raid_type',ssd_manu='$ssd_manufacture',controller='$controller',enclosure='$enclosure',slot='$slot'"}'
                                    ((i++))
                                fi
                                ;;
                            reallocated) # Deprecated
                                value=$(echo "$disk_smart" | grep "Reallocated_Sector_Ct" | awk '{print $4}')
                                if [ -z "$value" ];then
                                    continue
                                else
                                    ssd_result[i]='{"value": '$((10#$value))', "tags":"raid_type='$raid_type',ssd_manu='$ssd_manufacture',controller='$controller',enclosure='$enclosure',slot='$slot'"}'
                                    ((i++))
                                fi
                                ;;
                            reserved_block)
                                value=$(echo "$disk_smart" | grep -E "170 Unknown_|170 Reserved" | awk '{print $4}')
                                if [ -z "$value" ];then
                                    continue
                                else
                                    ssd_result[i]='{"value": '$((10#$value))', "tags":"raid_type='$raid_type',ssd_manu='$ssd_manufacture',controller='$controller',enclosure='$enclosure',slot='$slot'"}'
                                    ((i++))
                                fi
                                ;;
                            erase) # Deprecated
                                value=$(echo "$disk_smart" | grep "Erase_Fail_Count" | awk '{print $4}')
                                if [ -z "$value" ];then
                                    continue
                                else
                                    ssd_result[i]='{"value": '$((10#$value))', "tags":"raid_type='$raid_type',ssd_manu='$ssd_manufacture',controller='$controller',enclosure='$enclosure',slot='$slot'"}'
                                    ((i++))
                                fi
                                ;;
                            lifetime)
                                value=$(echo "$disk_smart" | grep -E "202 Unknown_|202 Percent_Lifetime" | awk '{print $4}')
                                if [ -z "$value" ];then
                                    continue
                                else
                                    ssd_result[i]='{"value": '$((10#$value))', "tags":"raid_type='$raid_type',ssd_manu='$ssd_manufacture',controller='$controller',enclosure='$enclosure',slot='$slot'"}'
                                    ((i++))
                                fi
                                ;;
                            uncorrect)
                                value=$(echo "$disk_smart" | grep "187 Reported" | awk '{print $NF}')
                                if [ -z "$value" ];then
                                    continue
                                else
                                    ssd_result[i]='{"value": '$((10#$value))', "tags":"raid_type='$raid_type',ssd_manu='$ssd_manufacture',controller='$controller',enclosure='$enclosure',slot='$slot'"}'
                                    ((i++))
                                fi
                                ;;
                            *)
                                continue
                                ;;
                        esac
                        ;;
                    sandisk)
                        case $flags in
                            reallocated)
                                value=$(echo "$disk_smart" | grep "5 Reallocated_Sector_Ct" | awk '{print $4}')
                                if [ -z "$value" ];then
                                    continue
                                else
                                    ssd_result[i]='{"value": '$((10#$value))', "tags":"raid_type='$raid_type',ssd_manu='$ssd_manufacture',controller='$controller',enclosure='$enclosure',slot='$slot'"}'
                                    ((i++))
                                fi
                                ;;
                            lifetime)
                                value=$(echo "$disk_smart" | grep "13 Read" | awk '{print $NF}')
                                if [ -z "$value" ];then
                                    continue
                                else
                                    ssd_result[i]='{"value": '$((10#$value))', "tags":"raid_type='$raid_type',ssd_manu='$ssd_manufacture',controller='$controller',enclosure='$enclosure',slot='$slot'"}'
                                    ((i++))
                                fi
                                ;;
                            temperature_celsius) # Deprecated
                                value=$(echo "$disk_smart" | grep "Temperature_Celsius" | awk '{print $4}')
                                if [ -z "$value" ];then
                                    continue
                                else
                                    ssd_result[i]='{"value": '$((10#$value))', "tags":"raid_type='$raid_type',ssd_manu='$ssd_manufacture',controller='$controller',enclosure='$enclosure',slot='$slot'"}'
                                    ((i++))
                                fi
                                ;;
                            reallocated_event)
                                value=$(echo "$disk_smart" | grep "196 Reallocated" | awk '{print $4}')
                                if [ -z "$value" ];then
                                    continue
                                else
                                    ssd_result[i]='{"value": '$((10#$value))', "tags":"raid_type='$raid_type',ssd_manu='$ssd_manufacture',controller='$controller',enclosure='$enclosure',slot='$slot'"}'
                                    ((i++))
                                fi
                                ;;
                            drive)
                                value=$(echo "$disk_smart" | grep "245 Unknown" | awk '{print $4}')
                                if [ -z "$value" ];then
                                    continue
                                else
                                    ssd_result[i]='{"value": '$((10#$value))', "tags":"raid_type='$raid_type',ssd_manu='$ssd_manufacture',controller='$controller',enclosure='$enclosure',slot='$slot'"}'
                                    ((i++))
                                fi
                                ;;
                            *)
                                continue
                                ;;
                        esac
                        ;;
                    *)
                        exit
                        ;;
                esac
            fi
        done
    done
    local res
    res=$(join_string "," ssd_result[@])
    if [ ${#ssd_result[@]} -eq 0 ];then
        exit
    fi

    echo -n "[$res]"
}

function hp_sata_ssd_collect() {
    check_cmd_exist $CMD_SSACLI
    local ssd_result=()
    local flags=$1
    local i=0
    ssd_manufacture='HP'

    ctrl_slot=`$CMD_SSACLI ctrl all show|sed -n "s/.*Slot \([0-9]\).*/\1/p"`
    pds_info=$($CMD_SSACLI ctrl slot=$ctrl_slot pd all show detail)
    pds=$(grep "physicaldrive" <<< "$pds_info")

    IFS=$'\n'
    for pd in `echo "$pds"`;do
        pd=`echo "$pd"|xargs`
        pd_info=`echo "$pds_info"|awk '/'"$pd"'/,/^$/{print}'`
        echo "$pd_info"|grep 'Interface Type'|grep -q 'Solid State SATA'
        if [[ $? -ne 0 ]];then
            continue
        fi
        case $flags in
            remaining)
                value=$(echo "$pd_info"|sed -n 's/.*Usage remaining: \(.*\)%.*/\1/p')
                if [ -z "$value" ];then
                    echo "get remaining failed"
                    exit 1
                else
                    ssd_result[i]='{"value": '$value', "tags":"raid_type='$raid_type',ssd_manu='$ssd_manufacture',controller='$ctrl_slot',slot='$pd'"}'
                    ((i++))
                fi
                ;;
            *)
                continue
                ;;
        esac
    done
    IFS=' '

    local res
    res=$(join_string "," ssd_result[@])
    if [ ${#ssd_result[@]} -eq 0 ];then
        exit
    fi

    echo -n "[$res]"
}

function pmchba_sata_smart_collect() {
    check_cmd_exist $CMD_SMARTCTL

    local controllers
    controllers=$(get_controller)
    local ssd_result=()
    local flags=$1
    local i=0

    for controller in "${controllers[@]}"; do
        local r
        r=$($CMD_ARCCONF getconfig "$controller" PD NODISKINFO| sed -n -e '/Device #/,/^$/p')
        local PHYSICAL_DISK_NUM
        PHYSICAL_DISK_NUM=$(echo "$r" | grep -c "Serial ")

        for ((j=0; j<"$PHYSICAL_DISK_NUM"; j++)); do
            local disk_smart
            disk_smart=$($CMD_SMARTCTL -a -d cciss,$j /dev/sda)
            local disk_rotation_rate
            disk_rotation_rate=$(get_value "$(grep "Rotation Rate:" <<< "$disk_smart")" ":")
            if [ "$disk_rotation_rate" == "Solid State Device" ];then
                lsi_check_ssd_man "$disk_smart"
                local get_sn
                get_sn=$(echo "$disk_smart" | grep "Serial Number" | awk '{print $3}')
                local find_location
                find_location=$(echo "$r" | grep -B 13 "$get_sn" | grep "Reported Location")
                local enclosure
                enclosure=$(echo "$find_location" | awk -F, '{print $1}' | awk '{print $NF}')
                local slot
                slot=$(echo "$find_location" | awk -F, '{print $2}' | sed 's/(.*)//g' | awk '{print $2}')

                case "$ssd_manufacture" in
                    intel)
                        case $flags in
                            end_error)
                                value=$(echo "$disk_smart" | grep "184 End-to-End_Error" | awk '{print $4}')
                                if [ -z "$value" ];then
                                    continue
                                else
                                    ssd_result[i]='{"value": '$((10#$value))', "tags":"raid_type='$raid_type',ssd_manu='$ssd_manufacture',controller='$controller',enclosure='$enclosure',slot='$slot'"}'
                                    ((i++))
                                fi
                                ;;
                            uncorrect)
                                value=$(echo "$disk_smart" | grep "187 Reported_Uncorrect" | awk '{print $NF}')
                                if [ -z "$value" ];then
                                    continue
                                else
                                    ssd_result[i]='{"value": '$((10#$value))', "tags":"raid_type='$raid_type',ssd_manu='$ssd_manufacture',controller='$controller',enclosure='$enclosure',slot='$slot'"}'
                                    ((i++))
                                fi
                                ;;
                            space)
                                value=$(echo "$disk_smart" | grep "232 Available_Reservd_Space" | awk '{print $4}')
                                if [ -z "$value" ];then
                                    continue
                                else
                                    ssd_result[i]='{"value": '$((10#$value))', "tags":"raid_type='$raid_type',ssd_manu='$ssd_manufacture',controller='$controller',enclosure='$enclosure',slot='$slot'"}'
                                    ((i++))
                                fi
                                ;;
                            *)
                                continue
                                ;;
                        esac
                        ;;
                    samsung)
                        case $flags in
                            reallocated)
                                value=$(echo "$disk_smart" | grep "5 Reallocated_Sector_Ct" | awk '{print $4}')
                                if [ -z "$value" ];then
                                    continue
                                else
                                    ssd_result[i]='{"value": '$((10#$value))', "tags":"raid_type='$raid_type',ssd_manu='$ssd_manufacture',controller='$controller',enclosure='$enclosure',slot='$slot'"}'
                                    ((i++))
                                fi
                                ;;
                            count)
                                value=$(echo "$disk_smart" | grep "177 Wear" | awk '{print $4}')
                                if [ -z "$value" ];then
                                    continue
                                else
                                    ssd_result[i]='{"value": '$((10#$value))', "tags":"raid_type='$raid_type',ssd_manu='$ssd_manufacture',controller='$controller',enclosure='$enclosure',slot='$slot'"}'
                                    ((i++))
                                fi
                                ;;
                            used_rsvd)
                                value=$(echo "$disk_smart" | grep "179 Used_" | awk '{print $4}')
                                if [ -z "$value" ];then
                                    continue
                                else
                                    ssd_result[i]='{"value": '$((10#$value))', "tags":"raid_type='$raid_type',ssd_manu='$ssd_manufacture',controller='$controller',enclosure='$enclosure',slot='$slot'"}'
                                    ((i++))
                                fi
                                ;;
                            unused_rsvd)
                                value=$(echo "$disk_smart" | grep "180 Unused_" | awk '{print $4}')
                                if [ -z "$value" ];then
                                    continue
                                else
                                    ssd_result[i]='{"value": '$((10#$value))', "tags":"raid_type='$raid_type',ssd_manu='$ssd_manufacture',controller='$controller',enclosure='$enclosure',slot='$slot'"}'
                                    ((i++))
                                fi
                                ;;
                            runtime)
                                value=$(echo "$disk_smart" | grep "183 Runtime_" | awk '{print $4}')
                                if [ -z "$value" ];then
                                    continue
                                else
                                    ssd_result[i]='{"value": '$((10#$value))', "tags":"raid_type='$raid_type',ssd_manu='$ssd_manufacture',controller='$controller',enclosure='$enclosure',slot='$slot'"}'
                                    ((i++))
                                fi
                                ;;
                            end_error)
                                value=$(echo "$disk_smart" | grep "184 End-to-End_Error" | awk '{print $4}')
                                if [ -z "$value" ];then
                                    continue
                                else
                                    ssd_result[i]='{"value": '$((10#$value))', "tags":"raid_type='$raid_type',ssd_manu='$ssd_manufacture',controller='$controller',enclosure='$enclosure',slot='$slot'"}'
                                    ((i++))
                                fi
                                ;;
                            uncorrect)
                                value=$(echo "$disk_smart" | grep "187 Reported" | awk '{print $NF}')
                                if [ -z "$value" ];then
                                    continue
                                else
                                    ssd_result[i]='{"value": '$((10#$value))', "tags":"raid_type='$raid_type',ssd_manu='$ssd_manufacture',controller='$controller',enclosure='$enclosure',slot='$slot'"}'
                                    ((i++))
                                fi
                                ;;
                            *)
                                continue
                                ;;
                        esac
                        ;;
                    micron)
                        case $flags in
                            error_rate)
                                value=$(echo "$disk_smart" | grep "1 Raw_Read_Error_Rate" | awk '{print $4}')
                                if [ -z "$value" ];then
                                    continue
                                else
                                    ssd_result[i]='{"value": '$((10#$value))', "tags":"raid_type='$raid_type',ssd_manu='$ssd_manufacture',controller='$controller',enclosure='$enclosure',slot='$slot'"}'
                                    ((i++))
                                fi
                                ;;
                            reallocated) # Deprecated
                                value=$(echo "$disk_smart" | grep "Reallocated_Sector_Ct" | awk '{print $4}')
                                if [ -z "$value" ];then
                                    continue
                                else
                                    ssd_result[i]='{"value": '$((10#$value))', "tags":"raid_type='$raid_type',ssd_manu='$ssd_manufacture',controller='$controller',enclosure='$enclosure',slot='$slot'"}'
                                    ((i++))
                                fi
                                ;;
                            reserved_block)
                                value=$(echo "$disk_smart" | grep -E "170 Unknown_|170 Reserved" | awk '{print $4}')
                                if [ -z "$value" ];then
                                    continue
                                else
                                    ssd_result[i]='{"value": '$((10#$value))', "tags":"raid_type='$raid_type',ssd_manu='$ssd_manufacture',controller='$controller',enclosure='$enclosure',slot='$slot'"}'
                                    ((i++))
                                fi
                                ;;
                            erase) # Deprecated
                                value=$(echo "$disk_smart" | grep "Erase_Fail_Count" | awk '{print $4}')
                                if [ -z "$value" ];then
                                    continue
                                else
                                    ssd_result[i]='{"value": '$((10#$value))', "tags":"raid_type='$raid_type',ssd_manu='$ssd_manufacture',controller='$controller',enclosure='$enclosure',slot='$slot'"}'
                                    ((i++))
                                fi
                                ;;
                            lifetime)
                                value=$(echo "$disk_smart" | grep -E "202 Unknown_|202 Percent_Lifetime" | awk '{print $4}')
                                if [ -z "$value" ];then
                                    continue
                                else
                                    ssd_result[i]='{"value": '$((10#$value))', "tags":"raid_type='$raid_type',ssd_manu='$ssd_manufacture',controller='$controller',enclosure='$enclosure',slot='$slot'"}'
                                    ((i++))
                                fi
                                ;;
                            uncorrect)
                                value=$(echo "$disk_smart" | grep "187 Reported" | awk '{print $NF}')
                                if [ -z "$value" ];then
                                    continue
                                else
                                    ssd_result[i]='{"value": '$((10#$value))', "tags":"raid_type='$raid_type',ssd_manu='$ssd_manufacture',controller='$controller',enclosure='$enclosure',slot='$slot'"}'
                                    ((i++))
                                fi
                                ;;
                            *)
                                continue
                                ;;
                        esac
                        ;;
                    sandisk)
                        case $flags in
                            reallocated)
                                value=$(echo "$disk_smart" | grep "5 Reallocated_Sector_Ct" | awk '{print $4}')
                                if [ -z "$value" ];then
                                    continue
                                else
                                    ssd_result[i]='{"value": '$((10#$value))', "tags":"raid_type='$raid_type',ssd_manu='$ssd_manufacture',controller='$controller',enclosure='$enclosure',slot='$slot'"}'
                                    ((i++))
                                fi
                                ;;
                            lifetime)
                                value=$(echo "$disk_smart" | grep "13 Read" | awk '{print $NF}')
                                if [ -z "$value" ];then
                                    continue
                                else
                                    ssd_result[i]='{"value": '$((10#$value))', "tags":"raid_type='$raid_type',ssd_manu='$ssd_manufacture',controller='$controller',enclosure='$enclosure',slot='$slot'"}'
                                    ((i++))
                                fi
                                ;;
                            temperature_celsius) # Deprecated
                                value=$(echo "$disk_smart" | grep "Temperature_Celsius" | awk '{print $4}')
                                if [ -z "$value" ];then
                                    continue
                                else
                                    ssd_result[i]='{"value": '$((10#$value))', "tags":"raid_type='$raid_type',ssd_manu='$ssd_manufacture',controller='$controller',enclosure='$enclosure',slot='$slot'"}'
                                    ((i++))
                                fi
                                ;;
                            reallocated_event)
                                value=$(echo "$disk_smart" | grep "196 Reallocated" | awk '{print $4}')
                                if [ -z "$value" ];then
                                    continue
                                else
                                    ssd_result[i]='{"value": '$((10#$value))', "tags":"raid_type='$raid_type',ssd_manu='$ssd_manufacture',controller='$controller',enclosure='$enclosure',slot='$slot'"}'
                                    ((i++))
                                fi
                                ;;
                            drive)
                                value=$(echo "$disk_smart" | grep "245 Unknown" | awk '{print $4}')
                                if [ -z "$value" ];then
                                    continue
                                else
                                    ssd_result[i]='{"value": '$((10#$value))', "tags":"raid_type='$raid_type',ssd_manu='$ssd_manufacture',controller='$controller',enclosure='$enclosure',slot='$slot'"}'
                                    ((i++))
                                fi
                                ;;
                            *)
                                continue
                                ;;
                        esac
                        ;;
                    *)
                        exit
                        ;;
                esac
            fi
        done
    done
    local res
    res=$(join_string "," ssd_result[@])
    if [ ${#ssd_result[@]} -eq 0 ];then
        exit
    fi

    echo -n "[$res]"
}

function lsi_check_ssd_man() {
    local flags=$1
    local device_model
    device_model=$(get_value "$(grep "Device Model:" <<< "$flags")" ":")
    case $device_model in
        *"SSD"*)
            ssd_manufacture="intel"
            ;;
        "SAMSUNG"*)
            ssd_manufacture="samsung"
            ;;
        "Micron"*)
            ssd_manufacture="micron"
            ;;
        "MTFDD"*)
            ssd_manufacture="micron"
            ;;
        "SDLF"*)
            ssd_manufacture="sandisk"
            ;;
        *)
            exit
            ;;
    esac
}

function get_slots() {
    local result
    result=$($CMD_MEGACLI -AdpAllInfo -aAll)
    local slots
    slots=$(get_value "$(grep "^Adapter" <<< "$result")" "#")
    echo -n "$slots"
}

function get_controller() {
    check_cmd_exist $CMD_ARCCONF
    controller_out=$($CMD_ARCCONF list NODISKINFO | grep -E "Controller [0-9]+" | awk -F: '{print $1}' | awk '{print $2}')
    echo "$controller_out"
}

collect_sata_ssd_info "$@"
