#!/bin/bash

#If any drive returns yes for a S.M.A.R.T alert email me

megacli -PDList -aALL | grep -i 's.m.a.r.t' | grep -i 'yes' &&\
\
echo -e "To: tobias@tobiastime.xyz\nFrom: snorlax@tobiastime.xyz\nSubject: RAID Drives\nA RAID Drive on Snorlax has had a flagged S.M.A.R.T alert at $(date)" | ssmtp tobias@tobiastime.xyz 

#If raid health is anything but optimal email me

raidhealth=$(megacli -LDInfo -Lall -aALL | awk 'NR==11 {print $3}')

[ "$raidhealth" != "Optimal" ] && \
echo -e "To: tobias@tobiastime.xyz\nFrom: snorlax@tobiastime.xyz\nSubject: RAID Array\nMegaCLI has reported the RAID array is not currently operating in an optimal state at $(date)" | ssmtp tobias@tobiastime.xyz 

#If the battery health is anything but optimal email me

batteryhealth=$(megacli -AdpBbuCmd -aALL | awk 'NR==8 {print $3}')

[ "$batteryhealth" != "Optimal" ] && \
echo -e "To: tobias@tobiastime.xyz\nFrom: snorlax@tobiastime.xyz\nSubject: RAID Array\nMegaCLI has reported the RAID card's battery is no longer in an optimal state of health at $(date)" | ssmtp tobias@tobiastime.xyz

#Write current RAID controller temp to log file

logfile=/var/log/raidtemp.log

raidtemp=$(megacli -AdpAllInfo -aAll | grep -i 'roc temperature' | awk '{print $4}')

echo "The RAID controller's temperature is "$raidtemp" degrees celsius at $(date)" >> "$logfile"

#If the RAID card's temp is above 80 degrees email me

[ "$raidtemp" -gt 80 ] && echo -e "To: tobias@tobiastime.xyz\nFrom: snorlax@tobiastime.xyz\nSubject: RAID Array\nMegaCLI has reported the RAID card's temperature is above 80 degrees celsius at $(date)" | ssmtp tobias@tobiastime.xyz 