Difference between revisions of "ShredderControl"

From Request Tracker Wiki
Jump to navigation Jump to search
 
Line 8: Line 8:


  <nowiki>#!/bin/bash
  <nowiki>#!/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
        [root@rt02 sbin]# cat shredder.sh
        #!/bin/bash
       
        usage()
        {
        cat &lt;&lt; EOF
       
        Usage: $0 &lt;status&gt; [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's 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'
        </nowiki>
I made some modifications, for 3.8.9 and Ubuntu Server 10.04 - Luciano
<nowiki>
  #!/bin/bash
    
    
  Statuses=( new open stalled resolved rejected deleted )
  usage () {
  if [ $1 ] ; then Statuses=( $* ) ; fi
 
    
    
  for Status in ${Statuses[@]} ; do
  echo "Usage: $0 <status> [num] [wait] [until]"
    echo -en "$Status:\t"
  echo "Options:"
    if [ ${#Status} -lt 7 ] ; then echo -en "\t" ; fi
  echo "    status - Ticket status name.  Required."
    count=`echo "select count(*) from Tickets where Tickets.Status=\"$Status\";" | mysql rt3`
  echo "    num    - Number of tickets to shred at a time.  Default=5"
    count=`echo $count | awk '{print $2}'`
  echo "    wait  - Number of seconds to wait between shred sets.  Default=1"
    echo -e "$count\t`date -d '7 hours ago' '+%D %T'`"
  echo "    until  - How long to continue running until X tickets are remaining.  Default=n/a"
  done
 
  [root@rt02 sbin]# cat shredder.sh
  echo " Examples:"
  #!/bin/bash
  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."
  usage()
  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."
  cat &lt;&lt; EOF
  exit 1
 
  Usage: $0 &lt;status&gt; [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's 5 or less tickets, shredding 10 at a time, with a second pause between shreds.
 
  EOF
   }
   }
    
    
  Statuses=( new open stalled resolved rejected deleted )
  Statuses=( New Open Stalled Resolved Rejected Deleted )
   if ! [ $1 ] ; then usage ; exit
   if ! [ $1 ] ; then usage ; exit
   else
   else
Line 79: Line 153:
   fi
   fi
    
    
  shredcmd="./rtx-shredder --force --plugin 'Tickets=status,$status;limit,$num'"
  echo -n "Started at:  " ; date +'%D %T %Z'
 
  echo -n "Started at:  " ; date +'%D %T %Z'
    
    
   while [ $numticketleft -gt $untl ] ; do
   while [ $numticketleft -gt $untl ] ; do
    eval $shredcmd &gt; /dev/null
  ./rt-shredder --force --plugin "Tickets=query,Status = '$status' AND LastUpdated < '30 days ago';limit,$num"
    sleep $wait
      sleep $wait
     numticketleft="`./count-tickets.sh $status | awk '{print $2}'`"
     numticketleft="`./count-tickets.sh $status | awk '{print $2}'`"
   done
   done
    
    
   echo -n "Finished at: " ; date +'%D %T %Z'
   echo -n "Finished at: " ; date +'%D %T %Z'
  </nowiki>
 
 
   </nowiki>
 
 
I made some modifications, for 3.8.9 and Ubuntu Server 10.04 - Luciano
 
<nowiki>
#!/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
# shredcmd="./rt-shredder --force --plugin 'Tickets=query,Status = $status;limit,$num'"
#shredcmd="sbin/rt-shredder --force --plugin "Tickets=query,Status = '$status' AND LastUpdated < '30 days ago';limit,$num"
#cat "(/usr/local/rt/sbin/rt-shredder --force --plugin "Tickets=query,Status = '$status' AND LastUpdated < '30 days ago';limit,$num")" > shredcmd
 
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"
# eval $shredcmd > /dev/null
  sleep $wait
  numticketleft="`./count-tickets.sh $status | awk '{print $2}'`"
done
echo -n "Finished at: " ; date +'%D %T %Z'
 
</nowiki>

Revision as of 12:11, 28 July 2011

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.

#!/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
        [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's 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 > /dev/null
          sleep $wait
          numticketleft="`./count-tickets.sh $status | awk '{print $2}'`"
        done
        
        echo -n "Finished at: " ; date +'%D %T %Z'
        


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

  #!/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'