#!/bin/bash

SCSI_DEVICES='/dev/[sh]d[a-z] /dev/[sh]d[a-z][a-z]'
CCISS_DEVICES=$SCSI_DEVICES
SCSI_DEVICES_MEGACLI='/dev/[sh]d[a-z] /dev/[sh]d[a-z][a-z]'
PARALLEL_LIMIT=16

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

alias filter-transport-protocol='awk -v transport="SAS" '"'"'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_scsi>>>'
if which smartctl > /dev/null 2>&1 ; then
    for D in $SCSI_DEVICES; do
      (  if [ `smartctl -c $D | grep -c "."` -eq 2 ] || [ `smartctl -c $D | grep -c "SCSI device successfully opened"` -eq 1 ] ; then
            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 | sed -e "/^$/d" -e "1,2d" -e "s|^|$D $VEND $MODEL |"
        fi | dd ibs=100k obs=100k ) &
        parallel_counter=$((parallel_counter+1))
        parallel_limit_check


    done 2>/dev/null

    for D in $SCSI_DEVICES_MEGACLI; do
        if which megacli > /dev/null 2>&1 ; then
            for i in `megacli -PDList -aALL -NoLog 2>/dev/null | grep "^Device Id" | awk '{print $NF}'`; 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 megaraid,${i} $D | filter-transport-protocol | sed "s|^|${D}_deviceID_$i $VEND $MODEL |") &
                parallel_counter=$((parallel_counter+1))
                parallel_limit_check
            done 2>/dev/null
        fi

    done 2>/dev/null

    for D in $CCISS_DEVICES; do
        N=${D##*/}
        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

    # specialni pripad na kuduk[8-12] s virtualnimi disky
    # target0:0:32 je nazev radice
    if which megacli > /dev/null 2>&1 ; then
        controller=$(echo /sys/bus/pci/drivers/megaraid_sas/*/host*/target0:0:32 2>/dev/null | grep -Po 'host\K[^/\*]*')
        while read -r host_id; do
            for i in `megacli -PDList -aALL -NoLog 2>/dev/null | grep "^Device Id" | awk '{print $NF}'`; do
                (smartctl -a -d megaraid,${i} /dev/bsg/$host_id:0:32:0 2>/dev/null | awk -v hostid=$host_id -v deviceid=$i '{
                    if ($1 == "Vendor:") vendor=$2;
                    if ($1 == "Product:") product = $2;
                    if ($1 == "Current" && $3 =="Temperature:") 
                        print "host_" hostid "_deviceID_" deviceid " " vendor " " product " " deviceid " Temperature_Celsius 0x0 0 0 0 none none - " $4
                    }') &
                parallel_counter=$((parallel_counter+1))
                parallel_limit_check
            done 2>/dev/null
        done <<< "$controller"
    fi

    wait

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