ze-filter  (ze-filter-0.8.0-develop-180218)
ze-printstats.c
Go to the documentation of this file.
1 /*
2  *
3  * ze-filter - Mail Server Filter for sendmail
4  *
5  * Copyright (c) 2001-2018 - Jose-Marcio Martins da Cruz
6  *
7  * Auteur : Jose Marcio Martins da Cruz
8  * jose.marcio.mc@gmail.org
9  *
10  * Historique :
11  * Creation : janvier 2002
12  *
13  * This program is free software, but with restricted license :
14  *
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
19  *
20  * More details about ze-filter license can be found at ze-filter
21  * web site : http://foss.jose-marcio.org
22  */
23 
24 #include <ze-sys.h>
25 #include <libze.h>
26 
27 #include "ze-filter.h"
28 #include "ze-filter-data.h"
29 
30 extern int ze_logLevel;
31 
32 
33 void usage();
34 
35 
36 
37 /* ****************************************************************************
38  * *
39  * *
40  **************************************************************************** */
41 int
42 main(int argc, char **argv)
43 {
44  const char *args = "hpgatdcfvqxl:m:n:";
45  int c;
46  int jp = 0, jg = 0;
47  int jt = 0;
48  int jq = 0, jx = 0;
49 
50  int verbose = 0;
51  int hostnames = 0;
52  bool newformat = FALSE;
53  uint32_t flags = 0;
54 
55  int nbRecs = 0;
56  int summary_type = H_SUMMARY;
57 
58  char host[256];
59 
60  long dt = 3600;
61 
63 
64  ze_logLevel = 9;
65  memset(host, 0, sizeof (host));
66 
67  while ((c = getopt(argc, argv, args)) != -1)
68  {
69  switch (c)
70  {
71  case 'h':
72  usage();
73  exit(0);
74  break;
75  case 'p':
76  jp = 1;
77  jg = 0;
78  break;
79  case 'g':
80  jp = 0;
81  jg = 1;
82  break;
83  case 'a':
84  jp = jg = 1;
85  break;
86  case 't':
87  jt++;
88  break;
89  case 'c':
90  cf_opt.arg_c = optarg;
91  break;
92  case 'd':
93  hostnames = TRUE;
94  break;
95  case 'v':
96  verbose = 1;
97  break;
98  case 'f':
99  newformat = TRUE;
100  break;
101  case 'q':
102  jq = 1;
103  break;
104  case 'x':
105  jx = 1;
106  break;
107  case 'l':
108  dt = zeStr2time(optarg, NULL, 3600);
109  break;
110  case 'm':
111  if (jq > 0)
112  {
113  if (strcmp("s", optarg) == 0)
114  {
115  summary_type = H_SUMMARY;
116  break;
117  }
118  if (strcmp("e", optarg) == 0)
119  {
120  summary_type = H_EMPTY;
121  break;
122  }
123  if (strcmp("re", optarg) == 0)
124  {
125  summary_type = H_REJ_EMPTY;
126  break;
127  }
128  if (strcmp("rg", optarg) == 0)
129  {
130  summary_type = H_REJ_GREY;
131  break;
132  }
133  if (strcmp("b", optarg) == 0)
134  {
135  summary_type = H_BADRCPT;
136  break;
137  }
138  if (strcmp("rb", optarg) == 0)
139  {
140  summary_type = H_REJ_BADRCPT;
141  break;
142  }
143  if (strcmp("rm", optarg) == 0)
144  {
145  summary_type = H_REJ_BADMX;
146  break;
147  }
148  if (strcmp("ro", optarg) == 0)
149  {
150  summary_type = H_REJ_OPEN;
151  break;
152  }
153  if (strcmp("t", optarg) == 0)
154  {
155  summary_type = H_REJ_THROTTLE;
156  break;
157  }
158  if (strcmp("rt", optarg) == 0)
159  {
160  summary_type = H_REJ_THROTTLE;
161  break;
162  }
163  if (strcmp("r", optarg) == 0)
164  {
165  summary_type = H_RESOLVE;
166  break;
167  }
168  if (strcmp("rr", optarg) == 0)
169  {
170  summary_type = H_REJ_RESOLVE;
171  break;
172  }
173  if (strcmp("c", optarg) == 0)
174  {
175  summary_type = H_REJ_REGEX;
176  break;
177  }
178  if (strcmp("x", optarg) == 0)
179  {
180  summary_type = H_XFILES;
181  break;
182  }
183  if (strcmp("st", optarg) == 0)
184  {
185  summary_type = H_SPAMTRAP;
186  break;
187  }
188  summary_type = H_SUMMARY;
189  break;
190  }
191  if (jt > 0)
192  {
193  flags |= smtprate_str2flags(optarg);
194  break;
195  }
196  break;
197  case 'n':
198  nbRecs = atoi(optarg);
199  break;
200  default:
201  printf("Error ... \n");
202  exit(0);
203  }
204  }
205  if (optind < argc && strlen(argv[optind]) > 0)
206  snprintf(host, sizeof (host), "%s", argv[optind]);
207 
208  if (!(jp || jt || jg || jq || jx))
209  {
210  usage();
211  exit(1);
212  }
213 
215 
216  if (cf_opt.arg_c != NULL)
218 
219  configure("ze-printstats", conf_file, FALSE);
220 
221  if (jp > 0 || jg > 0)
222  {
223  if (dump_state(STDOUT_FILENO, jp, jg, verbose, newformat) > 0)
224  printf("ERROR\n");
225  }
226 
227  if (0)
228  {
229  char dbdir[256];
230  char *dbenv = getenv("DBENV");
231 
232  char *dir;
233 
234  if (dbenv != NULL && STRCASEEQUAL(dbenv, "yes"))
235  {
236  dir = cf_get_str(CF_WDBDIR);
237 
238  memset(dbdir, 0, sizeof (dbdir));
239  if (dir != NULL && strlen(dir) > 0)
240  snprintf(dbdir, sizeof (dbdir), "%s/%s", dir, "db");
241 
242  if (!open_work_db_env(dbdir, ZE_WDBDIR, TRUE))
243  {
244  }
245  }
246  }
247 
249 
250  if (jt > 0)
251  {
252  if (dt == 0)
253  dt = 10 MINUTES;
254  if (flags == 0)
255  {
256  SET_BIT(flags, RATE_CONN);
257  SET_BIT(flags, RATE_RCPT);
258  }
259  smtprate_read_table(NULL);
260  smtprate_update_table(3600);
261  smtprate_print_table(STDOUT_FILENO, jt - 1, verbose, hostnames, dt, flags, nbRecs);
262  }
263 
264  if (jq > 0)
265  {
266  char *p = NULL, *name = NULL;
267  char ip[256];
268 
269  *ip = '\0';
270  if (strlen(host) > 0)
271  {
272  if (get_hostbyname(host, ip, sizeof (ip)))
273  {
274  name = host;
275  p = ip;
276  } else
277  p = host;
278  }
279 
280  printf("%-30s : %s\n", "Version", PACKAGE);
281 
282  (void) raw_history_open(TRUE);
283  res_history_update(NULL, p, 0, dt, verbose);
284  res_history_summary(NULL, p, 0, dt, verbose, hostnames, summary_type, nbRecs);
285  (void) raw_history_close();
286  }
287 
288  if (!(jp || jt || jg || jq || jx))
289  usage();
290 
291  return 0;
292 }
293 
294 /* ****************************************************************************
295  * *
296  * *
297  **************************************************************************** */
298 void
300 {
301  printf("Usage : ze-printstats options\n"
302  " ze-printstats -c conf_file\n"
303  " ze-printstats -a | -p | -g\n"
304  " -p : print running process ze-filter counters\n"
305  " -g : print ze-filter counters from last reset\n"
306  " -a : print both counters\n"
307  " ze-printstats -t[td]\n"
308  " -t : throttle data (summary)\n"
309  " -tt : throttle data (detail)\n"
310  " -d : resolve IP addresses\n"
311  " -m x1,x2,...\n"
312  " all,conn,rcpt,bounce,msgs,vol,svc\n"
313  " -l : period of interest - default value : 10m\n"
314  " period shall be smaller than 20m - default unit : s\n"
315  " -n N : max number of records to print\n"
316  " ze-printstats -q [-l dt [s|m|h|d]] [[-v | ip | hostname] | [-m x]]\n"
317  " -q : query gateway activity\n"
318  " -l : period of interest - default unit : secs\n"
319  " -v : verbose - meaninful only if gateway not specified\n"
320  " prints summary for each client gateway\n"
321  " -m x : select type of summary\n"
322  " x = s : Connection summary\n"
323  " x = e : Client doing empty connections\n"
324  " x = re : Client doing empty connections - reject\n"
325  " x = ro : Too much open connections - reject\n"
326  " x = rt : Connection rate too high - reject\n"
327  " x = rb : Too many bad recipients - reject\n"
328  " x = rg : Clients with messages rejected by greylisting\n"
329  " x = r : Clients with bad DNS resolution\n"
330  " x = rr : Clients with bad DNS resolution - reject\n"
331  " x = c : Clients being rejected by content checking\n"
332  " x = x : Clients sending X-Files or Virus\n"
333  " x = rm : Messages with BAD MX for sender domain\n"
334  " x = st : Clients sending messages to SpamTraps\n"
335  " -n N : max number of records to print\n\n"
336  " %s\n"
337  " %s\n"
338  " Compiled on %s %s\n\n", PACKAGE, COPYRIGHT, __DATE__, __TIME__);
339 }
#define H_REJ_GREY
Definition: ze-history.h:45
#define H_EMPTY
Definition: ze-history.h:32
#define H_XFILES
Definition: ze-history.h:42
int smtprate_update_table(time_t)
Definition: ze-smtprate.c:598
void res_history_summary(History_T *, char *, time_t, time_t, bool, bool, int, int)
Definition: ze-history.c:1044
#define H_REJ_BADMX
Definition: ze-history.h:44
void raw_history_close()
Definition: ze-history.c:296
uint32_t smtprate_str2flags(char *)
Definition: ze-smtprate.c:1150
#define ZE_WDBDIR
Definition: defs.h:35
#define H_REJ_REGEX
Definition: ze-history.h:41
#define H_RESOLVE
Definition: ze-history.h:39
#define COPYRIGHT
Definition: version.h:31
int ze_logLevel
Definition: zeSyslog.c:34
int main(int argc, char **argv)
Definition: ze-printstats.c:42
bool resolve_cache_init(char *dbdir, int rwmode)
bool res_history_update(History_T *, char *, time_t, time_t, bool)
Definition: ze-history.c:631
int dump_state(int, int, int, int, int)
void zeLog_SetOutput(bool, bool)
Definition: zeSyslog.c:490
#define H_SUMMARY
Definition: ze-history.h:31
#define FALSE
Definition: macros.h:160
#define H_SPAMTRAP
Definition: ze-history.h:43
bool open_work_db_env(char *defdir, char *cfdir, bool rdonly)
Definition: ze-databases.c:40
bool raw_history_open(bool)
Definition: ze-history.c:227
char * arg_c
Definition: ze-config.h:47
#define SET_BIT(p, i)
Definition: macros.h:166
#define H_REJ_BADRCPT
Definition: ze-history.h:35
int smtprate_read_table(char *)
Definition: ze-smtprate.c:784
void init_default_file_extensions()
Definition: ze-fileexp.c:195
#define H_REJ_OPEN
Definition: ze-history.h:36
#define H_BADRCPT
Definition: ze-history.h:34
#define H_REJ_EMPTY
Definition: ze-history.h:33
#define TRUE
Definition: macros.h:157
OPT_REC_T cf_opt
Definition: ze-config.c:40
#define RESOLVE_CACHE_RD
char * cf_get_str(int id)
Definition: ze-cf.c:854
#define RATE_RCPT
Definition: ze-smtprate.h:52
char * conf_file
Definition: ze-cf.c:38
#define CF_WDBDIR
Definition: cfh-defs.h:77
int configure(char *, char *, bool)
Definition: ze-cf.c:1203
void usage()
#define PACKAGE
Definition: version.h:28
#define H_REJ_RESOLVE
Definition: ze-history.h:40
#define MINUTES
Definition: macros.h:143
#define STRCASEEQUAL(a, b)
Definition: macros.h:72
bool get_hostbyname(char *name, char *ip, int len)
Definition: ze-inet.c:336
long uint32_t
Definition: ze-sys.h:489
time_t zeStr2time(char *s, int *error, time_t dval)
Definition: zeStrConvert.c:291
#define RATE_CONN
Definition: ze-smtprate.h:51
void smtprate_print_table(int, int, int, int, time_t, uint32_t, int)
#define H_REJ_THROTTLE
Definition: ze-history.h:38