xref: /csrg-svn/usr.sbin/sendmail/src/stats.c (revision 68839)
122714Sdist /*
2*68839Seric  * Copyright (c) 1983, 1995 Eric P. Allman
362532Sbostic  * Copyright (c) 1988, 1993
462532Sbostic  *	The Regents of the University of California.  All rights reserved.
533731Sbostic  *
642829Sbostic  * %sccs.include.redist.c%
733731Sbostic  */
822714Sdist 
922714Sdist #ifndef lint
10*68839Seric static char sccsid[] = "@(#)stats.c	8.4 (Berkeley) 04/21/95";
1133731Sbostic #endif /* not lint */
1222714Sdist 
134286Seric # include "sendmail.h"
1427681Sbloom # include "mailstats.h"
154286Seric 
169383Seric struct statistics	Stat;
1724944Seric 
1863753Seric bool	GotStats = FALSE;	/* set when we have stats to merge */
1963753Seric 
2024944Seric #define ONE_K		1000		/* one thousand (twenty-four?) */
2124944Seric #define KBYTES(x)	(((x) + (ONE_K - 1)) / ONE_K)
229383Seric /*
239383Seric **  MARKSTATS -- mark statistics
249383Seric */
259383Seric 
269383Seric markstats(e, to)
279383Seric 	register ENVELOPE *e;
289383Seric 	register ADDRESS *to;
299383Seric {
309383Seric 	if (to == NULL)
319383Seric 	{
3225689Seric 		if (e->e_from.q_mailer != NULL)
3324945Seric 		{
3424945Seric 			Stat.stat_nf[e->e_from.q_mailer->m_mno]++;
3524945Seric 			Stat.stat_bf[e->e_from.q_mailer->m_mno] +=
3655012Seric 				KBYTES(e->e_msgsize);
3724945Seric 		}
389383Seric 	}
399383Seric 	else
409383Seric 	{
419383Seric 		Stat.stat_nt[to->q_mailer->m_mno]++;
4255012Seric 		Stat.stat_bt[to->q_mailer->m_mno] += KBYTES(e->e_msgsize);
439383Seric 	}
4463753Seric 	GotStats = TRUE;
459383Seric }
469383Seric /*
474286Seric **  POSTSTATS -- post statistics in the statistics file
484286Seric **
494286Seric **	Parameters:
504286Seric **		sfile -- the name of the statistics file.
514286Seric **
524286Seric **	Returns:
534286Seric **		none.
544286Seric **
554286Seric **	Side Effects:
564286Seric **		merges the Stat structure with the sfile file.
574286Seric */
584286Seric 
594286Seric poststats(sfile)
604286Seric 	char *sfile;
614286Seric {
624286Seric 	register int fd;
634286Seric 	struct statistics stat;
6423123Seric 	extern off_t lseek();
654286Seric 
6663753Seric 	if (sfile == NULL || !GotStats)
6723516Seric 		return;
6823516Seric 
694319Seric 	(void) time(&Stat.stat_itime);
704286Seric 	Stat.stat_size = sizeof Stat;
714286Seric 
7264368Seric 	fd = open(sfile, O_RDWR);
734286Seric 	if (fd < 0)
7411937Seric 	{
7511937Seric 		errno = 0;
764286Seric 		return;
7711937Seric 	}
7864368Seric 	(void) lockfile(fd, sfile, NULL, LOCK_EX);
794319Seric 	if (read(fd, (char *) &stat, sizeof stat) == sizeof stat &&
804286Seric 	    stat.stat_size == sizeof stat)
814286Seric 	{
824286Seric 		/* merge current statistics into statfile */
834286Seric 		register int i;
844286Seric 
854286Seric 		for (i = 0; i < MAXMAILERS; i++)
864286Seric 		{
874286Seric 			stat.stat_nf[i] += Stat.stat_nf[i];
884286Seric 			stat.stat_bf[i] += Stat.stat_bf[i];
894286Seric 			stat.stat_nt[i] += Stat.stat_nt[i];
904286Seric 			stat.stat_bt[i] += Stat.stat_bt[i];
914286Seric 		}
924286Seric 	}
934286Seric 	else
9416887Seric 		bcopy((char *) &Stat, (char *) &stat, sizeof stat);
954286Seric 
964286Seric 	/* write out results */
9723123Seric 	(void) lseek(fd, (off_t) 0, 0);
984319Seric 	(void) write(fd, (char *) &stat, sizeof stat);
994319Seric 	(void) close(fd);
10063753Seric 
10163753Seric 	/* clear the structure to avoid future disappointment */
10263753Seric 	bzero(&Stat, sizeof stat);
10363753Seric 	GotStats = FALSE;
1044286Seric }
105