#! /bin/sh

# Copyright (c) 2002 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.

# Some operating systems have a command that finds processes that match
# certain conditions (by default usually those running specific commands)
# and sends them signals. If such a command is defined for your OS, the
# following variables are set and used.

multikill_cmd=killall
multikill_arg=exim

# In other operating systems, Exim has to use "ps" and "egrep" to find the
# processes itself. In those cases, the next three variables are used:

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

# In both cases, kill_arg is the argument for the (multi)kill command to send
# SIGUSR1 (at least one OS requires a numeric value).

signal=-USR1

# See if this installation is using the esoteric "USE_NODE" feature of Exim,
# in which it uses the host's name as a suffix for the configuration file name.
# Set $hostsuffix if a suffixed file is found.

configure_file_use_node=
if [ "$configure_file_use_node" = "yes" ]; then
  host=`uname -n`
  if [ "$host" != "" ]; then
      if [ -f /usr/local/etc/exim/configure.$host ]; then
          hostsuffix=.$host
      fi
  fi
fi

# Set the Exim configuration file

config=/usr/local/etc/exim/configure$hostsuffix

# Determine where the spool directory is. 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.

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 $config -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

# If there is a multikill command, use it. On some OS this command is called
# "killall" (Linux, FreeBSD). On Solaris it is called "pkill". Note that on
# Solaris, "killall" kills ALL processes - this is the System V version of this
# command, and not what we want!

if [ "$multikill_cmd" != "" ] ; then
  $multikill_cmd $signal "$multikill_arg"

# No multikill command; do it the hard way

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

sleep 1

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

# End of exiwhat
