2

Sometime the servers in my workplace experience some weird behavior, it happens in random physical racks around different DC's around the world. It seems like there's a networking bandwidth issues between different machine within the same rack and between one rack and anothers in the same DC. All the servers in the each rack have a mount point to an operations server in the same DC. Sometimes when the weird behavior takes place... it seems like there's not enough bandwidth between the various machines and copying files to that mount point on the operations server takes way too long.

In order to measure the problem when it happens I run the following command:

dd if=/dev/zero of=/proxy_dump/test1.dat bs=1024 count=102

The command fills a test1.dat file on the /proxy_dump mount point which is on the operations server. On a server which operates properly, the output will look like that:

[root@nyproxy5 ~]# /bin/dd if=/dev/zero of=/proxy_dump/test1.dat bs=1024 count=102 
102+0 records in
102+0 records out
104448 bytes (104 kB) copied, 0.003486 seconds, 30.0 MB/s
[root@nyproxy5 ~]#

On a problematic server during a networking issue:

[user@ams2proxy24 ~]$ dd if=/dev/zero of=/proxy_dump/test1.dat bs=1024 count=102
102+0 records in
102+0 records out
104448 bytes (104 kB) copied, 2.8736 s, 36.3 kB/s
[user@ams2proxy24 ~]$

So, in order to know it happens just when it does... I want to write a Nagios check which will run this command every 5 minutes or so and I want a part of it's output displayed.

The problem is that's i'm unable to redirect the output of the command in any way... not to a file and not into a variable in the script. I want to redirect it somehow in order to parse it and get just the information which interests me.

Does anybody know how to redirect the output of dd ?

2 Answers 2

4

You can use the redirection operator > and 2>&1 to redirect the output coming from the dd command to any files that you can write.

Example:

dd if=/dev/zero of=/proxy_dump/test1.dat bs=1024 count=102 > /proxy_dump/dd.log 2>&1

This will write the output from the dd to /proxy_dump/dd.log file.

It will create the dd.log file if it does not exist and over-write the contents if the file already exist. You can change the redirection symbol from > to >> if you don't want the previous contents of the /proxy_dump/dd.log to be over-written.

To know how this works, go through https://stackoverflow.com/questions/818255/in-the-shell-what-is-21

1

As you explicitely told "I want to write a Nagios check", you might found useful this little PERL script that I've just wrote down to fetch exactly your "dd" timing. It should be quite easy to cut/paste it, reusing some existing plugin:

#! /usr/bin/perl -w

# All code below, released under GPL 2 license

use strict;

my $res = undef;
my @lines = undef;
my %ERRORS;
my $line = undef;
my $TIMEOUT = 5;
my $time = undef;
my $verbose = 1;

# Just in case of problems, let's not hang Nagios
$SIG{'ALRM'} = sub {
        print "No Answer from dd\n";
        exit $ERRORS{"UNKNOWN"};
};
alarm($TIMEOUT);

# Execute a "dd" 
# get the results into $res
$res = qx|/bin/dd if=/dev/zero of=/tmp/test1.dat bs=1024 count=102 2>&1|;

#Turn off alarm
alarm(0);

#Split $res into an array of lines
@lines = split /\n/, $res;

my $count=0;

foreach $line (@lines) {
   print '[output line: '.$count++."] ".$line."\n" if $verbose;
   # pattern to search is:
   # 104448 bytes (104 kB) copied, 0,000541348 s, 193 MB/s
    if ($line =~ /copied,\s([\d.,]+)\ss,/) {
        $time = $1;
        $time =~ s/,/\./;
    }
}
if ($time) {
   print "dd took [".$time."] sec to complete\n";
} else {
   print "unable to fetch dd results\n";
}

When launched, it gives out this:

me@monitor:/tmp$ ./check_dd.pl 
[output line: 0] 102+0 records in
[output line: 1] 102+0 records out
[output line: 2] 104448 bytes (104 kB) copied, 0,000539951 s, 193 MB/s
dd took [0.000539951] sec to complete

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .