ubuntuusers.de

blkdevMonitor.sh

Datum:
8. Oktober 2013 23:30
Code:
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#!/bin/sh
#================================================================
# Copyright (C) 2008 QNAP Systems, Inc.
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#----------------------------------------------------------------
#
# blkdevMonitor_v2.sh
#
#	Abstract: 
#		A program of testing purpose on monitor block device
#
#	HISTORY:
#		2013/05/15	-	Created	- Kent
# 
#================================================================
/sbin/daemon_mgr klogd.sh stop "/etc/init.d/klogd.sh start"
/usr/bin/killall dd 2>/dev/null 1>/dev/null

MD_DEVIVES="md9 md13 md0 md1 md2"
SD_DEVIVES="sda sdb sdc sdd sde sdf sdg sdh"
Do_Log=0
MAXRUN=100
FORCE_STANDBY=0
BLKDEV_LOG=/root/blkdevMonitor_v2.log

analyse_kmsg()
{
	_klog=/.klog
	/bin/touch $_klog
	_standby=1

	# read /proc/kmsg
	while [ 1 ]; do
		/bin/dd if=/proc/kmsg of=$_klog bs=1 count=10240 2>/dev/null 1>/dev/null
		for i in $MD_DEVIVES; do
			/bin/cat $_klog | /bin/grep $i | /bin/grep "dirtied inode"
			if [ $? = 0 ]; then
				/bin/cat $_klog | /bin/grep $i | /bin/grep "dirtied inode" >> $BLKDEV_LOG
				_standby=0
			fi
			/bin/cat $_klog | /bin/grep $i | /bin/grep "block"
			if [ $? = 0 ]; then
				/bin/cat $_klog | /bin/grep $i | /bin/grep "block" >> $BLKDEV_LOG
				_standby=0
			fi
		done
		for i in $SD_DEVIVES; do
			/bin/cat $_klog | /bin/grep $i | /bin/grep "dirtied inode"
			if [ $? = 0 ]; then
				/bin/cat $_klog | /bin/grep $i | /bin/grep "dirtied inode" >> $BLKDEV_LOG
				_standby=0
			fi
			/bin/cat $_klog | /bin/grep $i | /bin/grep "block"
			if [ $? = 0 ]; then
				/bin/cat $_klog | /bin/grep $i | /bin/grep "block" >> $BLKDEV_LOG
				_standby=0
			fi
		done
		if [ $_standby = 0 ]; then
			return 1
		fi
	done
}

_countdown()
{
	/bin/sync;/bin/sync
	/bin/echo -n "Countdown: "
	cntdown=$1
	while [ ${cntdown} -gt 0 ]; do
		/bin/sleep 1
		/bin/echo -n "${cntdown} "
		cntdown=$[$cntdown-1]
	done
	echo
}

_check_standby()
{
	for i in $SD_DEVIVES; do
		/sbin/hdparm -C /dev/${i} 2>>/dev/null | /bin/grep active
		if [ $? -eq 0 ]; then
			echo /dev/${i}
			echo "Some process was waked up HDD..."
			return 1	# HDD is active
		fi
	done
	return 0
}

/bin/echo "===== Welcome to use blkdevMonitor_v2 on `/bin/date` ====="
case "$1" in
	-h)
	/bin/echo "Usage:"
	/bin/echo "  $0 [N]"
	/bin/echo "  N means that monitor block devices N times and generate N log files.(Default N = 100)"
	exit 0
	;;
esac

/bin/echo "Turn off/on VM block_dump & Clean dmesg"
/bin/echo 0 > /proc/sys/vm/block_dump
/bin/dmesg -c 2>/dev/null 1>/dev/null
/bin/dmesg -c 2>/dev/null 1>/dev/null
/bin/dmesg -c 2>/dev/null 1>/dev/null

_cnt=20
while [ ${_cnt} -gt 0 ]; do
	/bin/sync
	/bin/dmesg -c 2>/dev/null 1>/dev/null
	_cnt=$[$_cnt-1]
done
/bin/echo 1 > /proc/sys/vm/block_dump
/bin/date > /dev/null
_countdown 3
# ignore 10240 bytes
/bin/dd if=/proc/kmsg of=/dev/null bs=1 count=10240 2>/dev/null 1>/dev/null

/bin/echo "Start..."

[ "x$1" != "x" ] && MAXRUN=$1
_cnt=0

# remove log
/bin/rm -f $BLKDEV_LOG

while [ ${_cnt} -lt ${MAXRUN} ]; do
	echo "============= $_cnt/$MAXRUN test, `/bin/date` ==============="
	echo "============= $_cnt/$MAXRUN test, `/bin/date` ===============" >> $BLKDEV_LOG
	if [ $FORCE_STANDBY = 1 ]; then
		_check_standby
		if [ $? != 0 ]; then
			for i in $SD_DEVIVES; do
				# force standby
				/sbin/hdparm -y /dev/${i} 2>/dev/null 1>/dev/null
				if [ $? = 0 ]; then
					echo "Issuing standby command in /dev/${i}"
				else
					echo "/dev/${i} not found"
				fi
			done
		fi
		/bin/sleep 20
	fi
	analyse_kmsg
	_cnt=$[$_cnt+1]
	echo
	echo >> $BLKDEV_LOG
done

/bin/echo "Turn off block_dump"
/bin/echo 0 > /proc/sys/vm/block_dump