Difference between revisions of "RtReminderMails"

From Request Tracker Wiki
Jump to navigation Jump to search
 
(prob with sendmail?)
Line 4: Line 4:


  <nowiki>
  <nowiki>
    #!/usr/bin/perl
        #!/usr/bin/perl
    #
        #
    # This script outputs all reminders that are due within the next e.g. 2 days
        # This script outputs all reminders that are due within the next e.g. 2 days
    # and mails the result to the corresponding owners. christian <ch AT westend.com>
        # and mails the result to the corresponding owners. christian <ch AT westend.com>
    #####
        #####
    # FING - UdelaR
        # FING - UdelaR
    # Modified by Mario A. del Riego <delriego AT fing DOT edu DOT uy>
        # Modified by Mario A. del Riego <delriego AT fing DOT edu DOT uy>
    # Modified by Gary Greene <ggreene AT minervanetworks DOT com>
        # Modified by Gary Greene <ggreene AT minervanetworks DOT com>
    # Restructured to allow cleaner execution flow
        # Restructured to allow cleaner execution flow
    # Removed taint check as it does not work currently
        # Removed taint check as it does not work currently
    # Fixed an old variable dont defined
        # Fixed an old variable dont defined
    # Changed mail format (ticket id added)
        # Changed mail format (ticket id added)
    # Getopt added for parameters: --debug --reminder --ticket --interval and --help
        # Getopt added for parameters: --debug --reminder --ticket --interval and --help
    # File: http://www.fing.edu.uy/~delriego/RT_Reminder.pl
        # File: http://www.fing.edu.uy/~delriego/RT_Reminder.pl
    # v 1.5
        # v 1.5
   
       
    package RT;
        package RT;
   
       
    use strict;
        use strict;
    use warnings;
        use warnings;
    use Getopt::Long;
        use Getopt::Long;
    use Data::Dumper;
        use Data::Dumper;
    use Date::Calc qw(Today Add_Delta_Days);
        use Date::Calc qw(Today Add_Delta_Days);
    use RT::Interface::CLI qw(CleanEnv GetMessageContent loc);
        use RT::Interface::CLI qw(CleanEnv GetMessageContent loc);
   
       
    # Show reminders that are due between today and the day after tomorrow.
        # Show reminders that are due between today and the day after tomorrow.
    ## Bug fixed by sean <smahan (works for) smwm.com>
        ## Bug fixed by sean <smahan (works for) smwm.com>
    my $CONFIG_DUE_DAYS = 2;
        my $CONFIG_DUE_DAYS = 2;
    # The mail "From" address.
        # The mail "From" address.
    my $CONFIG_FROM = 'rt-helpdesk@minervanetworks.com';
        my $CONFIG_FROM = 'rt-helpdesk@minervanetworks.com';
    # The mail "To" address if the Reminder *or* Ticket owner is Nobody.
        # The mail "To" address if the Reminder *or* Ticket owner is Nobody.
    my $CONFIG_TO_NOBODY = 'blackhole@minervanetworks.com';
        my $CONFIG_TO_NOBODY = 'blackhole@minervanetworks.com';
    # The mail "Subject" line.
        # The mail "Subject" line.
    my $CONFIG_SUBJECT = 'RT Reminder';
        my $CONFIG_SUBJECT = 'RT Reminder';
   
       
    # Send mail to REMINDER OWNER
        # Send mail to REMINDER OWNER
    my $ReminderOwner;
        my $ReminderOwner;
    # Send mail to TICKER OWNER
        # Send mail to TICKER OWNER
    my $TicketOwner;
        my $TicketOwner;
    # Only show the mails for debugging
        # Only show the mails for debugging
    my $debug = 0;
        my $debug = 0;
    my $interval = undef;
        my $interval = undef;
   
       
    my $help;
        my $help;
   
       
    sub usage {
        sub usage {
        print "rt-reminders.pl - A script to send alerts for reminders in the RT  system.\n";
            print "rt-reminders.pl - A script to send alerts for reminders in the RT  system.\n";
      print "Contributed by several members of the RT community.\n";
            print "Contributed by several members of the RT community.\n";
      print "Under the same license as RT itself.\n";
            print "Under the same license as RT itself.\n";
      print "-------------------------------------------------------------------------\n\n";
            print "-------------------------------------------------------------------------\n\n";
      print "--reminder|-r        Send mail to the reminder's owner\n";
            print "--reminder|-r        Send mail to the reminder's owner\n";
      print "--ticket|-t          Send mail to the ticket's owner\n";
            print "--ticket|-t          Send mail to the ticket's owner\n";
      print "--interval|-i <int>  Days left to end time (default < $CONFIG_DUE_DAYS)\n";
            print "--interval|-i <int>  Days left to end time (default < $CONFIG_DUE_DAYS)\n";
      print "--debug|-d          Only show mails to send for debugging\n";
            print "--debug|-d          Only show mails to send for debugging\n";
      print "--help|-h            This help!\n\n";
            print "--help|-h            This help!\n\n";
      exit;
            exit;
    }
        }
   
       
    sub main {
        sub main {
      unless (defined($ReminderOwner) || defined($TicketOwner)) {
            unless (defined($ReminderOwner) || defined($TicketOwner)) {
    usage();
        usage();
      }
            }
   
       
      # RT API boilerplate +++++++++++++++++
            # RT API boilerplate +++++++++++++++++
      # Clean our the environment
            # Clean our the environment
      CleanEnv();
            CleanEnv();
      # Load the RT configuration
            # Load the RT configuration
      RT::LoadConfig();
            RT::LoadConfig();
      # Initialise RT
            # Initialise RT
      RT::Init();
            RT::Init();
   
       
      # Load rest of the RT stuff (seems to go after initialization)
            # Load rest of the RT stuff (seems to go after initialization)
      use RT::Date;
            use RT::Date;
      use RT::Queue;
            use RT::Queue;
      use RT::Tickets;
            use RT::Tickets;
      use RT::Action::SendEmail;
            use RT::Action::SendEmail;
      # END RT API boilerplate +++++++++++++
            # END RT API boilerplate +++++++++++++
   
       
      if (! defined($interval)) {
            if (! defined($interval)) {
          $interval = $CONFIG_DUE_DAYS;
                $interval = $CONFIG_DUE_DAYS;
      }
            }
   
       
      # Calculate date boundaries.
            # Calculate date boundaries.
      my ($dy,$dm,$dd) = Today();
            my ($dy,$dm,$dd) = Today();
      my $d_now  = sprintf('%04d-%02d-%02d 00:00:00', $dy, $dm, $dd);
            my $d_now  = sprintf('%04d-%02d-%02d 00:00:00', $dy, $dm, $dd);
      my $d_then = sprintf('%04d-%02d-%02d 23:59:59', Add_Delta_Days($dy, $dm, $dd, $interval));
            my $d_then = sprintf('%04d-%02d-%02d 23:59:59', Add_Delta_Days($dy, $dm, $dd, $interval));
   
       
      # Fetch list of matching tickets.
            # Fetch list of matching tickets.
      my $tickets = RT::Tickets->new($RT::SystemUser);
            my $tickets = RT::Tickets->new($RT::SystemUser);
      $tickets->FromSQL(
            $tickets->FromSQL(
                  'Type = "reminder" AND '.
                      'Type = "reminder" AND '.
                  '(Status = "new" OR Status = "open") AND '.
                      '(Status = "new" OR Status = "open") AND '.
                  'Due >= "'.$d_now.'" AND '.
                      'Due >= "'.$d_now.'" AND '.
                  'Due <= "'.$d_then.'"');
                      'Due <= "'.$d_then.'"');
      $tickets->OrderBy(FIELD => 'Due', ORDER => 'DESC');
            $tickets->OrderBy(FIELD => 'Due', ORDER => 'DESC');
   
       
      # Format result and group by e-mail address.
            # Format result and group by e-mail address.
      my %rcpts = ();
            my %rcpts = ();
      while (my $ticket = $tickets->Next) {
            while (my $ticket = $tickets->Next) {
          my $out;
                my $out;
   
       
          # Format:
                # Format:
          # "Reminder: <subject_reminder> ([RT #<TicketID> <TicketSubject>])
                # "Reminder: <subject_reminder> ([RT #<TicketID> <TicketSubject>])
          #      Due: <left_hour> hours"
                #      Due: <left_hour> hours"
   
       
          my $t = RT::Ticket->new($RT::SystemUser);
                my $t = RT::Ticket->new($RT::SystemUser);
          $t->Load($ticket->RefersTo->First->LocalTarget);
                $t->Load($ticket->RefersTo->First->LocalTarget);
   
       
          $out = sprintf(
                $out = sprintf(
                  "Reminder: %s ([RT #%s] %s )\n" .
                        "Reminder: %s ([RT #%s] %s )\n" .
                  "    Due: %s\n\n",
                        "    Due: %s\n\n",
                  $ticket->Subject, $ticket->RefersTo->First->LocalTarget,  
                        $ticket->Subject, $ticket->RefersTo->First->LocalTarget,  
                  $t->Subject, loc($ticket->DueObj->AgeAsString));
                        $t->Subject, loc($ticket->DueObj->AgeAsString));
   
       
          my $tmp_rcpt_reminder = undef;
                my $tmp_rcpt_reminder = undef;
          if ($ReminderOwner) {
                if ($ReminderOwner) {
              # Push reminder to array of distinct e-mail addresses for this ticket.
                    # Push reminder to array of distinct e-mail addresses for this ticket.
              $tmp_rcpt_reminder = $ticket->OwnerObj->EmailAddress || $CONFIG_TO_NOBODY;
                    $tmp_rcpt_reminder = $ticket->OwnerObj->EmailAddress || $CONFIG_TO_NOBODY;
              if (! defined($rcpts{$tmp_rcpt_reminder})) {
                    if (! defined($rcpts{$tmp_rcpt_reminder})) {
                  $rcpts{$tmp_rcpt_reminder} = ""
                        $rcpts{$tmp_rcpt_reminder} = ""
              }
                    }
              $rcpts{ $tmp_rcpt_reminder } .= $out;
                    $rcpts{ $tmp_rcpt_reminder } .= $out;
          }
                }
   
       
          if ($TicketOwner) {
                if ($TicketOwner) {
              # Notify ticket owner or "nobody" if ticket is unowned
                    # Notify ticket owner or "nobody" if ticket is unowned
              my $tmp_rcpt_ticket  = $t->OwnerObj->EmailAddress || $CONFIG_TO_NOBODY;
                    my $tmp_rcpt_ticket  = $t->OwnerObj->EmailAddress || $CONFIG_TO_NOBODY;
              if (defined($CONFIG_TO_NOBODY) && $tmp_rcpt_ticket ne $tmp_rcpt_reminder) {
                    if (defined($CONFIG_TO_NOBODY) && $tmp_rcpt_ticket ne $tmp_rcpt_reminder) {
                  $rcpts{$tmp_rcpt_ticket} .= $out;
                        $rcpts{$tmp_rcpt_ticket} .= $out;
        }
            }
    }
        }
      }
            }
   
       
      # Iterate over each of the tickets and send the email
            # Iterate over each of the tickets and send the email
      foreach my $rcpt (keys %rcpts) {
            foreach my $rcpt (keys %rcpts) {
          my $mail = "From: $CONFIG_FROM\n".
                my $mail = "From: $CONFIG_FROM\n".
                      "To: $rcpt\n".
                          "To: $rcpt\n".
                      "Subject: $CONFIG_SUBJECT\n\n".
                          "Subject: $CONFIG_SUBJECT\n\n".
                      $rcpts{$rcpt};
                          $rcpts{$rcpt};
          if ($debug) {
                if ($debug) {
              print $mail . "\n";
                    print $mail . "\n";
          } else {
                } else {
              # FIXME: Is there no proper RT library for this?
                    # FIXME: Is there no proper RT library for this?
              no warnings;
                    no warnings;
              open(MAIL, "| $RT::SendmailPath $RT::SendmailBounceArguments $RT::SendmailArguments") or die("open  sendmail: $!");
                    open(MAIL, "| $RT::SendmailPath $RT::SendmailBounceArguments $RT::SendmailArguments") or die("open  sendmail: $!");
              use warnings;
                    use warnings;
              print(MAIL $mail) or die("print sendmail: $!");
                    print(MAIL $mail) or die("print sendmail: $!");
              close(MAIL) or die("close sendmail: $!");
                    close(MAIL) or die("close sendmail: $!");
          }
                }
      }
            }
    }
        }
   
       
    GetOptions(
        GetOptions(
      'r|reminder' => \$ReminderOwner,
            'r|reminder' => \$ReminderOwner,
      't|ticket' => \$TicketOwner,
            't|ticket' => \$TicketOwner,
      'd|debug' => \$debug,
            'd|debug' => \$debug,
      'i|interval=i' => \$interval,
            'i|interval=i' => \$interval,
      'h|help' => sub { usage() }
            'h|help' => sub { usage() }
    ) or usage();
        ) or usage();
   
       
    # while having a main isn't required in Perl, this cleans up and organizes the flow of the code....
        # while having a main isn't required in Perl, this cleans up and organizes the flow of the code....
    main();
        main();
  </nowiki>
        </nowiki>


 
'''2015-01-15 danny70437'''
 
Modified script to run unter debian with perl installed in /opt/perl like this:
modified script to run unter debian with perl installed in /opt/perl like this:
<nowiki>
 
      #/!/opt/perl/bin/perl
#!/opt/perl/bin/perl                                                                                                                                                                                                              
      use lib "/opt/rt4/lib";
 
      ...
use lib "/opt/rt4/lib";
      open(MAIL,  
 
        "| $RT::SendmailPath $RT::SendmailBounceArguments $RT::SendmailArguments -- $rcpt")  
      open(MAIL, "| $RT::SendmailPath $RT::SendmailBounceArguments $RT::SendmailArguments -- $rcpt") or die("open  sendmail: $!");
        or die("open sendmail: $!");
 
    </nowiki>
2015-01-15 danny70447

Revision as of 07:47, 15 January 2015

This is a small cron script that sends daily emails about reminders that will be due in the next 48h to the owner of the original ticket as well as the owner of the reminder.

Note - it requires the Date::Calc and Getopt::Long perl modules.

         #!/usr/bin/perl
         #
         # This script outputs all reminders that are due within the next e.g. 2 days
         # and mails the result to the corresponding owners. christian <ch AT westend.com>
         #####
         # FING - UdelaR
         # Modified by Mario A. del Riego <delriego AT fing DOT edu DOT uy>
         # Modified by Gary Greene <ggreene AT minervanetworks DOT com>
         # Restructured to allow cleaner execution flow
         # Removed taint check as it does not work currently
         # Fixed an old variable dont defined
         # Changed mail format (ticket id added)
         # Getopt added for parameters: --debug --reminder --ticket --interval and --help
         # File: http://www.fing.edu.uy/~delriego/RT_Reminder.pl
         # v 1.5
         
         package RT;
         
         use strict;
         use warnings;
         use Getopt::Long;
         use Data::Dumper;
         use Date::Calc qw(Today Add_Delta_Days);
         use RT::Interface::CLI qw(CleanEnv GetMessageContent loc);
         
         # Show reminders that are due between today and the day after tomorrow.
         ## Bug fixed by sean <smahan (works for) smwm.com>
         my $CONFIG_DUE_DAYS = 2;
         # The mail "From" address.
         my $CONFIG_FROM = 'rt-helpdesk@minervanetworks.com';
         # The mail "To" address if the Reminder *or* Ticket owner is Nobody.
         my $CONFIG_TO_NOBODY = 'blackhole@minervanetworks.com';
         # The mail "Subject" line.
         my $CONFIG_SUBJECT = 'RT Reminder';
         
         # Send mail to REMINDER OWNER
         my $ReminderOwner;
         # Send mail to TICKER OWNER
         my $TicketOwner;
         # Only show the mails for debugging
         my $debug = 0;
         my $interval = undef;
         
         my $help;
         
         sub usage {
             print "rt-reminders.pl - A script to send alerts for reminders in the RT  system.\n";
            print "Contributed by several members of the RT community.\n";
            print "Under the same license as RT itself.\n";
            print "-------------------------------------------------------------------------\n\n";
            print "--reminder|-r        Send mail to the reminder's owner\n";
            print "--ticket|-t          Send mail to the ticket's owner\n";
            print "--interval|-i <int>  Days left to end time (default < $CONFIG_DUE_DAYS)\n";
            print "--debug|-d           Only show mails to send for debugging\n";
            print "--help|-h            This help!\n\n";
            exit;
         }
         
         sub main {
            unless (defined($ReminderOwner) || defined($TicketOwner)) {
         	usage();
            }
         
            # RT API boilerplate +++++++++++++++++
            # Clean our the environment
            CleanEnv();
            # Load the RT configuration
            RT::LoadConfig();
            # Initialise RT
            RT::Init();
         
            # Load rest of the RT stuff (seems to go after initialization)
            use RT::Date;
            use RT::Queue;
            use RT::Tickets;
            use RT::Action::SendEmail;
            # END RT API boilerplate +++++++++++++
         
            if (! defined($interval)) {
                $interval = $CONFIG_DUE_DAYS;
            }
         
            # Calculate date boundaries.
            my ($dy,$dm,$dd) = Today();
            my $d_now  = sprintf('%04d-%02d-%02d 00:00:00', $dy, $dm, $dd);
            my $d_then = sprintf('%04d-%02d-%02d 23:59:59', Add_Delta_Days($dy, $dm, $dd, $interval));
         
            # Fetch list of matching tickets.
            my $tickets = RT::Tickets->new($RT::SystemUser);
            $tickets->FromSQL(
                       'Type = "reminder" AND '.
                       '(Status = "new" OR Status = "open") AND '.
                       'Due >= "'.$d_now.'" AND '.
                       'Due <= "'.$d_then.'"');
            $tickets->OrderBy(FIELD => 'Due', ORDER => 'DESC');
         
            # Format result and group by e-mail address.
            my %rcpts = ();
            while (my $ticket = $tickets->Next) {
                my $out;
         
                # Format:
                # "Reminder: <subject_reminder> ([RT #<TicketID> <TicketSubject>])
                #       Due: <left_hour> hours"
         
                my $t = RT::Ticket->new($RT::SystemUser);
                $t->Load($ticket->RefersTo->First->LocalTarget);
         
                $out = sprintf(
                        "Reminder: %s ([RT #%s] %s )\n" .
                        "     Due: %s\n\n",
                        $ticket->Subject, $ticket->RefersTo->First->LocalTarget, 
                        $t->Subject, loc($ticket->DueObj->AgeAsString));
         
                my $tmp_rcpt_reminder = undef;
                if ($ReminderOwner) {
                    # Push reminder to array of distinct e-mail addresses for this ticket.
                    $tmp_rcpt_reminder = $ticket->OwnerObj->EmailAddress || $CONFIG_TO_NOBODY;
                    if (! defined($rcpts{$tmp_rcpt_reminder})) {
                        $rcpts{$tmp_rcpt_reminder} = ""
                    }
                    $rcpts{ $tmp_rcpt_reminder } .= $out;
                }
         
                if ($TicketOwner) {
                    # Notify ticket owner or "nobody" if ticket is unowned
                    my $tmp_rcpt_ticket   = $t->OwnerObj->EmailAddress || $CONFIG_TO_NOBODY;
                    if (defined($CONFIG_TO_NOBODY) && $tmp_rcpt_ticket ne $tmp_rcpt_reminder) {
                        $rcpts{$tmp_rcpt_ticket} .= $out;
         	    }
         	}
            }
         
            # Iterate over each of the tickets and send the email
            foreach my $rcpt (keys %rcpts) {
                my $mail = "From: $CONFIG_FROM\n".
                           "To: $rcpt\n".
                           "Subject: $CONFIG_SUBJECT\n\n".
                           $rcpts{$rcpt};
                if ($debug) {
                    print $mail . "\n";
                } else {
                    # FIXME: Is there no proper RT library for this?
                    no warnings;
                    open(MAIL, "| $RT::SendmailPath $RT::SendmailBounceArguments $RT::SendmailArguments") or die("open  sendmail: $!");
                    use warnings;
                    print(MAIL $mail) or die("print sendmail: $!");
                    close(MAIL) or die("close sendmail: $!");
                }
            }
         }
         
         GetOptions(
            'r|reminder' => \$ReminderOwner,
            't|ticket' => \$TicketOwner,
            'd|debug' => \$debug,
            'i|interval=i' => \$interval,
            'h|help' => sub { usage() }
         ) or usage();
         
         # while having a main isn't required in Perl, this cleans up and organizes the flow of the code....
         main();
        

2015-01-15 danny70437 Modified script to run unter debian with perl installed in /opt/perl like this:

       #/!/opt/perl/bin/perl
       use lib "/opt/rt4/lib";
       ...
       open(MAIL, 
        "| $RT::SendmailPath $RT::SendmailBounceArguments $RT::SendmailArguments -- $rcpt") 
        or die("open  sendmail: $!");