[rt-users] Initial version of SMS alerting

Craig Ringer craig at 2ndquadrant.com
Thu Feb 28 00:49:23 EST 2013


Hi all

Here's a simple action module to send SMS alerts to the user's
PagerPhone. As written it uses RedOxygen's SMS service, but since it's
using SMS::Send any driver for that will work, and new drivers are
pretty easy to write.

This is a bit of an early version; it doesn't take arguments to control
who gets notified yet. It should really be able to accept an argument
like "TicketAdminCcs,QueueAdminCcs" or
"TicketAdminCcs,Group:Level2Support" . Or the name of a Perl module
callback to filter the user list according to local rules like (say) who
is on shift.

It also hardcodes the SMS driver, username, password and account ID.
Pretty easy to change that so that the hash parms passed to
SMS::Send->new are obtained from RT_SiteConfig.pm, just haven't done it
yet, and I wanted to post this as a starting point for anyone needing to
do something similar. Once I've cleaned it up I'll pop it up on CPAN.


Usage via rt-crontool is a crontab entry like:

*/1 * * * * * rtuser rt-crontool --transaction last --search
RT::Search::FromSQL --search-arg "(Status='new' OR Status='open') AND
(Due > 'Jan 1, 1970') AND ((Due < '25 minutes' AND Due >= '24 minutes')
OR (Due < '12 minutes' AND Due >= '11 minutes') OR (Due < '5 minutes'
AND Due >= '3 minutes'))" --action RT::Action::SMSNotify --template 'SLA
Alert SMS'

where "rtuser" is your local rt-crontool user.


RT::Action::SMSNotify, the text of which follows, is placed in
rtlocallib under RT/Action/SMSNotify.pm :


#!/usr/bin/perl

package RT::Action::SMSNotify;
use strict;
use warnings;

use Data::Dumper;
use SMS::Send;
use SMS::Send::RedOxygen;

use base qw(RT::Action);

sub Prepare {
        my $self = shift;

        my (@memberlist);

        # get UserMembersObj of queue admincc members
        push(@memberlist, sort grep defined && length, map
$_->PagerPhone, @{
$self->TicketObj->QueueObj->AdminCc->UserMembersObj->ItemsArrayRef });

        # If the ticket has any adminCCs, add them too
        push(@memberlist, sort grep defined && length, map
$_->PagerPhone, @{
$self->TicketObj->AdminCc->UserMembersObj->ItemsArrayRef });

        RT::Logger->debug("Preparing to send SMSes to: " .
Dumper(@memberlist) );

        $self->{'PagerNumbers'} = \@memberlist;

        return scalar(@memberlist);
}

sub Commit {

        my $self = shift;

        my @memberlist = @{$self->{'PagerNumbers'}};

        my $sender = SMS::Send->new( 'RedOxygen',
                _accountid  => 'xxxxx',
                _email      => 'xxxxxx',
                _password   => 'xxxxxxx'
        );
        foreach my $ph (@memberlist) {

                # TODO: Sub in principal of current $ph
                my ($result, $message) = $self->TemplateObj->Parse(
                        Argument       => $self->Argument,
                        TicketObj      => $self->TicketObj,
                        TransactionObj => $self->TransactionObj
                );
                if ( !$result ) {
                        $RT::Logger->error("Failed to populate template:
$result, $message");
                        next;
                }

                my $MIMEObj = $self->TemplateObj->MIMEObj;
                my $msgstring = $MIMEObj->bodyhandle->as_string;

                eval {
                        $RT::Logger->debug("Notifying $ph about ticket
SLA");
                        $sender->send_sms(
                                text => $msgstring,
                                to   => $ph
                        );
                        $RT::Logger->info("Notified $ph about ticket SLA");
                };
                if ($@) {
                        $RT::Logger->error("Failed to notify $ph about
ticket SLA: $@");
                }
        }

        return 1;
}

1;


-- 
 Craig Ringer                   http://www.2ndQuadrant.com/
 PostgreSQL Development, 24x7 Support, Training & Services




More information about the rt-users mailing list