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

# 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 /etc/exim/exim.conf.$host ]; then
          hostsuffix=.$host
      fi
  fi
fi

# Set the Exim configuration file

config=/etc/exim/exim.conf$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/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

$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
