xref: /csrg-svn/usr.sbin/sendmail/src/stats.c (revision 24944)
122714Sdist /*
222714Sdist **  Sendmail
322714Sdist **  Copyright (c) 1983  Eric P. Allman
422714Sdist **  Berkeley, California
522714Sdist **
622714Sdist **  Copyright (c) 1983 Regents of the University of California.
722714Sdist **  All rights reserved.  The Berkeley software License Agreement
822714Sdist **  specifies the terms and conditions for redistribution.
922714Sdist */
1022714Sdist 
1122714Sdist #ifndef lint
12*24944Seric static char	SccsId[] = "@(#)stats.c	5.4.1.1 (Berkeley) 09/19/85";
1322714Sdist #endif not lint
1422714Sdist 
154286Seric # include "sendmail.h"
164286Seric 
174286Seric /*
189383Seric **  Statistics structure.
199383Seric */
209383Seric 
219383Seric struct statistics
229383Seric {
239383Seric 	time_t	stat_itime;		/* file initialization time */
249383Seric 	short	stat_size;		/* size of this structure */
259383Seric 	long	stat_nf[MAXMAILERS];	/* # msgs from each mailer */
269383Seric 	long	stat_bf[MAXMAILERS];	/* kbytes from each mailer */
279383Seric 	long	stat_nt[MAXMAILERS];	/* # msgs to each mailer */
289383Seric 	long	stat_bt[MAXMAILERS];	/* kbytes to each mailer */
299383Seric };
309383Seric 
319383Seric struct statistics	Stat;
32*24944Seric 
33*24944Seric #define ONE_K		1000		/* one thousand (twenty-four?) */
34*24944Seric #define KBYTES(x)	(((x) + (ONE_K - 1)) / ONE_K)
359383Seric /*
369383Seric **  MARKSTATS -- mark statistics
379383Seric */
389383Seric 
399383Seric markstats(e, to)
409383Seric 	register ENVELOPE *e;
419383Seric 	register ADDRESS *to;
429383Seric {
439383Seric 	if (to == NULL)
449383Seric 	{
45*24944Seric 		Stat.stat_nf[e->e_from.q_mailer->m_mno]++;
46*24944Seric 		Stat.stat_bf[e->e_from.q_mailer->m_mno] += KBYTES(CurEnv->e_msgsize);
479383Seric 	}
489383Seric 	else
499383Seric 	{
509383Seric 		Stat.stat_nt[to->q_mailer->m_mno]++;
51*24944Seric 		Stat.stat_bt[to->q_mailer->m_mno] += KBYTES(CurEnv->e_msgsize);
529383Seric 	}
539383Seric }
549383Seric /*
554286Seric **  POSTSTATS -- post statistics in the statistics file
564286Seric **
574286Seric **	Parameters:
584286Seric **		sfile -- the name of the statistics file.
594286Seric **
604286Seric **	Returns:
614286Seric **		none.
624286Seric **
634286Seric **	Side Effects:
644286Seric **		merges the Stat structure with the sfile file.
654286Seric */
664286Seric 
674286Seric poststats(sfile)
684286Seric 	char *sfile;
694286Seric {
704286Seric 	register int fd;
714286Seric 	struct statistics stat;
7223123Seric 	extern off_t lseek();
734286Seric 
7423516Seric 	if (sfile == NULL)
7523516Seric 		return;
7623516Seric 
774319Seric 	(void) time(&Stat.stat_itime);
784286Seric 	Stat.stat_size = sizeof Stat;
794286Seric 
804286Seric 	fd = open(sfile, 2);
814286Seric 	if (fd < 0)
8211937Seric 	{
8311937Seric 		errno = 0;
844286Seric 		return;
8511937Seric 	}
864319Seric 	if (read(fd, (char *) &stat, sizeof stat) == sizeof stat &&
874286Seric 	    stat.stat_size == sizeof stat)
884286Seric 	{
894286Seric 		/* merge current statistics into statfile */
904286Seric 		register int i;
914286Seric 
924286Seric 		for (i = 0; i < MAXMAILERS; i++)
934286Seric 		{
944286Seric 			stat.stat_nf[i] += Stat.stat_nf[i];
954286Seric 			stat.stat_bf[i] += Stat.stat_bf[i];
964286Seric 			stat.stat_nt[i] += Stat.stat_nt[i];
974286Seric 			stat.stat_bt[i] += Stat.stat_bt[i];
984286Seric 		}
994286Seric 	}
1004286Seric 	else
10116887Seric 		bcopy((char *) &Stat, (char *) &stat, sizeof stat);
1024286Seric 
1034286Seric 	/* write out results */
10423123Seric 	(void) lseek(fd, (off_t) 0, 0);
1054319Seric 	(void) write(fd, (char *) &stat, sizeof stat);
1064319Seric 	(void) close(fd);
1074286Seric }
108