ShredderControl

From Request Tracker Wiki
Jump to navigation Jump to search

This BASH script allows one to shred a large number of tickets hopefully without making the machine unresponsive.

It shreds tickets of a specified status, a specified number at a time, with a specified pause between shreds, until a specified number of tickets are left. If that sentence is confusing, check the usage text in the script.

This script requires the CountTickets script which should be named count-tickets.sh.

Ideally, both CountTickets and ShredderControl will be located in /path/to/rt/sbin where rtx-shredder is located.

<pre>
        #!/bin/bash
        
        Statuses=( new open stalled resolved rejected deleted )
        if [ $1 ] ; then Statuses=( $* ) ; fi
        
        for Status in ${Statuses[@]} ; do
          echo -en "$Status:\t"
          if [ ${#Status} -lt 7 ] ; then echo -en "\t" ; fi
          count=`echo "select count(*) from Tickets where Tickets.Status=\"$Status\";" | mysql rt3`
          count=`echo $count | awk '{print $2}'`
          echo -e "$count\t`date -d '7 hours ago' '+%D %T'`"
        done
</pre>
<pre>
        [root@rt02 sbin]# cat shredder.sh
        #!/bin/bash
        
        usage()
        {
        cat << EOF
        
        Usage: $0 <status> [num] [wait] [until]
          Options:
            status - Ticket status name.  Required.
            num    - Number of tickets to shred at a time.  Default=5
            wait   - Number of seconds to wait between shred sets.  Default=1
            until  - How long to continue running until X tickets are remaining.  Default=n/a
        
          Examples:
            $0 rejected
              This will run until there are no tickets left, shredding 5 tickets at a time, with a second pause between shreds.
            $0 deleted 10 1 5
              This will run until there is 5 or less tickets, shredding 10 at a time, with a second pause between shreds.
        
        EOF
        }
        
        Statuses=( new open stalled resolved rejected deleted )
        if ! [ $1 ] ; then usage ; exit
        else
          if ! [[ "${Statuses[*]}" =~ "$1" ]] ; then
            echo "Status $1 not recognized."
            echo "Statuses allowed: ${Statuses[*]}"
            exit
          fi
          status="$1"
        fi
        
        if [ $2 ] ; then
          num=$2
          if [ $num -lt 1 ] ; then echo "Use a value for num above 0 [zero]." ; exit ; fi
          if [ $num -gt 100 ] ; then echo "Use a value for num below 101." ; exit ; fi
        else num=5
        fi
        
        if [ $3 ] ; then
          wait=$3
          if [ $wait -lt 0 ] ; then echo "Use a positive, or zero, value for wait." ; exit ; fi
          if [ $wait -gt 100 ] ; then echo "Use a value for wait below 101." ; exit ; fi
        else wait=1
        fi
        
        if [ $4 ] ; then
          untl=$4
          if [ $wait -lt 0 ] ; then echo "Use a positive, or zero, value for until." ; exit ; fi
        else untl=0
        fi
        
        numticketleft="\./count-tickets.sh $status | awk '{print $2}'"
        if [ $numticketleft -le $untl ] ; then
          echo "The number of tickets ($numticketleft) is equal or less than the until limit set ($untl). No work to do."
          exit
        fi
        
        shredcmd="./rtx-shredder --force --plugin 'Tickets=status,$status;limit,$num'"
        
        echo -n "Started at:  " ; date +'%D %T %Z'
        
        while [ $numticketleft -gt $untl ] ; do
          eval $shredcmd &gt; /dev/null
          sleep $wait
          numticketleft="`./count-tickets.sh $status | awk '{print $2}'`"
        done
        
        echo -n "Finished at: " ; date +'%D %T %Z'
</pre>


I made some modifications, for 3.8.9 and Ubuntu Server 10.04 - Luciano

<pre>
  #!/bin/bash
   
  usage () {
  
   
  echo "Usage: $0 <status> [num] [wait] [until]"
  echo "Options:"
  echo "    status - Ticket status name.  Required."
  echo "    num    - Number of tickets to shred at a time.  Default=5"
  echo "    wait   - Number of seconds to wait between shred sets.  Default=1"
  echo "    until  - How long to continue running until X tickets are remaining.  Default=n/a"
  
  echo " Examples:"
  echo "    $0 Rejected"
  echo "    This will run until there are no tickets left, shredding 5 tickets at a time, with a second pause between shreds."
  echo "    $0 Deleted 10 1 5"
  echo "    This will run until theres 5 or less tickets, shredding 10 at a time, with a second pause between shreds."
  exit 1
   }
   
  Statuses=( New Open Stalled Resolved Rejected Deleted )
   if ! [ $1 ] ; then usage ; exit
   else
     if ! [[ "${Statuses[*]}" =~ "$1" ]] ; then
       echo "Status $1 not recognized."
       echo "Statuses allowed: ${Statuses[*]}"
       exit
     fi
     status="$1"
   fi
   
   if [ $2 ] ; then
     num=$2
     if [ $num -lt 1 ] ; then echo "Use a value for num above 0 [zero]." ; exit ; fi
     if [ $num -gt 100 ] ; then echo "Use a value for num below 101." ; exit ; fi
   else num=5
   fi
   
   if [ $3 ] ; then
     wait=$3
     if [ $wait -lt 0 ] ; then echo "Use a positive, or zero, value for wait." ; exit ; fi
     if [ $wait -gt 100 ] ; then echo "Use a value for wait below 101." ; exit ; fi
   else wait=1
   fi
   
   if [ $4 ] ; then
     untl=$4
     if [ $wait -lt 0 ] ; then echo "Use a positive, or zero, value for until." ; exit ; fi
   else untl=0
   fi
   
   numticketleft="`./count-tickets.sh $status | awk '{print $2}'`"
   if [ $numticketleft -le $untl ] ; then
     echo "The number of tickets ($numticketleft) is equal or less than the until limit set ($untl). No work to do."
     exit
   fi
   
  echo -n "Started at:  " ; date +'%D %T %Z'
   
   while [ $numticketleft -gt $untl ] ; do
  ./rt-shredder --force --plugin "Tickets=query,Status = '$status' AND LastUpdated < '30 days ago';limit,$num"
       sleep $wait
     numticketleft="`./count-tickets.sh $status | awk '{print $2}'`"
   done
   
   echo -n "Finished at: " ; date +'%D %T %Z'
  
</pre>