Files
perl-RT-admin-tools/rt-user-count

191 lines
4.2 KiB
Perl

#!/usr/bin/perl
#
# RT ticket counter
#
# This script counts tickets per user.
#
# TODO:
# + look at deleted tickets? they affect the counts.
# + set value of $pkgname from spec file ('sed')
# + command-line options (like 'rt-queue-count')
use strict;
use warnings;
# *** BEGIN: RT initialization ***
use RT;
use RT::Date;
use RT::Tickets;
my $SystemUser;
my $sendmail;
BEGIN {
RT->LoadConfig;
RT->Init;
# Become RT System User
$SystemUser = RT::CurrentUser->new;
$SystemUser->Load('root');
die "Could not become root" unless $SystemUser;
$sendmail = RT->Config->Get('SendmailPath') . " " . RT->Config->Get('SendmailArguments');
}
# *** END: RT initialization ***
# *** BEGIN: process command-line options ***
use Getopt::Long;
my $debug=0;
my $help=0;
Getopt::Long::Configure('bundling');
Getopt::Long::GetOptions(
"d|debug" => \$debug,
"h|help" => \$help,
) or Pod::Usage::pod2usage(2);
use Pod::Usage;
Pod::Usage::pod2usage(
-exitstatus => 0,
-verbose => 2, # 2 to print full pod
) if $help;
# *** END: process command-line options ***
# *** BEGIN: Load configuration from /etc/sysconfig
use Config::Simple;
my $pkgname = "perl-RT-admin-tools";
# silently set sane defaults if no configuration file can be found
my $recipient = "root\@localhost"; # where to send mail to
my $sender = "root\@localhost"; # whence the mail cometh
my @users = (); # which users to count
# tell Config::Simple about our config file
Config::Simple->import_from("/etc/sysconfig/$pkgname", \my %Config);
# overwrite the sane defaults if import_from successful
$recipient = $Config{'default.RECIPIENT'} if (defined $Config{'default.RECIPIENT'});
$sender = $Config{'default.SENDER'} if (defined $Config{'default.SENDER'});
@users = split ' ', $Config{'default.USERS'} if (defined $Config{'default.USERS'});
if ($debug) {
print "RECIPIENT: $recipient\n";
print "SENDER: $sender\n";
print "USERS: @users\n";
}
# *** END: Load configuration from /etc/sysconfig
# *** BEGIN: construct message ***
my $charset = "UTF-8"; # charset to use in emails
my($msg) = <<EOF;
Content-Type: text/plain; charset="$charset"
From: $sender
To: $recipient
Subject: RT ticket counts per user
Open/New tickets per User:
EOF
my $base_url=RT->Config->Get('WebURL');
foreach my $user (@users) {
my $user_ticket_count=0;
$msg .= "$user:\n";
my $tickets = RT::Tickets->new($RT::SystemUser);
my $tsql="Owner = '$user' AND (Status = 'new' OR Status = 'open')";
print "TSQL query: $tsql\n" if ($debug);
$tickets->FromSQL($tsql);
while (my $t = $tickets->Next) {
$user_ticket_count++;
# do stuff with each ticket $t here
$msg .= "* ".$t->id." :";
$msg .= " ".$t->Subject."\n";
}
$msg .= " [RT Count: $user_ticket_count]\n\n";
}
# *** END: construct message ***
# *** BEGIN: send the message ***
if (! $debug){
# Send the message
open(SENDMAIL, "|$sendmail") || die "Error sending mail: $!";
print SENDMAIL $msg;
close(SENDMAIL);
}
else {
print "$msg\n";
}
# *** END: send the message ***
# Disconnect before we finish off
$RT::Handle->Disconnect();
exit 0;
__END__
=head1 rt-user-count
rt-user-count counts resolved RT tickets by user
=head1 SYNOPSIS
./rt-user-count [OPTIONS]
Options:
-h | --help
-d | --debug
=head1 COMMAND LINE OPTIONS
=over 8
=item B<--help>
Produces this output.
=item B<--debug>
Print debug output on STDOUT.
=back
=head1 CONFIGURATION
This script is designed to be run on a machine with an active RT instance,
otherwise it will not work!
Also, for this script to do anything useful, reasonable values for
SENDER, RECIPIENT, and USERS should be defined in the configuration
file /etc/sysconfig/perl-RT-admin-tools
See the comments embedded in that file for more information.
=head1 AUTHORS
Written by Lars Vogdt <lrupp@suse.de>
Updated and packaged by Nathan Cutler <ncutler@suse.cz>
=head1 SUPPORT
Please use https://bugzilla.novell.com to submit patches or suggest improvements.
Include package name and version information with all correspondence (when possible use output from
'zypper if').