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
#78
Create Alerts for Important Users
Start working on your CEO's BlackBerry problem before she realizes there is one
The Code
[Discuss (0) | Link to this hack]

The Code

	use Text::CSV;
	use Mail::Sendmail;
	use strict;

	my %USERS_TO_CHECK = (
	   'email@domain.com' => -1, # pending count threshold for alerting
	);

	my $ALERTS_GO_TO = 'admin@domain.com';
	my $ALERTS_FROM = 'admin@domain.com';
	my $ALERTS_SUBJECT = "User Pending Count Threshold Exceeded";
	my $SMTP_SERVER = "smtp.server.com";

	my $BES_USER_ADMIN_CLIENT =
	  "besuseradminclient.exe"; # change to your specific path
	my %BES_SERVERS = (
		BES => "server.domain.com", # maps your BES name to the computer name
	);
	my $PASSWORD = "password"; # change to the password for BESUserAdmin
	my $TEMP_DIR = "c:\\temp";

	my $csv = Text::CSV->new;
	foreach my $bes (sort keys %BES_SERVERS) {
		my $output_file = "$TEMP_DIR\\bes.$bes.users.csv";
		my $error_file = "$TEMP_DIR\\error.$bes.users.csv";
		my $system_command = "\"$BES_USER_ADMIN_CLIENT\" -n " .
		  "$BES_SERVERS{$bes} -b $bes -p $PASSWORD -stats -users " .
		  "> $output_file 2> $error_file";
		my $return_code = system $system_command;
		open OUTPUT, $output_file || die "Can't open output: $!\n";

		my $count = 0;
		while (<OUTPUT>) {
			chomp;
			$csv->parse($_);
			my @fields = $csv->fields;
			my $email = $fields[17];
			$email =~ s/^smtp://i;
			my $pending_count = $fields[9];
			if ($USERS_TO_CHECK{$email}) {
				# "important" user to check
				my $pending_threshold = $USERS_TO_CHECK{$email};
				if ($pending_count > $pending_threshold) {
				print "Sending alert for $email\n";
				my %message_options = (
				To => $ALERTS_GO_TO,
				From => $ALERTS_FROM,
				Subject => $ALERTS_SUBJECT,
				Body => "Alert threshold exceeded for $email " .
					   "($pending_count)\n",
				smtp => $SMTP_SERVER,
				);
				sendmail(%message_options);
				} else {
				print "Pending count for $email was only $pending_count.\n";
				}
			}
		}
		close OUTPUT;
	}

This Perl code uses the Mail::Sendmail and Text::CSV Perl modules, which don't come with the default distribution from ActiveState (which you'll need to have installed from http://www.activestate.com/store/languages/register.plex?id=ActivePerl). You'll need to install it by typing ppm install Mail:: Sendmail and ppm install Text::CSV from a command prompt. Type the following code into Notepad (or your favorite text editor) and save it as user_alert.pl. Of course, you will need to modify the variables at the top of the file to customize it for your environment.


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.