[rt-users] Improved nag script (contribution)

cbailiff at awayweb.com cbailiff at awayweb.com
Fri May 3 06:32:13 EDT 2002


I made some changes to the nag script (contrib/rt2.0/nag) to make it a bit
more useful for our rt2 installation (as opposed to Jesses').

The script should be given a list of queues on the command line. For each
queue, each priviliged user will be mailed a list of open and new tickets,
listed in priority order (with due dates where set).

The script uses Mail::Send, so it should find a mailer OK on most systems,
otherwise you can modify the actual sending part quite easily.

Hope it's useful.

Cris

-------------- next part --------------
#!/usr/bin/perl -w

#
# $Id$
#
# $Log$

use strict;
use Carp;

use lib "/opt/rt2/lib";
use lib "/opt/rt2/etc";

use RT::Interface::CLI  qw(CleanEnv LoadConfig DBConnect 
			   GetCurrentUser GetMessageContent);

use Mail::Send;


#Clean out all the nasties from the environment
CleanEnv();

#Load etc/config.pm and drop privs
LoadConfig();

#Connect to the database and get RT::SystemUser and RT::Nobody loaded
DBConnect();

use RT::Date;
use RT::Queue;
use RT::Tickets;
use RT::Users;
use Data::Dumper;

foreach my $queue_name (@ARGV) {
    my $safe_queue_name=($queue_name=~tr/[a-zA-Z0-9\.-]//cd);

    my $queue = new RT::Queue($RT::SystemUser);


    $queue->Load($ARGV[0]);

    my $now = new RT::Date($RT::SystemUser);
    $now->SetToNow();

    # find users who might own tickets in this queue
    my $Users = new RT::Users($RT::SystemUser);

    # should work, but something wrong
    #$Users->HasQueueRight(  $queue->Id, Right=>'OwnTicket');

    # close enough
    $Users->LimitToPrivileged;

    while (my $Owner=$Users->Next) {
	my $tickets = new RT::Tickets($RT::SystemUser);
	$tickets->LimitStatus(VALUE => 'open');
	$tickets->LimitStatus(VALUE => 'new');
	$tickets->LimitQueue(VALUE => $queue->Id);
	$tickets->LimitOwner(VALUE => $Owner->Name);
	$tickets->OrderBy(FIELD => 'Priority', ORDER=>'DESC');
	
	my $body;
	my $count=0;
	while (my $Ticket = $tickets->Next) {
	    if (!defined($body)) { # 1st time through only
		$body.="New/Open tickets for ".$Owner->Name." for queue ".$queue->Name."\n\n";
		$body.="Id#   Pri Due Date                Subject\n";
	    }

	    $body.=sprintf "%-5d %3d %19.19s %-60s\n", $Ticket->Id, $Ticket->Priority,
		($Ticket->DueObj->Unix >0 ? $Ticket->DueObj->AsString : ''), $Ticket->Subject;
	    $count++;

	}
	if ($body && defined($Owner->EmailAddress)) {
	    my $msg = new Mail::Send Subject=>
		"RT: ".$queue->Name." - $count outstanding tickets for ".$Owner->Name,
		To=>$Owner->EmailAddress;

#               $msg->add("From",($RT::CommentAddress));

	    my $fh = $msg->open;
	    print $fh $body;
	    $fh->close;         # complete the message and send it
	}
    }

}

$RT::Handle->Disconnect();


More information about the rt-users mailing list