xref: /csrg-svn/usr.sbin/sendmail/src/stats.c (revision 25689)
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*25689Seric static char	SccsId[] = "@(#)stats.c	5.7 (Berkeley) 01/05/86";
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;
3224944Seric 
3324944Seric #define ONE_K		1000		/* one thousand (twenty-four?) */
3424944Seric #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*25689Seric 		if (e->e_from.q_mailer != NULL)
4624945Seric 		{
4724945Seric 			Stat.stat_nf[e->e_from.q_mailer->m_mno]++;
4824945Seric 			Stat.stat_bf[e->e_from.q_mailer->m_mno] +=
4924945Seric 				KBYTES(CurEnv->e_msgsize);
5024945Seric 		}
519383Seric 	}
529383Seric 	else
539383Seric 	{
549383Seric 		Stat.stat_nt[to->q_mailer->m_mno]++;
5524944Seric 		Stat.stat_bt[to->q_mailer->m_mno] += KBYTES(CurEnv->e_msgsize);
569383Seric 	}
579383Seric }
589383Seric /*
594286Seric **  POSTSTATS -- post statistics in the statistics file
604286Seric **
614286Seric **	Parameters:
624286Seric **		sfile -- the name of the statistics file.
634286Seric **
644286Seric **	Returns:
654286Seric **		none.
664286Seric **
674286Seric **	Side Effects:
684286Seric **		merges the Stat structure with the sfile file.
694286Seric */
704286Seric 
714286Seric poststats(sfile)
724286Seric 	char *sfile;
734286Seric {
744286Seric 	register int fd;
754286Seric 	struct statistics stat;
7623123Seric 	extern off_t lseek();
774286Seric 
7823516Seric 	if (sfile == NULL)
7923516Seric 		return;
8023516Seric 
814319Seric 	(void) time(&Stat.stat_itime);
824286Seric 	Stat.stat_size = sizeof Stat;
834286Seric 
844286Seric 	fd = open(sfile, 2);
854286Seric 	if (fd < 0)
8611937Seric 	{
8711937Seric 		errno = 0;
884286Seric 		return;
8911937Seric 	}
904319Seric 	if (read(fd, (char *) &stat, sizeof stat) == sizeof stat &&
914286Seric 	    stat.stat_size == sizeof stat)
924286Seric 	{
934286Seric 		/* merge current statistics into statfile */
944286Seric 		register int i;
954286Seric 
964286Seric 		for (i = 0; i < MAXMAILERS; i++)
974286Seric 		{
984286Seric 			stat.stat_nf[i] += Stat.stat_nf[i];
994286Seric 			stat.stat_bf[i] += Stat.stat_bf[i];
1004286Seric 			stat.stat_nt[i] += Stat.stat_nt[i];
1014286Seric 			stat.stat_bt[i] += Stat.stat_bt[i];
1024286Seric 		}
1034286Seric 	}
1044286Seric 	else
10516887Seric 		bcopy((char *) &Stat, (char *) &stat, sizeof stat);
1064286Seric 
1074286Seric 	/* write out results */
10823123Seric 	(void) lseek(fd, (off_t) 0, 0);
1094319Seric 	(void) write(fd, (char *) &stat, sizeof stat);
1104319Seric 	(void) close(fd);
1114286Seric }
112