1
2 /* lmtpstats.c -- automatically generated from /home/ellie/fastmail/cyrus-imapd/imap/lmtpstats.snmp by snmpgen */
3
4
5
6 #ifdef USING_SNMPGEN
7
8
9
10 /* We disable this code for now since it doesn't actually work and wastes
11
12 * resources. At some point in time, we'll make it work again as it would
13
14 * be useful to gather aggregate statistics on what commands are being used
15
16 * so we can better tune the server. This change closes bug #1191.
17
18 * New bug 1267 opened to re-enable the feature.
19
20 */
21
22
23
24 #ifdef HAVE_UNISTD_H
25
26 #include <unistd.h>
27
28 #endif
29
30 #include <stdio.h>
31
32 #include <stdlib.h>
33
34 #include <errno.h>
35
36 #include <string.h>
37
38 #include <sys/types.h>
39
40 #include <sys/socket.h>
41
42 #include <sys/un.h>
43
44 #include <fcntl.h>
45
46 #include <stdarg.h>
47
48
49
50 #include "index.h"
51
52 #include "lmtpstats.h"
53
54
55
56 extern int close(int);
57
58
59
60 int variable_value[2+1];
61
62 int variable_tmpvalue[2+1];
63
64
65
varvalue(lmtpstats_variable_t var)66 int varvalue(lmtpstats_variable_t var)
67
68 {
69
70 if (variable_tmpvalue[var]!=-1)
71
72 return variable_tmpvalue[var];
73
74
75
76 return variable_value[var];
77
78 }
79
80
81
snmp_getdescription(lmtpstats_t evt)82 const char *snmp_getdescription(lmtpstats_t evt)
83
84 {
85
86 switch (evt) {
87
88 case SIEVE_KEEP: return "sieve messages kept";
89 case SERVER_UPTIME: return "Amount of time server has been running";
90 case AUTHENTICATION_YES: return "Successful authentication of given mechanism";
91 case mtaReceivedRecipients: return "Recipients accepted";
92 case SIEVE_MESSAGES_PROCESSED: return "Number of messages processed by Sieve scripts";
93 case SIEVE_DISCARD: return "sieve discards";
94 case TOTAL_CONNECTIONS: return "Count of the total number of connections since the beginning of time";
95 case mtaReceivedMessages: return "Messages we've received";
96 case SIEVE_NOTIFY: return "sieve notifications sent";
97 case SIEVE_VACATION_TOTAL: return "vacation messages considered";
98 case mtaTransmittedMessages: return "Messages stored to disk";
99 case SERVER_NAME_VERSION: return "Name and version string for server";
100 case SIEVE_VACATION_REPLIED: return "vacation messages sent";
101 case mtaSuccessfulConvertedMessages: return "Messages converted because of 8bit foo";
102 case SIEVE_REJECT: return "sieve rejects";
103 case SIEVE_FILEINTO: return "sieve fileintos";
104 case SIEVE_REDIRECT: return "sieve redirects";
105 case ACTIVE_CONNECTIONS: return "Count of the active number of connections";
106 case mtaReceivedVolume: return "Kbytes received";
107 case AUTHENTICATION_NO: return "Failed authentication of given mechanism";
108 case mtaTransmittedVolume: return "Kbytes stored to disk";
109
110 }
111
112 return NULL;
113
114 }
115
116
117
snmp_getoid(const char * name,lmtpstats_t evt,char * buf,int buflen)118 const char *snmp_getoid(const char *name __attribute__((unused)),
119
120 lmtpstats_t evt, char *buf, int buflen)
121
122 {
123
124 switch (evt) {
125
126 case SIEVE_KEEP: snprintf(buf,buflen,"1.3.6.1.4.1.3.2.2.3.3.4.5"); return buf;
127 case SERVER_UPTIME: snprintf(buf,buflen,"1.3.6.1.4.1.3.2.2.3.2.3.1"); return buf;
128 case AUTHENTICATION_YES: snprintf(buf,buflen,"1.3.6.1.4.1.3.2.2.3.2.2.%d.0",varvalue(VARIABLE_AUTH)); return buf;
129 case mtaReceivedRecipients: snprintf(buf,buflen,"1.3.6.1.2.1.28.1.%d.7",varvalue(VARIABLE_MTA)); return buf;
130 case SIEVE_MESSAGES_PROCESSED: snprintf(buf,buflen,"1.3.6.1.4.1.3.2.2.3.3.4.0"); return buf;
131 case SIEVE_DISCARD: snprintf(buf,buflen,"1.3.6.1.4.1.3.2.2.3.3.4.2"); return buf;
132 case TOTAL_CONNECTIONS: snprintf(buf,buflen,"1.3.6.1.4.1.3.2.2.3.2.1.1"); return buf;
133 case mtaReceivedMessages: snprintf(buf,buflen,"1.3.6.1.2.1.28.1.%d.1",varvalue(VARIABLE_MTA)); return buf;
134 case SIEVE_NOTIFY: snprintf(buf,buflen,"1.3.6.1.4.1.3.2.2.3.3.4.6"); return buf;
135 case SIEVE_VACATION_TOTAL: snprintf(buf,buflen,"1.3.6.1.4.1.3.2.2.3.3.4.8"); return buf;
136 case mtaTransmittedMessages: snprintf(buf,buflen,"1.3.6.1.2.1.28.1.%d.3",varvalue(VARIABLE_MTA)); return buf;
137 case SERVER_NAME_VERSION: snprintf(buf,buflen,"1.3.6.1.4.1.3.2.2.3.2.3.0"); return buf;
138 case SIEVE_VACATION_REPLIED: snprintf(buf,buflen,"1.3.6.1.4.1.3.2.2.3.3.4.7"); return buf;
139 case mtaSuccessfulConvertedMessages: snprintf(buf,buflen,"1.3.6.1.2.1.28.1.%d.10",varvalue(VARIABLE_MTA)); return buf;
140 case SIEVE_REJECT: snprintf(buf,buflen,"1.3.6.1.4.1.3.2.2.3.3.4.3"); return buf;
141 case SIEVE_FILEINTO: snprintf(buf,buflen,"1.3.6.1.4.1.3.2.2.3.3.4.4"); return buf;
142 case SIEVE_REDIRECT: snprintf(buf,buflen,"1.3.6.1.4.1.3.2.2.3.3.4.1"); return buf;
143 case ACTIVE_CONNECTIONS: snprintf(buf,buflen,"1.3.6.1.4.1.3.2.2.3.2.1.2"); return buf;
144 case mtaReceivedVolume: snprintf(buf,buflen,"1.3.6.1.2.1.28.1.%d.4",varvalue(VARIABLE_MTA)); return buf;
145 case AUTHENTICATION_NO: snprintf(buf,buflen,"1.3.6.1.4.1.3.2.2.3.2.2.%d.1",varvalue(VARIABLE_AUTH)); return buf;
146 case mtaTransmittedVolume: snprintf(buf,buflen,"1.3.6.1.2.1.28.1.%d.6",varvalue(VARIABLE_MTA)); return buf;
147
148 }
149
150 return NULL;
151
152 }
153
154
155
156 #define SOCK_PATH "/tmp/.snmp_door"
157
158
159
160 static int mysock = -1;
161
162 static struct sockaddr_un remote;
163
164 static int sockaddr_len = 0;
165
166
167
snmp_send(char * str)168 static void snmp_send(char *str)
169
170 {
171
172 if (mysock == -1) return;
173
174
175
176 if (sendto(mysock, str, strlen(str), 0, (struct sockaddr *) &remote, sockaddr_len) == -1) {
177
178 return;
179
180 }
181
182
183
184 return;
185
186 }
187
188
189
snmp_connect(void)190 int snmp_connect(void)
191
192 {
193
194 int s;
195
196 int fdflags;
197
198 int lup;
199
200
201
202 if ((s = socket(AF_UNIX, SOCK_DGRAM, 0)) == -1) {
203
204 return 1;
205
206 }
207
208
209
210 for (lup=0;lup < 2+1; lup++)
211
212 variable_tmpvalue[lup] = -1;
213
214
215
216 remote.sun_family = AF_UNIX;
217
218 strlcpy(remote.sun_path, SOCK_PATH, sizeof(remote.sun_path));
219
220 sockaddr_len = strlen(remote.sun_path) + sizeof(remote.sun_family);
221
222
223
224 /* put us in non-blocking mode */
225
226 fdflags = fcntl(s, F_GETFD, 0);
227
228 if (fdflags != -1) fdflags = fcntl(s, F_SETFL, O_NONBLOCK | fdflags);
229
230 if (fdflags == -1) { close(s); return -1; }
231
232
233
234 mysock = s;
235
236 snmp_send("R 1.3.6.1.4.1.3.2.2.3.2.1\n");
237 snmp_send("R 1.3.6.1.4.1.3.2.2.3.2.2\n");
238 snmp_send("R 1.3.6.1.4.1.3.2.2.3.2.3\n");
239 snmp_send("R 1.3.6.1.2.1.28.1\n");
240 snmp_send("R 1.3.6.1.4.1.3.2.2.3.3.4\n");
241
242 return 0;
243
244 }
245
246
247
snmp_close(void)248 int snmp_close(void)
249
250 {
251
252 if (mysock > -1)
253
254 close(mysock);
255
256
257
258 return 0;
259
260 }
261
262
263
snmp_increment_args(lmtpstats_t cmd,int incr,...)264 int snmp_increment_args(lmtpstats_t cmd, int incr, ...)
265
266 {
267
268 char tosend[256]; /* xxx UDP max size??? */
269
270 char buf[256];
271
272
273
274 va_list ap; /* varargs thing */
275
276 lmtpstats_variable_t vval;
277
278 int ival;
279
280
281
282 if (mysock == -1) return 1;
283
284
285
286 va_start(ap, incr);
287
288
289
290 do {
291
292 vval = va_arg(ap, lmtpstats_variable_t); /* get the next arg */
293
294
295
296 if (vval!=VARIABLE_LISTEND)
297
298 {
299
300 ival = va_arg(ap, int); /* get the next arg */
301
302 variable_tmpvalue[vval] = ival;
303
304 }
305
306
307
308 } while ( vval != VARIABLE_LISTEND);
309
310
311
312 va_end(ap);
313
314
315
316 snprintf(tosend, sizeof(tosend),"C %s %d\n",snmp_getoid(NULL,cmd,buf,sizeof(buf)), incr);
317
318
319
320 if (sendto(mysock, tosend, strlen(tosend), 0, (struct sockaddr *) &remote, sockaddr_len) == -1) {
321
322 return 1;
323
324 }
325
326
327
328 /* set tmp variables back */
329
330 va_start(ap, incr);
331
332
333
334 do {
335
336 vval = va_arg(ap, lmtpstats_variable_t); /* get the next arg */
337
338
339
340 if (vval!=VARIABLE_LISTEND)
341
342 {
343
344 ival = va_arg(ap, int); /* get the next arg */
345
346 variable_tmpvalue[vval] = -1;
347
348 }
349
350
351
352 } while ( vval != VARIABLE_LISTEND);
353
354
355
356 va_end(ap);
357
358
359
360 return 0;
361
362 }
363
364
365
snmp_increment(lmtpstats_t cmd,int incr)366 int snmp_increment(lmtpstats_t cmd, int incr)
367
368 {
369
370 char tosend[256]; /* xxx UDP max size??? */
371
372 char buf[256];
373
374
375
376 if (mysock == -1) return 1;
377
378
379
380 snprintf(tosend, sizeof(tosend),"C %s %d\n",snmp_getoid(NULL,cmd,buf,sizeof(buf)), incr);
381
382
383
384 if (sendto(mysock, tosend, strlen(tosend), 0, (struct sockaddr *) &remote, sockaddr_len) == -1) {
385
386 return 1;
387
388 }
389
390
391
392 return 0;
393
394 }
395
396
397
snmp_set(lmtpstats_t cmd,int value)398 int snmp_set(lmtpstats_t cmd, int value)
399
400 {
401
402 char tosend[256];
403
404 char buf[256];
405
406
407
408 if (mysock == -1) return 1;
409
410
411
412 snprintf(tosend, sizeof(tosend),"I %s %d\n",snmp_getoid(NULL,cmd,buf,sizeof(buf)), value);
413
414
415
416 if (sendto(mysock, tosend, strlen(tosend), 0, (struct sockaddr *) &remote, sockaddr_len) == -1) {
417
418 return 1;
419
420 }
421
422
423
424 return 1;
425
426 }
427
428
429
snmp_set_str(lmtpstats_t cmd,char * value)430 int snmp_set_str(lmtpstats_t cmd, char *value)
431
432 {
433
434 char tosend[256];
435
436 char buf[256];
437
438
439
440 if (mysock == -1) return 1;
441
442
443
444 snprintf(tosend, sizeof(tosend),"S %s %s\n",snmp_getoid(NULL,cmd,buf,sizeof(buf)), value);
445
446
447
448 if (sendto(mysock, tosend, strlen(tosend), 0, (struct sockaddr *) &remote, sockaddr_len) == -1) {
449
450 return 1;
451
452 }
453
454
455
456 return 1;
457
458 }
459
460
461
snmp_set_time(lmtpstats_t cmd,time_t t)462 int snmp_set_time(lmtpstats_t cmd, time_t t)
463
464 {
465
466 char tosend[256];
467
468 char buf[256];
469
470
471
472 if (mysock == -1) return 1;
473
474
475
476 snprintf(tosend, sizeof(tosend),"T %s %lu\n",snmp_getoid(NULL,cmd,buf,sizeof(buf)), t);
477
478
479
480 if (sendto(mysock, tosend, strlen(tosend), 0, (struct sockaddr *) &remote, sockaddr_len) == -1) {
481
482 return 1;
483
484 }
485
486
487
488 return 1;
489
490 }
491
492
493
494 /* should use SNMPDEFINE's as parameter */
495
snmp_set_oid(lmtpstats_t cmd,char * str)496 int snmp_set_oid(lmtpstats_t cmd, char *str)
497
498 {
499
500 return snmp_set_str(cmd,str);
501
502 }
503
504
505
snmp_setvariable(lmtpstats_variable_t name,int value)506 void snmp_setvariable(lmtpstats_variable_t name, int value)
507
508 {
509
510 variable_value[name] = value;
511
512 }
513
514
515
516 #endif
517
518
519
520
521
522