#!/bin/bash
# check the wearout of Crucial SSDs
echo '<<<disk_wearout:sep(59)>>>'

# for every mounted SDD
for D in /dev/sd?; do

        smartres=$(smartctl -a "$D")

        # check if the "percent_lifetime_remain" option exists in the smartctl output
        line=$(grep "Percent_Lifetime_Remain" <<< "$smartres")
        if ! test -z "$line"; then

                # get wearout %
                wearout=$(grep "Percent_Lifetime_Remain" <<< "$smartres" | awk '{print $10}')

                # get device model
                model=$(grep "Device Model:" <<< "$smartres" | awk '{print $3}')

                # get serial number
                serial=$(grep "Serial Number:" <<< "$smartres" | awk '{print $3}')

                # get unused reserved block count
                unused=$(grep "Unused_Reserve_NAND_Blk" <<< "$smartres" | awk '{print $10}')

                # get RAIN successful recovery page count
                rain=$(grep "Success_RAIN_Recov_Cnt" <<< "$smartres" | awk '{print $10}')

        fi

        # send the raw data to checkmk
        echo "$D;$model;$serial;$wearout;$unused;$rain"
done
