2 * Copyright (c) 1998-2002 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.
18 "@(#) Copyright (c) 1998-2001 Sendmail, Inc. and its suppliers.\n\
19 All rights reserved.\n\
20 Copyright (c) 1988, 1993\n\
21 The Regents of the University of California. All rights reserved.\n")
23 SM_IDSTR(id, "@(#)$Id: mailstats.c,v 8.98 2002/05/24 23:10:15 gshapiro Exp $")
32 # undef EX_OK /* unistd.h may have another use for this */
36 #include <sm/errstring.h>
37 #include <sm/limits.h>
38 #include <sendmail/sendmail.h>
39 #include <sendmail/mailstats.h>
40 #include <sendmail/pathnames.h>
43 #define MNAMELEN 20 /* max length of mailer name */
60 long frmsgs = 0, frbytes = 0, tomsgs = 0, tobytes = 0, rejmsgs = 0;
64 #endif /* _FFR_QUARANTINE */
66 char mtable[MAXMAILERS][MNAMELEN + 1];
67 char sfilebuf[MAXPATHLEN];
69 struct statistics stats;
74 cfile = getcfname(0, 0, SM_GET_SENDMAIL_CF, NULL);
79 while ((ch = getopt(argc, argv, "cC:f:opP")) != -1)
84 cfile = getcfname(0, 0, SM_GET_SUBMIT_CF, NULL);
110 (void) sm_io_fputs(smioerr, SM_TIME_DEFAULT,
111 "usage: mailstats [-C cffile] [-c] [-P] [-f stfile] [-o] [-p]\n");
121 if ((cfp = sm_io_open(SmFtStdio, SM_TIME_DEFAULT, cfile, SM_IO_RDONLY,
125 (void) sm_io_fprintf(smioerr, SM_TIME_DEFAULT, "mailstats: ");
132 (void) sm_strlcpy(mtable[mno++], "prog", MNAMELEN + 1);
133 (void) sm_strlcpy(mtable[mno++], "*file*", MNAMELEN + 1);
134 (void) sm_strlcpy(mtable[mno++], "*include*", MNAMELEN + 1);
136 while (sm_io_fgets(cfp, SM_TIME_DEFAULT, buf, sizeof(buf)) != NULL)
142 b = strchr(buf, '#');
144 b = strchr(buf, '\n');
146 b = &buf[strlen(buf)];
147 while (isascii(*--b) && isspace(*b))
154 case 'M': /* mailer definition */
157 case 'O': /* option -- see if .st file */
158 if (sm_strncasecmp(b, " StatusFile", 11) == 0 &&
159 !(isascii(b[11]) && isalnum(b[11])))
161 /* new form -- find value */
165 while (isascii(*++b) && isspace(*b))
168 else if (*b++ != 'S')
170 /* something else boring */
174 /* this is the S or StatusFile option -- save it */
175 if (sm_strlcpy(sfilebuf, b, sizeof sfilebuf) >=
178 (void) sm_io_fprintf(smioerr, SM_TIME_DEFAULT,
179 "StatusFile filename too long: %.30s...\n",
190 if (mno >= MAXMAILERS)
192 (void) sm_io_fprintf(smioerr, SM_TIME_DEFAULT,
193 "Too many mailers defined, %d max.\n",
198 s = m + MNAMELEN; /* is [MNAMELEN + 1] */
199 while (*b != ',' && !(isascii(*b) && isspace(*b)) &&
203 for (i = 0; i < mno; i++)
205 if (strcmp(mtable[i], mtable[mno]) == 0)
211 (void) sm_io_close(cfp, SM_TIME_DEFAULT);
212 for (; mno < MAXMAILERS; mno++)
213 mtable[mno][0] = '\0';
217 (void) sm_io_fprintf(smioerr, SM_TIME_DEFAULT,
218 "mailstats: no statistics file located\n");
222 fd = open(sfile, O_RDONLY, 0600);
223 if ((fd < 0) || (i = read(fd, &stats, sizeof stats)) < 0)
226 (void) sm_io_fputs(smioerr, SM_TIME_DEFAULT, "mailstats: ");
234 if ((i = read(fd, &stats, sizeof stats)) < 0)
237 (void) sm_io_fputs(smioerr, SM_TIME_DEFAULT,
245 memset((ARBPTR_T) &stats, '\0', sizeof stats);
246 (void) time(&stats.stat_itime);
251 if (stats.stat_magic != STAT_MAGIC)
253 (void) sm_io_fprintf(smioerr, SM_TIME_DEFAULT,
254 "mailstats: incorrect magic number in %s\n",
258 else if (stats.stat_version != STAT_VERSION)
260 (void) sm_io_fprintf(smioerr, SM_TIME_DEFAULT,
261 "mailstats version (%d) incompatible with %s version (%d)\n",
267 else if (i != sizeof stats || stats.stat_size != sizeof(stats))
269 (void) sm_io_fputs(smioerr, SM_TIME_DEFAULT,
270 "mailstats: file size changed.\n");
278 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT, "%ld %ld\n",
279 (long) stats.stat_itime, (long) now);
283 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
284 "Statistics from %s",
285 ctime(&stats.stat_itime));
286 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
287 " M msgsfr bytes_from msgsto bytes_to msgsrej msgsdis");
289 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT, " msgsqur");
290 #endif /* _FFR_QUARANTINE */
291 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT, "%s\n",
292 mnames ? " Mailer" : "");
294 for (i = 0; i < MAXMAILERS; i++)
296 if (stats.stat_nf[i] || stats.stat_nt[i] ||
299 #endif /* _FFR_QUARANTINE */
300 stats.stat_nr[i] || stats.stat_nd[i])
305 format = "%2d %8ld %10ld %8ld %10ld %6ld %6ld";
307 format = "%2d %8ld %10ldK %8ld %10ldK %6ld %6ld";
308 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
317 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
318 " %6ld", stats.stat_nq[i]);
319 #endif /* _FFR_QUARANTINE */
321 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
324 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT, "\n");
325 frmsgs += stats.stat_nf[i];
326 frbytes += stats.stat_bf[i];
327 tomsgs += stats.stat_nt[i];
328 tobytes += stats.stat_bt[i];
329 rejmsgs += stats.stat_nr[i];
330 dismsgs += stats.stat_nd[i];
332 quarmsgs += stats.stat_nq[i];
333 #endif /* _FFR_QUARANTINE */
338 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
339 " T %8ld %10ld %8ld %10ld %6ld %6ld",
340 frmsgs, frbytes, tomsgs, tobytes, rejmsgs,
343 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
345 #endif /* _FFR_QUARANTINE */
346 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT, "\n");
347 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
348 " C %8ld %8ld %6ld\n",
349 stats.stat_cf, stats.stat_ct,
354 fd = open(sfile, O_RDWR | O_TRUNC, 0600);
361 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
362 "=============================================================");
364 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT, "========");
365 #endif /* _FFR_QUARANTINE */
366 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT, "\n");
367 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
368 " T %8ld %10ldK %8ld %10ldK %6ld %6ld",
369 frmsgs, frbytes, tomsgs, tobytes, rejmsgs,
372 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
374 #endif /* _FFR_QUARANTINE */
375 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT, "\n");
376 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
377 " C %8ld %10s %8ld %10s %6ld\n",
378 stats.stat_cf, "", stats.stat_ct, "",