xref: /csrg-svn/usr.sbin/sendmail/src/stats.c (revision 23123)
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*23123Seric static char	SccsId[] = "@(#)stats.c	5.3 (Berkeley) 06/08/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;
329383Seric extern long		kbytes();	/* for _bf, _bt */
339383Seric /*
349383Seric **  MARKSTATS -- mark statistics
359383Seric */
369383Seric 
379383Seric markstats(e, to)
389383Seric 	register ENVELOPE *e;
399383Seric 	register ADDRESS *to;
409383Seric {
419383Seric 	if (to == NULL)
429383Seric 	{
439383Seric 		Stat.stat_nf[e->e_from.q_mailer->m_mno]++;
449383Seric 		Stat.stat_bf[e->e_from.q_mailer->m_mno] += kbytes(CurEnv->e_msgsize);
459383Seric 	}
469383Seric 	else
479383Seric 	{
489383Seric 		Stat.stat_nt[to->q_mailer->m_mno]++;
499383Seric 		Stat.stat_bt[to->q_mailer->m_mno] += kbytes(CurEnv->e_msgsize);
509383Seric 	}
519383Seric }
529383Seric /*
534286Seric **  POSTSTATS -- post statistics in the statistics file
544286Seric **
554286Seric **	Parameters:
564286Seric **		sfile -- the name of the statistics file.
574286Seric **
584286Seric **	Returns:
594286Seric **		none.
604286Seric **
614286Seric **	Side Effects:
624286Seric **		merges the Stat structure with the sfile file.
634286Seric */
644286Seric 
654286Seric poststats(sfile)
664286Seric 	char *sfile;
674286Seric {
684286Seric 	register int fd;
694286Seric 	struct statistics stat;
70*23123Seric 	extern off_t lseek();
714286Seric 
724319Seric 	(void) time(&Stat.stat_itime);
734286Seric 	Stat.stat_size = sizeof Stat;
744286Seric 
754286Seric 	fd = open(sfile, 2);
764286Seric 	if (fd < 0)
7711937Seric 	{
7811937Seric 		errno = 0;
794286Seric 		return;
8011937Seric 	}
814319Seric 	if (read(fd, (char *) &stat, sizeof stat) == sizeof stat &&
824286Seric 	    stat.stat_size == sizeof stat)
834286Seric 	{
844286Seric 		/* merge current statistics into statfile */
854286Seric 		register int i;
864286Seric 
874286Seric 		for (i = 0; i < MAXMAILERS; i++)
884286Seric 		{
894286Seric 			stat.stat_nf[i] += Stat.stat_nf[i];
904286Seric 			stat.stat_bf[i] += Stat.stat_bf[i];
914286Seric 			stat.stat_nt[i] += Stat.stat_nt[i];
924286Seric 			stat.stat_bt[i] += Stat.stat_bt[i];
934286Seric 		}
944286Seric 	}
954286Seric 	else
9616887Seric 		bcopy((char *) &Stat, (char *) &stat, sizeof stat);
974286Seric 
984286Seric 	/* write out results */
99*23123Seric 	(void) lseek(fd, (off_t) 0, 0);
1004319Seric 	(void) write(fd, (char *) &stat, sizeof stat);
1014319Seric 	(void) close(fd);
1024286Seric }
1034286Seric /*
1044286Seric **  KBYTES -- given a number, returns the number of Kbytes.
1054286Seric **
1064286Seric **	Used in statistics gathering of message sizes to try to avoid
1074286Seric **	wraparound (at least for a while.....)
1084286Seric **
1094286Seric **	Parameters:
1104286Seric **		bytes -- actual number of bytes.
1114286Seric **
1124286Seric **	Returns:
1134286Seric **		number of kbytes.
1144286Seric **
1154286Seric **	Side Effects:
1164286Seric **		none.
1174286Seric **
1184286Seric **	Notes:
1194286Seric **		This function is actually a ceiling function to
1204286Seric **			the nearest K.
1214286Seric **		Honestly folks, floating point might be better.
1224286Seric **			Or perhaps a "statistical" log method.
1234286Seric */
1244286Seric 
1254286Seric long
1264286Seric kbytes(bytes)
1274286Seric 	long bytes;
1284286Seric {
1294286Seric 	return ((bytes + 999) / 1000);
1304286Seric }
131