Debian Codegraph @

src/snmp.c

raw
1 /**
2 * collectd - src/snmp.c
3 * Copyright (C) 2007-2012 Florian octo Forster
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
22 *
23 * Authors:
24 * Florian octo Forster <octo at collectd.org>
25 **/
26
27 #include "collectd.h"
28
29 #include "plugin.h"
30 #include "utils/common/common.h"
31 #include "utils/ignorelist/ignorelist.h"
32 #include "utils_complain.h"
33
34 #include <net-snmp/net-snmp-config.h>
35 #include <net-snmp/net-snmp-includes.h>
36
37 #include <fnmatch.h>
38
39 /*
40 * Private data structes
41 */
42 struct oid_s {
43 oid oid[MAX_OID_LEN];
44 size_t oid_len;
45 };
46 typedef struct oid_s oid_t;
47
48 struct instance_s {
49 bool configured;
50 oid_t oid;
51 char *prefix;
52 char *value;
53 };
54 typedef struct instance_s instance_t;
55
56 struct data_definition_s {
57 char *name; /* used to reference this from the `Collect' option */
58 char *type; /* used to find the data_set */
59 bool is_table;
60 instance_t type_instance;
61 instance_t plugin_instance;
62 instance_t host;
63 oid_t filter_oid;
64 ignorelist_t *ignorelist;
65 char *plugin_name;
66 oid_t *values;
67 size_t values_len;
68 double scale;
69 double shift;
70 struct data_definition_s *next;
71 char **ignores;
72 size_t ignores_len;
73 bool invert_match;
74 bool count;
75 };
76 typedef struct data_definition_s data_definition_t;
77
78 struct host_definition_s {
79 char *name;
80 char *address;
81 int version;
82 cdtime_t timeout;
83 int retries;
84
85 /* snmpv1/2 options */
86 char *community;
87
88 /* snmpv3 security options */
89 char *username;
90 oid *auth_protocol;
91 size_t auth_protocol_len;
92 char *auth_passphrase;
93 oid *priv_protocol;
94 size_t priv_protocol_len;
95 char *priv_passphrase;
96 int security_level;
97 char *context;
98
99 void *sess_handle;
100 c_complain_t complaint;
101 data_definition_t **data_list;
102 int data_list_len;
103 int bulk_size;
104 };
105 typedef struct host_definition_s host_definition_t;
106
107 /* These two types are used to cache values in `csnmp_read_table' to handle
108 * gaps in tables. */
109 struct csnmp_cell_char_s {
110 oid_t suffix;
111 char value[DATA_MAX_NAME_LEN];
112 struct csnmp_cell_char_s *next;
113 };
114 typedef struct csnmp_cell_char_s csnmp_cell_char_t;
115
116 struct csnmp_cell_value_s {
117 oid_t suffix;
118 value_t value;
119 struct csnmp_cell_value_s *next;
120 };
121 typedef struct csnmp_cell_value_s csnmp_cell_value_t;
122
123 typedef enum {
124 OID_TYPE_SKIP = 0,
125 OID_TYPE_VARIABLE,
126 OID_TYPE_TYPEINSTANCE,
127 OID_TYPE_PLUGININSTANCE,
128 OID_TYPE_HOST,
129 OID_TYPE_FILTER,
130 } csnmp_oid_type_t;
131
132 /*
133 * Private variables
134 */
135 static data_definition_t *data_head;
136
137 /*
138 * Prototypes
139 */
140 static int csnmp_read_host(user_data_t *ud);
141
142 /*
143 * Private functions
144 */
145 static void csnmp_oid_init(oid_t *dst, oid const *src, size_t n) {
146 assert(n <= STATIC_ARRAY_SIZE(dst->oid));
147 memcpy(dst->oid, src, sizeof(*src) * n);
148 dst->oid_len = n;
149 }
150
151 static int csnmp_oid_compare(oid_t const *left, oid_t const *right) {
152 return snmp_oid_compare(left->oid, left->oid_len, right->oid, right->oid_len);
153 }
154
155 static int csnmp_oid_suffix(oid_t *dst, oid_t const *src, oid_t const *root) {
156 /* Make sure "src" is in "root"s subtree. */
157 if (src->oid_len <= root->oid_len)
158 return EINVAL;
159 if (snmp_oid_ncompare(root->oid, root->oid_len, src->oid, src->oid_len,
160 /* n = */ root->oid_len) != 0)
161 return EINVAL;
162
163 memset(dst, 0, sizeof(*dst));
164 dst->oid_len = src->oid_len - root->oid_len;
165 memcpy(dst->oid, &src->oid[root->oid_len],
166 dst->oid_len * sizeof(dst->oid[0]));
167 return 0;
168 }
169
170 static int csnmp_oid_to_string(char *buffer, size_t buffer_size,
171 oid_t const *o) {
172 char oid_str[MAX_OID_LEN][16];
173 char *oid_str_ptr[MAX_OID_LEN];
174
175 for (size_t i = 0; i < o->oid_len; i++) {
176 ssnprintf(oid_str[i], sizeof(oid_str[i]), "%lu", (unsigned long)o->oid[i]);
177 oid_str_ptr[i] = oid_str[i];
178 }
179
180 return strjoin(buffer, buffer_size, oid_str_ptr, o->oid_len, ".");
181 }
182
183 static void csnmp_host_close_session(host_definition_t *host) /* {{{ */
184 {
185 if (host->sess_handle == NULL)
186 return;
187
188 snmp_sess_close(host->sess_handle);
189 host->sess_handle = NULL;
190 } /* }}} void csnmp_host_close_session */
191
192 static void csnmp_host_definition_destroy(void *arg) /* {{{ */
193 {
194 host_definition_t *hd;
195
196 hd = arg;
197
198 if (hd == NULL)
199 return;
200
201 if (hd->name != NULL) {
202 DEBUG("snmp plugin: Destroying host definition for host `%s'.", hd->name);
203 }
204
205 csnmp_host_close_session(hd);
206
207 sfree(hd->name);
208 sfree(hd->address);
209 sfree(hd->community);
210 sfree(hd->username);
211 sfree(hd->auth_passphrase);
212 sfree(hd->priv_passphrase);
213 sfree(hd->context);
214 sfree(hd->data_list);
215
216 sfree(hd);
217 } /* }}} void csnmp_host_definition_destroy */
218
219 static void csnmp_data_definition_destroy(data_definition_t *dd) {
220 sfree(dd->name);
221 sfree(dd->type);
222 sfree(dd->plugin_name);
223 sfree(dd->plugin_instance.prefix);
224 sfree(dd->plugin_instance.value);
225 sfree(dd->type_instance.prefix);
226 sfree(dd->type_instance.value);
227 sfree(dd->host.prefix);
228 sfree(dd->host.value);
229 sfree(dd->values);
230 sfree(dd->ignores);
231 ignorelist_free(dd->ignorelist);
232 sfree(dd);
233 } /* void csnmp_data_definition_destroy */
234
235 /* Many functions to handle the configuration. {{{ */
236 /* First there are many functions which do configuration stuff. It's a big
237 * bloated and messy, I'm afraid. */
238
239 /*
240 * Callgraph for the config stuff:
241 * csnmp_config
242 * +-> call_snmp_init_once
243 * +-> csnmp_config_add_data
244 * ! +-> csnmp_config_configure_data_instance
245 * ! +-> csnmp_config_add_data_values
246 * +-> csnmp_config_add_host
247 * +-> csnmp_config_add_host_version
248 * +-> csnmp_config_add_host_collect
249 * +-> csnmp_config_add_host_auth_protocol
250 * +-> csnmp_config_add_host_priv_protocol
251 * +-> csnmp_config_add_host_security_level
252 */
253 static void call_snmp_init_once(void) {
254 static int have_init;
255
256 if (have_init == 0)
257 init_snmp(PACKAGE_NAME);
258 have_init = 1;
259 } /* void call_snmp_init_once */
260
261 static int csnmp_config_configure_data_instance(instance_t *instance,
262 oconfig_item_t *ci) {
263 char buffer[DATA_MAX_NAME_LEN];
264
265 int status = cf_util_get_string_buffer(ci, buffer, sizeof(buffer));
266 if (status != 0)
267 return status;
268
269 instance->configured = true;
270
271 if (strlen(buffer) == 0) {
272 return 0;
273 }
274
275 instance->oid.oid_len = MAX_OID_LEN;
276
277 if (!read_objid(buffer, instance->oid.oid, &instance->oid.oid_len)) {
278 ERROR("snmp plugin: read_objid (%s) failed.", buffer);
279 return -1;
280 }
281
282 return 0;
283 } /* int csnmp_config_configure_data_instance */
284
285 static int csnmp_config_add_data_values(data_definition_t *dd,
286 oconfig_item_t *ci) {
287 if (ci->values_num < 1) {
288 WARNING("snmp plugin: `Values' needs at least one argument.");
289 return -1;
290 }
291
292 for (int i = 0; i < ci->values_num; i++)
293 if (ci->values[i].type != OCONFIG_TYPE_STRING) {
294 WARNING("snmp plugin: `Values' needs only string argument.");
295 return -1;
296 }
297
298 sfree(dd->values);
299 dd->values_len = 0;
300 dd->values = malloc(sizeof(*dd->values) * ci->values_num);
301 if (dd->values == NULL)
302 return -1;
303 dd->values_len = (size_t)ci->values_num;
304
305 for (int i = 0; i < ci->values_num; i++) {
306 dd->values[i].oid_len = MAX_OID_LEN;
307
308 if (NULL == snmp_parse_oid(ci->values[i].value.string, dd->values[i].oid,
309 &dd->values[i].oid_len)) {
310 ERROR("snmp plugin: snmp_parse_oid (%s) failed.",
311 ci->values[i].value.string);
312 free(dd->values);
313 dd->values = NULL;
314 dd->values_len = 0;
315 return -1;
316 }
317 }
318
319 return 0;
320 } /* int csnmp_config_configure_data_instance */
321
322 static int csnmp_config_add_data_blacklist(data_definition_t *dd,
323 oconfig_item_t *ci) {
324 if (ci->values_num < 1)
325 return 0;
326
327 for (int i = 0; i < ci->values_num; i++) {
328 if (ci->values[i].type != OCONFIG_TYPE_STRING) {
329 WARNING("snmp plugin: `Ignore' needs only string argument.");
330 return -1;
331 }
332 }
333
334 for (int i = 0; i < ci->values_num; ++i) {
335 if (strarray_add(&(dd->ignores), &(dd->ignores_len),
336 ci->values[i].value.string) != 0) {
337 ERROR("snmp plugin: Can't allocate memory");
338 strarray_free(dd->ignores, dd->ignores_len);
339 return ENOMEM;
340 }
341 }
342 return 0;
343 } /* int csnmp_config_add_data_blacklist */
344
345 static int csnmp_config_add_data_filter_values(data_definition_t *data,
346 oconfig_item_t *ci) {
347 if (ci->values_num < 1) {
348 WARNING("snmp plugin: `FilterValues' needs at least one argument.");
349 return -1;
350 }
351
352 for (int i = 0; i < ci->values_num; i++) {
353 if (ci->values[i].type != OCONFIG_TYPE_STRING) {
354 WARNING("snmp plugin: All arguments to `FilterValues' must be strings.");
355 return -1;
356 }
357 ignorelist_add(data->ignorelist, ci->values[i].value.string);
358 }
359
360 return 0;
361 } /* int csnmp_config_add_data_filter_values */
362
363 static int csnmp_config_add_data_filter_oid(data_definition_t *data,
364 oconfig_item_t *ci) {
365
366 char buffer[DATA_MAX_NAME_LEN];
367 int status = cf_util_get_string_buffer(ci, buffer, sizeof(buffer));
368 if (status != 0)
369 return status;
370
371 data->filter_oid.oid_len = MAX_OID_LEN;
372
373 if (!read_objid(buffer, data->filter_oid.oid, &data->filter_oid.oid_len)) {
374 ERROR("snmp plugin: read_objid (%s) failed.", buffer);
375 return -1;
376 }
377 return 0;
378 } /* int csnmp_config_add_data_filter_oid */
379
380 static int csnmp_config_add_data(oconfig_item_t *ci) {
381 data_definition_t *dd = calloc(1, sizeof(*dd));
382 if (dd == NULL)
383 return -1;
384
385 int status = cf_util_get_string(ci, &dd->name);
386 if (status != 0) {
387 sfree(dd);
388 return -1;
389 }
390
391 dd->scale = 1.0;
392 dd->shift = 0.0;
393 dd->ignores_len = 0;
394 dd->ignores = NULL;
395
396 dd->ignorelist = ignorelist_create(/* invert = */ 1);
397 if (dd->ignorelist == NULL) {
398 sfree(dd->name);
399 sfree(dd);
400 ERROR("snmp plugin: ignorelist_create() failed.");
401 return ENOMEM;
402 }
403
404 dd->plugin_name = strdup("snmp");
405 if (dd->plugin_name == NULL) {
406 ERROR("snmp plugin: Can't allocate memory");
407 return ENOMEM;
408 }
409
410 for (int i = 0; i < ci->children_num; i++) {
411 oconfig_item_t *option = ci->children + i;
412
413 if (strcasecmp("Type", option->key) == 0)
414 status = cf_util_get_string(option, &dd->type);
415 else if (strcasecmp("Table", option->key) == 0)
416 status = cf_util_get_boolean(option, &dd->is_table);
417 else if (strcasecmp("Plugin", option->key) == 0)
418 status = cf_util_get_string(option, &dd->plugin_name);
419 else if (strcasecmp("Instance", option->key) == 0) {
420 if (dd->is_table) {
421 /* Instance is OID */
422 WARNING(
423 "snmp plugin: data %s: Option `Instance' is deprecated, please use "
424 "option `TypeInstanceOID'.",
425 dd->name);
426 status =
427 csnmp_config_configure_data_instance(&dd->type_instance, option);
428 } else {
429 /* Instance is a simple string */
430 WARNING(
431 "snmp plugin: data %s: Option `Instance' is deprecated, please use "
432 "option `TypeInstance'.",
433 dd->name);
434 status = cf_util_get_string(option, &dd->type_instance.value);
435 }
436 } else if (strcasecmp("InstancePrefix", option->key) == 0) {
437 WARNING("snmp plugin: data %s: Option `InstancePrefix' is deprecated, "
438 "please use option `TypeInstancePrefix'.",
439 dd->name);
440 status = cf_util_get_string(option, &dd->type_instance.prefix);
441 } else if (strcasecmp("PluginInstance", option->key) == 0)
442 status = cf_util_get_string(option, &dd->plugin_instance.value);
443 else if (strcasecmp("TypeInstance", option->key) == 0)
444 status = cf_util_get_string(option, &dd->type_instance.value);
445 else if (strcasecmp("PluginInstanceOID", option->key) == 0)
446 status =
447 csnmp_config_configure_data_instance(&dd->plugin_instance, option);
448 else if (strcasecmp("PluginInstancePrefix", option->key) == 0)
449 status = cf_util_get_string(option, &dd->plugin_instance.prefix);
450 else if (strcasecmp("TypeInstanceOID", option->key) == 0)
451 status = csnmp_config_configure_data_instance(&dd->type_instance, option);
452 else if (strcasecmp("TypeInstancePrefix", option->key) == 0)
453 status = cf_util_get_string(option, &dd->type_instance.prefix);
454 else if (strcasecmp("HostOID", option->key) == 0)
455 status = csnmp_config_configure_data_instance(&dd->host, option);
456 else if (strcasecmp("HostPrefix", option->key) == 0)
457 status = cf_util_get_string(option, &dd->host.prefix);
458 else if (strcasecmp("Values", option->key) == 0)
459 status = csnmp_config_add_data_values(dd, option);
460 else if (strcasecmp("Shift", option->key) == 0)
461 status = cf_util_get_double(option, &dd->shift);
462 else if (strcasecmp("Scale", option->key) == 0)
463 status = cf_util_get_double(option, &dd->scale);
464 else if (strcasecmp("Ignore", option->key) == 0)
465 status = csnmp_config_add_data_blacklist(dd, option);
466 else if (strcasecmp("InvertMatch", option->key) == 0)
467 status = cf_util_get_boolean(option, &dd->invert_match);
468 else if (strcasecmp("FilterOID", option->key) == 0) {
469 status = csnmp_config_add_data_filter_oid(dd, option);
470 } else if (strcasecmp("FilterValues", option->key) == 0) {
471 status = csnmp_config_add_data_filter_values(dd, option);
472 } else if (strcasecmp("FilterIgnoreSelected", option->key) == 0) {
473 bool t;
474 status = cf_util_get_boolean(option, &t);
475 if (status == 0)
476 ignorelist_set_invert(dd->ignorelist, /* invert = */ !t);
477 } else if (strcasecmp("Count", option->key) == 0)
478 status = cf_util_get_boolean(option, &dd->count);
479 else {
480 WARNING("snmp plugin: data %s: Option `%s' not allowed here.", dd->name,
481 option->key);
482 status = -1;
483 }
484
485 if (status != 0)
486 break;
487 } /* for (ci->children) */
488
489 while (status == 0) {
490 if (dd->is_table) {
491 /* Set type_instance to SUBID by default */
492 if (!dd->plugin_instance.configured && !dd->host.configured)
493 dd->type_instance.configured = true;
494
495 if (dd->plugin_instance.value && dd->plugin_instance.configured) {
496 WARNING(
497 "snmp plugin: data %s: Option `PluginInstance' will be ignored.",
498 dd->name);
499 }
500 if (dd->type_instance.value && dd->type_instance.configured) {
501 WARNING("snmp plugin: data %s: Option `TypeInstance' will be ignored.",
502 dd->name);
503 }
504 if (dd->type_instance.prefix && !dd->type_instance.configured) {
505 WARNING("snmp plugin: data %s: Option `TypeInstancePrefix' will be "
506 "ignored.",
507 dd->name);
508 }
509 if (dd->plugin_instance.prefix && !dd->plugin_instance.configured) {
510 WARNING("snmp plugin: data %s: Option `PluginInstancePrefix' will be "
511 "ignored.",
512 dd->name);
513 }
514 if (dd->host.prefix && !dd->host.configured) {
515 WARNING("snmp plugin: data %s: Option `HostPrefix' will be ignored.",
516 dd->name);
517 }
518 } else {
519 if (dd->count) {
520 WARNING("snmp plugin: data %s: Option `Count' is ignored when `Table' "
521 "set to `false'.",
522 dd->name);
523 }
524 }
525 if (!dd->is_table || dd->count) {
526 if (dd->plugin_instance.oid.oid_len > 0) {
527 WARNING("snmp plugin: data %s: Option `PluginInstanceOID' will be "
528 "ignored.",
529 dd->name);
530 }
531 if (dd->type_instance.oid.oid_len > 0) {
532 WARNING(
533 "snmp plugin: data %s: Option `TypeInstanceOID' will be ignored.",
534 dd->name);
535 }
536 if (dd->type_instance.prefix) {
537 WARNING("snmp plugin: data %s: Option `TypeInstancePrefix' is ignored "
538 "when `Table' set to `false' or `Count' set to `true'.",
539 dd->name);
540 }
541 if (dd->plugin_instance.prefix) {
542 WARNING("snmp plugin: data %s: Option `PluginInstancePrefix' is "
543 "ignored when `Table' set to `false' or `Count' set to `true'.",
544 dd->name);
545 }
546 if (dd->host.prefix) {
547 WARNING(
548 "snmp plugin: data %s: Option `HostPrefix' is ignored when `Table' "
549 "set to `false' or `Count' set to `true'.",
550 dd->name);
551 }
552 }
553
554 if (dd->type == NULL) {
555 WARNING("snmp plugin: `Type' not given for data `%s'", dd->name);
556 status = -1;
557 break;
558 }
559 if (dd->values == NULL) {
560 WARNING("snmp plugin: No `Value' given for data `%s'", dd->name);
561 status = -1;
562 break;
563 }
564
565 break;
566 } /* while (status == 0) */
567
568 if (status != 0) {
569 csnmp_data_definition_destroy(dd);
570 return -1;
571 }
572
573 DEBUG("snmp plugin: dd = { name = %s, type = %s, is_table = %s, values_len = "
574 "%" PRIsz ",",
575 dd->name, dd->type, (dd->is_table) ? "true" : "false", dd->values_len);
576
577 DEBUG("snmp plugin: plugin_instance = %s, type_instance = %s,",
578 dd->plugin_instance.value, dd->type_instance.value);
579
580 DEBUG("snmp plugin: type_instance_by_oid = %s, plugin_instance_by_oid "
581 "= %s }",
582 (dd->type_instance.oid.oid_len > 0)
583 ? "true"
584 : ((dd->type_instance.configured) ? "SUBID" : "false"),
585 (dd->plugin_instance.oid.oid_len > 0)
586 ? "true"
587 : ((dd->plugin_instance.configured) ? "SUBID" : "false"));
588
589 if (data_head == NULL)
590 data_head = dd;
591 else {
592 data_definition_t *last;
593 last = data_head;
594 while (last->next != NULL)
595 last = last->next;
596 last->next = dd;
597 }
598
599 return 0;
600 } /* int csnmp_config_add_data */
601
602 static int csnmp_config_add_host_version(host_definition_t *hd,
603 oconfig_item_t *ci) {
604 int version;
605
606 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_NUMBER)) {
607 WARNING("snmp plugin: The `Version' config option needs exactly one number "
608 "argument.");
609 return -1;
610 }
611
612 version = (int)ci->values[0].value.number;
613 if ((version < 1) || (version > 3)) {
614 WARNING("snmp plugin: `Version' must either be `1', `2', or `3'.");
615 return -1;
616 }
617
618 hd->version = version;
619
620 return 0;
621 } /* int csnmp_config_add_host_version */
622
623 static int csnmp_config_add_host_collect(host_definition_t *host,
624 oconfig_item_t *ci) {
625 data_definition_t *data;
626 data_definition_t **data_list;
627 int data_list_len;
628
629 if (ci->values_num < 1) {
630 WARNING("snmp plugin: `Collect' needs at least one argument.");
631 return -1;
632 }
633
634 for (int i = 0; i < ci->values_num; i++)
635 if (ci->values[i].type != OCONFIG_TYPE_STRING) {
636 WARNING("snmp plugin: All arguments to `Collect' must be strings.");
637 return -1;
638 }
639
640 data_list_len = host->data_list_len + ci->values_num;
641 data_list =
642 realloc(host->data_list, sizeof(data_definition_t *) * data_list_len);
643 if (data_list == NULL)
644 return -1;
645 host->data_list = data_list;
646
647 for (int i = 0; i < ci->values_num; i++) {
648 for (data = data_head; data != NULL; data = data->next)
649 if (strcasecmp(ci->values[i].value.string, data->name) == 0)
650 break;
651
652 if (data == NULL) {
653 WARNING("snmp plugin: No such data configured: `%s'",
654 ci->values[i].value.string);
655 continue;
656 }
657
658 DEBUG("snmp plugin: Collect: host = %s, data[%i] = %s;", host->name,
659 host->data_list_len, data->name);
660
661 host->data_list[host->data_list_len] = data;
662 host->data_list_len++;
663 } /* for (values_num) */
664
665 return 0;
666 } /* int csnmp_config_add_host_collect */
667
668 static int csnmp_config_add_host_auth_protocol(host_definition_t *hd,
669 oconfig_item_t *ci) {
670 char buffer[4];
671 int status;
672
673 status = cf_util_get_string_buffer(ci, buffer, sizeof(buffer));
674 if (status != 0)
675 return status;
676
677 if (strcasecmp("MD5", buffer) == 0) {
678 hd->auth_protocol = usmHMACMD5AuthProtocol;
679 hd->auth_protocol_len = sizeof(usmHMACMD5AuthProtocol) / sizeof(oid);
680 } else if (strcasecmp("SHA", buffer) == 0) {
681 hd->auth_protocol = usmHMACSHA1AuthProtocol;
682 hd->auth_protocol_len = sizeof(usmHMACSHA1AuthProtocol) / sizeof(oid);
683 } else {
684 WARNING("snmp plugin: The `AuthProtocol' config option must be `MD5' or "
685 "`SHA'.");
686 return -1;
687 }
688
689 DEBUG("snmp plugin: host = %s; host->auth_protocol = %s;", hd->name,
690 hd->auth_protocol == usmHMACMD5AuthProtocol ? "MD5" : "SHA");
691
692 return 0;
693 } /* int csnmp_config_add_host_auth_protocol */
694
695 static int csnmp_config_add_host_priv_protocol(host_definition_t *hd,
696 oconfig_item_t *ci) {
697 char buffer[4];
698 int status;
699
700 status = cf_util_get_string_buffer(ci, buffer, sizeof(buffer));
701 if (status != 0)
702 return status;
703
704 if (strcasecmp("AES", buffer) == 0) {
705 hd->priv_protocol = usmAESPrivProtocol;
706 hd->priv_protocol_len = sizeof(usmAESPrivProtocol) / sizeof(oid);
707 } else if (strcasecmp("DES", buffer) == 0) {
708 hd->priv_protocol = usmDESPrivProtocol;
709 hd->priv_protocol_len = sizeof(usmDESPrivProtocol) / sizeof(oid);
710 } else {
711 WARNING("snmp plugin: The `PrivProtocol' config option must be `AES' or "
712 "`DES'.");
713 return -1;
714 }
715
716 DEBUG("snmp plugin: host = %s; host->priv_protocol = %s;", hd->name,
717 hd->priv_protocol == usmAESPrivProtocol ? "AES" : "DES");
718
719 return 0;
720 } /* int csnmp_config_add_host_priv_protocol */
721
722 static int csnmp_config_add_host_security_level(host_definition_t *hd,
723 oconfig_item_t *ci) {
724 char buffer[16];
725 int status;
726
727 status = cf_util_get_string_buffer(ci, buffer, sizeof(buffer));
728 if (status != 0)
729 return status;
730
731 if (strcasecmp("noAuthNoPriv", buffer) == 0)
732 hd->security_level = SNMP_SEC_LEVEL_NOAUTH;
733 else if (strcasecmp("authNoPriv", buffer) == 0)
734 hd->security_level = SNMP_SEC_LEVEL_AUTHNOPRIV;
735 else if (strcasecmp("authPriv", buffer) == 0)
736 hd->security_level = SNMP_SEC_LEVEL_AUTHPRIV;
737 else {
738 WARNING("snmp plugin: The `SecurityLevel' config option must be "
739 "`noAuthNoPriv', `authNoPriv', or `authPriv'.");
740 return -1;
741 }
742
743 DEBUG("snmp plugin: host = %s; host->security_level = %d;", hd->name,
744 hd->security_level);
745
746 return 0;
747 } /* int csnmp_config_add_host_security_level */
748
749 static int csnmp_config_add_host(oconfig_item_t *ci) {
750 host_definition_t *hd;
751 int status = 0;
752
753 /* Registration stuff. */
754 cdtime_t interval = 0;
755 char cb_name[DATA_MAX_NAME_LEN];
756
757 hd = calloc(1, sizeof(*hd));
758 if (hd == NULL)
759 return -1;
760 hd->version = 2;
761 C_COMPLAIN_INIT(&hd->complaint);
762
763 status = cf_util_get_string(ci, &hd->name);
764 if (status != 0) {
765 sfree(hd);
766 return status;
767 }
768
769 hd->sess_handle = NULL;
770
771 /* These mean that we have not set a timeout or retry value */
772 hd->timeout = 0;
773 hd->retries = -1;
774 hd->bulk_size = 0;
775
776 for (int i = 0; i < ci->children_num; i++) {
777 oconfig_item_t *option = ci->children + i;
778
779 if (strcasecmp("Address", option->key) == 0)
780 status = cf_util_get_string(option, &hd->address);
781 else if (strcasecmp("Community", option->key) == 0)
782 status = cf_util_get_string(option, &hd->community);
783 else if (strcasecmp("Version", option->key) == 0)
784 status = csnmp_config_add_host_version(hd, option);
785 else if (strcasecmp("Timeout", option->key) == 0)
786 status = cf_util_get_cdtime(option, &hd->timeout);
787 else if (strcasecmp("Retries", option->key) == 0)
788 status = cf_util_get_int(option, &hd->retries);
789 else if (strcasecmp("Collect", option->key) == 0)
790 status = csnmp_config_add_host_collect(hd, option);
791 else if (strcasecmp("Interval", option->key) == 0)
792 status = cf_util_get_cdtime(option, &interval);
793 else if (strcasecmp("Username", option->key) == 0)
794 status = cf_util_get_string(option, &hd->username);
795 else if (strcasecmp("AuthProtocol", option->key) == 0)
796 status = csnmp_config_add_host_auth_protocol(hd, option);
797 else if (strcasecmp("PrivacyProtocol", option->key) == 0)
798 status = csnmp_config_add_host_priv_protocol(hd, option);
799 else if (strcasecmp("AuthPassphrase", option->key) == 0)
800 status = cf_util_get_string(option, &hd->auth_passphrase);
801 else if (strcasecmp("PrivacyPassphrase", option->key) == 0)
802 status = cf_util_get_string(option, &hd->priv_passphrase);
803 else if (strcasecmp("SecurityLevel", option->key) == 0)
804 status = csnmp_config_add_host_security_level(hd, option);
805 else if (strcasecmp("Context", option->key) == 0)
806 status = cf_util_get_string(option, &hd->context);
807 else if (strcasecmp("BulkSize", option->key) == 0)
808 status = cf_util_get_int(option, &hd->bulk_size);
809 else {
810 WARNING(
811 "snmp plugin: csnmp_config_add_host: Option `%s' not allowed here.",
812 option->key);
813 status = -1;
814 }
815
816 if (status != 0)
817 break;
818 } /* for (ci->children) */
819
820 while (status == 0) {
821 if (hd->address == NULL) {
822 WARNING("snmp plugin: `Address' not given for host `%s'", hd->name);
823 status = -1;
824 break;
825 }
826 if (hd->community == NULL && hd->version < 3) {
827 WARNING("snmp plugin: `Community' not given for host `%s'", hd->name);
828 status = -1;
829 break;
830 }
831 if (hd->bulk_size > 0 && hd->version < 2) {
832 WARNING("snmp plugin: Bulk transfers is only available for SNMP v2 and "
833 "later, host '%s' is configured as version '%d'",
834 hd->name, hd->version);
835 }
836 if (hd->version == 3) {
837 if (hd->username == NULL) {
838 WARNING("snmp plugin: `Username' not given for host `%s'", hd->name);
839 status = -1;
840 break;
841 }
842 if (hd->security_level == 0) {
843 WARNING("snmp plugin: `SecurityLevel' not given for host `%s'",
844 hd->name);
845 status = -1;
846 break;
847 }
848 if (hd->security_level == SNMP_SEC_LEVEL_AUTHNOPRIV ||
849 hd->security_level == SNMP_SEC_LEVEL_AUTHPRIV) {
850 if (hd->auth_protocol == NULL) {
851 WARNING("snmp plugin: `AuthProtocol' not given for host `%s'",
852 hd->name);
853 status = -1;
854 break;
855 }
856 if (hd->auth_passphrase == NULL) {
857 WARNING("snmp plugin: `AuthPassphrase' not given for host `%s'",
858 hd->name);
859 status = -1;
860 break;
861 }
862 }
863 if (hd->security_level == SNMP_SEC_LEVEL_AUTHPRIV) {
864 if (hd->priv_protocol == NULL) {
865 WARNING("snmp plugin: `PrivacyProtocol' not given for host `%s'",
866 hd->name);
867 status = -1;
868 break;
869 }
870 if (hd->priv_passphrase == NULL) {
871 WARNING("snmp plugin: `PrivacyPassphrase' not given for host `%s'",
872 hd->name);
873 status = -1;
874 break;
875 }
876 }
877 }
878
879 break;
880 } /* while (status == 0) */
881
882 if (status != 0) {
883 csnmp_host_definition_destroy(hd);
884 return -1;
885 }
886
887 DEBUG("snmp plugin: hd = { name = %s, address = %s, community = %s, version "
888 "= %i }",
889 hd->name, hd->address, hd->community, hd->version);
890
891 ssnprintf(cb_name, sizeof(cb_name), "snmp-%s", hd->name);
892
893 status = plugin_register_complex_read(
894 /* group = */ NULL, cb_name, csnmp_read_host, interval,
895 &(user_data_t){
896 .data = hd,
897 .free_func = csnmp_host_definition_destroy,
898 });
899 if (status != 0) {
900 ERROR("snmp plugin: Registering complex read function failed.");
901 return -1;
902 }
903
904 return 0;
905 } /* int csnmp_config_add_host */
906
907 static int csnmp_config(oconfig_item_t *ci) {
908 call_snmp_init_once();
909
910 for (int i = 0; i < ci->children_num; i++) {
911 oconfig_item_t *child = ci->children + i;
912 if (strcasecmp("Data", child->key) == 0)
913 csnmp_config_add_data(child);
914 else if (strcasecmp("Host", child->key) == 0)
915 csnmp_config_add_host(child);
916 else {
917 WARNING("snmp plugin: Ignoring unknown config option `%s'.", child->key);
918 }
919 } /* for (ci->children) */
920
921 return 0;
922 } /* int csnmp_config */
923
924 /* }}} End of the config stuff. Now the interesting part begins */
925
926 static void csnmp_host_open_session(host_definition_t *host) {
927 struct snmp_session sess;
928 int error;
929
930 if (host->sess_handle != NULL)
931 csnmp_host_close_session(host);
932
933 snmp_sess_init(&sess);
934 sess.peername = host->address;
935 switch (host->version) {
936 case 1:
937 sess.version = SNMP_VERSION_1;
938 break;
939 case 3:
940 sess.version = SNMP_VERSION_3;
941 break;
942 default:
943 sess.version = SNMP_VERSION_2c;
944 break;
945 }
946
947 if (host->version == 3) {
948 sess.securityName = host->username;
949 sess.securityNameLen = strlen(host->username);
950 sess.securityLevel = host->security_level;
951
952 if (sess.securityLevel == SNMP_SEC_LEVEL_AUTHNOPRIV ||
953 sess.securityLevel == SNMP_SEC_LEVEL_AUTHPRIV) {
954 sess.securityAuthProto = host->auth_protocol;
955 sess.securityAuthProtoLen = host->auth_protocol_len;
956 sess.securityAuthKeyLen = USM_AUTH_KU_LEN;
957 error = generate_Ku(sess.securityAuthProto, sess.securityAuthProtoLen,
958 (u_char *)host->auth_passphrase,
959 strlen(host->auth_passphrase), sess.securityAuthKey,
960 &sess.securityAuthKeyLen);
961 if (error != SNMPERR_SUCCESS) {
962 ERROR("snmp plugin: host %s: Error generating Ku from auth_passphrase. "
963 "(Error %d)",
964 host->name, error);
965 }
966 }
967
968 if (sess.securityLevel == SNMP_SEC_LEVEL_AUTHPRIV) {
969 sess.securityPrivProto = host->priv_protocol;
970 sess.securityPrivProtoLen = host->priv_protocol_len;
971 sess.securityPrivKeyLen = USM_PRIV_KU_LEN;
972 error = generate_Ku(sess.securityAuthProto, sess.securityAuthProtoLen,
973 (u_char *)host->priv_passphrase,
974 strlen(host->priv_passphrase), sess.securityPrivKey,
975 &sess.securityPrivKeyLen);
976 if (error != SNMPERR_SUCCESS) {
977 ERROR("snmp plugin: host %s: Error generating Ku from priv_passphrase. "
978 "(Error %d)",
979 host->name, error);
980 }
981 }
982
983 if (host->context != NULL) {
984 sess.contextName = host->context;
985 sess.contextNameLen = strlen(host->context);
986 }
987 } else /* SNMPv1/2 "authenticates" with community string */
988 {
989 sess.community = (u_char *)host->community;
990 sess.community_len = strlen(host->community);
991 }
992
993 /* Set timeout & retries, if they have been changed from the default */
994 if (host->timeout != 0) {
995 /* net-snmp expects microseconds */
996 sess.timeout = CDTIME_T_TO_US(host->timeout);
997 }
998 if (host->retries >= 0) {
999 sess.retries = host->retries;
1000 }
1001
1002 /* snmp_sess_open will copy the `struct snmp_session *'. */
1003 host->sess_handle = snmp_sess_open(&sess);
1004
1005 if (host->sess_handle == NULL) {
1006 char *errstr = NULL;
1007
1008 snmp_error(&sess, NULL, NULL, &errstr);
1009
1010 ERROR("snmp plugin: host %s: snmp_sess_open failed: %s", host->name,
1011 (errstr == NULL) ? "Unknown problem" : errstr);
1012 sfree(errstr);
1013 }
1014 } /* void csnmp_host_open_session */
1015
1016 /* TODO: Check if negative values wrap around. Problem: negative temperatures.
1017 */
1018 static value_t csnmp_value_list_to_value(const struct variable_list *vl,
1019 int type, double scale, double shift,
1020 const char *host_name,
1021 const char *data_name) {
1022 value_t ret;
1023 uint64_t tmp_unsigned = 0;
1024 int64_t tmp_signed = 0;
1025 bool defined = 1;
1026 /* Set to true when the original SNMP type appears to have been signed. */
1027 bool prefer_signed = 0;
1028
1029 if ((vl->type == ASN_INTEGER) || (vl->type == ASN_UINTEGER) ||
1030 (vl->type == ASN_COUNTER)
1031 #ifdef ASN_TIMETICKS
1032 || (vl->type == ASN_TIMETICKS)
1033 #endif
1034 || (vl->type == ASN_GAUGE)) {
1035 tmp_unsigned = (uint32_t)*vl->val.integer;
1036 tmp_signed = (int32_t)*vl->val.integer;
1037
1038 if (vl->type == ASN_INTEGER)
1039 prefer_signed = 1;
1040
1041 DEBUG("snmp plugin: Parsed int32 value is %" PRIu64 ".", tmp_unsigned);
1042 } else if (vl->type == ASN_COUNTER64) {
1043 tmp_unsigned = (uint32_t)vl->val.counter64->high;
1044 tmp_unsigned = tmp_unsigned << 32;
1045 tmp_unsigned += (uint32_t)vl->val.counter64->low;
1046 tmp_signed = (int64_t)tmp_unsigned;
1047 DEBUG("snmp plugin: Parsed int64 value is %" PRIu64 ".", tmp_unsigned);
1048 } else if (vl->type == ASN_OCTET_STR) {
1049 /* We'll handle this later.. */
1050 } else {
1051 char oid_buffer[1024] = {0};
1052
1053 snprint_objid(oid_buffer, sizeof(oid_buffer) - 1, vl->name,
1054 vl->name_length);
1055
1056 #ifdef ASN_NULL
1057 if (vl->type == ASN_NULL)
1058 INFO("snmp plugin: OID \"%s\" is undefined (type ASN_NULL)", oid_buffer);
1059 else
1060 #endif
1061 WARNING("snmp plugin: I don't know the ASN type #%i "
1062 "(OID: \"%s\", data block \"%s\", host block \"%s\")",
1063 (int)vl->type, oid_buffer,
1064 (data_name != NULL) ? data_name : "UNKNOWN",
1065 (host_name != NULL) ? host_name : "UNKNOWN");
1066
1067 defined = 0;
1068 }
1069
1070 if (vl->type == ASN_OCTET_STR) {
1071 int status = -1;
1072
1073 if (vl->val.string != NULL) {
1074 char string[64];
1075 size_t string_length;
1076
1077 string_length = sizeof(string) - 1;
1078 if (vl->val_len < string_length)
1079 string_length = vl->val_len;
1080
1081 /* The strings we get from the Net-SNMP library may not be null
1082 * terminated. That is why we're using `memcpy' here and not `strcpy'.
1083 * `string_length' is set to `vl->val_len' which holds the length of the
1084 * string. -octo */
1085 memcpy(string, vl->val.string, string_length);
1086 string[string_length] = 0;
1087
1088 status = parse_value(string, &ret, type);
1089 if (status != 0) {
1090 ERROR("snmp plugin: host %s: csnmp_value_list_to_value: Parsing string "
1091 "as %s failed: %s",
1092 (host_name != NULL) ? host_name : "UNKNOWN",
1093 DS_TYPE_TO_STRING(type), string);
1094 }
1095 }
1096
1097 if (status != 0) {
1098 switch (type) {
1099 case DS_TYPE_COUNTER:
1100 case DS_TYPE_DERIVE:
1101 case DS_TYPE_ABSOLUTE:
1102 memset(&ret, 0, sizeof(ret));
1103 break;
1104
1105 case DS_TYPE_GAUGE:
1106 ret.gauge = NAN;
1107 break;
1108
1109 default:
1110 ERROR("snmp plugin: csnmp_value_list_to_value: Unknown "
1111 "data source type: %i.",
1112 type);
1113 ret.gauge = NAN;
1114 }
1115 }
1116 } /* if (vl->type == ASN_OCTET_STR) */
1117 else if (type == DS_TYPE_COUNTER) {
1118 ret.counter = tmp_unsigned;
1119 } else if (type == DS_TYPE_GAUGE) {
1120 if (!defined)
1121 ret.gauge = NAN;
1122 else if (prefer_signed)
1123 ret.gauge = (scale * tmp_signed) + shift;
1124 else
1125 ret.gauge = (scale * tmp_unsigned) + shift;
1126 } else if (type == DS_TYPE_DERIVE) {
1127 if (prefer_signed)
1128 ret.derive = (derive_t)tmp_signed;
1129 else
1130 ret.derive = (derive_t)tmp_unsigned;
1131 } else if (type == DS_TYPE_ABSOLUTE) {
1132 ret.absolute = (absolute_t)tmp_unsigned;
1133 } else {
1134 ERROR("snmp plugin: csnmp_value_list_to_value: Unknown data source "
1135 "type: %i.",
1136 type);
1137 ret.gauge = NAN;
1138 }
1139
1140 return ret;
1141 } /* value_t csnmp_value_list_to_value */
1142
1143 /* csnmp_strvbcopy_hexstring converts the bit string contained in "vb" to a hex
1144 * representation and writes it to dst. Returns zero on success and ENOMEM if
1145 * dst is not large enough to hold the string. dst is guaranteed to be
1146 * nul-terminated. */
1147 static int csnmp_strvbcopy_hexstring(char *dst, /* {{{ */
1148 const struct variable_list *vb,
1149 size_t dst_size) {
1150 char *buffer_ptr;
1151 size_t buffer_free;
1152
1153 dst[0] = 0;
1154
1155 buffer_ptr = dst;
1156 buffer_free = dst_size;
1157
1158 for (size_t i = 0; i < vb->val_len; i++) {
1159 int status;
1160
1161 status = ssnprintf(buffer_ptr, buffer_free, (i == 0) ? "%02x" : ":%02x",
1162 (unsigned int)vb->val.bitstring[i]);
1163 assert(status >= 0);
1164
1165 if (((size_t)status) >= buffer_free) /* truncated */
1166 {
1167 dst[dst_size - 1] = '\0';
1168 return ENOMEM;
1169 } else /* if (status < buffer_free) */
1170 {
1171 buffer_ptr += (size_t)status;
1172 buffer_free -= (size_t)status;
1173 }
1174 }
1175
1176 return 0;
1177 } /* }}} int csnmp_strvbcopy_hexstring */
1178
1179 /* csnmp_strvbcopy copies the octet string or bit string contained in vb to
1180 * dst. If non-printable characters are detected, it will switch to a hex
1181 * representation of the string. Returns zero on success, EINVAL if vb does not
1182 * contain a string and ENOMEM if dst is not large enough to contain the
1183 * string. */
1184 static int csnmp_strvbcopy(char *dst, /* {{{ */
1185 const struct variable_list *vb, size_t dst_size) {
1186 char *src;
1187 size_t num_chars;
1188
1189 if (vb->type == ASN_OCTET_STR)
1190 src = (char *)vb->val.string;
1191 else if (vb->type == ASN_BIT_STR)
1192 src = (char *)vb->val.bitstring;
1193 else if (vb->type == ASN_IPADDRESS) {
1194 return ssnprintf(dst, dst_size,
1195 "%" PRIu8 ".%" PRIu8 ".%" PRIu8 ".%" PRIu8 "",
1196 (uint8_t)vb->val.string[0], (uint8_t)vb->val.string[1],
1197 (uint8_t)vb->val.string[2], (uint8_t)vb->val.string[3]);
1198 } else {
1199 dst[0] = 0;
1200 return EINVAL;
1201 }
1202
1203 num_chars = dst_size - 1;
1204 if (num_chars > vb->val_len)
1205 num_chars = vb->val_len;
1206
1207 for (size_t i = 0; i < num_chars; i++) {
1208 /* Check for control characters. */
1209 if ((unsigned char)src[i] < 32)
1210 return csnmp_strvbcopy_hexstring(dst, vb, dst_size);
1211 dst[i] = src[i];
1212 }
1213 dst[num_chars] = 0;
1214 dst[dst_size - 1] = '\0';
1215
1216 if (dst_size <= vb->val_len)
1217 return ENOMEM;
1218
1219 return 0;
1220 } /* }}} int csnmp_strvbcopy */
1221
1222 static csnmp_cell_char_t *csnmp_get_char_cell(const struct variable_list *vb,
1223 const oid_t *root_oid,
1224 const host_definition_t *hd,
1225 const data_definition_t *dd) {
1226
1227 if (vb == NULL)
1228 return NULL;
1229
1230 csnmp_cell_char_t *il = calloc(1, sizeof(*il));
1231 if (il == NULL) {
1232 ERROR("snmp plugin: calloc failed.");
1233 return NULL;
1234 }
1235 il->next = NULL;
1236
1237 oid_t vb_name;
1238 csnmp_oid_init(&vb_name, vb->name, vb->name_length);
1239
1240 if (csnmp_oid_suffix(&il->suffix, &vb_name, root_oid) != 0) {
1241 sfree(il);
1242 return NULL;
1243 }
1244
1245 /* Get value */
1246 if ((vb->type == ASN_OCTET_STR) || (vb->type == ASN_BIT_STR) ||
1247 (vb->type == ASN_IPADDRESS)) {
1248
1249 csnmp_strvbcopy(il->value, vb, sizeof(il->value));
1250
1251 } else {
1252 value_t val = csnmp_value_list_to_value(
1253 vb, DS_TYPE_COUNTER,
1254 /* scale = */ 1.0, /* shift = */ 0.0, hd->name, dd->name);
1255 ssnprintf(il->value, sizeof(il->value), "%" PRIu64, (uint64_t)val.counter);
1256 }
1257
1258 return il;
1259 } /* csnmp_cell_char_t csnmp_get_char_cell */
1260
1261 static void csnmp_cells_append(csnmp_cell_char_t **head,
1262 csnmp_cell_char_t **tail,
1263 csnmp_cell_char_t *il) {
1264 if (*head == NULL)
1265 *head = il;
1266 else
1267 (*tail)->next = il;
1268 *tail = il;
1269 } /* void csnmp_cells_append */
1270
1271 static bool csnmp_ignore_instance(csnmp_cell_char_t *cell,
1272 const data_definition_t *dd) {
1273 bool is_matched = 0;
1274 for (uint32_t i = 0; i < dd->ignores_len; i++) {
1275 int status = fnmatch(dd->ignores[i], cell->value, 0);
1276 if (status == 0) {
1277 if (!dd->invert_match) {
1278 return 1;
1279 } else {
1280 is_matched = 1;
1281 break;
1282 }
1283 }
1284 }
1285 if (dd->invert_match && !is_matched) {
1286 return 1;
1287 }
1288 return 0;
1289 } /* bool csnmp_ignore_instance */
1290
1291 static void csnmp_cell_replace_reserved_chars(csnmp_cell_char_t *cell) {
1292 for (char *ptr = cell->value; *ptr != '\0'; ptr++) {
1293 if ((*ptr > 0) && (*ptr < 32))
1294 *ptr = ' ';
1295 else if (*ptr == '/')
1296 *ptr = '_';
1297 }
1298 } /* void csnmp_cell_replace_reserved_chars */
1299
1300 static int csnmp_dispatch_table(host_definition_t *host,
1301 data_definition_t *data,
1302 csnmp_cell_char_t *type_instance_cells,
1303 csnmp_cell_char_t *plugin_instance_cells,
1304 csnmp_cell_char_t *hostname_cells,
1305 csnmp_cell_char_t *filter_cells,
1306 csnmp_cell_value_t **value_cells,
1307 bool count_values) {
1308 const data_set_t *ds;
1309 value_list_t vl = VALUE_LIST_INIT;
1310
1311 csnmp_cell_char_t *type_instance_cell_ptr = type_instance_cells;
1312 csnmp_cell_char_t *plugin_instance_cell_ptr = plugin_instance_cells;
1313 csnmp_cell_char_t *hostname_cell_ptr = hostname_cells;
1314 csnmp_cell_char_t *filter_cell_ptr = filter_cells;
1315 csnmp_cell_value_t *value_cell_ptr[data->values_len];
1316
1317 size_t i;
1318 bool have_more;
1319 oid_t current_suffix;
1320 size_t count;
1321
1322 ds = plugin_get_ds(data->type);
1323 if (!ds) {
1324 ERROR("snmp plugin: DataSet `%s' not defined.", data->type);
1325 return -1;
1326 }
1327 if (count_values) {
1328 assert(ds->ds_num == 1);
1329 count = 0;
1330 } else
1331 assert(ds->ds_num == data->values_len);
1332 assert(data->values_len > 0);
1333
1334 for (i = 0; i < data->values_len; i++)
1335 value_cell_ptr[i] = value_cells[i];
1336
1337 sstrncpy(vl.plugin, data->plugin_name, sizeof(vl.plugin));
1338 sstrncpy(vl.type, data->type, sizeof(vl.type));
1339
1340 have_more = 1;
1341 while (have_more) {
1342 bool suffix_skipped = 0;
1343
1344 /* Determine next suffix to handle. */
1345 if (type_instance_cells != NULL) {
1346 if (type_instance_cell_ptr == NULL) {
1347 have_more = 0;
1348 continue;
1349 }
1350
1351 memcpy(&current_suffix, &type_instance_cell_ptr->suffix,
1352 sizeof(current_suffix));
1353 } else {
1354 /* no instance configured */
1355 csnmp_cell_value_t *ptr = value_cell_ptr[0];
1356 if (ptr == NULL) {
1357 have_more = 0;
1358 continue;
1359 }
1360
1361 memcpy(&current_suffix, &ptr->suffix, sizeof(current_suffix));
1362 }
1363
1364 /*
1365 char oid_buffer[1024] = {0};
1366 snprint_objid(oid_buffer, sizeof(oid_buffer) - 1, current_suffix.oid,
1367 current_suffix.oid_len);
1368 DEBUG("SNMP PLUGIN: SUFFIX %s", oid_buffer);
1369 */
1370
1371 /* Update plugin_instance_cell_ptr to point expected suffix */
1372 if (plugin_instance_cells != NULL) {
1373 while ((plugin_instance_cell_ptr != NULL) &&
1374 (csnmp_oid_compare(&plugin_instance_cell_ptr->suffix,
1375 &current_suffix) < 0))
1376 plugin_instance_cell_ptr = plugin_instance_cell_ptr->next;
1377
1378 if (plugin_instance_cell_ptr == NULL) {
1379 have_more = 0;
1380 continue;
1381 }
1382
1383 if (csnmp_oid_compare(&plugin_instance_cell_ptr->suffix,
1384 &current_suffix) > 0) {
1385 /* This suffix is missing in the subtree. Indicate this with the
1386 * "suffix_skipped" flag and try the next instance / suffix. */
1387 suffix_skipped = 1;
1388 }
1389 }
1390
1391 /* Update hostname_cell_ptr to point expected suffix */
1392 if (hostname_cells != NULL) {
1393 while (
1394 (hostname_cell_ptr != NULL) &&
1395 (csnmp_oid_compare(&hostname_cell_ptr->suffix, &current_suffix) < 0))
1396 hostname_cell_ptr = hostname_cell_ptr->next;
1397
1398 if (hostname_cell_ptr == NULL) {
1399 have_more = 0;
1400 continue;
1401 }
1402
1403 if (csnmp_oid_compare(&hostname_cell_ptr->suffix, &current_suffix) > 0) {
1404 /* This suffix is missing in the subtree. Indicate this with the
1405 * "suffix_skipped" flag and try the next instance / suffix. */
1406 suffix_skipped = 1;
1407 }
1408 }
1409
1410 /* Update filter_cell_ptr to point expected suffix */
1411 if (filter_cells != NULL) {
1412 while ((filter_cell_ptr != NULL) &&
1413 (csnmp_oid_compare(&filter_cell_ptr->suffix, &current_suffix) < 0))
1414 filter_cell_ptr = filter_cell_ptr->next;
1415
1416 if (filter_cell_ptr == NULL) {
1417 have_more = 0;
1418 continue;
1419 }
1420
1421 if (csnmp_oid_compare(&filter_cell_ptr->suffix, &current_suffix) > 0) {
1422 /* This suffix is missing in the subtree. Indicate this with the
1423 * "suffix_skipped" flag and try the next instance / suffix. */
1424 suffix_skipped = 1;
1425 }
1426 }
1427
1428 /* Update all the value_cell_ptr to point at the entry with the same
1429 * trailing partial OID */
1430 for (i = 0; i < data->values_len; i++) {
1431 while (
1432 (value_cell_ptr[i] != NULL) &&
1433 (csnmp_oid_compare(&value_cell_ptr[i]->suffix, &current_suffix) < 0))
1434 value_cell_ptr[i] = value_cell_ptr[i]->next;
1435
1436 if (value_cell_ptr[i] == NULL) {
1437 have_more = 0;
1438 break;
1439 } else if (csnmp_oid_compare(&value_cell_ptr[i]->suffix,
1440 &current_suffix) > 0) {
1441 /* This suffix is missing in the subtree. Indicate this with the
1442 * "suffix_skipped" flag and try the next instance / suffix. */
1443 suffix_skipped = 1;
1444 break;
1445 }
1446 } /* for (i = 0; i < columns; i++) */
1447
1448 if (!have_more)
1449 break;
1450
1451 /* Matching the values failed. Start from the beginning again. */
1452 if (suffix_skipped) {
1453 if (type_instance_cells != NULL)
1454 type_instance_cell_ptr = type_instance_cell_ptr->next;
1455 else
1456 value_cell_ptr[0] = value_cell_ptr[0]->next;
1457
1458 continue;
1459 }
1460
1461 /* if we reach this line, all value_cell_ptr[i] are non-NULL and are set
1462 * to the same subid. type_instance_cell_ptr is either NULL or points to the
1463 * same subid, too. */
1464 #if COLLECT_DEBUG
1465 for (i = 1; i < data->values_len; i++) {
1466 assert(value_cell_ptr[i] != NULL);
1467 assert(csnmp_oid_compare(&value_cell_ptr[i - 1]->suffix,
1468 &value_cell_ptr[i]->suffix) == 0);
1469 }
1470 assert((type_instance_cell_ptr == NULL) ||
1471 (csnmp_oid_compare(&type_instance_cell_ptr->suffix,
1472 &value_cell_ptr[0]->suffix) == 0));
1473 assert((plugin_instance_cell_ptr == NULL) ||
1474 (csnmp_oid_compare(&plugin_instance_cell_ptr->suffix,
1475 &value_cell_ptr[0]->suffix) == 0));
1476 assert((hostname_cell_ptr == NULL) ||
1477 (csnmp_oid_compare(&hostname_cell_ptr->suffix,
1478 &value_cell_ptr[0]->suffix) == 0));
1479 assert((filter_cell_ptr == NULL) ||
1480 (csnmp_oid_compare(&filter_cell_ptr->suffix,
1481 &value_cell_ptr[0]->suffix) == 0));
1482 #endif
1483
1484 /* Check the value in filter column */
1485 if (filter_cell_ptr &&
1486 ignorelist_match(data->ignorelist, filter_cell_ptr->value) != 0) {
1487 if (type_instance_cells != NULL)
1488 type_instance_cell_ptr = type_instance_cell_ptr->next;
1489 else
1490 value_cell_ptr[0] = value_cell_ptr[0]->next;
1491
1492 continue;
1493 }
1494
1495 if (count_values) {
1496 count++;
1497 } else {
1498 /* set vl.host */
1499 if (data->host.configured) {
1500 char temp[DATA_MAX_NAME_LEN];
1501 if (hostname_cell_ptr == NULL)
1502 csnmp_oid_to_string(temp, sizeof(temp), &current_suffix);
1503 else
1504 sstrncpy(temp, hostname_cell_ptr->value, sizeof(temp));
1505
1506 if (data->host.prefix == NULL)
1507 sstrncpy(vl.host, temp, sizeof(vl.host));
1508 else
1509 ssnprintf(vl.host, sizeof(vl.host), "%s%s", data->host.prefix, temp);
1510 } else {
1511 sstrncpy(vl.host, host->name, sizeof(vl.host));
1512 }
1513
1514 /* set vl.type_instance */
1515 if (data->type_instance.configured) {
1516 char temp[DATA_MAX_NAME_LEN];
1517 if (type_instance_cell_ptr == NULL)
1518 csnmp_oid_to_string(temp, sizeof(temp), &current_suffix);
1519 else
1520 sstrncpy(temp, type_instance_cell_ptr->value, sizeof(temp));
1521
1522 if (data->type_instance.prefix == NULL)
1523 sstrncpy(vl.type_instance, temp, sizeof(vl.type_instance));
1524 else
1525 ssnprintf(vl.type_instance, sizeof(vl.type_instance), "%s%s",
1526 data->type_instance.prefix, temp);
1527 } else if (data->type_instance.value) {
1528 sstrncpy(vl.type_instance, data->type_instance.value,
1529 sizeof(vl.type_instance));
1530 }
1531
1532 /* set vl.plugin_instance */
1533 if (data->plugin_instance.configured) {
1534 char temp[DATA_MAX_NAME_LEN];
1535 if (plugin_instance_cell_ptr == NULL)
1536 csnmp_oid_to_string(temp, sizeof(temp), &current_suffix);
1537 else
1538 sstrncpy(temp, plugin_instance_cell_ptr->value, sizeof(temp));
1539
1540 if (data->plugin_instance.prefix == NULL)
1541 sstrncpy(vl.plugin_instance, temp, sizeof(vl.plugin_instance));
1542 else
1543 ssnprintf(vl.plugin_instance, sizeof(vl.plugin_instance), "%s%s",
1544 data->plugin_instance.prefix, temp);
1545 } else if (data->plugin_instance.value) {
1546 sstrncpy(vl.plugin_instance, data->plugin_instance.value,
1547 sizeof(vl.plugin_instance));
1548 }
1549
1550 vl.values_len = data->values_len;
1551 value_t values[vl.values_len];
1552 vl.values = values;
1553
1554 for (i = 0; i < data->values_len; i++)
1555 vl.values[i] = value_cell_ptr[i]->value;
1556
1557 plugin_dispatch_values(&vl);
1558
1559 /* prevent leakage of pointer to local variable. */
1560 vl.values_len = 0;
1561 vl.values = NULL;
1562 }
1563
1564 if (type_instance_cells != NULL)
1565 type_instance_cell_ptr = type_instance_cell_ptr->next;
1566 else
1567 value_cell_ptr[0] = value_cell_ptr[0]->next;
1568 } /* while (have_more) */
1569
1570 if (count_values) {
1571 /* the first `ds' means `data set', the second means `data source' */
1572 int type = ds->ds[0].type;
1573 sstrncpy(vl.host, host->name, sizeof(vl.host));
1574 sstrncpy(vl.plugin, data->plugin_name, sizeof(vl.plugin));
1575 sstrncpy(vl.type, data->type, sizeof(vl.type));
1576 if (data->type_instance.value)
1577 sstrncpy(vl.type_instance, data->type_instance.value,
1578 sizeof(vl.type_instance));
1579 if (data->plugin_instance.value)
1580 sstrncpy(vl.plugin_instance, data->plugin_instance.value,
1581 sizeof(vl.plugin_instance));
1582 vl.values_len = 1;
1583 vl.values = malloc(sizeof(*vl.values));
1584 if (vl.values == NULL)
1585 return -1;
1586 switch (type) {
1587 case DS_TYPE_COUNTER:
1588 vl.values[0].counter = count;
1589 break;
1590 case DS_TYPE_GAUGE:
1591 vl.values[0].gauge = count;
1592 break;
1593 case DS_TYPE_DERIVE:
1594 vl.values[0].derive = count;
1595 break;
1596 case DS_TYPE_ABSOLUTE:
1597 vl.values[0].absolute = count;
1598 break;
1599 default:
1600 ERROR("snmp plugin: csnmp_dispatch_table: Unknown "
1601 "data source type: %i.",
1602 type);
1603 }
1604 plugin_dispatch_values(&vl);
1605 sfree(vl.values);
1606 }
1607
1608 return 0;
1609 } /* int csnmp_dispatch_table */
1610
1611 static int csnmp_read_table(host_definition_t *host, data_definition_t *data) {
1612 struct snmp_pdu *req;
1613 struct snmp_pdu *res = NULL;
1614 struct variable_list *vb;
1615
1616 const data_set_t *ds;
1617
1618 size_t oid_list_len = data->values_len;
1619
1620 if (data->type_instance.oid.oid_len > 0)
1621 oid_list_len++;
1622
1623 if (data->plugin_instance.oid.oid_len > 0)
1624 oid_list_len++;
1625
1626 if (data->host.oid.oid_len > 0)
1627 oid_list_len++;
1628
1629 if (data->filter_oid.oid_len > 0)
1630 oid_list_len++;
1631
1632 /* Holds the last OID returned by the device. We use this in the GETNEXT
1633 * request to proceed. */
1634 oid_t oid_list[oid_list_len];
1635 /* Set to false when an OID has left its subtree so we don't re-request it
1636 * again. */
1637 csnmp_oid_type_t oid_list_todo[oid_list_len];
1638
1639 int status;
1640 size_t i;
1641
1642 /* `value_list_head' and `value_cells_tail' implement a linked list for each
1643 * value. `instance_cells_head' and `instance_cells_tail' implement a linked
1644 * list of instance names. This is used to jump gaps in the table. */
1645 csnmp_cell_char_t *type_instance_cells_head = NULL;
1646 csnmp_cell_char_t *type_instance_cells_tail = NULL;
1647 csnmp_cell_char_t *plugin_instance_cells_head = NULL;
1648 csnmp_cell_char_t *plugin_instance_cells_tail = NULL;
1649 csnmp_cell_char_t *hostname_cells_head = NULL;
1650 csnmp_cell_char_t *hostname_cells_tail = NULL;
1651 csnmp_cell_char_t *filter_cells_head = NULL;
1652 csnmp_cell_char_t *filter_cells_tail = NULL;
1653 csnmp_cell_value_t **value_cells_head;
1654 csnmp_cell_value_t **value_cells_tail;
1655
1656 DEBUG("snmp plugin: csnmp_read_table (host = %s, data = %s)", host->name,
1657 data->name);
1658
1659 if (host->sess_handle == NULL) {
1660 DEBUG("snmp plugin: csnmp_read_table: host->sess_handle == NULL");
1661 return -1;
1662 }
1663
1664 ds = plugin_get_ds(data->type);
1665 if (!ds) {
1666 ERROR("snmp plugin: DataSet `%s' not defined.", data->type);
1667 return -1;
1668 }
1669
1670 if (data->count) {
1671 if (ds->ds_num != 1) {
1672 ERROR("snmp plugin: DataSet `%s' requires %" PRIsz
1673 " values, but `Count' option only delivers one",
1674 data->type, ds->ds_num);
1675 return -1;
1676 }
1677 } else {
1678 if (ds->ds_num != data->values_len) {
1679 ERROR("snmp plugin: DataSet `%s' requires %" PRIsz
1680 " values, but config talks "
1681 "about %" PRIsz,
1682 data->type, ds->ds_num, data->values_len);
1683 return -1;
1684 }
1685 }
1686 assert(data->values_len > 0);
1687
1688 for (i = 0; i < data->values_len; i++)
1689 oid_list_todo[i] = OID_TYPE_VARIABLE;
1690
1691 /* We need a copy of all the OIDs, because GETNEXT will destroy them. */
1692 memcpy(oid_list, data->values, data->values_len * sizeof(oid_t));
1693
1694 if (data->type_instance.oid.oid_len > 0) {
1695 memcpy(oid_list + i, &data->type_instance.oid, sizeof(oid_t));
1696 oid_list_todo[i] = OID_TYPE_TYPEINSTANCE;
1697 i++;
1698 }
1699
1700 if (data->plugin_instance.oid.oid_len > 0) {
1701 memcpy(oid_list + i, &data->plugin_instance.oid, sizeof(oid_t));
1702 oid_list_todo[i] = OID_TYPE_PLUGININSTANCE;
1703 i++;
1704 }
1705
1706 if (data->host.oid.oid_len > 0) {
1707 memcpy(oid_list + i, &data->host.oid, sizeof(oid_t));
1708 oid_list_todo[i] = OID_TYPE_HOST;
1709 i++;
1710 }
1711
1712 if (data->filter_oid.oid_len > 0) {
1713 memcpy(oid_list + i, &data->filter_oid, sizeof(oid_t));
1714 oid_list_todo[i] = OID_TYPE_FILTER;
1715 i++;
1716 }
1717
1718 /* We're going to construct n linked lists, one for each "value".
1719 * value_cells_head will contain pointers to the heads of these linked lists,
1720 * value_cells_tail will contain pointers to the tail of the lists. */
1721 value_cells_head = calloc(data->values_len, sizeof(*value_cells_head));
1722 value_cells_tail = calloc(data->values_len, sizeof(*value_cells_tail));
1723 if ((value_cells_head == NULL) || (value_cells_tail == NULL)) {
1724 ERROR("snmp plugin: csnmp_read_table: calloc failed.");
1725 sfree(value_cells_head);
1726 sfree(value_cells_tail);
1727 return -1;
1728 }
1729
1730 status = 0;
1731 while (status == 0) {
1732 /* If SNMP v2 and later and bulk transfers enabled, use GETBULK PDU */
1733 if (host->version > 1 && host->bulk_size > 0) {
1734 req = snmp_pdu_create(SNMP_MSG_GETBULK);
1735 req->non_repeaters = 0;
1736 req->max_repetitions = host->bulk_size;
1737 } else {
1738 req = snmp_pdu_create(SNMP_MSG_GETNEXT);
1739 }
1740 if (req == NULL) {
1741 ERROR("snmp plugin: snmp_pdu_create failed.");
1742 status = -1;
1743 break;
1744 }
1745
1746 size_t oid_list_todo_num = 0;
1747 size_t var_idx[oid_list_len];
1748 memset(var_idx, 0, sizeof(var_idx));
1749
1750 for (i = 0; i < oid_list_len; i++) {
1751 /* Do not rerequest already finished OIDs */
1752 if (!oid_list_todo[i])
1753 continue;
1754 snmp_add_null_var(req, oid_list[i].oid, oid_list[i].oid_len);
1755 var_idx[oid_list_todo_num] = i;
1756 oid_list_todo_num++;
1757 }
1758
1759 if (oid_list_todo_num == 0) {
1760 /* The request is still empty - so we are finished */
1761 DEBUG("snmp plugin: all variables have left their subtree");
1762 snmp_free_pdu(req);
1763 status = 0;
1764 break;
1765 }
1766
1767 if (req->command == SNMP_MSG_GETBULK) {
1768 /* In bulk mode the host will send 'max_repetitions' values per
1769 requested variable, so we need to split it per number of variable
1770 to stay 'in budget' */
1771 req->max_repetitions = floor(host->bulk_size / oid_list_todo_num);
1772 }
1773
1774 res = NULL;
1775 status = snmp_sess_synch_response(host->sess_handle, req, &res);
1776
1777 /* snmp_sess_synch_response always frees our req PDU */
1778 req = NULL;
1779
1780 if ((status != STAT_SUCCESS) || (res == NULL)) {
1781 char *errstr = NULL;
1782
1783 snmp_sess_error(host->sess_handle, NULL, NULL, &errstr);
1784
1785 c_complain(LOG_ERR, &host->complaint,
1786 "snmp plugin: host %s: snmp_sess_synch_response failed: %s",
1787 host->name, (errstr == NULL) ? "Unknown problem" : errstr);
1788
1789 if (res != NULL)
1790 snmp_free_pdu(res);
1791 res = NULL;
1792
1793 sfree(errstr);
1794 csnmp_host_close_session(host);
1795
1796 status = -1;
1797 break;
1798 }
1799
1800 status = 0;
1801 assert(res != NULL);
1802 c_release(LOG_INFO, &host->complaint,
1803 "snmp plugin: host %s: snmp_sess_synch_response successful.",
1804 host->name);
1805
1806 vb = res->variables;
1807 if (vb == NULL) {
1808 status = -1;
1809 break;
1810 }
1811
1812 if (res->errstat != SNMP_ERR_NOERROR) {
1813 if (res->errindex != 0) {
1814 /* Find the OID which caused error */
1815 for (i = 1, vb = res->variables; vb != NULL && i != res->errindex;
1816 vb = vb->next_variable, i++)
1817 /* do nothing */;
1818 }
1819
1820 if ((res->errindex == 0) || (vb == NULL)) {
1821 ERROR("snmp plugin: host %s; data %s: response error: %s (%li) ",
1822 host->name, data->name, snmp_errstring(res->errstat),
1823 res->errstat);
1824 status = -1;
1825 break;
1826 }
1827
1828 char oid_buffer[1024] = {0};
1829 snprint_objid(oid_buffer, sizeof(oid_buffer) - 1, vb->name,
1830 vb->name_length);
1831 NOTICE("snmp plugin: host %s; data %s: OID `%s` failed: %s", host->name,
1832 data->name, oid_buffer, snmp_errstring(res->errstat));
1833
1834 /* Get value index from todo list and skip OID found */
1835 assert(res->errindex <= oid_list_todo_num);
1836 i = var_idx[res->errindex - 1];
1837 assert(i < oid_list_len);
1838 oid_list_todo[i] = 0;
1839
1840 snmp_free_pdu(res);
1841 res = NULL;
1842 continue;
1843 }
1844
1845 size_t j;
1846 for (vb = res->variables, j = 0; (vb != NULL);
1847 vb = vb->next_variable, j++) {
1848 i = j;
1849 /* If bulk request is active convert value index of the extra value */
1850 if (host->version > 1 && host->bulk_size > 0) {
1851 i %= oid_list_todo_num;
1852 }
1853 /* Calculate value index from todo list */
1854 while ((i < oid_list_len) && !oid_list_todo[i]) {
1855 i++;
1856 j++;
1857 }
1858 if (i >= oid_list_len) {
1859 break;
1860 }
1861
1862 /* An instance is configured and the res variable we process is the
1863 * instance value */
1864 if (oid_list_todo[i] == OID_TYPE_TYPEINSTANCE) {
1865 if ((vb->type == SNMP_ENDOFMIBVIEW) ||
1866 (snmp_oid_ncompare(data->type_instance.oid.oid,
1867 data->type_instance.oid.oid_len, vb->name,
1868 vb->name_length,
1869 data->type_instance.oid.oid_len) != 0)) {
1870 DEBUG("snmp plugin: host = %s; data = %s; TypeInstance left its "
1871 "subtree.",
1872 host->name, data->name);
1873 oid_list_todo[i] = 0;
1874 continue;
1875 }
1876
1877 /* Allocate a new `csnmp_cell_char_t', insert the instance name and
1878 * add it to the list */
1879 csnmp_cell_char_t *cell =
1880 csnmp_get_char_cell(vb, &data->type_instance.oid, host, data);
1881 if (cell == NULL) {
1882 ERROR("snmp plugin: host %s: csnmp_get_char_cell() failed.",
1883 host->name);
1884 status = -1;
1885 break;
1886 }
1887
1888 if (csnmp_ignore_instance(cell, data)) {
1889 sfree(cell);
1890 } else {
1891 csnmp_cell_replace_reserved_chars(cell);
1892
1893 DEBUG("snmp plugin: il->type_instance = `%s';", cell->value);
1894 csnmp_cells_append(&type_instance_cells_head,
1895 &type_instance_cells_tail, cell);
1896 }
1897 } else if (oid_list_todo[i] == OID_TYPE_PLUGININSTANCE) {
1898 if ((vb->type == SNMP_ENDOFMIBVIEW) ||
1899 (snmp_oid_ncompare(data->plugin_instance.oid.oid,
1900 data->plugin_instance.oid.oid_len, vb->name,
1901 vb->name_length,
1902 data->plugin_instance.oid.oid_len) != 0)) {
1903 DEBUG("snmp plugin: host = %s; data = %s; TypeInstance left its "
1904 "subtree.",
1905 host->name, data->name);
1906 oid_list_todo[i] = 0;
1907 continue;
1908 }
1909
1910 /* Allocate a new `csnmp_cell_char_t', insert the instance name and
1911 * add it to the list */
1912 csnmp_cell_char_t *cell =
1913 csnmp_get_char_cell(vb, &data->plugin_instance.oid, host, data);
1914 if (cell == NULL) {
1915 ERROR("snmp plugin: host %s: csnmp_get_char_cell() failed.",
1916 host->name);
1917 status = -1;
1918 break;
1919 }
1920
1921 csnmp_cell_replace_reserved_chars(cell);
1922
1923 DEBUG("snmp plugin: il->plugin_instance = `%s';", cell->value);
1924 csnmp_cells_append(&plugin_instance_cells_head,
1925 &plugin_instance_cells_tail, cell);
1926 } else if (oid_list_todo[i] == OID_TYPE_HOST) {
1927 if ((vb->type == SNMP_ENDOFMIBVIEW) ||
1928 (snmp_oid_ncompare(data->host.oid.oid, data->host.oid.oid_len,
1929 vb->name, vb->name_length,
1930 data->host.oid.oid_len) != 0)) {
1931 DEBUG("snmp plugin: host = %s; data = %s; Host left its subtree.",
1932 host->name, data->name);
1933 oid_list_todo[i] = 0;
1934 continue;
1935 }
1936
1937 /* Allocate a new `csnmp_cell_char_t', insert the instance name and
1938 * add it to the list */
1939 csnmp_cell_char_t *cell =
1940 csnmp_get_char_cell(vb, &data->host.oid, host, data);
1941 if (cell == NULL) {
1942 ERROR("snmp plugin: host %s: csnmp_get_char_cell() failed.",
1943 host->name);
1944 status = -1;
1945 break;
1946 }
1947
1948 csnmp_cell_replace_reserved_chars(cell);
1949
1950 DEBUG("snmp plugin: il->hostname = `%s';", cell->value);
1951 csnmp_cells_append(&hostname_cells_head, &hostname_cells_tail, cell);
1952 } else if (oid_list_todo[i] == OID_TYPE_FILTER) {
1953 if ((vb->type == SNMP_ENDOFMIBVIEW) ||
1954 (snmp_oid_ncompare(data->filter_oid.oid, data->filter_oid.oid_len,
1955 vb->name, vb->name_length,
1956 data->filter_oid.oid_len) != 0)) {
1957 DEBUG("snmp plugin: host = %s; data = %s; Host left its subtree.",
1958 host->name, data->name);
1959 oid_list_todo[i] = 0;
1960 continue;
1961 }
1962
1963 /* Allocate a new `csnmp_cell_char_t', insert the instance name and
1964 * add it to the list */
1965 csnmp_cell_char_t *cell =
1966 csnmp_get_char_cell(vb, &data->filter_oid, host, data);
1967 if (cell == NULL) {
1968 ERROR("snmp plugin: host %s: csnmp_get_char_cell() failed.",
1969 host->name);
1970 status = -1;
1971 break;
1972 }
1973
1974 csnmp_cell_replace_reserved_chars(cell);
1975
1976 DEBUG("snmp plugin: il->filter = `%s';", cell->value);
1977 csnmp_cells_append(&filter_cells_head, &filter_cells_tail, cell);
1978 } else /* The variable we are processing is a normal value */
1979 {
1980 assert(oid_list_todo[i] == OID_TYPE_VARIABLE);
1981
1982 csnmp_cell_value_t *vt;
1983 oid_t vb_name;
1984 oid_t suffix;
1985 int ret;
1986
1987 csnmp_oid_init(&vb_name, vb->name, vb->name_length);
1988
1989 /* Calculate the current suffix. This is later used to check that the
1990 * suffix is increasing. This also checks if we left the subtree */
1991 ret = csnmp_oid_suffix(&suffix, &vb_name, data->values + i);
1992 if (ret != 0) {
1993 DEBUG("snmp plugin: host = %s; data = %s; i = %" PRIsz "; "
1994 "Value probably left its subtree.",
1995 host->name, data->name, i);
1996 oid_list_todo[i] = 0;
1997 continue;
1998 }
1999
2000 /* Make sure the OIDs returned by the agent are increasing. Otherwise
2001 * our table matching algorithm will get confused. */
2002 if ((value_cells_tail[i] != NULL) &&
2003 (csnmp_oid_compare(&suffix, &value_cells_tail[i]->suffix) <= 0)) {
2004 DEBUG("snmp plugin: host = %s; data = %s; i = %" PRIsz "; "
2005 "Suffix is not increasing.",
2006 host->name, data->name, i);
2007 oid_list_todo[i] = 0;
2008 continue;
2009 }
2010
2011 vt = calloc(1, sizeof(*vt));
2012 if (vt == NULL) {
2013 ERROR("snmp plugin: calloc failed.");
2014 status = -1;
2015 break;
2016 }
2017
2018 vt->value =
2019 csnmp_value_list_to_value(vb, ds->ds[i].type, data->scale,
2020 data->shift, host->name, data->name);
2021 memcpy(&vt->suffix, &suffix, sizeof(vt->suffix));
2022 vt->next = NULL;
2023
2024 if (value_cells_tail[i] == NULL)
2025 value_cells_head[i] = vt;
2026 else
2027 value_cells_tail[i]->next = vt;
2028 value_cells_tail[i] = vt;
2029 }
2030
2031 /* Copy OID to oid_list[i] */
2032 memcpy(oid_list[i].oid, vb->name, sizeof(oid) * vb->name_length);
2033 oid_list[i].oid_len = vb->name_length;
2034
2035 } /* for (vb = res->variables ...) */
2036
2037 if (res != NULL)
2038 snmp_free_pdu(res);
2039 res = NULL;
2040 } /* while (status == 0) */
2041
2042 if (res != NULL)
2043 snmp_free_pdu(res);
2044 res = NULL;
2045
2046 if (status == 0)
2047 csnmp_dispatch_table(host, data, type_instance_cells_head,
2048 plugin_instance_cells_head, hostname_cells_head,
2049 filter_cells_head, value_cells_head, data->count);
2050
2051 /* Free all allocated variables here */
2052 while (type_instance_cells_head != NULL) {
2053 csnmp_cell_char_t *next = type_instance_cells_head->next;
2054 sfree(type_instance_cells_head);
2055 type_instance_cells_head = next;
2056 }
2057
2058 while (plugin_instance_cells_head != NULL) {
2059 csnmp_cell_char_t *next = plugin_instance_cells_head->next;
2060 sfree(plugin_instance_cells_head);
2061 plugin_instance_cells_head = next;
2062 }
2063
2064 while (hostname_cells_head != NULL) {
2065 csnmp_cell_char_t *next = hostname_cells_head->next;
2066 sfree(hostname_cells_head);
2067 hostname_cells_head = next;
2068 }
2069
2070 while (filter_cells_head != NULL) {
2071 csnmp_cell_char_t *next = filter_cells_head->next;
2072 sfree(filter_cells_head);
2073 filter_cells_head = next;
2074 }
2075
2076 for (i = 0; i < data->values_len; i++) {
2077 while (value_cells_head[i] != NULL) {
2078 csnmp_cell_value_t *next = value_cells_head[i]->next;
2079 sfree(value_cells_head[i]);
2080 value_cells_head[i] = next;
2081 }
2082 }
2083
2084 sfree(value_cells_head);
2085 sfree(value_cells_tail);
2086
2087 return 0;
2088 } /* int csnmp_read_table */
2089
2090 static int csnmp_read_value(host_definition_t *host, data_definition_t *data) {
2091 struct snmp_pdu *req;
2092 struct snmp_pdu *res = NULL;
2093 struct variable_list *vb;
2094
2095 const data_set_t *ds;
2096 value_list_t vl = VALUE_LIST_INIT;
2097
2098 int status;
2099 size_t i;
2100
2101 DEBUG("snmp plugin: csnmp_read_value (host = %s, data = %s)", host->name,
2102 data->name);
2103
2104 if (host->sess_handle == NULL) {
2105 DEBUG("snmp plugin: csnmp_read_value: host->sess_handle == NULL");
2106 return -1;
2107 }
2108
2109 ds = plugin_get_ds(data->type);
2110 if (!ds) {
2111 ERROR("snmp plugin: DataSet `%s' not defined.", data->type);
2112 return -1;
2113 }
2114
2115 if (ds->ds_num != data->values_len) {
2116 ERROR("snmp plugin: DataSet `%s' requires %" PRIsz
2117 " values, but config talks "
2118 "about %" PRIsz,
2119 data->type, ds->ds_num, data->values_len);
2120 return -1;
2121 }
2122
2123 vl.values_len = ds->ds_num;
2124 vl.values = malloc(sizeof(*vl.values) * vl.values_len);
2125 if (vl.values == NULL)
2126 return -1;
2127 for (i = 0; i < vl.values_len; i++) {
2128 if (ds->ds[i].type == DS_TYPE_COUNTER)
2129 vl.values[i].counter = 0;
2130 else
2131 vl.values[i].gauge = NAN;
2132 }
2133
2134 sstrncpy(vl.host, host->name, sizeof(vl.host));
2135 sstrncpy(vl.plugin, data->plugin_name, sizeof(vl.plugin));
2136 sstrncpy(vl.type, data->type, sizeof(vl.type));
2137 if (data->type_instance.value)
2138 sstrncpy(vl.type_instance, data->type_instance.value,
2139 sizeof(vl.type_instance));
2140 if (data->plugin_instance.value)
2141 sstrncpy(vl.plugin_instance, data->plugin_instance.value,
2142 sizeof(vl.plugin_instance));
2143
2144 req = snmp_pdu_create(SNMP_MSG_GET);
2145 if (req == NULL) {
2146 ERROR("snmp plugin: snmp_pdu_create failed.");
2147 sfree(vl.values);
2148 return -1;
2149 }
2150
2151 for (i = 0; i < data->values_len; i++)
2152 snmp_add_null_var(req, data->values[i].oid, data->values[i].oid_len);
2153
2154 status = snmp_sess_synch_response(host->sess_handle, req, &res);
2155
2156 if ((status != STAT_SUCCESS) || (res == NULL)) {
2157 char *errstr = NULL;
2158
2159 snmp_sess_error(host->sess_handle, NULL, NULL, &errstr);
2160 ERROR("snmp plugin: host %s: snmp_sess_synch_response failed: %s",
2161 host->name, (errstr == NULL) ? "Unknown problem" : errstr);
2162
2163 if (res != NULL)
2164 snmp_free_pdu(res);
2165
2166 sfree(errstr);
2167 sfree(vl.values);
2168 csnmp_host_close_session(host);
2169
2170 return -1;
2171 }
2172
2173 for (vb = res->variables; vb != NULL; vb = vb->next_variable) {
2174 #if COLLECT_DEBUG
2175 char buffer[1024];
2176 snprint_variable(buffer, sizeof(buffer), vb->name, vb->name_length, vb);
2177 DEBUG("snmp plugin: Got this variable: %s", buffer);
2178 #endif /* COLLECT_DEBUG */
2179
2180 for (i = 0; i < data->values_len; i++)
2181 if (snmp_oid_compare(data->values[i].oid, data->values[i].oid_len,
2182 vb->name, vb->name_length) == 0)
2183 vl.values[i] =
2184 csnmp_value_list_to_value(vb, ds->ds[i].type, data->scale,
2185 data->shift, host->name, data->name);
2186 } /* for (res->variables) */
2187
2188 snmp_free_pdu(res);
2189
2190 DEBUG("snmp plugin: -> plugin_dispatch_values (&vl);");
2191 plugin_dispatch_values(&vl);
2192 sfree(vl.values);
2193
2194 return 0;
2195 } /* int csnmp_read_value */
2196
2197 static int csnmp_read_host(user_data_t *ud) {
2198 host_definition_t *host;
2199 int status;
2200 int success;
2201 int i;
2202
2203 host = ud->data;
2204
2205 if (host->sess_handle == NULL)
2206 csnmp_host_open_session(host);
2207
2208 if (host->sess_handle == NULL)
2209 return -1;
2210
2211 success = 0;
2212 for (i = 0; i < host->data_list_len; i++) {
2213 data_definition_t *data = host->data_list[i];
2214
2215 if (data->is_table)
2216 status = csnmp_read_table(host, data);
2217 else
2218 status = csnmp_read_value(host, data);
2219
2220 if (status == 0)
2221 success++;
2222 }
2223
2224 if (success == 0)
2225 return -1;
2226
2227 return 0;
2228 } /* int csnmp_read_host */
2229
2230 static int csnmp_init(void) {
2231 call_snmp_init_once();
2232
2233 return 0;
2234 } /* int csnmp_init */
2235
2236 static int csnmp_shutdown(void) {
2237 data_definition_t *data_this;
2238 data_definition_t *data_next;
2239
2240 /* When we get here, the read threads have been stopped and all the
2241 * `host_definition_t' will be freed. */
2242 DEBUG("snmp plugin: Destroying all data definitions.");
2243
2244 data_this = data_head;
2245 data_head = NULL;
2246 while (data_this != NULL) {
2247 data_next = data_this->next;
2248
2249 csnmp_data_definition_destroy(data_this);
2250
2251 data_this = data_next;
2252 }
2253
2254 return 0;
2255 } /* int csnmp_shutdown */
2256
2257 void module_register(void) {
2258 plugin_register_complex_config("snmp", csnmp_config);
2259 plugin_register_init("snmp", csnmp_init);
2260 plugin_register_shutdown("snmp", csnmp_shutdown);
2261 } /* void module_register */