#!/bin/bash

ATA_DEVICES='/dev/[sh]d[a-z] /dev/[sh]d[a-z][a-z]'
NVME_DEVICES='/dev/nvme[0-9]'
ATA_DEVICES_MEGACLI=''
PARALLEL_LIMIT=16

if which megaclisas-status > /dev/null 2>&1 ; then
    ATA_DEVICES_MEGACLI=$(megaclisas-status 2>/dev/null | 
        awk 'BEGIN {section=""; split("", contr); split("", dev)} 
                   {
                        if ($0 ~ /-- .* --/) {match($0, /-- (.*) --/, a); section=a[1];}
                        if (section == "Controller information" && length($0) > 0 && $0 !~ /--.*/) contr[length(contr)+1]=$1;
                        if (section == "Array information" && length($0) > 0 && $0 !~ /--.*/)
                            for (key in contr) {r=contr[key] ".*"; if (dev[key] == "" && $0 ~ r) dev[key]=$16}
                   } 
             END {for (key in contr) {printf dev[key] " "}}')
fi

test -f /etc/check_mk_agent.conf && . /etc/check_mk_agent.conf

alias filter-transport-protocol='awk -v transport="ATA" '"'"'BEGIN{output=""; show=0} {output = output "\n" $0; if ($0 ~ transport) show=1;} END {if (show) print output}'"'"
shopt -s expand_aliases

parallel_counter=0
function parallel_limit_check {
    if [ "$parallel_counter" -ge "$PARALLEL_LIMIT" ]; then
        wait
        parallel_counter=0
    fi
}

echo '<<<smart_meta>>>'
# Only handle always updated values, add device path and vendor/model

if which megaclisas-status > /dev/null 2>&1 ; then
	for D in $ATA_DEVICES_MEGACLI; do
	    N=${D##*/}
        VEND=$(tr -d ' ' < /sys/block/$N/device/vendor)
        MODEL=$(sed -e 's/ /_/g' -e 's/_*$//g' < /sys/block/$N/device/model)

        # specialni pripad megacli
        if which megacli > /dev/null 2>&1 ; then
            for i in `megacli -PDList -aALL -NoLog 2>/dev/null | grep "^Device Id" | awk '{print $NF}' | sort | uniq`; do 
                (smartctl -a -d megaraid,${i} $D | filter-transport-protocol | grep Always | sed "s|^|${D}_deviceID_$i $VEND $MODEL |") &
                parallel_counter=$((parallel_counter+1))
                parallel_limit_check
            done 2>/dev/null
        fi
        
    done 2>/dev/null

    wait
fi

if which smartctl > /dev/null 2>&1 ; then
    for D in $ATA_DEVICES; do
        N=${D##*/}
        VEND=$(tr -d ' ' < /sys/block/$N/device/vendor)
        MODEL=$(sed -e 's/ /_/g' -e 's/_*$//g' < /sys/block/$N/device/model)
        (smartctl -A $D | grep Always | sed "s|^|$D $VEND $MODEL |") &
        parallel_counter=$((parallel_counter+1))
        parallel_limit_check

        for i in $(seq 0 11) ; do
            if [ ! -e /sys/block/$N/device/raid_level ]; then
                continue
            fi
            (smartctl -a -d cciss,$i $D 2>/dev/null | filter-transport-protocol | awk -v drive=$D -v slot=$i '{
                    if ($1 == "Vendor:") vendor=$2;
                    if ($1 == "Product:") product = $2;
                    if ($1 == "Current" && $3 =="Temperature:")
                        print drive "_slot_" slot " " vendor " " product " " slot " Temperature_Celsius 0x0 0 0 0 none none - " $4
                    }') &
            parallel_counter=$((parallel_counter+1))
            parallel_limit_check
        done

    done 2>/dev/null
    
    for D in $NVME_DEVICES; do
        N=${D##*/}

        MODEL=$(smartctl --info $D | grep "Model Number:" | awk '{print $NF}')
        (smartctl -A $D | grep -E "Temperature:|Critical Warning:|Temperature Sensor|Data Units Read:|Data Units Written:" | \
			awk -v dev=$D -v model=$MODEL '{print dev " NVME " model " " $0}') &
        parallel_counter=$((parallel_counter+1))
        parallel_limit_check
        
    done 2>/dev/null

    wait

    rm -f MegaSAS.log &>/dev/null
fi
