2 * Copyright (c) 1998-2000 Sendmail, Inc. and its suppliers.
4 * Copyright (c) 1983 Eric P. Allman. All rights reserved.
5 * Copyright (c) 1988, 1993
6 * The Regents of the University of California. All rights reserved.
8 * By using this file, you agree to the terms and conditions set
9 * forth in the LICENSE file which can be found at the top level of
10 * the sendmail distribution.
16 static char copyright[] =
17 "@(#) Copyright (c) 1998, 1999 Sendmail, Inc. and its suppliers.\n\
18 All rights reserved.\n\
19 Copyright (c) 1988, 1993\n\
20 The Regents of the University of California. All rights reserved.\n";
24 static char id[] = "@(#)$Id: mailstats.c,v 8.53.16.10 2000/07/18 05:51:15 gshapiro Exp $";
36 # undef EX_OK /* unistd.h may have another use for this */
40 #include <sendmail/sendmail.h>
41 #include <sendmail/mailstats.h>
42 #include <sendmail/pathnames.h>
45 #define MNAMELEN 20 /* max length of mailer name */
62 long frmsgs = 0, frbytes = 0, tomsgs = 0, tobytes = 0, rejmsgs = 0;
65 char mtable[MAXMAILERS][MNAMELEN + 1];
66 char sfilebuf[MAXLINE];
68 struct statistics stats;
74 cfile = _PATH_SENDMAILCF;
78 while ((ch = getopt(argc, argv, "C:f:op")) != -1)
101 (void) fputs("usage: mailstats [-C cffile] [-f stfile] [-o] [-p]\n",
112 if ((cfp = fopen(cfile, "r")) == NULL)
115 fprintf(stderr, "mailstats: ");
122 (void) strlcpy(mtable[mno++], "prog", MNAMELEN + 1);
123 (void) strlcpy(mtable[mno++], "*file*", MNAMELEN + 1);
124 (void) strlcpy(mtable[mno++], "*include*", MNAMELEN + 1);
126 while (fgets(buf, sizeof(buf), cfp) != NULL)
135 case 'M': /* mailer definition */
138 case 'O': /* option -- see if .st file */
139 if (strncasecmp(b, " StatusFile", 11) == 0 &&
140 !(isascii(b[11]) && isalnum(b[11])))
142 /* new form -- find value */
146 while (isascii(*++b) && isspace(*b))
149 else if (*b++ != 'S')
151 /* something else boring */
155 /* this is the S or StatusFile option -- save it */
156 if (strlcpy(sfilebuf, b, sizeof sfilebuf) >=
160 "StatusFile filename too long: %.30s...\n",
164 b = strchr(sfilebuf, '#');
166 b = strchr(sfilebuf, '\n');
168 b = &sfilebuf[strlen(sfilebuf)];
169 while (isascii(*--b) && isspace(*b))
179 if (mno >= MAXMAILERS)
182 "Too many mailers defined, %d max.\n",
187 s = m + MNAMELEN; /* is [MNAMELEN + 1] */
188 while (*b != ',' && !(isascii(*b) && isspace(*b)) &&
192 for (i = 0; i < mno; i++)
194 if (strcmp(mtable[i], mtable[mno]) == 0)
201 for (; mno < MAXMAILERS; mno++)
206 fprintf(stderr, "mailstats: no statistics file located\n");
210 fd = open(sfile, O_RDONLY);
211 if ((fd < 0) || (i = read(fd, &stats, sizeof stats)) < 0)
214 (void) fputs("mailstats: ", stderr);
222 if ((i = read(fd, &stats, sizeof stats)) < 0)
225 (void) fputs("mailstats: ", stderr);
232 memset((ARBPTR_T) &stats, '\0', sizeof stats);
233 (void) time(&stats.stat_itime);
238 if (stats.stat_magic != STAT_MAGIC)
241 "mailstats: incorrect magic number in %s\n",
245 else if (stats.stat_version != STAT_VERSION)
248 "mailstats version (%d) incompatible with %s version (%d)\n",
249 STAT_VERSION, sfile, stats.stat_version);
252 else if (i != sizeof stats || stats.stat_size != sizeof(stats))
254 (void) fputs("mailstats: file size changed.\n", stderr);
262 printf("%ld %ld\n", (long) stats.stat_itime, (long) now);
266 printf("Statistics from %s", ctime(&stats.stat_itime));
267 printf(" M msgsfr bytes_from msgsto bytes_to msgsrej msgsdis%s\n",
268 mnames ? " Mailer" : "");
270 for (i = 0; i < MAXMAILERS; i++)
272 if (stats.stat_nf[i] || stats.stat_nt[i] ||
273 stats.stat_nr[i] || stats.stat_nd[i])
278 format = "%2d %8ld %10ld %8ld %10ld %6ld %6ld";
280 format = "%2d %8ld %10ldK %8ld %10ldK %6ld %6ld";
282 stats.stat_nf[i], stats.stat_bf[i],
283 stats.stat_nt[i], stats.stat_bt[i],
284 stats.stat_nr[i], stats.stat_nd[i]);
286 printf(" %s", mtable[i]);
288 frmsgs += stats.stat_nf[i];
289 frbytes += stats.stat_bf[i];
290 tomsgs += stats.stat_nt[i];
291 tobytes += stats.stat_bt[i];
292 rejmsgs += stats.stat_nr[i];
293 dismsgs += stats.stat_nd[i];
298 printf(" T %8ld %10ld %8ld %10ld %6ld %6ld\n",
299 frmsgs, frbytes, tomsgs, tobytes, rejmsgs, dismsgs);
300 printf(" C %8ld %8ld %6ld\n",
301 stats.stat_cf, stats.stat_ct, stats.stat_cr);
303 fd = open(sfile, O_RDWR | O_TRUNC);
309 printf("=============================================================\n");
310 printf(" T %8ld %10ldK %8ld %10ldK %6ld %6ld\n",
311 frmsgs, frbytes, tomsgs, tobytes, rejmsgs, dismsgs);
312 printf(" C %8ld %10s %8ld %10s %6ld\n",
313 stats.stat_cf, "", stats.stat_ct, "", stats.stat_cr);