RT count still running on 3.8.10 - porting to RT4.
[usit-rt.git] / local / bin / rt-count
1 #!/local/bin/perl5.8.8
2
3 # RT3 ticket counter
4 #
5 # This script counts tickets.
6 #
7 # T.D.Bishop@kent.ac.uk, May 2004.
8 # Updated, May 2005.
9 #   (using changes sent by Jamie L. Penman-Smithson <jamie@silverdream.org>)
10 # Updated to display sums, June 2005.
11 #   (using changes sent by Petter Reinholdtsen <pere@hungry.com>)
12 #
13 # Copied from <URL:http://www.cs.kent.ac.uk/people/staff/tdb/rt3/rt-count> and
14 # modified to fit local needs.
15 #  - Petter Reinholdtsen 2005-05-19
16 # Add code to list deleted tickets as well
17 #  - Petter Reinholdtsen 2006-08-17
18
19 ### Configuration
20
21 my(@queues); # do not delete this
22
23 # Queues to operate on (default is all)
24 #@queues = qw ( queue1 queue2 );
25
26 # Where to store state
27 # Remember to change to correct path on current RT instance
28 my($statedir) = "/www/var/run/rt/rt-count";
29
30 # Where to mail report to (and from)
31 my($recipient) = 'RT driftsgruppe <rt-drift@usit.uio.no>, '.
32     '<rapportering@usit.uio.no>';
33 my($sender) = 'RT queue summary <request-tracker@hjelp.uio.no>';
34
35 # charset to use in emails
36 my($charset) = "ISO-8859-15";
37
38 # Location of RT's libs and scripts
39 # Remember to change to correct path on current RT instance
40 use lib '/www/data/rt/rt-perl/current/share/perl5/5.8.8';
41 use lib '/www/data/rt/rt-perl/current/lib/perl5/5.8.8';
42 use lib '/www/data/rt/rt-perl/current/lib/perl5';
43 use lib ("/www/var/hjelp/local/lib", "/www/var/hjelp/lib");
44
45 ### Code
46
47 use strict;
48 use Carp;
49
50 # Pull in the RT stuff
51 package RT;
52 use RT::Interface::CLI qw(CleanEnv GetCurrentUser GetMessageContent loc);
53
54 # Clean our the environment
55 CleanEnv();
56
57 # Load the RT configuration
58 RT::LoadConfig();
59
60 # Initialise RT
61 RT::Init();
62
63 # Drop any setgid permissions we might have
64 #RT::DropSetGIDPermissions();
65
66 use RT::Date;
67 use RT::Tickets;
68
69 # Path to sendmail and flags.  Need to set this after RT::Init()
70 my($sendmail) = "$RT::SendmailPath $RT::SendmailArguments -t";
71
72 if (!@queues) {
73     my $queues = new RT::Queues($RT::SystemUser);
74     $queues->LimitToEnabled();
75     foreach my $queue (@{$queues->ItemsArrayRef()}) {
76         push @queues, $queue->Name;
77     }
78 }
79
80 # Create message
81     my($msg) = <<EOF;
82 Content-Type: text/plain; charset="$charset"
83 From: $sender
84 To: $recipient
85 Subject: RT ticket counts for hjelp.uio.no
86
87 Queue statistics for RT:
88
89 EOF
90
91 $msg .= sprintf(
92     "      %25s |%8s |%8s |%8s |%8s |%8s\n",
93     "queue", "new", "resolved", "stalled", "rejected", "deleted",
94 );
95 $msg .= "      --------------------------+---------+---------+---------+---------+--------\n";
96
97 my($ticketsum) = 0;
98 my($resolvedsum) = 0;
99 my($stalledsum) = 0;
100 my($rejectedsum) = 0;
101 my($deletedsum) = 0;
102
103 foreach my $queuename (@queues) {
104     # Load in the queue
105     my $queue = new RT::Queue($RT::SystemUser);
106     $queue->Load($queuename);
107
108     my $now = new RT::Date($RT::SystemUser);
109     $now->SetToNow();
110
111     # Get hold of new and open tickets only
112     my $tickets = new RT::Tickets($RT::SystemUser);
113     $tickets->{'allow_deleted_search'} = 1;
114     $tickets->LimitQueue(VALUE => $queue->Id);
115
116     my($ticketcount) = 0;
117
118     my($resolvedcount) = 0;
119     my($stalledcount) = 0;
120     my($rejectedcount) = 0;
121     my($deletedcount) = 0;
122
123     # Count number of tickets
124     while (my $ticket = $tickets->Next) {
125         $ticketcount++;
126         if ($ticket->Status eq "resolved") {
127             $resolvedcount++;
128             next;
129         }
130         if ($ticket->Status eq "stalled") {
131             $stalledcount++;
132             next;
133         }
134         if ($ticket->Status eq "rejected") {
135             $rejectedcount++;
136             next;
137         }
138         if ($ticket->Status eq "deleted") {
139             $deletedcount++;
140             next;
141         }
142     }
143
144     # Open old counts
145     my($oldticketcount) = 0;
146     my($oldresolvedcount) = 0;
147     my($oldstalledcount) = 0;
148     my($oldrejectedcount) = 0;
149     my($olddeletedcount) = 0;
150
151     if (-f "$statedir/rt-count_${queuename}_tickets") {
152         open(TMP, "$statedir/rt-count_${queuename}_tickets") || die "Error opening rt-count_${queuename}_tickets: $!";
153         $oldticketcount = <TMP>;
154         close(TMP);
155     }
156     if (-f "$statedir/rt-count_${queuename}_resolved") {
157         open(TMP, "$statedir/rt-count_${queuename}_resolved") || die "Error opening rt-count_${queuename}_resolved: $!";
158         $oldresolvedcount = <TMP>;
159         close(TMP);
160     }
161     if (-f "$statedir/rt-count_${queuename}_stalled") {
162         open(TMP, "$statedir/rt-count_${queuename}_stalled") || die "Error opening rt-count_${queuename}_stalled: $!";
163         $oldstalledcount = <TMP>;
164         close(TMP);
165     }
166     if (-f "$statedir/rt-count_${queuename}_rejected") {
167         open(TMP, "$statedir/rt-count_${queuename}_rejected") || die "Error opening rt-count_${queuename}_rejected: $!";
168         $oldrejectedcount = <TMP>;
169         close(TMP);
170     }
171     if (-f "$statedir/rt-count_${queuename}_deleted") {
172         open(TMP, "$statedir/rt-count_${queuename}_deleted") || die "Error opening rt-count_${queuename}_deleted: $!";
173         $olddeletedcount = <TMP>;
174         close(TMP);
175     }
176
177     my($newtickets) = $ticketcount - $oldticketcount;
178     my($newresolved) = $resolvedcount - $oldresolvedcount;
179     my($newstalled) = $stalledcount - $oldstalledcount;
180     my($newrejected) = $rejectedcount - $oldrejectedcount;
181     my($newdeleted) = $deletedcount - $olddeletedcount;
182
183     $ticketsum += $newtickets;
184     $resolvedsum += $newresolved;
185     $stalledsum += $newstalled;
186     $rejectedsum += $newrejected;
187     $deletedsum += $newdeleted;
188
189     $msg .= sprintf(
190         "      %25s |%8d |%8d |%8d |%8d |%8d\n",
191         $queuename, $newtickets, $newresolved, $newstalled, $newrejected, $newdeleted
192     );
193
194     open(TMP, ">$statedir/rt-count_${queuename}_tickets") || die "Error writing rt-count_${queuename}_tickets: $!";
195     print TMP $ticketcount;
196     close(TMP);
197     open(TMP, ">$statedir/rt-count_${queuename}_resolved") || die "Error writing rt-count_${queuename}_resolved: $!";
198     print TMP $resolvedcount;
199     close(TMP);
200     open(TMP, ">$statedir/rt-count_${queuename}_stalled") || die "Error writing rt-count_${queuename}_stalled: $!";
201     print TMP $stalledcount;
202     close(TMP);
203     open(TMP, ">$statedir/rt-count_${queuename}_rejected") || die "Error writing rt-count_${queuename}_rejected: $!";
204     print TMP $rejectedcount;
205     close(TMP);
206     open(TMP, ">$statedir/rt-count_${queuename}_deleted") || die "Error writing rt-count_${queuename}_deleted: $!";
207     print TMP $deletedcount;
208     close(TMP);
209 }
210
211 $msg .= "      --------------------------+---------+---------+---------+---------+--------\n";
212 $msg .= sprintf(
213                 "      %25s |%8d |%8d |%8d |%8d |%8d\n",
214                 "sum", $ticketsum, $resolvedsum, $stalledsum, $rejectedsum, $deletedsum
215                 );
216
217 my($lastrun) = "FIRST RUN";
218
219 if (-f "$statedir/rt-count_lastrun") {
220     open(TMP, "$statedir/rt-count_lastrun") || die "Error opening rt-count_lastrun: $!";
221     $lastrun = <TMP>;
222     close(TMP);
223     $lastrun = localtime($lastrun);
224 }
225
226 my $now = time;
227 $now = localtime($now);
228
229 $msg .= <<EOF;
230
231 These statistics are for the period:
232
233         $lastrun -> $now
234
235 $RT::WebURL
236 EOF
237
238 open(TMP, ">$statedir/rt-count_lastrun") || die "Error writing rt-count_lastrun: $!";
239 print TMP time;
240 close(TMP);
241
242 # Send the message
243 open(SENDMAIL, "|$sendmail") || die "Error sending mail: $!";
244 print SENDMAIL $msg;
245 close(SENDMAIL);
246
247 # Disconnect before we finish off
248 $RT::Handle->Disconnect();
249 exit 0;