#! /bin/sh

# Copyright (c) 1999 University of Cambridge.
# See the file NOTICE for conditions of use and distribution.


# Shell script for seeing what the exim processes are doing. It gets rid
# of the old process log, then sends SIGUSR1 to all exim processes to get
# them to write their state to the log. Then it displays the contents of
# the log.

# The following lines are generated from Exim's configuration file when
# this source is built into a script, but you can subsequently edit them
# without rebuilding things, as long are you are careful not to overwrite
# the script in the next Exim rebuild/install. However, it's best to
# arrange your build-time configuration file to get the correct values.
# The variable ps_cmd is the path to the "ps" command, ps_arg is the argument
# for the ps command, kill_arg is the argument for the kill command to send
# SIGUSR1 (at least one OS requires a numeric value), and egrep_arg is the
# argument for egrep to find the instances of exim in the ps output.

ps_cmd=/bin/ps
ps_arg=-ax
kill_arg=-USR1
egrep_arg='/exim( |$)'

# Determine where the spool directory is. The strings /etc/exim/configure and
# /usr/local/sbin below are replaced by their compile-time settings when
# this source is build into a script. Search for an exim_path setting
# in the configure file; otherwise use the bin directory. Call that version
# of Exim to find the spool directory.

config=/etc/exim/configure

# Add code here to redefine "config" if an alternative configuration file
# should be used in some circumstances.

exim_path=`grep '^[      ]*exim_path' $config | sed 's/.*=[       ]*//'`
if test "$exim_path" = ""; then exim_path=/usr/local/sbin/exim; fi
spool_directory=`$exim_path -C /etc/exim/configure -bP spool_directory | sed 's/.*=[  ]*//'`

# The file that Exim writes when sent the SIGUSR1 signal is called
# "exim-process.info" in the spool directory.

log=$spool_directory/exim-process.info

# Now do the job.

/bin/rm -f ${log}
if [ -f ${log} ]; then
  echo "** Failed to remove ${log}"
  exit 1
fi

$ps_cmd $ps_arg | \
  egrep "$egrep_arg" | \
  awk "{print \"kill $kill_arg \"\$1}" | \
  uniq | sh

sleep 1

if [ ! -s ${log} ] ; then echo "No exim process data" ;
  else cut -c20-999 ${log} | sort -n | uniq ; fi

# End of exiwhat
