LMZ
LMZ

Reputation: 25

hdparm checking 3 to 16 drives

I am using command to bring back the Read MB/s.

hdparm -t /dev/sda | awk '/seconds/{print $11}'

From what I was reading it was a good idea to test three times. Add those values up and then divide by 3 for your average.

Sometimes I will have 3 to 16 drives, so I would like to create a question that ask how many drives I have installed. Then perform the hdparm on each drive... Was wondering if there was a simple way to change the SDA all the way up to SDB, SDC, SDD, etc. without typing that command so many times...

Thank you

Upvotes: 1

Views: 501

Answers (2)

marchelly
marchelly

Reputation: 15

Just use without any loops:

#hdparm -i /dev/sd{a..d}

Upvotes: 0

Michael Kropat
Michael Kropat

Reputation: 15227

Bash makes it easy to enumerate all the drives:

$ echo /dev/sd{a..h}
/dev/sda /dev/sdb /dev/sdc /dev/sdd /dev/sde /dev/sdf /dev/sdg /dev/sdh

Then you said you wanted to average the timing output, so let's define a function to do that:

perform_timing() {
    for i in {1..3}; do hdparm -t "$1"; done |
        awk '/seconds/ { total += $11; count++ } END { print (total / count) }'
}

Then you can run it on all the drives:

for drive in /dev/sd{a..h}; do
    printf '%s: %s\n' "$drive" "$(perform_timing "$drive")"
done

Beaking it Down

The perform_timing function does two things: 1) runs hdparm three times, then 2) averages the output. You can see how the first part works by running it manually:

# for i in {1..3}; do hdparm -t "/dev/sdc"; done

/dev/sdc:
 Timing buffered disk reads: 1536 MB in  3.00 seconds = 511.55 MB/sec

/dev/sdc:
 Timing buffered disk reads: 1536 MB in  3.00 seconds = 511.97 MB/sec

/dev/sdc:
 Timing buffered disk reads: 1538 MB in  3.00 seconds = 512.24 MB/sec

The second part combines your awk code with logic to average all the lines, instead of printing them individually. You can see how the averaging works with a simple awk example:

$ printf '1\n4\n5\n'
1
4
5
$ printf '1\n4\n5\n' | awk '{ total += $1; count++ } END { print (total / count) }'
3.33333

We wrap all that logic in a function called perform_timing as a good programming practice. That lets us call it as if it were any other command:

# perform_timing /dev/sdc
512.303

Finally, instead of writing:

perform_timing /dev/sda
perform_timing /dev/sdb
...

We wrap it all in a loop, which this simplified loop should help explain:

# for drive in /dev/sd{a..c}; do printf '%s\n' "$drive"; done
/dev/sda
/dev/sdb
/dev/sdc

Upvotes: 1

Related Questions