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-2002 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.100 2002/06/27 23:24:06 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 char mtable
[MAXMAILERS
][MNAMELEN
+ 1];
65 char sfilebuf
[MAXPATHLEN
];
67 struct statistics stats
;
72 cfile
= getcfname(0, 0, SM_GET_SENDMAIL_CF
, NULL
);
77 while ((ch
= getopt(argc
, argv
, "cC:f:opP")) != -1)
82 cfile
= getcfname(0, 0, SM_GET_SUBMIT_CF
, NULL
);
108 (void) sm_io_fputs(smioerr
, SM_TIME_DEFAULT
,
109 "usage: mailstats [-C cffile] [-c] [-P] [-f stfile] [-o] [-p]\n");
119 if ((cfp
= sm_io_open(SmFtStdio
, SM_TIME_DEFAULT
, cfile
, SM_IO_RDONLY
,
123 (void) sm_io_fprintf(smioerr
, SM_TIME_DEFAULT
, "mailstats: ");
130 (void) sm_strlcpy(mtable
[mno
++], "prog", MNAMELEN
+ 1);
131 (void) sm_strlcpy(mtable
[mno
++], "*file*", MNAMELEN
+ 1);
132 (void) sm_strlcpy(mtable
[mno
++], "*include*", MNAMELEN
+ 1);
134 while (sm_io_fgets(cfp
, SM_TIME_DEFAULT
, buf
, sizeof(buf
)) != NULL
)
140 b
= strchr(buf
, '#');
142 b
= strchr(buf
, '\n');
144 b
= &buf
[strlen(buf
)];
145 while (isascii(*--b
) && isspace(*b
))
152 case 'M': /* mailer definition */
155 case 'O': /* option -- see if .st file */
156 if (sm_strncasecmp(b
, " StatusFile", 11) == 0 &&
157 !(isascii(b
[11]) && isalnum(b
[11])))
159 /* new form -- find value */
163 while (isascii(*++b
) && isspace(*b
))
166 else if (*b
++ != 'S')
168 /* something else boring */
172 /* this is the S or StatusFile option -- save it */
173 if (sm_strlcpy(sfilebuf
, b
, sizeof sfilebuf
) >=
176 (void) sm_io_fprintf(smioerr
, SM_TIME_DEFAULT
,
177 "StatusFile filename too long: %.30s...\n",
188 if (mno
>= MAXMAILERS
)
190 (void) sm_io_fprintf(smioerr
, SM_TIME_DEFAULT
,
191 "Too many mailers defined, %d max.\n",
196 s
= m
+ MNAMELEN
; /* is [MNAMELEN + 1] */
197 while (*b
!= ',' && !(isascii(*b
) && isspace(*b
)) &&
201 for (i
= 0; i
< mno
; i
++)
203 if (strcmp(mtable
[i
], mtable
[mno
]) == 0)
209 (void) sm_io_close(cfp
, SM_TIME_DEFAULT
);
210 for (; mno
< MAXMAILERS
; mno
++)
211 mtable
[mno
][0] = '\0';
215 (void) sm_io_fprintf(smioerr
, SM_TIME_DEFAULT
,
216 "mailstats: no statistics file located\n");
220 fd
= open(sfile
, O_RDONLY
, 0600);
221 if ((fd
< 0) || (i
= read(fd
, &stats
, sizeof stats
)) < 0)
224 (void) sm_io_fputs(smioerr
, SM_TIME_DEFAULT
, "mailstats: ");
232 if ((i
= read(fd
, &stats
, sizeof stats
)) < 0)
235 (void) sm_io_fputs(smioerr
, SM_TIME_DEFAULT
,
243 memset((ARBPTR_T
) &stats
, '\0', sizeof stats
);
244 (void) time(&stats
.stat_itime
);
249 if (stats
.stat_magic
!= STAT_MAGIC
)
251 (void) sm_io_fprintf(smioerr
, SM_TIME_DEFAULT
,
252 "mailstats: incorrect magic number in %s\n",
256 else if (stats
.stat_version
!= STAT_VERSION
)
258 (void) sm_io_fprintf(smioerr
, SM_TIME_DEFAULT
,
259 "mailstats version (%d) incompatible with %s version (%d)\n",
265 else if (i
!= sizeof stats
|| stats
.stat_size
!= sizeof(stats
))
267 (void) sm_io_fputs(smioerr
, SM_TIME_DEFAULT
,
268 "mailstats: file size changed.\n");
276 (void) sm_io_fprintf(smioout
, SM_TIME_DEFAULT
, "%ld %ld\n",
277 (long) stats
.stat_itime
, (long) now
);
281 (void) sm_io_fprintf(smioout
, SM_TIME_DEFAULT
,
282 "Statistics from %s",
283 ctime(&stats
.stat_itime
));
284 (void) sm_io_fprintf(smioout
, SM_TIME_DEFAULT
,
285 " M msgsfr bytes_from msgsto bytes_to msgsrej msgsdis");
286 (void) sm_io_fprintf(smioout
, SM_TIME_DEFAULT
, " msgsqur");
287 (void) sm_io_fprintf(smioout
, SM_TIME_DEFAULT
, "%s\n",
288 mnames
? " Mailer" : "");
290 for (i
= 0; i
< MAXMAILERS
; i
++)
292 if (stats
.stat_nf
[i
] || stats
.stat_nt
[i
] ||
294 stats
.stat_nr
[i
] || stats
.stat_nd
[i
])
299 format
= "%2d %8ld %10ld %8ld %10ld %6ld %6ld";
301 format
= "%2d %8ld %10ldK %8ld %10ldK %6ld %6ld";
302 (void) sm_io_fprintf(smioout
, SM_TIME_DEFAULT
,
310 (void) sm_io_fprintf(smioout
, SM_TIME_DEFAULT
,
311 " %6ld", stats
.stat_nq
[i
]);
313 (void) sm_io_fprintf(smioout
, SM_TIME_DEFAULT
,
316 (void) sm_io_fprintf(smioout
, SM_TIME_DEFAULT
, "\n");
317 frmsgs
+= stats
.stat_nf
[i
];
318 frbytes
+= stats
.stat_bf
[i
];
319 tomsgs
+= stats
.stat_nt
[i
];
320 tobytes
+= stats
.stat_bt
[i
];
321 rejmsgs
+= stats
.stat_nr
[i
];
322 dismsgs
+= stats
.stat_nd
[i
];
323 quarmsgs
+= stats
.stat_nq
[i
];
328 (void) sm_io_fprintf(smioout
, SM_TIME_DEFAULT
,
329 " T %8ld %10ld %8ld %10ld %6ld %6ld",
330 frmsgs
, frbytes
, tomsgs
, tobytes
, rejmsgs
,
332 (void) sm_io_fprintf(smioout
, SM_TIME_DEFAULT
,
334 (void) sm_io_fprintf(smioout
, SM_TIME_DEFAULT
, "\n");
335 (void) sm_io_fprintf(smioout
, SM_TIME_DEFAULT
,
336 " C %8ld %8ld %6ld\n",
337 stats
.stat_cf
, stats
.stat_ct
,
342 fd
= open(sfile
, O_RDWR
| O_TRUNC
, 0600);
349 (void) sm_io_fprintf(smioout
, SM_TIME_DEFAULT
,
350 "=============================================================");
351 (void) sm_io_fprintf(smioout
, SM_TIME_DEFAULT
, "========");
352 (void) sm_io_fprintf(smioout
, SM_TIME_DEFAULT
, "\n");
353 (void) sm_io_fprintf(smioout
, SM_TIME_DEFAULT
,
354 " T %8ld %10ldK %8ld %10ldK %6ld %6ld",
355 frmsgs
, frbytes
, tomsgs
, tobytes
, rejmsgs
,
357 (void) sm_io_fprintf(smioout
, SM_TIME_DEFAULT
,
359 (void) sm_io_fprintf(smioout
, SM_TIME_DEFAULT
, "\n");
360 (void) sm_io_fprintf(smioout
, SM_TIME_DEFAULT
,
361 " C %8ld %10s %8ld %10s %6ld\n",
362 stats
.stat_cf
, "", stats
.stat_ct
, "",