xref: /csrg-svn/usr.sbin/sendmail/src/stats.c (revision 33731)
122714Sdist /*
2*33731Sbostic  * Copyright (c) 1988 Regents of the University of California.
3*33731Sbostic  * All rights reserved.
4*33731Sbostic  *
5*33731Sbostic  * Redistribution and use in source and binary forms are permitted
6*33731Sbostic  * provided that this notice is preserved and that due credit is given
7*33731Sbostic  * to the University of California at Berkeley. The name of the University
8*33731Sbostic  * may not be used to endorse or promote products derived from this
9*33731Sbostic  * software without specific prior written permission. This software
10*33731Sbostic  * is provided ``as is'' without express or implied warranty.
11*33731Sbostic  *
12*33731Sbostic  *  Sendmail
13*33731Sbostic  *  Copyright (c) 1983  Eric P. Allman
14*33731Sbostic  *  Berkeley, California
15*33731Sbostic  */
1622714Sdist 
1722714Sdist #ifndef lint
18*33731Sbostic static char sccsid[] = "@(#)stats.c	5.9 (Berkeley) 03/13/88";
19*33731Sbostic #endif /* not lint */
2022714Sdist 
214286Seric # include "sendmail.h"
2227681Sbloom # include "mailstats.h"
234286Seric 
249383Seric struct statistics	Stat;
2524944Seric 
2624944Seric #define ONE_K		1000		/* one thousand (twenty-four?) */
2724944Seric #define KBYTES(x)	(((x) + (ONE_K - 1)) / ONE_K)
289383Seric /*
299383Seric **  MARKSTATS -- mark statistics
309383Seric */
319383Seric 
329383Seric markstats(e, to)
339383Seric 	register ENVELOPE *e;
349383Seric 	register ADDRESS *to;
359383Seric {
369383Seric 	if (to == NULL)
379383Seric 	{
3825689Seric 		if (e->e_from.q_mailer != NULL)
3924945Seric 		{
4024945Seric 			Stat.stat_nf[e->e_from.q_mailer->m_mno]++;
4124945Seric 			Stat.stat_bf[e->e_from.q_mailer->m_mno] +=
4224945Seric 				KBYTES(CurEnv->e_msgsize);
4324945Seric 		}
449383Seric 	}
459383Seric 	else
469383Seric 	{
479383Seric 		Stat.stat_nt[to->q_mailer->m_mno]++;
4824944Seric 		Stat.stat_bt[to->q_mailer->m_mno] += KBYTES(CurEnv->e_msgsize);
499383Seric 	}
509383Seric }
519383Seric /*
524286Seric **  POSTSTATS -- post statistics in the statistics file
534286Seric **
544286Seric **	Parameters:
554286Seric **		sfile -- the name of the statistics file.
564286Seric **
574286Seric **	Returns:
584286Seric **		none.
594286Seric **
604286Seric **	Side Effects:
614286Seric **		merges the Stat structure with the sfile file.
624286Seric */
634286Seric 
644286Seric poststats(sfile)
654286Seric 	char *sfile;
664286Seric {
674286Seric 	register int fd;
684286Seric 	struct statistics stat;
6923123Seric 	extern off_t lseek();
704286Seric 
7123516Seric 	if (sfile == NULL)
7223516Seric 		return;
7323516Seric 
744319Seric 	(void) time(&Stat.stat_itime);
754286Seric 	Stat.stat_size = sizeof Stat;
764286Seric 
774286Seric 	fd = open(sfile, 2);
784286Seric 	if (fd < 0)
7911937Seric 	{
8011937Seric 		errno = 0;
814286Seric 		return;
8211937Seric 	}
834319Seric 	if (read(fd, (char *) &stat, sizeof stat) == sizeof stat &&
844286Seric 	    stat.stat_size == sizeof stat)
854286Seric 	{
864286Seric 		/* merge current statistics into statfile */
874286Seric 		register int i;
884286Seric 
894286Seric 		for (i = 0; i < MAXMAILERS; i++)
904286Seric 		{
914286Seric 			stat.stat_nf[i] += Stat.stat_nf[i];
924286Seric 			stat.stat_bf[i] += Stat.stat_bf[i];
934286Seric 			stat.stat_nt[i] += Stat.stat_nt[i];
944286Seric 			stat.stat_bt[i] += Stat.stat_bt[i];
954286Seric 		}
964286Seric 	}
974286Seric 	else
9816887Seric 		bcopy((char *) &Stat, (char *) &stat, sizeof stat);
994286Seric 
1004286Seric 	/* write out results */
10123123Seric 	(void) lseek(fd, (off_t) 0, 0);
1024319Seric 	(void) write(fd, (char *) &stat, sizeof stat);
1034319Seric 	(void) close(fd);
1044286Seric }
105