Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions hd_state_change_alert_all
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/bin/bash
#disks=`ls /dev|grep sd'[a-z]*$'`
disks=`ls -l /dev/disk/by-id/|grep -v 'dm'|grep -v 'part'|grep -v 'SSD'|grep -v 'nvme'|grep -v 'wwn'|awk -F '/' 'NR>1{print $NF}'`
if [ ! -f /usr/bin/hdspindown ];then
cp ./hdspindown /usr/bin/
fi
uniq_disks=`echo ${disks} xxc`
uniq_disks=`echo ${uniq_disks}|awk 'BEGIN{RS=" "; ORS=" "}{ if(a[$0] == 0){a[$0]+=1; print $0}}'`
uniq_disks=`echo ${uniq_disks}|sed 's/xxc/ /'`

for i in $uniq_disks;
do
#echo $i
~/hdspindown/hdstatealert.sh $i
done
51 changes: 30 additions & 21 deletions hdspindown
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ STATS=`grep $disk /proc/diskstats|awk 'NR==1{print $0}'`
LOCKFILE=/tmp/spinDown.lock
STATFILE=/tmp/spinDown
DOWNFILE=/tmp/spinDown
LOGFILE=/tmp/spinDown.log
LOGFILE=/root/hdspindown/spinDown.log

if [ -f $LOCKFILE ]; then
# we are already running
Expand All @@ -20,39 +20,48 @@ if [ ! -f $LOGFILE ];then
fi
touch $LOCKFILE

DATE=`date`
DATE=`date +"%m-%d %H:%M"`

#echo "${STATS}----$DATE" >> /root/hdspindown/STAT.${disk}.log

if [ -f ${STATFILE}.${disk}.stat ]; then
PREVSTATS=`cat ${STATFILE}.${disk}.stat`
if [ "$STATS" = "$PREVSTATS" ] && [ ! -f ${DOWNFILE}.${disk}.down ]; then
# Disk stats unchanged and the disk spinning. Hence, idle -> spin down.
echo "$DATE ${disk} idle, spinning down" >> /tmp/spinDown.log
#service smartd stop > /dev/null
#sdparm --command=sync /dev/${disk} &> /dev/null
#sdparm --command=stop /dev/${disk} &> /dev/null
/sbin/hdparm -y /dev/${disk} &> /dev/null
STATS=`grep $disk /proc/diskstats|awk 'NR==1{print $0}'`
echo "$STATS" > ${STATFILE}.${disk}.stat
touch ${DOWNFILE}.${disk}.down
if [ "$STATS" = "$PREVSTATS" ]; then
if [ ! -f ${DOWNFILE}.${disk}.down ]; then
# Disk stats unchanged and the disk spinning. Hence, idle -> spin down.
echo "$DATE ${disk} idle, spinning down" >> ${LOGFILE}
#service smartd stop > /dev/null
#sdparm --command=sync /dev/${disk} &> /dev/null
#sdparm --command=stop /dev/${disk} &> /dev/null
/sbin/hdparm -y /dev/${disk} >/dev/null 2>&1
STATS=`grep $disk /proc/diskstats|awk 'NR==1{print $0}'`
echo "$STATS" > ${STATFILE}.${disk}.stat
touch ${DOWNFILE}.${disk}.down
else
STAT_CMD=`/sbin/hdparm -C /dev/${disk} | grep 'state'|awk '{print $4}'`
if [ "x$STAT_CMD" = "xactive/idle" ];then
echo "$DATE ${disk} stat cmd error" >> ${LOGFILE}
rm -f ${DOWNFILE}.${disk}.down
fi

fi
else
if [ "$STATS" != "$PREVSTATS" ] && [ -f ${DOWNFILE}.${disk}.down ]; then
if [ -f ${DOWNFILE}.${disk}.down ]; then
# Disk stats are different & disk was down. Remove the down state file.
echo "$DATE stats different for ${disk}" >> /tmp/spinDown.log
echo "$DATE stats different for ${disk}" >> ${LOGFILE}
#service smartd start > /dev/null
rm -f ${DOWNFILE}.${disk}.down
fi
if [ "$STATS" != "$PREVSTATS" ]; then
echo "$STATS" > ${STATFILE}.${disk}.stat
fi
echo "$STATS" > ${STATFILE}.${disk}.stat
fi
else
# Initialise the state file
echo "$DATE initialising for ${disk}" >> /tmp/spinDown.log
echo "$DATE initialising for ${disk}" >> ${LOGFILE}
echo "$STATS" > ${STATFILE}.${disk}.stat
fi

rm -f $LOCKFILE
if [ `cat /tmp/spinDown.log|wc -l` -gt 200 ];then
mv /tmp/spinDown.log /tmp/spinDown.log.1
touch /tmp/spinDown.log
if [ `cat ${LOGFILE}|wc -l` -gt 200 ];then
mv ${LOGFILE} ${LOGFILE}.1
touch ${LOGFILE}
fi
19 changes: 19 additions & 0 deletions hdstatealert.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#!/bin/sh

disk=$1

STATS=`/sbin/hdparm -C /dev/${disk} | grep 'state'|awk '{print $4}'`
STATCMDFILE=/tmp/spinDown.CMD
LOGFILE=/root/hdspindown/spinDown
DATE=`date +"%m-%d %H:%M"`
if [ -f ${STATCMDFILE}.${disk} ]; then
PREVSTATS=`cat ${STATCMDFILE}.${disk}`
if [ "$STATS" = "$PREVSTATS" ]; then
exit 0
else
echo "$DATE state change $PREVSTATS -->$STATS">>${LOGFILE}.${disk}.alert.log
echo "$STATS" > ${STATCMDFILE}.${disk}
fi
else
echo "$STATS" > ${STATCMDFILE}.${disk}
fi
12 changes: 8 additions & 4 deletions spindownall
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
#!/bin/bash
#disks=`ls /dev|grep sd'[a-z]*$'`
disks=`ls -l /dev/disk/by-id/|grep -v 'part'|grep -v 'SSD'|grep -v 'nvme'|grep -v 'wwn'|awk -F '/' 'NR>1{print $NF}'`
disks=`ls -l /dev/disk/by-id/|grep -v 'dm'|grep -v 'part'|grep -v 'SSD'|grep -v 'nvme'|grep -v 'wwn'|awk -F '/' 'NR>1{print $NF}'`
if [ ! -f /usr/bin/hdspindown ];then
cp ./hdspindown /usr/bin/
fi
for i in $disks;
uniq_disks=`echo ${disks} xxc`
uniq_disks=`echo ${uniq_disks}|awk 'BEGIN{RS=" "; ORS=" "}{ if(a[$0] == 0){a[$0]+=1; print $0}}'`
uniq_disks=`echo ${uniq_disks}|sed 's/xxc/ /'`

for i in $uniq_disks;
do
echo $i
hdspindown $i
#echo $i
hdspindown $i
done