So I have a script that monitors my drives (/dev/sda and /dev/sdb) using smartctl (smartmontools). I'm by no means an expert in scripting, so this was my attempt at creating a way to email me if one of the values in smartctl output goes above a set threshold.
My question is, I'm trying to edit the "Subject" line of the email that it sends, so that I can tell which 'test' is failing. Is it temperature_celsius, etc? Is there a way to name each one and then use a variable like $failedtest in the subject line?
I parse those datalines and comparing values.
I also create variable which name is column ATTRIBUTE_NAME. Possible to use variable and comparing those. Only example if needed.
Code:
# ... set variables and run commands, look original solution
msg=/var/tmp/$$.msg.txt
> $msg
err=""
for f in $SMARTMESSAGE1 $SMARTMESSAGE2
do
data=0
cat $f | while read line
do
# only datalines include values ...
case "$line" in
ID*ATTRIBUTE_NAME*) data=1 ; continue ;;
SMART*Error*Log*Ver*) data=0 ; continue ;;
"") continue ;;
esac
((data==0)) && continue
flds=($line) # parse to array
f2="${flds[1]}"
nflds=${#flds[*]}
((nflds=nflds-1))
lastfld="${flds[$nflds]}"
# extra lines, example to create variable and set value
f2=${f2//-/_} # cariablename can't include - => change it to the _
eval $f2=\""$lastfld"\"
# extra end
case "$f2" in
Temperature_Celsius) ((lastfld > 40 )) && echo "$f $f2 $lastfld" >> $msg && err="$err $f2";;
*) ((lastfld > 0 )) && echo "$f $f2 $lastfld" >> $msg && err="$err $f2" ;;
esac
done
done
SUBJECT="$SUBJECT $err"
lines=$(cat $msg | wc -l)
# if msg file include lines - we have something to tell
if ((lines < 1 ))
then
# no problem
echo "$(date) no problem" >> $LOG
rm -f $msg 2>/dev/null
exit 0
fi
# Houston - we have problem
# send mail
echo "Some problem, sending mail " >&2
date >> $msg
# sendmail here
# rm tmp file
rm -f $msg 2>/dev/null
the code given as solution is nothing but reading the output file from /tmp it is indisual script just using variable of origional script you can put in end of your origional script.
---------- Post updated at 01:07 PM ---------- Previous update was at 01:00 PM ----------
the code given as solution is nothing but reading the output file from /tmp it is indisual script just using variable of origional script you can put in end of your origional script.
---------- Post updated at 01:07 PM ---------- Previous update was at 01:07 PM ----------
the code given as solution is nothing but reading the output file from /tmp it is indisual script just using variable of origional script you can put in end of your origional script.
Also all those awks are expensive and your script will take a long time to run. You might want to consider using a case statement instead. Further, you could use a list of devices so the script becomes generic and it becomes easier to log to what device an error belongs and what the error message was. For example (not tested):
Code:
PROBLEM=false
DEVS="/dev/sda /dev/sdb"
for dev in $DEVS; do
smartctl -d marvell -a $dev | tee -a "$MAILMESSAGE" |
while read line; do
case $line in
*Calibration_Retry_Count*|\
*Current_Pending_Sector* |\
*Multi_Zone_Error_Rate* |\
*Offline_Uncorrectable* |\
*Raw_Read_Error_Rate* |\
*Reallocated_Event_Count*|\
*Reallocated_Sector_Ct* |\
*Seek_Error_Rate* |\
*Spin_Retry_Count* |\
*UDMA_CRC_Error_Count*)
if [ "${line##* }" -gt 0 ]; then # {line##* } is the last word on the line
printf "%s: %s\n" "$dev" "$line"
PROBLEM=true
fi ;;
*Temperature_Celsius*)
if [ "${line##* }" -gt 40 ]; then
printf "%s: %s\n" "$dev" "$line"
PROBLEM=true
fi ;;
esac
done
logmessage "$dev scanned."
done >> "$MAILMESSAGE"
The variable PROBLEM indicates if a problem occurred. You can test for it like this:
Code:
if $PROBLEM; then ...
Last edited by Scrutinizer; 01-22-2011 at 06:16 AM..
I am installing Oracle DBD to PERL 5.16.3 and during make test , I am running into this error :rm -f blib/arch/auto/DBD/Oracle/Oracle.so
LD_RUN_PATH="/opt/oracle/product/11.2.0/racdb11204/lib" gcc -m32 -shared -O2 -L/usr/local/lib -fstack-protector Oracle.o dbdimp.o oci8.o -o... (3 Replies)
Testing some old script developed by different user.
#!/usr/bin/sh
case "$0" in
*/*) cmd="$0";;
*) cmd=`which "$0"`;;
esac
dir=`dirname "$cmd"`
node="$dir/."
echo $node
below two simple tests are failing, I am not seeing any Control+M characters in the script file and I am not able... (4 Replies)
I have a following script to evaluate if file exist in the directory and then archive it.
#!/bin/bash
#master directory
scriptdir="/flex/sh/interfaces" #change this path only - all other paths are connected with it
filedir="/flex/interfaces" #change this path only - all other paths are... (3 Replies)
Hi,
I have a Python unit test cases source code file which contains more than a hundred test case methods. In that, some of the test case methods already have prefix 'test' where as some of them do not have. Now, I need to add the string 'test' (case-sensitive) as a prefix to those of the... (5 Replies)
This is the code:
while test 1 -eq 1
do
read a
$a
if test $a = stop
then
break
fi
done
I read a command on every loop an execute it.
I check if the string equals the word stop to end the loop,but it say that I gave too many arguments to test.
For example echo hello.
Now the... (1 Reply)