[rt-users] A small script

Mikhail Sobolev mikhail.sobolev at transas.com
Wed Sep 3 10:58:07 EDT 2003


Maybe this script will be of use for somebody.

I wanted to receive a summary of the current situations with our queues,
and hacked a small script, which should be attached to this message.

It has only argument: number of days.

We currently use it through crontab:

    MAILTO=sysadms at example.com

    10 1 * * Tue-Sat        root    [ -x /usr/bin/rt-statuses ] && /usr/bin/rt-statuses 1
    10 1 * * Mon            root    [ -x /usr/bin/rt-statuses ] && /usr/bin/rt-statuses 2

Hope this helps,

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

use strict;

my $my_address = 'rt-owner at example.com';
my $admin_address = 'sysadms at examples.com';

my $days;

if (defined ($ARGV[0])) {
    $days = int ($ARGV[0]);
} else {
    $days = 1;
}

if ($days <= 0) { $days = 1; }

my $toc = '';
my $body = '';

package RT;

use RT::Interface::CLI qw(CleanEnv GetCurrentUser GetMessageContent loc);
use RT::Tickets;

CleanEnv();

RT::LoadConfig();
RT::Init();
RT::DropSetGIDPermissions();

my $webbase = $RT::WebURL . "Ticket/Display.html?id=";

my $Users = RT::Users->new($RT::SystemUser);
$Users->UnLimit();
$Users->LimitToPrivileged();

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

my $prevday_begin = RT::Date->new($RT::SystemUser);
$prevday_begin->Set (Value => $prevday_end->SetToMidnight () - $RT::Date::DAY * $days);

my ($dfrom, $dto) = ($prevday_begin->AsString, $prevday_end->AsString);

if (0) {
    $body .= "\n<h2>Users</h2>\n";

    my ($user);

    while ($user = $Users->Next) {
        my $id = $user->id;
        my $nid = $user->Name;
        my $name = $user->RealName;
        my $email = $user->EmailAddress;

        $body .= "$nid ($id): $name <$email>\n";
    }
}

my $Queues = RT::Queues->new($RT::SystemUser);
$Queues->UnLimit();

$toc = "
<h2>Quick Jump</h2>
<table>
<tr>
<th>Queue<th>Description<th>Not Taken<th>Resolved<th>Being Worked On\n";

my ($queue, $Ticket, $Tickets);

while (my $queue = $Queues->Next) {
    my $name = $queue->Name;
    my $desc = $queue->Description;

    my ($nottaken, $resolved, $workedon) = (0, 0, 0);

    $body .= "<a name=\"$name\"><h2>$name: $desc</h2></a>\n";

    $body .= "<h3>Require attention</h3>\n";

    $Tickets = RT::Tickets->new($RT::SystemUser);

    $Tickets->LimitStatus(VALUE => 'resolved', OPERATOR => '!=');
    $Tickets->LimitQueue(VALUE => $queue->Id);
    $Tickets->LimitOwner(VALUE => $RT::Nobody->Id);
    $Tickets->OrderBy(FIELD => 'LastUpdated', ORDER => 'ASC');

    $body .= "<table>
<tr>
<th>Id<th>Description<th>Status<th>Created<th>Last Updated\n";

    while (my $Ticket = $Tickets->Next) {
        $body .= "<tr>";

        $body .= "<td><a href=\"$webbase" . $Ticket->id . "\">" . $Ticket->id . "</a>" .
              "<td><a href=\"$webbase" . $Ticket->id . "\">" .$Ticket->Subject . "</a>" .
              "<td>" . $Ticket->Status .
              "<td>" . $Ticket->CreatedObj->AgeAsString .
              "<td>" . $Ticket->LastUpdatedObj->AgeAsString;

        $body .= "</tr>\n";

        $nottaken += 1;
    }

    $body .= "</table>\n";

    $body .= "<h3>Resolved within previous " . $days * 24 . " hours</h3>\n";
    $body .= "<p><small>(during the period from $dfrom to $dto)</small>";

    $Tickets = RT::Tickets->new($RT::SystemUser);

    $Tickets->LimitQueue(VALUE => $queue->Id);
    $Tickets->LimitStatus(VALUE => 'resolved');
    $Tickets->LimitResolved(VALUE => $prevday_begin->ISO, OPERATOR => '>=');
    $Tickets->LimitResolved(VALUE => $prevday_end->ISO, OPERATOR => '<');
    $Tickets->OrderBy(FIELD => 'Resolved', ORDER => 'ASC');

    $body .= "<table>
<tr>
<th>Id<th>Description<th>Status<th>Owner<th>Created<th>Last Updated\n";

    while (my $Ticket = $Tickets->Next) {
        $body .= "<tr>
<td><a href=\"$webbase" . $Ticket->id . "\">" . $Ticket->id . "</a>" . "
<td><a href=\"$webbase" . $Ticket->id . "\">" . $Ticket->Subject . "</a>" . "
<td>" . $Ticket->Status . "
<td>" . $Ticket->OwnerObj->Name . "
<td>" . $Ticket->CreatedObj->AgeAsString . "
<td>" . $Ticket->LastUpdatedObj->AgeAsString . "
</tr>\n";

        $resolved += 1;
    }
    $body .= "</table>\n";

    $body .= "<h3>Being dealt with</h3>\n";

    $Tickets = RT::Tickets->new($RT::SystemUser);

    $Tickets->LimitStatus(VALUE => 'resolved', OPERATOR => '!=');
    $Tickets->LimitQueue(VALUE => $queue->Id);
    $Tickets->LimitOwner(VALUE => $RT::Nobody->Id, OPERATOR => '!=');
    $Tickets->OrderBy(FIELD => 'LastUpdated', ORDER => 'ASC');

    my %peruser = ();

    $body .= "<table>
<tr>
<th>Id<th>Description<th>Status<th>Owner<th>Created<th>Last Updated\n";

    while (my $Ticket = $Tickets->Next) {
        $body .= "<tr>";

        $body .= "<td><a href=\"$webbase" . $Ticket->id . "\">" . $Ticket->id . "</a>" .
              "<td><a href=\"$webbase" . $Ticket->id . "\">" . $Ticket->Subject . "</a>" .
              "<td>" . $Ticket->Status .
              "<td>" . $Ticket->OwnerObj->Name .
              "<td>" . $Ticket->CreatedObj->AgeAsString .
              "<td>" . $Ticket->LastUpdatedObj->AgeAsString;

        $body .= "</tr>\n";

        $workedon += 1;
    }

    $body .= "</table>\n";

    $toc .= "<tr><td><a href=\"#$name\">$name</a><td><a href=\"#$name\">$desc</a><td>$nottaken<td>$resolved<td>$workedon\n";
}

$toc .= "</table>\n";

$RT::Handle->Disconnect();

my $pid = open(MAIL, "|-");
if (! defined $pid) {
    die "Couldn't fork: $!";
}

if ($pid) {
print MAIL <<EOM;
From: $my_address
To: $admin_address
Subject: RT Status Report
Content-Type: text/html; charset=utf-8

<html>
<head>
    <title>Current Status</title>
    <meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">
</head>
<body>

$toc
$body

</body>
</html>
EOM

close (MAIL);

} else {
    # child
    exec("/usr/sbin/sendmail", "-f", "$my_address") or die "error running sendmail: $!";
}


More information about the rt-users mailing list