#!/bin/sh
#
# count and log number of exim and proxy processes and uptime.
#
# $Cambridge: hermes/conf/exim/sbin/countprocs,v 1.11 2007/05/11 18:34:07 fanf2 Exp $

PATH=/opt/exim/bin:$PATH

log () {
	read line
	logger -p mail.info -t countprocs $* $line
}

exim=`mktemp -p /tmp exim.XXXXXXXXXX`
proxy=`mktemp -p /tmp proxy.XXXXXXXXXX`

ps -Ao args | grep 'exim[ ]' >$exim
ps -Ao args | grep 'proxy: ' >$proxy

grep -cv '[-]bd'	$exim	| log exim delivering:
grep -c  '[-]bd'	$exim	| log exim incoming:
grep -c  imap[s-]	$proxy	| log proxy imaps:
grep -c  imap[^s-]	$proxy	| log proxy imap:
grep -c  pop[s-]	$proxy	| log proxy pops:
grep -c  pop[^s-]	$proxy	| log proxy pop:
uptime | sed s/.*://		| log load average:

num=" +([0-9]+)"
free | sed -r "/^Mem:$num$num$num$num$num$num/!d;
               s//total=\1 free=\3 buffers=\5 cached=\6/" | log memory:

# keep or skip a number
k="([0-9]+) +"
s="[0-9]+ +"
# number of read and write operations, and time spent doing IO
cat /sys/block/sda/stat |
	sed -r "s/$k$s$s$s$k$s$s$s$s$k.*/rd=\1 wr=\2 time=\3/" | log disk io:

rm $exim $proxy

exit
