LINUX.ORG.RU
ФорумAdmin

Watch raw read write bytes stats per block device in real time

 , , ,


0

1
#! /bin/bash

#----------------------------------------------------------------------
# Description: Watch raw read write bytes stats per block device in real time
# Author: Artem S. Tashkinov
# Created at: Sat Apr  6 18:34:33 YEKT 2013
# Computer: localhost.localdomain
# System: Linux 3.8.7-ic on i686
#
# Copyright (c) 2013 Artem S. Tashkinov  All rights reserved.
#
#----------------------------------------------------------------------

export LANG=en_US.UTF-8

dsrc=/sys/block
cd $dsrc || die "$dsrc not found"

doformat()
{
	awk "{printf \"%-7s %-18'd %-18'd\", \$1, \$4*512, \$8*512}"
}

resolve()
{
	out=`egrep "^/dev/$1 " /proc/mounts | awk '{print $2}'`
	root=`grep "root=/dev/$1" /proc/cmdline`
	test -n "$root" && out=/
	echo $out # Otherwise awk may print nothing
}

while :; do
	clear
	echo "Device  Read bytes         Written bytes     Mount point"
	for i in sd*; do
		for j in $i/$i*; do
			dev=`echo $j | awk -F "/" '{print $2}'`
			( echo -n "$dev "; cat $j/stat ) | doformat
			resolve $dev
		done
		( echo -n "$i "; cat $i/stat ) | doformat
		echo
		echo
	done
	sleep 2
done
★★★★★

Что спросить-то хотел? Или просто говнокодом хвастаешся?

anonymous ()

Косяк нашёл - если у раздела нет партиций, то будет вываливаться ошибка.

Исправлять лень, уж простите.

anonymous ()

а теперь убери это и перепиши на нормальном яп

anonymous ()

die

ORLY? И где оно определено?

export

Зачем?

Дальше не читал, но два echo подряд порадовали.

anonymous ()

So, for complete and utter idiots.

1) I won't rewrite it in any other language - it's not worth it;

2) iotop, atop show per process stats;

3) iostat has a fucked up formatting;

4) export is required for proper formatting;

So, fsck you very much and good bye.

// b.

anonymous ()
25 августа 2013 г.

Rewritten and made three time faster:

#! /bin/bash

#----------------------------------------------------------------------
# Description: watch raw input output per block device stats in real time
# Author: Artem S. Tashkinov
# Created at: Sat Apr  6 18:34:33 YEKT 2013
# Computer: localhost.localdomain
# System: Linux 3.8.7-ic on i686
#
# Copyright (c) 2013 Artem S. Tashkinov  All rights reserved.
# Sat Aug 24 19:59:32 YEKT 2013 - implemented read/write interval counters
# Sun Aug 25 23:51:14 YEKT 2013 - made refreshing three times faster
#
#----------------------------------------------------------------------

export LANG=en_US.UTF-8
rr=2

dsrc=/sys/block
cd $dsrc || die "$dsrc not found"

declare -A intr intw blkr bytesw

while :; do
	clear
	echo "Refresh rate: ${rr} seconds"
	echo "Device         Read bytes       Written bytes   Interval read Interval written Mount point"
	for i in sd*; do # adjust for your environment
		for j in $i/$i* $i; do
			dev=${j##*/}
			test -z "$dev" && dev="$j" # when iterating $i itself

			read blkr blkw < <(awk '{print $3" "$7}' $j/stat) # let's speed up this a bit
			blkr["$dev"]=$blkr
			blkw["$dev"]=$blkw

			test -z "${intr[$dev]}" && intr[$dev]=0
			test -z "${intw[$dev]}" && intw[$dev]=0
			intr[$dev]=$((${blkr[$dev]}-${intr[$dev]}))
			intw[$dev]=$((${blkw[$dev]}-${intw[$dev]}))
			test "${blkr[$dev]}" == "${intr[$dev]}" && intr[$dev]=0 # Don't show when iterating for the first time
			test "${blkw[$dev]}" == "${intw[$dev]}" && intw[$dev]=0

			printf "%-7s %17d   %17d  %14d   %14d " $dev $((blkr[$dev]*512)) $((blkw[$dev]*512)) $((intr[$dev]*512)) $((intw[$dev]*512))

			mntp=`awk '/^\/dev\/'$dev' /{print $2}' /proc/mounts`
			if [ -z "$mntp" ]; then
				grep -q "root=/dev/$dev " /proc/cmdline && mntp="/"
			fi
			echo "$mntp"

			intr["$dev"]=${blkr["$dev"]}
			intw["$dev"]=${blkw["$dev"]}
		done
		echo
	done
	sleep $rr
done
anonymous ()
Ответ на: комментарий от anonymous

Ошибки закрались, простите.

#! /bin/bash

#----------------------------------------------------------------------
# Description: watch raw input output per block device stats in real time
# Author: Artem S. Tashkinov
# Created at: Sat Apr  6 18:34:33 YEKT 2013
# Computer: localhost.localdomain
# System: Linux 3.8.7-ic on i686
#
# Copyright (c) 2013 Artem S. Tashkinov  All rights reserved.
# Sat Aug 24 19:59:32 YEKT 2013 - implemented read/write interval counters
# Sun Aug 25 23:51:14 YEKT 2013 - made refreshing three times faster
#
#----------------------------------------------------------------------

shopt -s nullglob
export LANG=en_US.UTF-8
rr=2

dsrc=/sys/block
cd $dsrc || die "$dsrc not found"

declare -A intr intw blkr blkw

while :; do
	clear
	echo "Refresh rate: ${rr} seconds"
	echo "Device         Read bytes       Written bytes   Interval read Interval written Mount point"
	for i in sd* md*; do # adjust for your environment
		for j in $i/$i* $i; do
			dev=${j##*/}
			test -z "$dev" && dev="$j" # when iterating $i itself

			read blkr blkw < <(awk '{print $3" "$7}' $j/stat) # let's speed up this a bit
			blkr["$dev"]=$blkr
			blkw["$dev"]=$blkw

			test -z "${intr[$dev]}" && intr[$dev]=0
			test -z "${intw[$dev]}" && intw[$dev]=0
			intr[$dev]=$((${blkr[$dev]}-${intr[$dev]}))
			intw[$dev]=$((${blkw[$dev]}-${intw[$dev]}))
			test "${blkr[$dev]}" == "${intr[$dev]}" && intr[$dev]=0 # Don't show when iterating for the first time
			test "${blkw[$dev]}" == "${intw[$dev]}" && intw[$dev]=0

			printf "%-7s %'17d   %'17d  %'14d   %'14d " $dev $((blkr[$dev]*512)) $((blkw[$dev]*512)) $((intr[$dev]*512)) $((intw[$dev]*512))

			mntp=`awk '/^\/dev\/'$dev' /{print $2}' /proc/mounts`
			if [ -z "$mntp" ]; then
				grep -q "root=/dev/$dev " /proc/cmdline && mntp="/"
			fi
			echo "$mntp"

			intr["$dev"]=${blkr["$dev"]}
			intw["$dev"]=${blkw["$dev"]}
		done
		echo
	done
	sleep $rr
done
anonymous ()
Вы не можете добавлять комментарии в эту тему. Тема перемещена в архив.