From fc3fc12e3b8f4c335b721b8444aedfe29b099a88 Mon Sep 17 00:00:00 2001 From: root Date: Mon, 4 Dec 2023 12:38:46 +0800 Subject: [PATCH 1/2] state change log dev name uniq --- hd_state_change_alert_all | 15 ++++++++++++ hdspindown | 49 +++++++++++++++++++++++---------------- hdstatealert.sh | 19 +++++++++++++++ spindownall | 10 +++++--- 4 files changed, 70 insertions(+), 23 deletions(-) create mode 100755 hd_state_change_alert_all create mode 100755 hdstatealert.sh diff --git a/hd_state_change_alert_all b/hd_state_change_alert_all new file mode 100755 index 0000000..60c88da --- /dev/null +++ b/hd_state_change_alert_all @@ -0,0 +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}'` +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 diff --git a/hdspindown b/hdspindown index f344390..0bfa644 100755 --- a/hdspindown +++ b/hdspindown @@ -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 @@ -22,37 +22,46 @@ touch $LOCKFILE DATE=`date` +echo "${STATS}----$DATE" >> 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 + 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 'active'|awk '{print $4}'` + if [ "x$STAT_CMD" = "xactive/idle" ];then + echo "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 diff --git a/hdstatealert.sh b/hdstatealert.sh new file mode 100755 index 0000000..5ecaa12 --- /dev/null +++ b/hdstatealert.sh @@ -0,0 +1,19 @@ +#!/bin/sh + +disk=$1 + +STATS=`/sbin/hdparm -C /dev/${disk} | grep 'active'|awk '{print $4}'` +STATCMDFILE=/tmp/spinDown.CMD +LOGFILE=/root/hdspindown/spinDown +DATE=`date` +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 diff --git a/spindownall b/spindownall index 98504d4..e4c1652 100755 --- a/spindownall +++ b/spindownall @@ -4,8 +4,12 @@ disks=`ls -l /dev/disk/by-id/|grep -v 'part'|grep -v 'SSD'|grep -v 'nvme'|grep - 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 From 1dacee4690d2e25bda6b72219fd9e1f306593a9a Mon Sep 17 00:00:00 2001 From: root Date: Wed, 13 Dec 2023 21:44:05 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E5=B0=8F=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- hd_state_change_alert_all | 2 +- hdspindown | 10 +++++----- hdstatealert.sh | 4 ++-- spindownall | 2 +- 4 files changed, 9 insertions(+), 9 deletions(-) diff --git a/hd_state_change_alert_all b/hd_state_change_alert_all index 60c88da..7a6a423 100755 --- a/hd_state_change_alert_all +++ b/hd_state_change_alert_all @@ -1,6 +1,6 @@ #!/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 diff --git a/hdspindown b/hdspindown index 0bfa644..3c18242 100755 --- a/hdspindown +++ b/hdspindown @@ -20,9 +20,9 @@ if [ ! -f $LOGFILE ];then fi touch $LOCKFILE -DATE=`date` +DATE=`date +"%m-%d %H:%M"` -echo "${STATS}----$DATE" >> STAT.${disk}.log +#echo "${STATS}----$DATE" >> /root/hdspindown/STAT.${disk}.log if [ -f ${STATFILE}.${disk}.stat ]; then PREVSTATS=`cat ${STATFILE}.${disk}.stat` @@ -33,14 +33,14 @@ if [ -f ${STATFILE}.${disk}.stat ]; then #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 + /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 'active'|awk '{print $4}'` + STAT_CMD=`/sbin/hdparm -C /dev/${disk} | grep 'state'|awk '{print $4}'` if [ "x$STAT_CMD" = "xactive/idle" ];then - echo "echo $DATE ${disk} stat cmd error" >> ${LOGFILE} + echo "$DATE ${disk} stat cmd error" >> ${LOGFILE} rm -f ${DOWNFILE}.${disk}.down fi diff --git a/hdstatealert.sh b/hdstatealert.sh index 5ecaa12..0518de2 100755 --- a/hdstatealert.sh +++ b/hdstatealert.sh @@ -2,10 +2,10 @@ disk=$1 -STATS=`/sbin/hdparm -C /dev/${disk} | grep 'active'|awk '{print $4}'` +STATS=`/sbin/hdparm -C /dev/${disk} | grep 'state'|awk '{print $4}'` STATCMDFILE=/tmp/spinDown.CMD LOGFILE=/root/hdspindown/spinDown -DATE=`date` +DATE=`date +"%m-%d %H:%M"` if [ -f ${STATCMDFILE}.${disk} ]; then PREVSTATS=`cat ${STATCMDFILE}.${disk}` if [ "$STATS" = "$PREVSTATS" ]; then diff --git a/spindownall b/spindownall index e4c1652..3f29dc4 100755 --- a/spindownall +++ b/spindownall @@ -1,6 +1,6 @@ #!/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