root/src/modules/snmp.c

Revision d659a9d1a9285c0627fb0b3114abefade0b0730b, 41.0 kB (checked in by Theo Schlossnagle <jesus@omniti.com>, 3 years ago)

add the version to reporting

  • Property mode set to 100644
Line 
1 /*
2  * Copyright (c) 2007, OmniTI Computer Consulting, Inc.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are
7  * met:
8  *
9  *     * Redistributions of source code must retain the above copyright
10  *       notice, this list of conditions and the following disclaimer.
11  *     * Redistributions in binary form must reproduce the above
12  *       copyright notice, this list of conditions and the following
13  *       disclaimer in the documentation and/or other materials provided
14  *       with the distribution.
15  *     * Neither the name OmniTI Computer Consulting, Inc. nor the names
16  *       of its contributors may be used to endorse or promote products
17  *       derived from this software without specific prior written
18  *       permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32
33 #include "noit_defines.h"
34
35 #include <stdio.h>
36 #include <unistd.h>
37 #include <errno.h>
38 #include <assert.h>
39 #include <math.h>
40 #include <ctype.h>
41
42 #include <net-snmp/net-snmp-config.h>
43 #include <net-snmp/net-snmp-includes.h>
44
45 #include "noit_module.h"
46 #include "noit_check.h"
47 #include "noit_check_tools.h"
48 #include "utils/noit_log.h"
49 #include "utils/noit_hash.h"
50
51 static noit_log_stream_t nlerr = NULL;
52 static noit_log_stream_t nldeb = NULL;
53 static int __snmp_initialize_once = 0;
54
55 #define SNMPV2_TRAPS_PREFIX     SNMP_OID_SNMPMODULES,1,1,5
56 oid trap_prefix[]    = { SNMPV2_TRAPS_PREFIX };
57 oid cold_start_oid[] = { SNMPV2_TRAPS_PREFIX, 1 };  /* SNMPv2-MIB */
58 oid warm_start_oid[] = { SNMPV2_TRAPS_PREFIX, 2 };  /* SNMPv2-MIB */
59 oid link_down_oid[]  = { SNMPV2_TRAPS_PREFIX, 3 };  /* IF-MIB */
60 oid link_up_oid[]    = { SNMPV2_TRAPS_PREFIX, 4 };  /* IF-MIB */
61 oid auth_fail_oid[]  = { SNMPV2_TRAPS_PREFIX, 5 };  /* SNMPv2-MIB */
62 oid egp_xxx_oid[]    = { SNMPV2_TRAPS_PREFIX, 99 }; /* ??? */
63
64 #define SNMPV2_TRAP_OBJS_PREFIX SNMP_OID_SNMPMODULES,1,1,4
65 oid snmptrap_oid[] = { SNMPV2_TRAP_OBJS_PREFIX, 1, 0 };
66 size_t snmptrap_oid_len = OID_LENGTH(snmptrap_oid);
67 oid snmptrapenterprise_oid[] = { SNMPV2_TRAP_OBJS_PREFIX, 3, 0 };
68 size_t snmptrapenterprise_oid_len = OID_LENGTH(snmptrapenterprise_oid);
69 oid sysuptime_oid[] = { SNMP_OID_MIB2, 1, 3, 0 };
70 size_t sysuptime_oid_len = OID_LENGTH(sysuptime_oid);
71
72 #define SNMPV2_COMM_OBJS_PREFIX SNMP_OID_SNMPMODULES,18,1
73 oid agentaddr_oid[] = { SNMPV2_COMM_OBJS_PREFIX, 3, 0 };
74 size_t agentaddr_oid_len = OID_LENGTH(agentaddr_oid);
75 oid community_oid[] = { SNMPV2_COMM_OBJS_PREFIX, 4, 0 };
76 size_t community_oid_len = OID_LENGTH(community_oid);
77
78 #define RECONNOITER_PREFIX     SNMP_OID_ENTERPRISES,32863,1
79 oid reconnoiter_oid[] = { RECONNOITER_PREFIX };
80 size_t reconnoiter_oid_len = OID_LENGTH(reconnoiter_oid);
81 oid reconnoiter_check_prefix_oid[] = { RECONNOITER_PREFIX,1,1 };
82 size_t reconnoiter_check_prefix_oid_len =
83   OID_LENGTH(reconnoiter_check_prefix_oid);
84 size_t reconnoiter_check_oid_len = OID_LENGTH(reconnoiter_check_prefix_oid) + 8;
85 oid reconnoiter_metric_prefix_oid[] = { RECONNOITER_PREFIX,1,2 };
86 size_t reconnoiter_metric_prefix_oid_len =
87   OID_LENGTH(reconnoiter_metric_prefix_oid);
88
89 oid reconnoiter_check_status_oid[] = { RECONNOITER_PREFIX,1,3};
90 size_t reconnoiter_check_status_oid_len =
91   OID_LENGTH(reconnoiter_check_status_oid);
92 oid reconnoiter_check_state_oid[] = { RECONNOITER_PREFIX,1,3,1};
93 size_t reconnoiter_check_state_oid_len =
94   OID_LENGTH(reconnoiter_check_state_oid);
95 oid reconnoiter_check_state_unknown_oid[] = { RECONNOITER_PREFIX,1,3,1,0};
96 oid reconnoiter_check_state_good_oid[] = { RECONNOITER_PREFIX,1,3,1,1};
97 oid reconnoiter_check_state_bad_oid[] = { RECONNOITER_PREFIX,1,3,1,2};
98 size_t reconnoiter_check_state_val_len =
99   OID_LENGTH(reconnoiter_check_state_unknown_oid);
100 /* Boolean */
101 oid reconnoiter_check_available_oid[] = { RECONNOITER_PREFIX,1,3,2};
102 size_t reconnoiter_check_available_oid_len =
103   OID_LENGTH(reconnoiter_check_available_oid);
104 oid reconnoiter_check_available_unknown_oid[] = { RECONNOITER_PREFIX,1,3,2,0};
105 oid reconnoiter_check_available_yes_oid[] = { RECONNOITER_PREFIX,1,3,2,1};
106 oid reconnoiter_check_available_no_oid[] = { RECONNOITER_PREFIX,1,3,2,2};
107 size_t reconnoiter_check_available_val_len =
108   OID_LENGTH(reconnoiter_check_available_unknown_oid);
109 /* timeticks? gauge/unsigned? */
110 oid reconnoiter_check_duration_oid[] = { RECONNOITER_PREFIX,1,3,3};
111 size_t reconnoiter_check_duration_oid_len =
112   OID_LENGTH(reconnoiter_check_duration_oid);
113 /* string */
114 oid reconnoiter_check_status_msg_oid[] = { RECONNOITER_PREFIX,1,3,4};
115 size_t reconnoiter_check_status_msg_oid_len =
116   OID_LENGTH(reconnoiter_check_status_msg_oid);
117
118 typedef struct _mod_config {
119   noit_hash_table *options;
120   noit_hash_table target_sessions;
121 } snmp_mod_config_t;
122
123 struct target_session {
124   void *sess_handle;
125   noit_module_t *self;
126   char *key;
127   char *target;
128   eventer_t timeoutevent;
129   int version;
130   int fd;
131   int in_table;
132   int refcnt;
133   struct timeval last_open;
134 };
135
136 struct snmp_check_closure {
137   noit_module_t *self;
138   noit_check_t *check;
139 };
140
141 struct check_info {
142   int reqid;
143   int timedout;
144   struct {
145      char *confname;
146      char *oidname;
147      oid oid[MAX_OID_LEN];
148      size_t oidlen;
149      metric_type_t type_override;
150      noit_boolean type_should_override;
151   } *oids;
152   int noids;
153   eventer_t timeoutevent;
154   noit_module_t *self;
155   noit_check_t *check;
156   struct target_session *ts;
157   int version;
158 };
159
160 /* We hold struct check_info's in there key's by their reqid.
161  *   If they timeout, we remove them.
162  *
163  *   When SNMP queries complete, we look them up, if we find them
164  *   then we know we can remove the timeout and  complete the check.
165  *   If we don't find them, the timeout fired and removed the check.
166  */
167 noit_hash_table active_checks = NOIT_HASH_EMPTY;
168 static void add_check(struct check_info *c) {
169   noit_hash_store(&active_checks, (char *)&c->reqid, sizeof(c->reqid), c);
170 }
171 static struct check_info *get_check(int reqid) {
172   void *vc;
173   if(noit_hash_retrieve(&active_checks, (char *)&reqid, sizeof(reqid), &vc))
174     return (struct check_info *)vc;
175   return NULL;
176 }
177 static void remove_check(struct check_info *c) {
178   noit_hash_delete(&active_checks, (char *)&c->reqid, sizeof(c->reqid),
179                    NULL, NULL);
180 }
181
182 struct target_session *
183 _get_target_session(noit_module_t *self, char *target, int version) {
184   char key[128];
185   void *vts;
186   struct target_session *ts;
187   snmp_mod_config_t *conf;
188   conf = noit_module_get_userdata(self);
189   snprintf(key, sizeof(key), "%s:v%d", target, version);
190   if(!noit_hash_retrieve(&conf->target_sessions,
191                          key, strlen(key), &vts)) {
192     ts = calloc(1, sizeof(*ts));
193     ts->self = self;
194     ts->version = version;
195     ts->fd = -1;
196     ts->refcnt = 0;
197     ts->target = strdup(target);
198     ts->key = strdup(key);
199     ts->in_table = 1;
200     noit_hash_store(&conf->target_sessions,
201                     ts->key, strlen(ts->key), ts);
202     vts = ts;
203   }
204   return (struct target_session *)vts;
205 }
206
207 /* Handling of results */
208 static void noit_snmp_log_results(noit_module_t *self, noit_check_t *check,
209                                   struct snmp_pdu *pdu) {
210   struct check_info *info = check->closure;
211   struct variable_list *vars;
212   struct timeval duration;
213   char buff[128];
214   stats_t current;
215   int nresults = 0;
216
217   noit_check_stats_clear(&current);
218
219   if(pdu)
220     for(vars = pdu->variables; vars; vars = vars->next_variable)
221       nresults++;
222
223   gettimeofday(&current.whence, NULL);
224   sub_timeval(current.whence, check->last_fire_time, &duration);
225   current.duration = duration.tv_sec * 1000 + duration.tv_usec / 1000;
226   current.available = pdu ? NP_AVAILABLE : NP_UNAVAILABLE;
227   current.state = (nresults == info->noids) ? NP_GOOD : NP_BAD;
228   snprintf(buff, sizeof(buff), "%d/%d gets", nresults, info->noids);
229   current.status = buff;
230
231   /* We have no results over which to iterate. */
232   if(!pdu) {
233     noit_check_set_stats(self, check, &current);
234     return;
235   }
236
237   /* manipulate the information ourselves */
238   nresults = 0;
239   for(vars = pdu->variables; vars; vars = vars->next_variable) {
240     char *sp;
241     int oid_idx;
242     double float_conv;
243     u_int64_t u64;
244     int64_t i64;
245     char *endptr;
246     char varbuff[256];
247
248     /* find the oid to which this is the response */
249     oid_idx = nresults; /* our current idx is the most likely */
250     if(info->oids[oid_idx].oidlen != vars->name_length ||
251        memcmp(info->oids[oid_idx].oid, vars->name,
252               vars->name_length * sizeof(oid))) {
253       /* Not the most obvious guess */
254       for(oid_idx = info->noids - 1; oid_idx >= 0; oid_idx--) {
255         if(info->oids[oid_idx].oidlen == vars->name_length &&
256            memcmp(info->oids[oid_idx].oid, vars->name,
257                   vars->name_length * sizeof(oid))) break;
258       }
259     }
260     if(oid_idx < 0) {
261       snprint_variable(varbuff, sizeof(varbuff),
262                        vars->name, vars->name_length, vars);
263       noitL(nlerr, "Unexpected oid results to %s`%s`%s: %s\n",
264             check->target, check->module, check->name, varbuff);
265       nresults++;
266       continue;
267     }
268
269 #define SETM(a,b) noit_stats_set_metric(&current, \
270                                         info->oids[oid_idx].confname, a, b)
271     if(info->oids[oid_idx].type_should_override) {
272       snprint_value(varbuff, sizeof(varbuff), vars->name, vars->name_length, vars);
273       sp = strchr(varbuff, ' ');
274       if(sp) sp++;
275       noit_stats_set_metric_coerce(&current, info->oids[oid_idx].confname,
276                                    info->oids[oid_idx].type_override,
277                                    sp);
278     }
279     else {
280       switch(vars->type) {
281         case ASN_OCTET_STR:
282           sp = malloc(1 + vars->val_len);
283           memcpy(sp, vars->val.string, vars->val_len);
284           sp[vars->val_len] = '\0';
285           SETM(METRIC_STRING, sp);
286           free(sp);
287           break;
288         case ASN_INTEGER:
289         case ASN_GAUGE:
290           SETM(METRIC_INT32, vars->val.integer);
291           break;
292         case ASN_TIMETICKS:
293         case ASN_COUNTER:
294           SETM(METRIC_UINT32, vars->val.integer);
295           break;
296         case ASN_INTEGER64:
297           printI64(varbuff, vars->val.counter64);
298           i64 = strtoll(varbuff, &endptr, 10);
299           SETM(METRIC_INT64, (varbuff == endptr) ? NULL : &i64);
300           break;
301         case ASN_COUNTER64:
302           printU64(varbuff, vars->val.counter64);
303           u64 = strtoull(varbuff, &endptr, 10);
304           SETM(METRIC_UINT64, (varbuff == endptr) ? NULL : &u64);
305           break;
306         case ASN_FLOAT:
307           if(vars->val.floatVal) float_conv = *(vars->val.floatVal);
308           SETM(METRIC_DOUBLE, vars->val.floatVal ? &float_conv : NULL);
309           break;
310         case ASN_DOUBLE:
311           SETM(METRIC_DOUBLE, vars->val.doubleVal);
312           break;
313         case SNMP_NOSUCHOBJECT:
314         case SNMP_NOSUCHINSTANCE:
315           SETM(METRIC_STRING, NULL);
316           break;
317         default:
318           snprint_variable(varbuff, sizeof(varbuff), vars->name, vars->name_length, vars);
319           /* Advance passed the first space and use that unless there
320            * is no space or we have no more string left.
321            */
322           sp = strchr(varbuff, ' ');
323           if(sp) sp++;
324           SETM(METRIC_STRING, (sp && *sp) ? sp : NULL);
325       }
326     }
327     nresults++;
328   }
329   noit_check_set_stats(self, check, &current);
330 }
331
332 static int noit_snmp_session_cleanse(struct target_session *ts) {
333   if(ts->refcnt == 0 && ts->sess_handle) {
334     eventer_remove_fd(ts->fd);
335     ts->fd = -1;
336     if(ts->timeoutevent) {
337       eventer_remove(ts->timeoutevent);
338       ts->timeoutevent = NULL;
339     }
340     snmp_sess_close(ts->sess_handle);
341     ts->sess_handle = NULL;
342     if(!ts->in_table) {
343       free(ts);
344     }
345     return 1;
346   }
347   return 0;
348 }
349
350 static int noit_snmp_session_timeout(eventer_t e, int mask, void *closure,
351                                      struct timeval *now) {
352   struct target_session *ts = closure;
353   snmp_sess_timeout(ts->sess_handle);
354   noit_snmp_session_cleanse(ts);
355   if(ts->timeoutevent == e)
356     ts->timeoutevent = NULL; /* this will be freed on return */
357   return 0;
358 }
359
360 static int noit_snmp_check_timeout(eventer_t e, int mask, void *closure,
361                                    struct timeval *now) {
362   struct check_info *info = closure;
363   info->timedout = 1;
364   if(info->ts) {
365     info->ts->refcnt--;
366     noit_snmp_session_cleanse(info->ts);
367     info->ts = NULL;
368   }
369   remove_check(info);
370   /* Log our findings */
371   noit_snmp_log_results(info->self, info->check, NULL);
372   info->check->flags &= ~NP_RUNNING;
373   return 0;
374 }
375
376 static void _set_ts_timeout(struct target_session *ts, struct timeval *t) {
377   struct timeval now;
378   eventer_t e = NULL;
379   if(ts->timeoutevent) {
380     e = eventer_remove(ts->timeoutevent);
381     ts->timeoutevent = NULL;
382   }
383   if(!t) return;
384
385   gettimeofday(&now, NULL);
386   if(!e) e = eventer_alloc();
387   e->callback = noit_snmp_session_timeout;
388   e->closure = ts;
389   e->mask = EVENTER_TIMER;
390   add_timeval(now, *t, &e->whence);
391   ts->timeoutevent = e;
392   eventer_add(e);
393 }
394
395 static int noit_snmp_handler(eventer_t e, int mask, void *closure,
396                              struct timeval *now) {
397   fd_set fdset;
398   int fds, block = 0;
399   struct timeval timeout = { 0, 0 };
400   struct target_session *ts = closure;
401   FD_ZERO(&fdset);
402   FD_SET(e->fd, &fdset);
403   fds = e->fd + 1;
404   snmp_sess_read(ts->sess_handle, &fdset);
405   if(noit_snmp_session_cleanse(ts))
406     return 0;
407   snmp_sess_select_info(ts->sess_handle, &fds, &fdset, &timeout, &block);
408   _set_ts_timeout(ts, block ? &timeout : NULL);
409   return EVENTER_READ | EVENTER_EXCEPTION;
410 }
411
412 /* This 'convert_v1pdu_to_v2' was cribbed directly from netsnmp */
413 static netsnmp_pdu *
414 convert_v1pdu_to_v2( netsnmp_pdu* template_v1pdu ) {
415   netsnmp_pdu *template_v2pdu;
416   netsnmp_variable_list *var;
417   oid enterprise[MAX_OID_LEN];
418   size_t enterprise_len;
419
420   /*
421    * Make a copy of the v1 Trap PDU
422    *   before starting to convert this
423    *   into a v2 Trap PDU.
424    */
425   template_v2pdu = snmp_clone_pdu( template_v1pdu);
426   if(!template_v2pdu) {
427     snmp_log(LOG_WARNING,
428              "send_trap: failed to copy v2 template PDU\n");
429     return NULL;
430   }
431   template_v2pdu->command = SNMP_MSG_TRAP2;
432
433   /*
434    * Insert an snmpTrapOID varbind before the original v1 varbind list
435    *   either using one of the standard defined trap OIDs,
436    *   or constructing this from the PDU enterprise & specific trap fields
437    */
438   if(template_v1pdu->trap_type == SNMP_TRAP_ENTERPRISESPECIFIC) {
439     memcpy(enterprise, template_v1pdu->enterprise,
440            template_v1pdu->enterprise_length*sizeof(oid));
441     enterprise_len = template_v1pdu->enterprise_length;
442     enterprise[enterprise_len++] = 0;
443     enterprise[enterprise_len++] = template_v1pdu->specific_type;
444   } else {
445     memcpy(enterprise, cold_start_oid, sizeof(cold_start_oid));
446     enterprise[9]  = template_v1pdu->trap_type+1;
447     enterprise_len = sizeof(cold_start_oid)/sizeof(oid);
448   }
449
450   var = NULL;
451   if(!snmp_varlist_add_variable(&var,
452                                 snmptrap_oid, snmptrap_oid_len,
453                                 ASN_OBJECT_ID,
454                                 (u_char*)enterprise,
455                                 enterprise_len*sizeof(oid))) {
456     noitL(nlerr, "send_trap: failed to insert copied snmpTrapOID varbind\n");
457     snmp_free_pdu(template_v2pdu);
458     return NULL;
459   }
460   var->next_variable        = template_v2pdu->variables;
461   template_v2pdu->variables = var;
462
463   /*
464    * Insert a sysUptime varbind at the head of the v2 varbind list
465    */
466   var = NULL;
467   if(!snmp_varlist_add_variable(&var,
468                                 sysuptime_oid, sysuptime_oid_len,
469                                 ASN_TIMETICKS,
470                                 (u_char*)&(template_v1pdu->time),
471                                 sizeof(template_v1pdu->time))) {
472     noitL(nlerr, "send_trap: failed to insert copied sysUptime varbind\n");
473     snmp_free_pdu(template_v2pdu);
474     return NULL;
475   }
476   var->next_variable = template_v2pdu->variables;
477   template_v2pdu->variables = var;
478
479   /*
480    * Append the other three conversion varbinds,
481    *  (snmpTrapAgentAddr, snmpTrapCommunity & snmpTrapEnterprise)
482    *  if they're not already present.
483    *  But don't bomb out completely if there are problems.
484    */
485   var = find_varbind_in_list(template_v2pdu->variables,
486                              agentaddr_oid, agentaddr_oid_len);
487   if(!var && (template_v1pdu->agent_addr[0]
488               || template_v1pdu->agent_addr[1]
489               || template_v1pdu->agent_addr[2]
490               || template_v1pdu->agent_addr[3])) {
491     if(!snmp_varlist_add_variable(&(template_v2pdu->variables),
492                                   agentaddr_oid, agentaddr_oid_len,
493                                   ASN_IPADDRESS,
494                                   (u_char*)&(template_v1pdu->agent_addr),
495                                   sizeof(template_v1pdu->agent_addr)))
496       noitL(nlerr, "send_trap: failed to append snmpTrapAddr varbind\n");
497   }
498   var = find_varbind_in_list(template_v2pdu->variables,
499                              community_oid, community_oid_len);
500   if(!var && template_v1pdu->community) {
501     if(!snmp_varlist_add_variable(&(template_v2pdu->variables),
502                                   community_oid, community_oid_len,
503                                   ASN_OCTET_STR,
504                                   template_v1pdu->community,
505                                   template_v1pdu->community_len))
506       noitL(nlerr, "send_trap: failed to append snmpTrapCommunity varbind\n");
507   }
508   var = find_varbind_in_list(template_v2pdu->variables,
509                              snmptrapenterprise_oid,
510                              snmptrapenterprise_oid_len);
511   if(!var &&
512      template_v1pdu->trap_type != SNMP_TRAP_ENTERPRISESPECIFIC) {
513     if(!snmp_varlist_add_variable(&(template_v2pdu->variables),
514                                   snmptrapenterprise_oid,
515                                   snmptrapenterprise_oid_len,
516                                   ASN_OBJECT_ID,
517                                   (u_char*)template_v1pdu->enterprise,
518                                   template_v1pdu->enterprise_length*sizeof(oid)))
519       noitL(nlerr, "send_trap: failed to append snmpEnterprise varbind\n");
520   }
521   return template_v2pdu;
522 }
523
524 static int noit_snmp_oid_to_checkid(oid *o, int l, uuid_t checkid, char *out) {
525   int i;
526   char _uuid_str[UUID_STR_LEN+1], *cp, *uuid_str;
527
528   uuid_str = out ? out : _uuid_str;
529   if(l != reconnoiter_check_oid_len) {
530     noitL(nlerr, "unsupported (length) trap recieved\n");
531     return -1;
532   }
533   if(netsnmp_oid_equals(o,
534                         reconnoiter_check_prefix_oid_len,
535                         reconnoiter_check_prefix_oid,
536                         reconnoiter_check_prefix_oid_len) != 0) {
537     noitL(nlerr, "unsupported (wrong namespace) trap recieved\n");
538     return -1;
539   }
540   /* encode this as a uuid */
541   cp = uuid_str;
542   for(i=0;
543       i < reconnoiter_check_oid_len - reconnoiter_check_prefix_oid_len;
544       i++) {
545     oid v = o[i + reconnoiter_check_prefix_oid_len];
546     if(v > 0xffff) {
547       noitL(nlerr, "trap target oid [%ld] out of range\n", (long int)v);
548       return -1;
549     }
550     snprintf(cp, 5, "%04x", (unsigned short)(v & 0xffff));
551     cp += 4;
552     /* hyphens after index 1,2,3,4 */
553     if(i > 0 && i < 5) *cp++ = '-';
554   }
555   if(uuid_parse(uuid_str, checkid) != 0) {
556     noitL(nlerr, "unexpected error decoding trap uuid '%s'\n", uuid_str);
557     return -1;
558   }
559   return 0;
560 }
561
562 #define isoid(a,b,c,d) (netsnmp_oid_equals(a,b,c,d) == 0)
563 #define isoidprefix(a,b,c,d) (netsnmp_oid_equals(a,MIN(b,d),c,d) == 0)
564 #define setstatus(st,soid,sv) \
565   if(isoid(o,l,soid,reconnoiter_check_state_val_len)) current->st = sv
566
567 static int
568 noit_snmp_trapvars_to_stats(stats_t *current, netsnmp_variable_list *var) {
569   if(isoidprefix(var->name, var->name_length, reconnoiter_check_status_oid,
570                  reconnoiter_check_status_oid_len)) {
571     if(var->type == ASN_OBJECT_ID) {
572       if(isoid(var->name, var->name_length,
573                reconnoiter_check_state_oid, reconnoiter_check_state_oid_len)) {
574         oid *o = var->val.objid;
575         size_t l = var->val_len / sizeof(*o);
576         setstatus(state, reconnoiter_check_state_unknown_oid, NP_UNKNOWN);
577         else setstatus(state, reconnoiter_check_state_good_oid, NP_GOOD);
578         else setstatus(state, reconnoiter_check_state_bad_oid, NP_BAD);
579         else return -1;
580       }
581       else if(isoid(var->name, var->name_length,
582                     reconnoiter_check_available_oid,
583                     reconnoiter_check_available_oid_len)) {
584         oid *o = var->val.objid;
585         size_t l = var->val_len / sizeof(*o);
586         setstatus(available, reconnoiter_check_available_unknown_oid, NP_UNKNOWN);
587         else setstatus(available, reconnoiter_check_available_yes_oid, NP_AVAILABLE);
588         else setstatus(available, reconnoiter_check_available_no_oid, NP_UNAVAILABLE);
589         else return -1;
590       }
591       else {
592         /* We don't unerstand any other OBJECT_ID types */
593         return -1;
594       }
595     }
596     else if(var->type == ASN_UNSIGNED) {
597       /* This is only for the duration (in ms) */
598       if(isoid(var->name, var->name_length,
599                reconnoiter_check_duration_oid,
600                reconnoiter_check_duration_oid_len)) {
601         current->duration = *(var->val.integer);
602       }
603       else
604         return -1;
605     }
606     else if(var->type == ASN_OCTET_STR) {
607       /* This is only for the status message */
608       if(isoid(var->name, var->name_length,
609                reconnoiter_check_status_msg_oid,
610                reconnoiter_check_status_msg_oid_len)) {
611         current->status = malloc(var->val_len + 1);
612         memcpy(current->status, var->val.string, var->val_len);
613         current->status[var->val_len] = '\0';
614       }
615       else
616         return -1;
617     }
618     else {
619       /* I don't understand any other type of status message */
620       return -1;
621     }
622   }
623   else if(isoidprefix(var->name, var->name_length,
624                       reconnoiter_metric_prefix_oid,
625                       reconnoiter_metric_prefix_oid_len)) {
626     /* decode the metric and store the value */
627     int i, len;
628     u_int64_t u64;
629     double doubleVal;
630     char metric_name[128], buff[128], *cp;
631     if(var->name_length <= reconnoiter_metric_prefix_oid_len) return -1;
632     len = var->name[reconnoiter_metric_prefix_oid_len];
633     if(var->name_length != (reconnoiter_metric_prefix_oid_len + 1 + len) ||
634        len > sizeof(metric_name) - 1) {
635       noitL(nlerr, "snmp trap, malformed metric name\n");
636       return -1;
637     }
638     for(i=0;i<len;i++) {
639       ((unsigned char *)metric_name)[i] =
640         (unsigned char)var->name[reconnoiter_metric_prefix_oid_len + 1 + i];
641       if(!isprint(metric_name[i])) {
642         noitL(nlerr, "metric_name contains unprintable characters\n");
643         return -1;
644       }
645     }
646     metric_name[i] = '\0';
647     switch(var->type) {
648       case ASN_INTEGER:
649       case ASN_UINTEGER:
650       case ASN_TIMETICKS:
651       case ASN_INTEGER64:
652         noit_stats_set_metric(current, metric_name,
653                               METRIC_INT64, var->val.integer);
654         break;
655       case ASN_COUNTER64:
656         u64 = ((u_int64_t)var->val.counter64->high) << 32;
657         u64 |= var->val.counter64->low;
658         noit_stats_set_metric(current, metric_name,
659                               METRIC_UINT64, &u64);
660         break;
661       case ASN_OPAQUE_FLOAT:
662         doubleVal = (double)*var->val.floatVal;
663         noit_stats_set_metric(current, metric_name,
664                               METRIC_DOUBLE, &doubleVal);
665         break;
666       case ASN_OPAQUE_DOUBLE:
667         noit_stats_set_metric(current, metric_name,
668                               METRIC_DOUBLE, var->val.doubleVal);
669         break;
670       case ASN_OCTET_STR:
671         snprint_value(buff, sizeof(buff), var->name, var->name_length, var);
672         /* Advance passed the first space and use that unless there
673          * is no space or we have no more string left.
674          */
675         cp = strchr(buff, ' ');
676         if(cp) {
677           char *ecp;
678           cp++;
679           if(*cp == '"') {
680             ecp = cp + strlen(cp) - 1;
681             if(*ecp == '"') {
682               cp++; *ecp = '\0';
683             }
684           }
685         }
686         noit_stats_set_metric(current, metric_name,
687                               METRIC_STRING, (cp && *cp) ? cp : NULL);
688         break;
689       default:
690         noitL(nlerr, "snmp trap unsupport data type %d\n", var->type);
691     }
692     noitL(nldeb, "metric_name -> '%s'\n", metric_name);
693   }
694   else {
695     /* No idea what this is */
696     return -1;
697   }
698   return 0;
699 }
700 static int noit_snmp_trapd_response(int operation, struct snmp_session *sp,
701                                     int reqid, struct snmp_pdu *pdu,
702                                     void *magic) {
703   /* the noit pieces */
704   noit_check_t *check;
705   struct target_session *ts = magic;
706   snmp_mod_config_t *conf;
707   const char *community = NULL;
708   stats_t current;
709   int success = 0;
710
711   /* parsing destination */
712   char uuid_str[UUID_STR_LEN + 1];
713   uuid_t checkid;
714
715   /* snmp oid parsing helper vars */
716   netsnmp_pdu *newpdu = pdu;
717   netsnmp_variable_list *var;
718
719   conf = noit_module_get_userdata(ts->self);
720
721   if(pdu->version == SNMP_VERSION_1)
722     newpdu = convert_v1pdu_to_v2(pdu);
723   if(!newpdu || newpdu->version != SNMP_VERSION_2c) goto cleanup;
724
725   for(var = newpdu->variables; var != NULL; var = var->next_variable) {
726     if(netsnmp_oid_equals(var->name, var->name_length,
727                           snmptrap_oid, snmptrap_oid_len) == 0)
728       break;
729   }
730
731   if (!var || var->type != ASN_OBJECT_ID) {
732     noitL(nlerr, "unsupport trap (not a trap?) received\n");
733     goto cleanup;
734   }
735
736   /* var is the oid on which we are trapping.
737    * It should be in the reconnoiter check prefix.
738    */
739   if(noit_snmp_oid_to_checkid(var->val.objid, var->val_len/sizeof(oid),
740                               checkid, uuid_str)) {
741     goto cleanup;
742   }
743   noitL(nldeb, "recieved trap for %s\n", uuid_str);
744   check = noit_poller_lookup(checkid);
745   if(!check) {
746     noitL(nlerr, "trap received for non-existent check '%s'\n", uuid_str);
747     goto cleanup;
748   }
749   if(!noit_hash_retr_str(check->config, "community", strlen("community"),
750                          &community) &&
751      !noit_hash_retr_str(conf->options, "community", strlen("community"),
752                          &community)) {
753     noitL(nlerr, "No community defined for check, dropping trap\n");
754     goto cleanup;
755   }
756
757   if(strlen(community) != newpdu->community_len ||
758      memcmp(community, newpdu->community, newpdu->community_len)) {
759     noitL(nlerr, "trap attempt with wrong community string\n");
760     goto cleanup;
761   }
762
763   /* We have a check. The trap is authorized. Now, extract everything. */
764   memset(&current, 0, sizeof(current));
765   gettimeofday(&current.whence, NULL);
766   current.available = NP_AVAILABLE;
767
768   /* Rate limit */
769   if(((current.whence.tv_sec * 1000 +
770        current.whence.tv_usec / 1000) -
771       (check->last_fire_time.tv_sec * 1000 +
772        check->last_fire_time.tv_usec / 1000)) < check->period) goto cleanup;
773
774   /* update the last fire time... */
775   gettimeofday(&check->last_fire_time, NULL);
776
777   for(; var != NULL; var = var->next_variable)
778     if(noit_snmp_trapvars_to_stats(&current, var) == 0) success++;
779   if(success) {
780     char buff[24];
781     snprintf(buff, sizeof(buff), "%d datum", success);
782     current.state = NP_GOOD;
783     current.status = strdup(buff);
784   }
785   else {
786     current.state = NP_BAD;
787     current.status = strdup("no data");
788   }
789   noit_check_set_stats(ts->self, check, &current);
790
791  cleanup:
792   if(newpdu != pdu)
793     snmp_free_pdu(newpdu);
794   return 0;
795 }
796 static int noit_snmp_asynch_response(int operation, struct snmp_session *sp,
797                                      int reqid, struct snmp_pdu *pdu,
798                                      void *magic) {
799   struct check_info *info;
800   /* We don't deal with refcnt hitting zero here.  We could only be hit from
801    * the snmp read/timeout stuff.  Handle it there.
802    */
803
804   info = get_check(reqid);
805   if(!info) return 1;
806   if(info->ts) {
807     info->ts->refcnt--;
808     info->ts = NULL;
809   }
810   remove_check(info);
811   if(info->timeoutevent) {
812     eventer_remove(info->timeoutevent);
813     eventer_free(info->timeoutevent);
814     info->timeoutevent = NULL;
815   }
816
817   /* Log our findings */
818   noit_snmp_log_results(info->self, info->check, pdu);
819   info->check->flags &= ~NP_RUNNING;
820   return 1;
821 }
822
823 static void noit_snmp_sess_open(struct target_session *ts,
824                                 noit_check_t *check) {
825   const char *community;
826   struct snmp_session sess;
827   struct check_info *info = check->closure;
828   snmp_sess_init(&sess);
829   sess.version = info->version;
830   sess.peername = ts->target;
831   if(!noit_hash_retr_str(check->config, "community", strlen("community"),
832                          &community)) {
833     community = "public";
834   }
835   sess.community = (unsigned char *)community;
836   sess.community_len = strlen(community);
837   sess.callback = noit_snmp_asynch_response;
838   sess.callback_magic = ts;
839   ts->sess_handle = snmp_sess_open(&sess);
840   gettimeofday(&ts->last_open, NULL);
841 }
842
843 static int noit_snmp_fill_req(struct snmp_pdu *req, noit_check_t *check) {
844   int i, klen;
845   noit_hash_iter iter = NOIT_HASH_ITER_ZERO;
846   const char *name, *value;
847   struct check_info *info = check->closure;
848   noit_hash_table check_attrs_hash = NOIT_HASH_EMPTY;
849
850   /* Toss the old set and bail if we have zero */
851   if(info->oids) {
852     for(i=0; i<info->noids;i++) {
853       if(info->oids[i].confname) free(info->oids[i].confname);
854       if(info->oids[i].oidname) free(info->oids[i].oidname);
855     }
856     free(info->oids);
857   }
858   info->noids = 0;
859   info->oids = NULL;
860
861   /* Figure our how many. */
862   while(noit_hash_next_str(check->config, &iter, &name, &klen, &value)) {
863     if(!strncasecmp(name, "oid_", 4)) {
864       info->noids++;
865     }
866   }
867
868   if(info->noids == 0) return 0;
869
870   /* Create a hash of important check attributes */
871   noit_check_make_attrs(check, &check_attrs_hash);
872
873   /* Fill out the new set of required oids */
874   info->oids = calloc(info->noids, sizeof(*info->oids));
875   memset(&iter, 0, sizeof(iter));
876   i = 0;
877   while(noit_hash_next_str(check->config, &iter, &name, &klen, &value)) {
878     if(!strncasecmp(name, "oid_", 4)) {
879       const char *type_override;
880       char oidbuff[128], typestr[256];
881       name += 4;
882       info->oids[i].confname = strdup(name);
883       noit_check_interpolate(oidbuff, sizeof(oidbuff), value,
884                              &check_attrs_hash, check->config);
885       info->oids[i].oidname = strdup(oidbuff);
886       info->oids[i].oidlen = MAX_OID_LEN;
887       if(oidbuff[0] == '.')
888         read_objid(oidbuff, info->oids[i].oid, &info->oids[i].oidlen);
889       else
890         get_node(oidbuff, info->oids[i].oid, &info->oids[i].oidlen);
891       snprintf(typestr, sizeof(typestr), "type_%s", name);
892       if(noit_hash_retr_str(check->config, typestr, strlen(typestr),
893                             &type_override)) {
894         int type_enum_fake = *type_override;
895
896         if(!strcasecmp(type_override, "guess"))
897           type_enum_fake = METRIC_GUESS;
898         else if(!strcasecmp(type_override, "int32"))
899           type_enum_fake = METRIC_INT32;
900         else if(!strcasecmp(type_override, "uint32"))
901           type_enum_fake = METRIC_UINT32;
902         else if(!strcasecmp(type_override, "int64"))
903           type_enum_fake = METRIC_INT64;
904         else if(!strcasecmp(type_override, "uint64"))
905           type_enum_fake = METRIC_UINT64;
906         else if(!strcasecmp(type_override, "double"))
907           type_enum_fake = METRIC_DOUBLE;
908         else if(!strcasecmp(type_override, "string"))
909           type_enum_fake = METRIC_STRING;
910
911         switch(type_enum_fake) {
912           case METRIC_GUESS:
913           case METRIC_INT32: case METRIC_UINT32:
914           case METRIC_INT64: case METRIC_UINT64:
915           case METRIC_DOUBLE: case METRIC_STRING:
916             info->oids[i].type_override = *type_override;
917             info->oids[i].type_should_override = noit_true;
918           default: break;
919         }
920       }
921       snmp_add_null_var(req, info->oids[i].oid, info->oids[i].oidlen);
922       i++;
923     }
924   }
925   assert(info->noids == i);
926   noit_hash_destroy(&check_attrs_hash, NULL, NULL);
927   return info->noids;
928 }
929 static int noit_snmp_send(noit_module_t *self, noit_check_t *check,
930                           noit_check_t *cause) {
931   struct snmp_pdu *req;
932   struct target_session *ts;
933   struct check_info *info = check->closure;
934   int port = 161;
935   const char *portstr, *versstr;
936   char target_port[64];
937
938   info->version = SNMP_VERSION_2c;
939   info->self = self;
940   info->check = check;
941   info->timedout = 0;
942
943   check->flags |= NP_RUNNING;
944
945   if(noit_hash_retr_str(check->config, "port", strlen("port"),
946                         &portstr)) {
947     port = atoi(portstr);
948   }
949   if(noit_hash_retr_str(check->config, "version", strlen("version"),
950                         &versstr)) {
951     /* We don't care about 2c or others... as they all default to 2c */
952     if(!strcmp(versstr, "1")) info->version = SNMP_VERSION_1;
953     if(!strcmp(versstr, "3")) info->version = SNMP_VERSION_3;
954   }
955   snprintf(target_port, sizeof(target_port), "%s:%d", check->target_ip, port);
956   ts = _get_target_session(self, target_port, info->version);
957   gettimeofday(&check->last_fire_time, NULL);
958   if(!ts->refcnt) {
959     eventer_t newe;
960     int fds, block;
961     struct timeval timeout;
962     fd_set fdset;
963     noit_snmp_sess_open(ts, check);
964     block = 0;
965     fds = 0;
966     FD_ZERO(&fdset);
967     snmp_sess_select_info(ts->sess_handle, &fds, &fdset, &timeout, &block);
968     assert(fds > 0);
969     ts->fd = fds-1;
970     newe = eventer_alloc();
971     newe->fd = ts->fd;
972     newe->callback = noit_snmp_handler;
973     newe->closure = ts;
974     newe->mask = EVENTER_READ | EVENTER_EXCEPTION;
975     eventer_add(newe);
976   }
977   if(!ts->sess_handle) {
978     /* Error */
979     /* No need to do anything, this will be handled in the else below */
980   }
981   ts->refcnt++; /* Increment here, decrement when this check completes */
982
983   req = snmp_pdu_create(SNMP_MSG_GET);
984   if(req) noit_snmp_fill_req(req, check);
985   req->version = info->version;
986   /* Setup out snmp requests */
987   if(ts->sess_handle && req &&
988      (info->reqid = snmp_sess_send(ts->sess_handle, req)) != 0) {
989     struct timeval when, to;
990     info->ts = ts;
991     info->timeoutevent = eventer_alloc();
992     info->timeoutevent->callback = noit_snmp_check_timeout;
993     info->timeoutevent->closure = info;
994     info->timeoutevent->mask = EVENTER_TIMER;
995
996     noitL(nldeb, "Sending snmp get\n");
997     gettimeofday(&when, NULL);
998     to.tv_sec = check->timeout / 1000;
999     to.tv_usec = (check->timeout % 1000) * 1000;
1000     add_timeval(when, to, &info->timeoutevent->whence);
1001     eventer_add(info->timeoutevent);
1002     add_check(info);
1003   }
1004   else {
1005     ts->refcnt--;
1006     noitL(nlerr, "Error sending snmp get request.\n");
1007     noit_snmp_session_cleanse(ts);
1008     /* Error */
1009     if(req) snmp_free_pdu(req);
1010     /* Log our findings */
1011     noit_snmp_log_results(self, check, NULL);
1012     check->flags &= ~NP_RUNNING;
1013   }
1014   return 0;
1015 }
1016
1017 static int noit_snmp_initiate_check(noit_module_t *self, noit_check_t *check,
1018                                     int once, noit_check_t *cause) {
1019   if(!check->closure) check->closure = calloc(1, sizeof(struct check_info));
1020   INITIATE_CHECK(noit_snmp_send, self, check, cause);
1021   return 0;
1022 }
1023
1024 static int noit_snmptrap_initiate_check(noit_module_t *self,
1025                                         noit_check_t *check,
1026                                         int once, noit_check_t *cause) {
1027   /* We don't do anything for snmptrap checks.  Not intuitive... but they
1028    * never "run."  We accept input out-of-band via snmp traps.
1029    */
1030   return 0;
1031 }
1032
1033 static int noit_snmp_config(noit_module_t *self, noit_hash_table *options) {
1034   snmp_mod_config_t *conf;
1035   conf = noit_module_get_userdata(self);
1036   if(conf) {
1037     if(conf->options) {
1038       noit_hash_destroy(conf->options, free, free);
1039       free(conf->options);
1040     }
1041   }
1042   else
1043     conf = calloc(1, sizeof(*conf));
1044   conf->options = options;
1045   noit_module_set_userdata(self, conf);
1046   return 1;
1047 }
1048 static int noit_snmp_onload(noit_image_t *self) {
1049   if(!nlerr) nlerr = noit_log_stream_find("error/snmp");
1050   if(!nldeb) nldeb = noit_log_stream_find("debug/snmp");
1051   if(!nlerr) nlerr = noit_stderr;
1052   if(!nldeb) nldeb = noit_debug;
1053   eventer_name_callback("noit_snmp/check_timeout", noit_snmp_check_timeout);
1054   eventer_name_callback("noit_snmp/session_timeout", noit_snmp_session_timeout);
1055   eventer_name_callback("noit_snmp/handler", noit_snmp_handler);
1056   return 0;
1057 }
1058
1059 static int noit_snmptrap_onload(noit_image_t *self) {
1060   if(!nlerr) nlerr = noit_log_stream_find("error/snmp");
1061   if(!nldeb) nldeb = noit_log_stream_find("debug/snmp");
1062   if(!nlerr) nlerr = noit_stderr;
1063   if(!nldeb) nldeb = noit_debug;
1064   eventer_name_callback("noit_snmp/session_timeout", noit_snmp_session_timeout);
1065   eventer_name_callback("noit_snmp/handler", noit_snmp_handler);
1066   return 0;
1067 }
1068
1069 static void
1070 nc_printf_snmpts_brief(noit_console_closure_t ncct,
1071                        struct target_session *ts) {
1072   char fd[32];
1073   struct timeval now, diff;
1074   const char *snmpvers = "v(unknown)";
1075   gettimeofday(&now, NULL);
1076   sub_timeval(now, ts->last_open, &diff);
1077   if(ts->fd < 0)
1078     snprintf(fd, sizeof(fd), "%s", "(closed)");
1079   else
1080     snprintf(fd, sizeof(fd), "%d", ts->fd);
1081   switch(ts->version) {
1082     case SNMP_VERSION_1: snmpvers = "v1"; break;
1083     case SNMP_VERSION_2c: snmpvers = "v2c"; break;
1084     case SNMP_VERSION_3: snmpvers = "v3"; break;
1085   }
1086   nc_printf(ncct, "[%s %s]\n\topened: %0.3fs ago\n\tFD: %s\n\trefcnt: %d\n",
1087             ts->target, snmpvers, diff.tv_sec + (float)diff.tv_usec/1000000,
1088             fd, ts->refcnt);
1089 }
1090
1091 static int
1092 noit_console_show_snmp(noit_console_closure_t ncct,
1093                        int argc, char **argv,
1094                        noit_console_state_t *dstate,
1095                        void *closure) {
1096   noit_hash_iter iter = NOIT_HASH_ITER_ZERO;
1097   uuid_t key_id;
1098   int klen;
1099   void *vts;
1100   snmp_mod_config_t *conf = closure;
1101
1102   while(noit_hash_next(&conf->target_sessions, &iter,
1103                        (const char **)key_id, &klen,
1104                        &vts)) {
1105     struct target_session *ts = vts;
1106     nc_printf_snmpts_brief(ncct, ts);
1107   }
1108   return 0;
1109 }
1110
1111 static void
1112 register_console_snmp_commands(snmp_mod_config_t *conf) {
1113   noit_console_state_t *tl;
1114   cmd_info_t *showcmd;
1115
1116   tl = noit_console_state_initial();
1117   showcmd = noit_console_state_get_cmd(tl, "show");
1118   assert(showcmd && showcmd->dstate);
1119   noit_console_state_add_cmd(showcmd->dstate,
1120     NCSCMD("snmp", noit_console_show_snmp, NULL, NULL, conf));
1121 }
1122
1123 static int noit_snmp_init(noit_module_t *self) {
1124   const char *opt;
1125   snmp_mod_config_t *conf;
1126
1127   conf = noit_module_get_userdata(self);
1128
1129   if(!__snmp_initialize_once) {
1130     register_mib_handlers();
1131     read_premib_configs();
1132     read_configs();
1133     init_snmp("noitd");
1134     __snmp_initialize_once = 1;
1135   }
1136   if(strcmp(self->hdr.name, "snmp") == 0) {
1137     register_console_snmp_commands(conf);
1138   }
1139
1140   if(strcmp(self->hdr.name, "snmptrap") == 0) {
1141     eventer_t newe;
1142     int i, block = 0, fds = 0;
1143     fd_set fdset;
1144     struct timeval timeout = { 0, 0 };
1145     struct target_session *ts;
1146     netsnmp_transport *transport;
1147     netsnmp_session sess, *session = &sess;
1148
1149     if(!noit_hash_retrieve(conf->options,
1150                            "snmptrapd_port", strlen("snmptrapd_port"),
1151                            (void **)&opt))
1152       opt = "162";
1153
1154     transport = netsnmp_transport_open_server("snmptrap", opt);
1155     if(!transport) {
1156       noitL(nlerr, "cannot open netsnmp transport for trap daemon\n");
1157       return -1;
1158     }
1159     ts = _get_target_session(self, "snmptrapd", SNMP_DEFAULT_VERSION);
1160     snmp_sess_init(session);
1161     session->peername = SNMP_DEFAULT_PEERNAME;
1162     session->version = SNMP_DEFAULT_VERSION;
1163     session->community_len = SNMP_DEFAULT_COMMUNITY_LEN;
1164     session->retries = SNMP_DEFAULT_RETRIES;
1165     session->timeout = SNMP_DEFAULT_TIMEOUT;
1166     session->callback = noit_snmp_trapd_response;
1167     session->callback_magic = (void *) ts;
1168     session->authenticator = NULL;
1169     session->isAuthoritative = SNMP_SESS_UNKNOWNAUTH;
1170     ts->sess_handle = snmp_sess_add(session, transport, NULL, NULL);
1171
1172     FD_ZERO(&fdset);
1173     snmp_sess_select_info(ts->sess_handle, &fds, &fdset, &timeout, &block);
1174     assert(fds > 0);
1175     for(i=0; i<fds; i++) {
1176       if(FD_ISSET(i, &fdset)) {
1177         ts->refcnt++;
1178         ts->fd = i;
1179         newe = eventer_alloc();
1180         newe->fd = ts->fd;
1181         newe->callback = noit_snmp_handler;
1182         newe->closure = ts;
1183         newe->mask = EVENTER_READ | EVENTER_EXCEPTION;
1184         eventer_add(newe);
1185       }
1186     }
1187   }
1188   return 0;
1189 }
1190
1191 #include "snmp.xmlh"
1192 noit_module_t snmp = {
1193   {
1194     NOIT_MODULE_MAGIC,
1195     NOIT_MODULE_ABI_VERSION,
1196     "snmp",
1197     "SNMP collection",
1198     snmp_xml_description,
1199     noit_snmp_onload
1200   },
1201   noit_snmp_config,
1202   noit_snmp_init,
1203   noit_snmp_initiate_check,
1204   NULL /* noit_snmp_cleanup */
1205 };
1206
1207 #include "snmptrap.xmlh"
1208 noit_module_t snmptrap = {
1209   {
1210     NOIT_MODULE_MAGIC,
1211     NOIT_MODULE_ABI_VERSION,
1212     "snmptrap",
1213     "SNMP trap collection",
1214     snmptrap_xml_description,
1215     noit_snmptrap_onload
1216   },
1217   noit_snmp_config,
1218   noit_snmp_init,
1219   noit_snmptrap_initiate_check,
1220   NULL /* noit_snmp_cleanup */
1221 };
Note: See TracBrowser for help on using the browser.