O'Reilly Hacks
oreilly.comO'Reilly NetworkSafari BookshelfConferences Sign In/My Account | View Cart   
Book List Learning Lab PDFs O'Reilly Gear Newsletters Press Room Jobs  


 
Buy the book!
BlackBerry Hacks
By Dave Mabe
October 2005
More Info

HACK
#77
Track Message Delivery Time
Use a built-in feature of the BlackBerry to monitor message delivery times
[Discuss (0) | Link to this hack]

How can you determine whether your BlackBerry platform is working and delivery times are "normal"? This is not an easy question to answer. You could send yourself a test message and manually watch your device for it to arrive. However, a successful test may indicate that the BlackBerry Enterprise Server that your device is homed on is functioning properly, but there may be other BES servers in your architecture that aren't functioning optimally.

Also, if there is a delay in receiving your test message, is the delay a normal duration or is it a significant delay that requires further troubleshooting?

You can use the built-in message delivery confirmation feature to build a simple delivery time–monitoring tool to create a benchmark for message delivery times in your BlackBerry platform.

Calculate the Delivery Time

Once the message is sent to the BlackBerry device, you'll receive the delivery confirmation email at the address specified in $from_address. The subject of the message is always "BlackBerry Delivery Confirmation," and the subject of your original sent message is in the message body. This allows you to check the confirmation message and compute the round-trip delivery time. Just take the received time of the confirmation, convert it to epoch time, and subtract the value of the epoch time that the original message was sent (I put it in the subject line as $now).

Here is the code I use to check the mailbox, perform the calculations on the delivery probe messages I sent using the first part of the script, and then delete the message. You'll need to change the values of $pop_server, $username, and $password to make them specific to your environment. Although it is possible to check the mailbox using a variety of protocols, I chose POP3 because of its broad support and ease of use.

	#! perl -w
	use strict;
	use Time::Local;
	use Mail::POP3Client;

	my $pop3_server = "mail.server.com";
	my $username = "username";
	my $password = "password";

	my $pop = Mail::POP3Client->new(
		USER        => $username,
		PASSWORD    => $password,
		HOST        => $pop3_server,
	);

	for (my $i = 1; $i <= $pop->Count(); $i++) {
	   my $header = $pop->Head($i);
	   if ($header =~ /Subject: BlackBerry Delivery Confirmation/) {
		  my $body = $pop->Body($i);
		  my $received_time = "";
		  ($received_time) = ($header =~ /Received: from .*?; (.+?)\n/s);
		  my $probe_sent = "";
		  ($probe_sent) = ($body =~ /BB Delivery Time: (\d+)/);
		  my $received_epoch = get_epoch($received_time);
		  my $duration = $received_epoch - $probe_sent;
		  print "Delivery took $duration seconds for" .
		    "probe sent at $probe_sent.\n";
		  $pop->Delete($i);
	   }
	}
	$pop->Close;

	sub get_time_stamp {
		my ($epoch) = @_;

		my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) =
			localtime($epoch);
		my $yyyymmdd = sprintf("%04d%02d%02d",$year+1900,$mon+1,$mday);
		my $time_stamp = sprintf ("%04d-%02d-%02d %02d:%02d:%02d",
			$year+1900,$mon+1,$mday,$hour,$min,$sec);
	}
	sub get_epoch {
		my ($smtp_timestamp) = @_;
		my @parts = split / /, $smtp_timestamp;

		my $mday = $parts[1];
		my $mon = $parts[2];
		my $year = $parts[3];
		my ($hour,$minute,$second) = split /:/, $parts[4];
		my $tz = $parts[5];

		my %month = (
		   Jan => 1,
		   Feb => 2,
		   Mar => 3,
		   Apr => 4,
		   May => 5,
		   Jun => 6,
		   Jul => 7,
		   Aug => 8,
		   Sep => 9,
		   Oct => 10,
		   Nov => 11,
		   Dec => 12,
		);

		my $epoch_wo_tz = timegm($second,$minute,$hour,$mday,
		   $month{$mon}-1,$year);
		$tz = $tz / 100;
		my $epoch = $epoch_wo_tz - ($tz * 60 * 60);
	}

TIP

Because the server where your mail resides stamps the received time on the message, you won't have to worry about when you run the delivery time calculation portion of the hack. All the data points you need to calculate the delivery time are in the message as it is received. You could even send out several test messages during the day, and then calculate them only once a day.


O'Reilly Home | Privacy Policy

© 2007 O'Reilly Media, Inc.
Website: | Customer Service: | Book issues:

All trademarks and registered trademarks appearing on oreilly.com are the property of their respective owners.