RT count still running on 3.8.10 - porting to RT4.
authorMikal Kolbein Gule <m.k.gule@usit.uio.no>
Mon, 30 Apr 2012 10:34:43 +0000 (12:34 +0200)
committerMikal Kolbein Gule <m.k.gule@usit.uio.no>
Mon, 30 Apr 2012 10:34:43 +0000 (12:34 +0200)
local/bin/rt-count [new file with mode: 0644]

diff --git a/local/bin/rt-count b/local/bin/rt-count
new file mode 100644 (file)
index 0000000..6c8189b
--- /dev/null
@@ -0,0 +1,249 @@
+#!/local/bin/perl5.8.8
+
+# RT3 ticket counter
+#
+# This script counts tickets.
+#
+# T.D.Bishop@kent.ac.uk, May 2004.
+# Updated, May 2005.
+#   (using changes sent by Jamie L. Penman-Smithson <jamie@silverdream.org>)
+# Updated to display sums, June 2005.
+#   (using changes sent by Petter Reinholdtsen <pere@hungry.com>)
+#
+# Copied from <URL:http://www.cs.kent.ac.uk/people/staff/tdb/rt3/rt-count> and
+# modified to fit local needs.
+#  - Petter Reinholdtsen 2005-05-19
+# Add code to list deleted tickets as well
+#  - Petter Reinholdtsen 2006-08-17
+
+### Configuration
+
+my(@queues); # do not delete this
+
+# Queues to operate on (default is all)
+#@queues = qw ( queue1 queue2 );
+
+# Where to store state
+# Remember to change to correct path on current RT instance
+my($statedir) = "/www/var/run/rt/rt-count";
+
+# Where to mail report to (and from)
+my($recipient) = 'RT driftsgruppe <rt-drift@usit.uio.no>, '.
+    '<rapportering@usit.uio.no>';
+my($sender) = 'RT queue summary <request-tracker@hjelp.uio.no>';
+
+# charset to use in emails
+my($charset) = "ISO-8859-15";
+
+# Location of RT's libs and scripts
+# Remember to change to correct path on current RT instance
+use lib '/www/data/rt/rt-perl/current/share/perl5/5.8.8';
+use lib '/www/data/rt/rt-perl/current/lib/perl5/5.8.8';
+use lib '/www/data/rt/rt-perl/current/lib/perl5';
+use lib ("/www/var/hjelp/local/lib", "/www/var/hjelp/lib");
+
+### Code
+
+use strict;
+use Carp;
+
+# Pull in the RT stuff
+package RT;
+use RT::Interface::CLI qw(CleanEnv GetCurrentUser GetMessageContent loc);
+
+# Clean our the environment
+CleanEnv();
+
+# Load the RT configuration
+RT::LoadConfig();
+
+# Initialise RT
+RT::Init();
+
+# Drop any setgid permissions we might have
+#RT::DropSetGIDPermissions();
+
+use RT::Date;
+use RT::Tickets;
+
+# Path to sendmail and flags.  Need to set this after RT::Init()
+my($sendmail) = "$RT::SendmailPath $RT::SendmailArguments -t";
+
+if (!@queues) {
+    my $queues = new RT::Queues($RT::SystemUser);
+    $queues->LimitToEnabled();
+    foreach my $queue (@{$queues->ItemsArrayRef()}) {
+        push @queues, $queue->Name;
+    }
+}
+
+# Create message
+    my($msg) = <<EOF;
+Content-Type: text/plain; charset="$charset"
+From: $sender
+To: $recipient
+Subject: RT ticket counts for hjelp.uio.no
+
+Queue statistics for RT:
+
+EOF
+
+$msg .= sprintf(
+    "      %25s |%8s |%8s |%8s |%8s |%8s\n",
+    "queue", "new", "resolved", "stalled", "rejected", "deleted",
+);
+$msg .= "      --------------------------+---------+---------+---------+---------+--------\n";
+
+my($ticketsum) = 0;
+my($resolvedsum) = 0;
+my($stalledsum) = 0;
+my($rejectedsum) = 0;
+my($deletedsum) = 0;
+
+foreach my $queuename (@queues) {
+    # Load in the queue
+    my $queue = new RT::Queue($RT::SystemUser);
+    $queue->Load($queuename);
+
+    my $now = new RT::Date($RT::SystemUser);
+    $now->SetToNow();
+
+    # Get hold of new and open tickets only
+    my $tickets = new RT::Tickets($RT::SystemUser);
+    $tickets->{'allow_deleted_search'} = 1;
+    $tickets->LimitQueue(VALUE => $queue->Id);
+
+    my($ticketcount) = 0;
+
+    my($resolvedcount) = 0;
+    my($stalledcount) = 0;
+    my($rejectedcount) = 0;
+    my($deletedcount) = 0;
+
+    # Count number of tickets
+    while (my $ticket = $tickets->Next) {
+        $ticketcount++;
+        if ($ticket->Status eq "resolved") {
+            $resolvedcount++;
+            next;
+        }
+        if ($ticket->Status eq "stalled") {
+            $stalledcount++;
+            next;
+        }
+        if ($ticket->Status eq "rejected") {
+            $rejectedcount++;
+            next;
+        }
+        if ($ticket->Status eq "deleted") {
+            $deletedcount++;
+            next;
+        }
+    }
+
+    # Open old counts
+    my($oldticketcount) = 0;
+    my($oldresolvedcount) = 0;
+    my($oldstalledcount) = 0;
+    my($oldrejectedcount) = 0;
+    my($olddeletedcount) = 0;
+
+    if (-f "$statedir/rt-count_${queuename}_tickets") {
+        open(TMP, "$statedir/rt-count_${queuename}_tickets") || die "Error opening rt-count_${queuename}_tickets: $!";
+        $oldticketcount = <TMP>;
+        close(TMP);
+    }
+    if (-f "$statedir/rt-count_${queuename}_resolved") {
+        open(TMP, "$statedir/rt-count_${queuename}_resolved") || die "Error opening rt-count_${queuename}_resolved: $!";
+        $oldresolvedcount = <TMP>;
+        close(TMP);
+    }
+    if (-f "$statedir/rt-count_${queuename}_stalled") {
+        open(TMP, "$statedir/rt-count_${queuename}_stalled") || die "Error opening rt-count_${queuename}_stalled: $!";
+        $oldstalledcount = <TMP>;
+        close(TMP);
+    }
+    if (-f "$statedir/rt-count_${queuename}_rejected") {
+        open(TMP, "$statedir/rt-count_${queuename}_rejected") || die "Error opening rt-count_${queuename}_rejected: $!";
+        $oldrejectedcount = <TMP>;
+        close(TMP);
+    }
+    if (-f "$statedir/rt-count_${queuename}_deleted") {
+        open(TMP, "$statedir/rt-count_${queuename}_deleted") || die "Error opening rt-count_${queuename}_deleted: $!";
+        $olddeletedcount = <TMP>;
+        close(TMP);
+    }
+
+    my($newtickets) = $ticketcount - $oldticketcount;
+    my($newresolved) = $resolvedcount - $oldresolvedcount;
+    my($newstalled) = $stalledcount - $oldstalledcount;
+    my($newrejected) = $rejectedcount - $oldrejectedcount;
+    my($newdeleted) = $deletedcount - $olddeletedcount;
+
+    $ticketsum += $newtickets;
+    $resolvedsum += $newresolved;
+    $stalledsum += $newstalled;
+    $rejectedsum += $newrejected;
+    $deletedsum += $newdeleted;
+
+    $msg .= sprintf(
+        "      %25s |%8d |%8d |%8d |%8d |%8d\n",
+        $queuename, $newtickets, $newresolved, $newstalled, $newrejected, $newdeleted
+    );
+
+    open(TMP, ">$statedir/rt-count_${queuename}_tickets") || die "Error writing rt-count_${queuename}_tickets: $!";
+    print TMP $ticketcount;
+    close(TMP);
+    open(TMP, ">$statedir/rt-count_${queuename}_resolved") || die "Error writing rt-count_${queuename}_resolved: $!";
+    print TMP $resolvedcount;
+    close(TMP);
+    open(TMP, ">$statedir/rt-count_${queuename}_stalled") || die "Error writing rt-count_${queuename}_stalled: $!";
+    print TMP $stalledcount;
+    close(TMP);
+    open(TMP, ">$statedir/rt-count_${queuename}_rejected") || die "Error writing rt-count_${queuename}_rejected: $!";
+    print TMP $rejectedcount;
+    close(TMP);
+    open(TMP, ">$statedir/rt-count_${queuename}_deleted") || die "Error writing rt-count_${queuename}_deleted: $!";
+    print TMP $deletedcount;
+    close(TMP);
+}
+
+$msg .= "      --------------------------+---------+---------+---------+---------+--------\n";
+$msg .= sprintf(
+                "      %25s |%8d |%8d |%8d |%8d |%8d\n",
+                "sum", $ticketsum, $resolvedsum, $stalledsum, $rejectedsum, $deletedsum
+                );
+
+my($lastrun) = "FIRST RUN";
+
+if (-f "$statedir/rt-count_lastrun") {
+    open(TMP, "$statedir/rt-count_lastrun") || die "Error opening rt-count_lastrun: $!";
+    $lastrun = <TMP>;
+    close(TMP);
+    $lastrun = localtime($lastrun);
+}
+
+my $now = time;
+$now = localtime($now);
+
+$msg .= <<EOF;
+
+These statistics are for the period:
+
+        $lastrun -> $now
+
+$RT::WebURL
+EOF
+
+open(TMP, ">$statedir/rt-count_lastrun") || die "Error writing rt-count_lastrun: $!";
+print TMP time;
+close(TMP);
+
+# Send the message
+open(SENDMAIL, "|$sendmail") || die "Error sending mail: $!";
+print SENDMAIL $msg;
+close(SENDMAIL);
+
+# Disconnect before we finish off
+$RT::Handle->Disconnect();
+exit 0;