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 **/
27 #include "collectd.h"
28 #include "common.h"
29 #include "plugin.h"
30 #include "utils_complain.h"
32 #include <net-snmp/net-snmp-config.h>
33 #include <net-snmp/net-snmp-includes.h>
35 #include <fnmatch.h>
37 /*
38 * Private data structes
39 */
40 struct oid_s
41 {
42 oid oid[MAX_OID_LEN];
43 size_t oid_len;
44 };
45 typedef struct oid_s oid_t;
47 union instance_u
48 {
49 char string[DATA_MAX_NAME_LEN];
50 oid_t oid;
51 };
52 typedef union instance_u instance_t;
54 struct data_definition_s
55 {
56 char *name; /* used to reference this from the `Collect' option */
57 char *type; /* used to find the data_set */
58 _Bool is_table;
59 instance_t instance;
60 char *instance_prefix;
61 oid_t *values;
62 size_t values_len;
63 double scale;
64 double shift;
65 struct data_definition_s *next;
66 char **ignores;
67 size_t ignores_len;
68 int invert_match;
69 };
70 typedef struct data_definition_s data_definition_t;
72 struct host_definition_s
73 {
74 char *name;
75 char *address;
76 int version;
78 /* snmpv1/2 options */
79 char *community;
81 /* snmpv3 security options */
82 char *username;
83 oid *auth_protocol;
84 size_t auth_protocol_len;
85 char *auth_passphrase;
86 oid *priv_protocol;
87 size_t priv_protocol_len;
88 char *priv_passphrase;
89 int security_level;
90 char *context;
92 void *sess_handle;
93 c_complain_t complaint;
94 cdtime_t interval;
95 data_definition_t **data_list;
96 int data_list_len;
97 };
98 typedef struct host_definition_s host_definition_t;
100 /* These two types are used to cache values in `csnmp_read_table' to handle
101 * gaps in tables. */
102 struct csnmp_list_instances_s
103 {
104 oid_t suffix;
105 char instance[DATA_MAX_NAME_LEN];
106 struct csnmp_list_instances_s *next;
107 };
108 typedef struct csnmp_list_instances_s csnmp_list_instances_t;
110 struct csnmp_table_values_s
111 {
112 oid_t suffix;
113 value_t value;
114 struct csnmp_table_values_s *next;
115 };
116 typedef struct csnmp_table_values_s csnmp_table_values_t;
118 /*
119 * Private variables
120 */
121 static data_definition_t *data_head = NULL;
123 /*
124 * Prototypes
125 */
126 static int csnmp_read_host (user_data_t *ud);
128 /*
129 * Private functions
130 */
131 static void csnmp_oid_init (oid_t *dst, oid const *src, size_t n)
132 {
133 assert (n <= STATIC_ARRAY_SIZE (dst->oid));
134 memcpy (dst->oid, src, sizeof (*src) * n);
135 dst->oid_len = n;
136 }
138 static int csnmp_oid_compare (oid_t const *left, oid_t const *right)
139 {
140 return (snmp_oid_compare (left->oid, left->oid_len,
141 right->oid, right->oid_len));
142 }
144 static int csnmp_oid_suffix (oid_t *dst, oid_t const *src,
145 oid_t const *root)
146 {
147 /* Make sure "src" is in "root"s subtree. */
148 if (src->oid_len <= root->oid_len)
149 return (EINVAL);
150 if (snmp_oid_ncompare (root->oid, root->oid_len,
151 src->oid, src->oid_len,
152 /* n = */ root->oid_len) != 0)
153 return (EINVAL);
155 memset (dst, 0, sizeof (*dst));
156 dst->oid_len = src->oid_len - root->oid_len;
157 memcpy (dst->oid, &src->oid[root->oid_len],
158 dst->oid_len * sizeof (dst->oid[0]));
159 return (0);
160 }
162 static int csnmp_oid_to_string (char *buffer, size_t buffer_size,
163 oid_t const *o)
164 {
165 char oid_str[MAX_OID_LEN][16];
166 char *oid_str_ptr[MAX_OID_LEN];
167 size_t i;
169 for (i = 0; i < o->oid_len; i++)
170 {
171 ssnprintf (oid_str[i], sizeof (oid_str[i]), "%lu", (unsigned long) o->oid[i]);
172 oid_str_ptr[i] = oid_str[i];
173 }
175 return (strjoin (buffer, buffer_size,
176 oid_str_ptr, o->oid_len, /* separator = */ "."));
177 }
179 static void csnmp_host_close_session (host_definition_t *host) /* {{{ */
180 {
181 if (host->sess_handle == NULL)
182 return;
184 snmp_sess_close (host->sess_handle);
185 host->sess_handle = NULL;
186 } /* }}} void csnmp_host_close_session */
188 static void csnmp_host_definition_destroy (void *arg) /* {{{ */
189 {
190 host_definition_t *hd;
192 hd = arg;
194 if (hd == NULL)
195 return;
197 if (hd->name != NULL)
198 {
199 DEBUG ("snmp plugin: Destroying host definition for host `%s'.",
200 hd->name);
201 }
203 csnmp_host_close_session (hd);
205 sfree (hd->name);
206 sfree (hd->address);
207 sfree (hd->community);
208 sfree (hd->username);
209 sfree (hd->auth_passphrase);
210 sfree (hd->priv_passphrase);
211 sfree (hd->context);
212 sfree (hd->data_list);
214 sfree (hd);
215 } /* }}} void csnmp_host_definition_destroy */
217 /* Many functions to handle the configuration. {{{ */
218 /* First there are many functions which do configuration stuff. It's a big
219 * bloated and messy, I'm afraid. */
221 /*
222 * Callgraph for the config stuff:
223 * csnmp_config
224 * +-> call_snmp_init_once
225 * +-> csnmp_config_add_data
226 * ! +-> csnmp_config_add_data_instance
227 * ! +-> csnmp_config_add_data_instance_prefix
228 * ! +-> csnmp_config_add_data_values
229 * +-> csnmp_config_add_host
230 * +-> csnmp_config_add_host_version
231 * +-> csnmp_config_add_host_collect
232 * +-> csnmp_config_add_host_auth_protocol
233 * +-> csnmp_config_add_host_priv_protocol
234 * +-> csnmp_config_add_host_security_level
235 */
236 static void call_snmp_init_once (void)
237 {
238 static int have_init = 0;
240 if (have_init == 0)
241 init_snmp (PACKAGE_NAME);
242 have_init = 1;
243 } /* void call_snmp_init_once */
245 static int csnmp_config_add_data_instance (data_definition_t *dd, oconfig_item_t *ci)
246 {
247 char buffer[DATA_MAX_NAME_LEN];
248 int status;
250 status = cf_util_get_string_buffer(ci, buffer, sizeof(buffer));
251 if (status != 0)
252 return status;
254 if (dd->is_table)
255 {
256 /* Instance is an OID */
257 dd->instance.oid.oid_len = MAX_OID_LEN;
259 if (!read_objid (buffer,
260 dd->instance.oid.oid, &dd->instance.oid.oid_len))
261 {
262 ERROR ("snmp plugin: read_objid (%s) failed.", buffer);
263 return (-1);
264 }
265 }
266 else
267 {
268 /* Instance is a simple string */
269 sstrncpy (dd->instance.string, buffer,
270 sizeof (dd->instance.string));
271 }
273 return (0);
274 } /* int csnmp_config_add_data_instance */
276 static int csnmp_config_add_data_instance_prefix (data_definition_t *dd,
277 oconfig_item_t *ci)
278 {
279 int status;
281 if (!dd->is_table)
282 {
283 WARNING ("snmp plugin: data %s: InstancePrefix is ignored when `Table' "
284 "is set to `false'.", dd->name);
285 return (-1);
286 }
288 status = cf_util_get_string(ci, &dd->instance_prefix);
289 return status;
290 } /* int csnmp_config_add_data_instance_prefix */
292 static int csnmp_config_add_data_values (data_definition_t *dd, oconfig_item_t *ci)
293 {
294 int i;
296 if (ci->values_num < 1)
297 {
298 WARNING ("snmp plugin: `Values' needs at least one argument.");
299 return (-1);
300 }
302 for (i = 0; i < ci->values_num; i++)
303 if (ci->values[i].type != OCONFIG_TYPE_STRING)
304 {
305 WARNING ("snmp plugin: `Values' needs only string argument.");
306 return (-1);
307 }
309 sfree (dd->values);
310 dd->values_len = 0;
311 dd->values = malloc (sizeof (*dd->values) * ci->values_num);
312 if (dd->values == NULL)
313 return (-1);
314 dd->values_len = (size_t) ci->values_num;
316 for (i = 0; i < ci->values_num; i++)
317 {
318 dd->values[i].oid_len = MAX_OID_LEN;
320 if (NULL == snmp_parse_oid (ci->values[i].value.string,
321 dd->values[i].oid, &dd->values[i].oid_len))
322 {
323 ERROR ("snmp plugin: snmp_parse_oid (%s) failed.",
324 ci->values[i].value.string);
325 free (dd->values);
326 dd->values = NULL;
327 dd->values_len = 0;
328 return (-1);
329 }
330 }
332 return (0);
333 } /* int csnmp_config_add_data_instance */
335 static int csnmp_config_add_data_blacklist(data_definition_t *dd, oconfig_item_t *ci)
336 {
337 int i;
339 if (ci->values_num < 1)
340 return (0);
342 for (i = 0; i < ci->values_num; i++)
343 {
344 if (ci->values[i].type != OCONFIG_TYPE_STRING)
345 {
346 WARNING ("snmp plugin: `Ignore' needs only string argument.");
347 return (-1);
348 }
349 }
351 dd->ignores_len = 0;
352 dd->ignores = NULL;
354 for (i = 0; i < ci->values_num; ++i)
355 {
356 if (strarray_add(&(dd->ignores), &(dd->ignores_len), ci->values[i].value.string) != 0)
357 {
358 ERROR("snmp plugin: Can't allocate memory");
359 strarray_free(dd->ignores, dd->ignores_len);
360 return (ENOMEM);
361 }
362 }
363 return 0;
364 } /* int csnmp_config_add_data_blacklist */
366 static int csnmp_config_add_data_blacklist_match_inverted(data_definition_t *dd, oconfig_item_t *ci)
367 {
368 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_BOOLEAN))
369 {
370 WARNING ("snmp plugin: `InvertMatch' needs exactly one boolean argument.");
371 return (-1);
372 }
374 dd->invert_match = ci->values[0].value.boolean ? 1 : 0;
376 return (0);
377 } /* int csnmp_config_add_data_blacklist_match_inverted */
379 static int csnmp_config_add_data (oconfig_item_t *ci)
380 {
381 data_definition_t *dd;
382 int status = 0;
383 int i;
385 dd = calloc (1, sizeof (*dd));
386 if (dd == NULL)
387 return (-1);
389 status = cf_util_get_string(ci, &dd->name);
390 if (status != 0)
391 {
392 free (dd);
393 return (-1);
394 }
396 dd->scale = 1.0;
397 dd->shift = 0.0;
399 for (i = 0; i < ci->children_num; i++)
400 {
401 oconfig_item_t *option = ci->children + i;
403 if (strcasecmp ("Type", option->key) == 0)
404 status = cf_util_get_string(option, &dd->type);
405 else if (strcasecmp ("Table", option->key) == 0)
406 status = cf_util_get_boolean(option, &dd->is_table);
407 else if (strcasecmp ("Instance", option->key) == 0)
408 status = csnmp_config_add_data_instance (dd, option);
409 else if (strcasecmp ("InstancePrefix", option->key) == 0)
410 status = csnmp_config_add_data_instance_prefix (dd, option);
411 else if (strcasecmp ("Values", option->key) == 0)
412 status = csnmp_config_add_data_values (dd, option);
413 else if (strcasecmp ("Shift", option->key) == 0)
414 status = cf_util_get_double(option, &dd->shift);
415 else if (strcasecmp ("Scale", option->key) == 0)
416 status = cf_util_get_double(option, &dd->scale);
417 else if (strcasecmp ("Ignore", option->key) == 0)
418 status = csnmp_config_add_data_blacklist(dd, option);
419 else if (strcasecmp ("InvertMatch", option->key) == 0)
420 status = csnmp_config_add_data_blacklist_match_inverted(dd, option);
421 else
422 {
423 WARNING ("snmp plugin: Option `%s' not allowed here.", option->key);
424 status = -1;
425 }
427 if (status != 0)
428 break;
429 } /* for (ci->children) */
431 while (status == 0)
432 {
433 if (dd->type == NULL)
434 {
435 WARNING ("snmp plugin: `Type' not given for data `%s'", dd->name);
436 status = -1;
437 break;
438 }
439 if (dd->values == NULL)
440 {
441 WARNING ("snmp plugin: No `Value' given for data `%s'", dd->name);
442 status = -1;
443 break;
444 }
446 break;
447 } /* while (status == 0) */
449 if (status != 0)
450 {
451 sfree (dd->name);
452 sfree (dd->instance_prefix);
453 sfree (dd->values);
454 sfree (dd->ignores);
455 sfree (dd);
456 return (-1);
457 }
459 DEBUG ("snmp plugin: dd = { name = %s, type = %s, is_table = %s, values_len = %zu }",
460 dd->name, dd->type, (dd->is_table != 0) ? "true" : "false", dd->values_len);
462 if (data_head == NULL)
463 data_head = dd;
464 else
465 {
466 data_definition_t *last;
467 last = data_head;
468 while (last->next != NULL)
469 last = last->next;
470 last->next = dd;
471 }
473 return (0);
474 } /* int csnmp_config_add_data */
476 static int csnmp_config_add_host_version (host_definition_t *hd, oconfig_item_t *ci)
477 {
478 int version;
480 if ((ci->values_num != 1)
481 || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
482 {
483 WARNING ("snmp plugin: The `Version' config option needs exactly one number argument.");
484 return (-1);
485 }
487 version = (int) ci->values[0].value.number;
488 if ((version < 1) || (version > 3))
489 {
490 WARNING ("snmp plugin: `Version' must either be `1', `2', or `3'.");
491 return (-1);
492 }
494 hd->version = version;
496 return (0);
497 } /* int csnmp_config_add_host_address */
499 static int csnmp_config_add_host_collect (host_definition_t *host,
500 oconfig_item_t *ci)
501 {
502 data_definition_t *data;
503 data_definition_t **data_list;
504 int data_list_len;
505 int i;
507 if (ci->values_num < 1)
508 {
509 WARNING ("snmp plugin: `Collect' needs at least one argument.");
510 return (-1);
511 }
513 for (i = 0; i < ci->values_num; i++)
514 if (ci->values[i].type != OCONFIG_TYPE_STRING)
515 {
516 WARNING ("snmp plugin: All arguments to `Collect' must be strings.");
517 return (-1);
518 }
520 data_list_len = host->data_list_len + ci->values_num;
521 data_list = realloc (host->data_list,
522 sizeof (data_definition_t *) * data_list_len);
523 if (data_list == NULL)
524 return (-1);
525 host->data_list = data_list;
527 for (i = 0; i < ci->values_num; i++)
528 {
529 for (data = data_head; data != NULL; data = data->next)
530 if (strcasecmp (ci->values[i].value.string, data->name) == 0)
531 break;
533 if (data == NULL)
534 {
535 WARNING ("snmp plugin: No such data configured: `%s'",
536 ci->values[i].value.string);
537 continue;
538 }
540 DEBUG ("snmp plugin: Collect: host = %s, data[%i] = %s;",
541 host->name, host->data_list_len, data->name);
543 host->data_list[host->data_list_len] = data;
544 host->data_list_len++;
545 } /* for (values_num) */
547 return (0);
548 } /* int csnmp_config_add_host_collect */
550 static int csnmp_config_add_host_auth_protocol (host_definition_t *hd, oconfig_item_t *ci)
551 {
552 char buffer[4];
553 int status;
555 status = cf_util_get_string_buffer(ci, buffer, sizeof(buffer));
556 if (status != 0)
557 return status;
559 if (strcasecmp("MD5", buffer) == 0) {
560 hd->auth_protocol = usmHMACMD5AuthProtocol;
561 hd->auth_protocol_len = sizeof(usmHMACMD5AuthProtocol)/sizeof(oid);
562 }
563 else if (strcasecmp("SHA", buffer) == 0) {
564 hd->auth_protocol = usmHMACSHA1AuthProtocol;
565 hd->auth_protocol_len = sizeof(usmHMACSHA1AuthProtocol)/sizeof(oid);
566 }
567 else
568 {
569 WARNING ("snmp plugin: The `AuthProtocol' config option must be `MD5' or `SHA'.");
570 return (-1);
571 }
573 DEBUG ("snmp plugin: host = %s; host->auth_protocol = %s;",
574 hd->name, hd->auth_protocol == usmHMACMD5AuthProtocol ? "MD5" : "SHA");
576 return (0);
577 } /* int csnmp_config_add_host_auth_protocol */
579 static int csnmp_config_add_host_priv_protocol (host_definition_t *hd, oconfig_item_t *ci)
580 {
581 char buffer[4];
582 int status;
584 status = cf_util_get_string_buffer(ci, buffer, sizeof(buffer));
585 if (status != 0)
586 return status;
588 if (strcasecmp("AES", buffer) == 0)
589 {
590 hd->priv_protocol = usmAESPrivProtocol;
591 hd->priv_protocol_len = sizeof(usmAESPrivProtocol)/sizeof(oid);
592 }
593 else if (strcasecmp("DES", buffer) == 0) {
594 hd->priv_protocol = usmDESPrivProtocol;
595 hd->priv_protocol_len = sizeof(usmDESPrivProtocol)/sizeof(oid);
596 }
597 else
598 {
599 WARNING ("snmp plugin: The `PrivProtocol' config option must be `AES' or `DES'.");
600 return (-1);
601 }
603 DEBUG ("snmp plugin: host = %s; host->priv_protocol = %s;",
604 hd->name, hd->priv_protocol == usmAESPrivProtocol ? "AES" : "DES");
606 return (0);
607 } /* int csnmp_config_add_host_priv_protocol */
609 static int csnmp_config_add_host_security_level (host_definition_t *hd, oconfig_item_t *ci)
610 {
611 char buffer[16];
612 int status;
614 status = cf_util_get_string_buffer(ci, buffer, sizeof(buffer));
615 if (status != 0)
616 return status;
618 if (strcasecmp("noAuthNoPriv", buffer) == 0)
619 hd->security_level = SNMP_SEC_LEVEL_NOAUTH;
620 else if (strcasecmp("authNoPriv", buffer) == 0)
621 hd->security_level = SNMP_SEC_LEVEL_AUTHNOPRIV;
622 else if (strcasecmp("authPriv", buffer) == 0)
623 hd->security_level = SNMP_SEC_LEVEL_AUTHPRIV;
624 else
625 {
626 WARNING ("snmp plugin: The `SecurityLevel' config option must be `noAuthNoPriv', `authNoPriv', or `authPriv'.");
627 return (-1);
628 }
630 DEBUG ("snmp plugin: host = %s; host->security_level = %d;",
631 hd->name, hd->security_level);
633 return (0);
634 } /* int csnmp_config_add_host_security_level */
636 static int csnmp_config_add_host (oconfig_item_t *ci)
637 {
638 host_definition_t *hd;
639 int status = 0;
640 int i;
642 /* Registration stuff. */
643 char cb_name[DATA_MAX_NAME_LEN];
644 user_data_t cb_data = { 0 };
646 hd = calloc (1, sizeof (*hd));
647 if (hd == NULL)
648 return (-1);
649 hd->version = 2;
650 C_COMPLAIN_INIT (&hd->complaint);
652 status = cf_util_get_string(ci, &hd->name);
653 if (status != 0)
654 {
655 sfree (hd);
656 return status;
657 }
659 hd->sess_handle = NULL;
660 hd->interval = 0;
662 for (i = 0; i < ci->children_num; i++)
663 {
664 oconfig_item_t *option = ci->children + i;
665 status = 0;
667 if (strcasecmp ("Address", option->key) == 0)
668 status = cf_util_get_string(option, &hd->address);
669 else if (strcasecmp ("Community", option->key) == 0)
670 status = cf_util_get_string(option, &hd->community);
671 else if (strcasecmp ("Version", option->key) == 0)
672 status = csnmp_config_add_host_version (hd, option);
673 else if (strcasecmp ("Collect", option->key) == 0)
674 csnmp_config_add_host_collect (hd, option);
675 else if (strcasecmp ("Interval", option->key) == 0)
676 cf_util_get_cdtime (option, &hd->interval);
677 else if (strcasecmp ("Username", option->key) == 0)
678 status = cf_util_get_string(option, &hd->username);
679 else if (strcasecmp ("AuthProtocol", option->key) == 0)
680 status = csnmp_config_add_host_auth_protocol (hd, option);
681 else if (strcasecmp ("PrivacyProtocol", option->key) == 0)
682 status = csnmp_config_add_host_priv_protocol (hd, option);
683 else if (strcasecmp ("AuthPassphrase", option->key) == 0)
684 status = cf_util_get_string(option, &hd->auth_passphrase);
685 else if (strcasecmp ("PrivacyPassphrase", option->key) == 0)
686 status = cf_util_get_string(option, &hd->priv_passphrase);
687 else if (strcasecmp ("SecurityLevel", option->key) == 0)
688 status = csnmp_config_add_host_security_level (hd, option);
689 else if (strcasecmp ("Context", option->key) == 0)
690 status = cf_util_get_string(option, &hd->context);
691 else
692 {
693 WARNING ("snmp plugin: csnmp_config_add_host: Option `%s' not allowed here.", option->key);
694 status = -1;
695 }
697 if (status != 0)
698 break;
699 } /* for (ci->children) */
701 while (status == 0)
702 {
703 if (hd->address == NULL)
704 {
705 WARNING ("snmp plugin: `Address' not given for host `%s'", hd->name);
706 status = -1;
707 break;
708 }
709 if (hd->community == NULL && hd->version < 3)
710 {
711 WARNING ("snmp plugin: `Community' not given for host `%s'", hd->name);
712 status = -1;
713 break;
714 }
715 if (hd->version == 3)
716 {
717 if (hd->username == NULL)
718 {
719 WARNING ("snmp plugin: `Username' not given for host `%s'", hd->name);
720 status = -1;
721 break;
722 }
723 if (hd->security_level == 0)
724 {
725 WARNING ("snmp plugin: `SecurityLevel' not given for host `%s'", hd->name);
726 status = -1;
727 break;
728 }
729 if (hd->security_level == SNMP_SEC_LEVEL_AUTHNOPRIV || hd->security_level == SNMP_SEC_LEVEL_AUTHPRIV)
730 {
731 if (hd->auth_protocol == NULL)
732 {
733 WARNING ("snmp plugin: `AuthProtocol' not given for host `%s'", hd->name);
734 status = -1;
735 break;
736 }
737 if (hd->auth_passphrase == NULL)
738 {
739 WARNING ("snmp plugin: `AuthPassphrase' not given for host `%s'", hd->name);
740 status = -1;
741 break;
742 }
743 }
744 if (hd->security_level == SNMP_SEC_LEVEL_AUTHPRIV)
745 {
746 if (hd->priv_protocol == NULL)
747 {
748 WARNING ("snmp plugin: `PrivacyProtocol' not given for host `%s'", hd->name);
749 status = -1;
750 break;
751 }
752 if (hd->priv_passphrase == NULL)
753 {
754 WARNING ("snmp plugin: `PrivacyPassphrase' not given for host `%s'", hd->name);
755 status = -1;
756 break;
757 }
758 }
759 }
761 break;
762 } /* while (status == 0) */
764 if (status != 0)
765 {
766 csnmp_host_definition_destroy (hd);
767 return (-1);
768 }
770 DEBUG ("snmp plugin: hd = { name = %s, address = %s, community = %s, version = %i }",
771 hd->name, hd->address, hd->community, hd->version);
773 ssnprintf (cb_name, sizeof (cb_name), "snmp-%s", hd->name);
775 cb_data.data = hd;
776 cb_data.free_func = csnmp_host_definition_destroy;
778 status = plugin_register_complex_read (/* group = */ NULL, cb_name,
779 csnmp_read_host, hd->interval, /* user_data = */ &cb_data);
780 if (status != 0)
781 {
782 ERROR ("snmp plugin: Registering complex read function failed.");
783 csnmp_host_definition_destroy (hd);
784 return (-1);
785 }
787 return (0);
788 } /* int csnmp_config_add_host */
790 static int csnmp_config (oconfig_item_t *ci)
791 {
792 int i;
794 call_snmp_init_once ();
796 for (i = 0; i < ci->children_num; i++)
797 {
798 oconfig_item_t *child = ci->children + i;
799 if (strcasecmp ("Data", child->key) == 0)
800 csnmp_config_add_data (child);
801 else if (strcasecmp ("Host", child->key) == 0)
802 csnmp_config_add_host (child);
803 else
804 {
805 WARNING ("snmp plugin: Ignoring unknown config option `%s'.", child->key);
806 }
807 } /* for (ci->children) */
809 return (0);
810 } /* int csnmp_config */
812 /* }}} End of the config stuff. Now the interesting part begins */
814 static void csnmp_host_open_session (host_definition_t *host)
815 {
816 struct snmp_session sess;
817 int error;
819 if (host->sess_handle != NULL)
820 csnmp_host_close_session (host);
822 snmp_sess_init (&sess);
823 sess.peername = host->address;
824 switch (host->version)
825 {
826 case 1:
827 sess.version = SNMP_VERSION_1;
828 break;
829 case 3:
830 sess.version = SNMP_VERSION_3;
831 break;
832 default:
833 sess.version = SNMP_VERSION_2c;
834 break;
835 }
837 if (host->version == 3)
838 {
839 sess.securityName = host->username;
840 sess.securityNameLen = strlen (host->username);
841 sess.securityLevel = host->security_level;
843 if (sess.securityLevel == SNMP_SEC_LEVEL_AUTHNOPRIV || sess.securityLevel == SNMP_SEC_LEVEL_AUTHPRIV)
844 {
845 sess.securityAuthProto = host->auth_protocol;
846 sess.securityAuthProtoLen = host->auth_protocol_len;
847 sess.securityAuthKeyLen = USM_AUTH_KU_LEN;
848 error = generate_Ku (sess.securityAuthProto,
849 sess.securityAuthProtoLen,
850 (u_char *) host->auth_passphrase,
851 strlen(host->auth_passphrase),
852 sess.securityAuthKey,
853 &sess.securityAuthKeyLen);
854 if (error != SNMPERR_SUCCESS) {
855 ERROR ("snmp plugin: host %s: Error generating Ku from auth_passphrase. (Error %d)", host->name, error);
856 }
857 }
859 if (sess.securityLevel == SNMP_SEC_LEVEL_AUTHPRIV)
860 {
861 sess.securityPrivProto = host->priv_protocol;
862 sess.securityPrivProtoLen = host->priv_protocol_len;
863 sess.securityPrivKeyLen = USM_PRIV_KU_LEN;
864 error = generate_Ku (sess.securityAuthProto,
865 sess.securityAuthProtoLen,
866 (u_char *) host->priv_passphrase,
867 strlen(host->priv_passphrase),
868 sess.securityPrivKey,
869 &sess.securityPrivKeyLen);
870 if (error != SNMPERR_SUCCESS) {
871 ERROR ("snmp plugin: host %s: Error generating Ku from priv_passphrase. (Error %d)", host->name, error);
872 }
873 }
875 if (host->context != NULL)
876 {
877 sess.contextName = host->context;
878 sess.contextNameLen = strlen (host->context);
879 }
880 }
881 else /* SNMPv1/2 "authenticates" with community string */
882 {
883 sess.community = (u_char *) host->community;
884 sess.community_len = strlen (host->community);
885 }
887 /* snmp_sess_open will copy the `struct snmp_session *'. */
888 host->sess_handle = snmp_sess_open (&sess);
890 if (host->sess_handle == NULL)
891 {
892 char *errstr = NULL;
894 snmp_error (&sess, NULL, NULL, &errstr);
896 ERROR ("snmp plugin: host %s: snmp_sess_open failed: %s",
897 host->name, (errstr == NULL) ? "Unknown problem" : errstr);
898 sfree (errstr);
899 }
900 } /* void csnmp_host_open_session */
902 /* TODO: Check if negative values wrap around. Problem: negative temperatures. */
903 static value_t csnmp_value_list_to_value (struct variable_list *vl, int type,
904 double scale, double shift,
905 const char *host_name, const char *data_name)
906 {
907 value_t ret;
908 uint64_t tmp_unsigned = 0;
909 int64_t tmp_signed = 0;
910 _Bool defined = 1;
911 /* Set to true when the original SNMP type appears to have been signed. */
912 _Bool prefer_signed = 0;
914 if ((vl->type == ASN_INTEGER)
915 || (vl->type == ASN_UINTEGER)
916 || (vl->type == ASN_COUNTER)
917 #ifdef ASN_TIMETICKS
918 || (vl->type == ASN_TIMETICKS)
919 #endif
920 || (vl->type == ASN_GAUGE))
921 {
922 tmp_unsigned = (uint32_t) *vl->val.integer;
923 tmp_signed = (int32_t) *vl->val.integer;
925 if (vl->type == ASN_INTEGER)
926 prefer_signed = 1;
928 DEBUG ("snmp plugin: Parsed int32 value is %"PRIu64".", tmp_unsigned);
929 }
930 else if (vl->type == ASN_COUNTER64)
931 {
932 tmp_unsigned = (uint32_t) vl->val.counter64->high;
933 tmp_unsigned = tmp_unsigned << 32;
934 tmp_unsigned += (uint32_t) vl->val.counter64->low;
935 tmp_signed = (int64_t) tmp_unsigned;
936 DEBUG ("snmp plugin: Parsed int64 value is %"PRIu64".", tmp_unsigned);
937 }
938 else if (vl->type == ASN_OCTET_STR)
939 {
940 /* We'll handle this later.. */
941 }
942 else
943 {
944 char oid_buffer[1024] = { 0 };
946 snprint_objid (oid_buffer, sizeof (oid_buffer) - 1,
947 vl->name, vl->name_length);
949 #ifdef ASN_NULL
950 if (vl->type == ASN_NULL)
951 INFO ("snmp plugin: OID \"%s\" is undefined (type ASN_NULL)",
952 oid_buffer);
953 else
954 #endif
955 WARNING ("snmp plugin: I don't know the ASN type #%i "
956 "(OID: \"%s\", data block \"%s\", host block \"%s\")",
957 (int) vl->type, oid_buffer,
958 (data_name != NULL) ? data_name : "UNKNOWN",
959 (host_name != NULL) ? host_name : "UNKNOWN");
961 defined = 0;
962 }
964 if (vl->type == ASN_OCTET_STR)
965 {
966 int status = -1;
968 if (vl->val.string != NULL)
969 {
970 char string[64];
971 size_t string_length;
973 string_length = sizeof (string) - 1;
974 if (vl->val_len < string_length)
975 string_length = vl->val_len;
977 /* The strings we get from the Net-SNMP library may not be null
978 * terminated. That is why we're using `memcpy' here and not `strcpy'.
979 * `string_length' is set to `vl->val_len' which holds the length of the
980 * string. -octo */
981 memcpy (string, vl->val.string, string_length);
982 string[string_length] = 0;
984 status = parse_value (string, &ret, type);
985 if (status != 0)
986 {
987 ERROR ("snmp plugin: host %s: csnmp_value_list_to_value: Parsing string as %s failed: %s",
988 (host_name != NULL) ? host_name : "UNKNOWN",
989 DS_TYPE_TO_STRING (type), string);
990 }
991 }
993 if (status != 0)
994 {
995 switch (type)
996 {
997 case DS_TYPE_COUNTER:
998 case DS_TYPE_DERIVE:
999 case DS_TYPE_ABSOLUTE:
1000 memset (&ret, 0, sizeof (ret));
1001 break;
1003 case DS_TYPE_GAUGE:
1004 ret.gauge = NAN;
1005 break;
1007 default:
1008 ERROR ("snmp plugin: csnmp_value_list_to_value: Unknown "
1009 "data source type: %i.", type);
1010 ret.gauge = NAN;
1011 }
1012 }
1013 } /* if (vl->type == ASN_OCTET_STR) */
1014 else if (type == DS_TYPE_COUNTER)
1015 {
1016 ret.counter = tmp_unsigned;
1017 }
1018 else if (type == DS_TYPE_GAUGE)
1019 {
1020 if (!defined)
1021 ret.gauge = NAN;
1022 else if (prefer_signed)
1023 ret.gauge = (scale * tmp_signed) + shift;
1024 else
1025 ret.gauge = (scale * tmp_unsigned) + shift;
1026 }
1027 else if (type == DS_TYPE_DERIVE)
1028 {
1029 if (prefer_signed)
1030 ret.derive = (derive_t) tmp_signed;
1031 else
1032 ret.derive = (derive_t) tmp_unsigned;
1033 }
1034 else if (type == DS_TYPE_ABSOLUTE)
1035 {
1036 ret.absolute = (absolute_t) tmp_unsigned;
1037 }
1038 else
1039 {
1040 ERROR ("snmp plugin: csnmp_value_list_to_value: Unknown data source "
1041 "type: %i.", type);
1042 ret.gauge = NAN;
1043 }
1045 return (ret);
1046 } /* value_t csnmp_value_list_to_value */
1048 /* csnmp_strvbcopy_hexstring converts the bit string contained in "vb" to a hex
1049 * representation and writes it to dst. Returns zero on success and ENOMEM if
1050 * dst is not large enough to hold the string. dst is guaranteed to be
1051 * nul-terminated. */
1052 static int csnmp_strvbcopy_hexstring (char *dst, /* {{{ */
1053 const struct variable_list *vb, size_t dst_size)
1054 {
1055 char *buffer_ptr;
1056 size_t buffer_free;
1057 size_t i;
1059 dst[0] = 0;
1061 buffer_ptr = dst;
1062 buffer_free = dst_size;
1064 for (i = 0; i < vb->val_len; i++)
1065 {
1066 int status;
1068 status = snprintf (buffer_ptr, buffer_free,
1069 (i == 0) ? "%02x" : ":%02x", (unsigned int) vb->val.bitstring[i]);
1070 assert (status >= 0);
1072 if (((size_t) status) >= buffer_free) /* truncated */
1073 {
1074 dst[dst_size - 1] = 0;
1075 return ENOMEM;
1076 }
1077 else /* if (status < buffer_free) */
1078 {
1079 buffer_ptr += (size_t) status;
1080 buffer_free -= (size_t) status;
1081 }
1082 }
1084 return 0;
1085 } /* }}} int csnmp_strvbcopy_hexstring */
1087 /* csnmp_strvbcopy copies the octet string or bit string contained in vb to
1088 * dst. If non-printable characters are detected, it will switch to a hex
1089 * representation of the string. Returns zero on success, EINVAL if vb does not
1090 * contain a string and ENOMEM if dst is not large enough to contain the
1091 * string. */
1092 static int csnmp_strvbcopy (char *dst, /* {{{ */
1093 const struct variable_list *vb, size_t dst_size)
1094 {
1095 char *src;
1096 size_t num_chars;
1097 size_t i;
1099 if (vb->type == ASN_OCTET_STR)
1100 src = (char *) vb->val.string;
1101 else if (vb->type == ASN_BIT_STR)
1102 src = (char *) vb->val.bitstring;
1103 else if (vb->type == ASN_IPADDRESS)
1104 {
1105 return ssnprintf (dst, dst_size, "%"PRIu8".%"PRIu8".%"PRIu8".%"PRIu8"",
1106 (uint8_t) vb->val.string[0],
1107 (uint8_t) vb->val.string[1],
1108 (uint8_t) vb->val.string[2],
1109 (uint8_t) vb->val.string[3]);
1110 }
1111 else
1112 {
1113 dst[0] = 0;
1114 return (EINVAL);
1115 }
1117 num_chars = dst_size - 1;
1118 if (num_chars > vb->val_len)
1119 num_chars = vb->val_len;
1121 for (i = 0; i < num_chars; i++)
1122 {
1123 /* Check for control characters. */
1124 if ((unsigned char)src[i] < 32)
1125 return (csnmp_strvbcopy_hexstring (dst, vb, dst_size));
1126 dst[i] = src[i];
1127 }
1128 dst[num_chars] = 0;
1129 dst[dst_size - 1] = 0;
1131 if (dst_size <= vb->val_len)
1132 return ENOMEM;
1134 return 0;
1135 } /* }}} int csnmp_strvbcopy */
1137 static int csnmp_instance_list_add (csnmp_list_instances_t **head,
1138 csnmp_list_instances_t **tail,
1139 const struct snmp_pdu *res,
1140 const host_definition_t *hd, const data_definition_t *dd)
1141 {
1142 csnmp_list_instances_t *il;
1143 struct variable_list *vb;
1144 oid_t vb_name;
1145 int status;
1146 uint32_t i;
1147 uint32_t is_matched;
1149 /* Set vb on the last variable */
1150 for (vb = res->variables;
1151 (vb != NULL) && (vb->next_variable != NULL);
1152 vb = vb->next_variable)
1153 /* do nothing */;
1154 if (vb == NULL)
1155 return (-1);
1157 csnmp_oid_init (&vb_name, vb->name, vb->name_length);
1159 il = calloc (1, sizeof (*il));
1160 if (il == NULL)
1161 {
1162 ERROR ("snmp plugin: calloc failed.");
1163 return (-1);
1164 }
1165 il->next = NULL;
1167 status = csnmp_oid_suffix (&il->suffix, &vb_name, &dd->instance.oid);
1168 if (status != 0)
1169 {
1170 sfree (il);
1171 return (status);
1172 }
1174 /* Get instance name */
1175 if ((vb->type == ASN_OCTET_STR) || (vb->type == ASN_BIT_STR) || (vb->type == ASN_IPADDRESS))
1176 {
1177 char *ptr;
1179 csnmp_strvbcopy (il->instance, vb, sizeof (il->instance));
1180 is_matched = 0;
1181 for (i = 0; i < dd->ignores_len; i++)
1182 {
1183 status = fnmatch(dd->ignores[i], il->instance, 0);
1184 if (status == 0)
1185 {
1186 if (dd->invert_match == 0)
1187 {
1188 sfree(il);
1189 return 0;
1190 }
1191 else
1192 {
1193 is_matched = 1;
1194 break;
1195 }
1196 }
1197 }
1198 if (dd->invert_match != 0 && is_matched == 0)
1199 {
1200 sfree(il);
1201 return 0;
1202 }
1203 for (ptr = il->instance; *ptr != '\0'; ptr++)
1204 {
1205 if ((*ptr > 0) && (*ptr < 32))
1206 *ptr = ' ';
1207 else if (*ptr == '/')
1208 *ptr = '_';
1209 }
1210 DEBUG ("snmp plugin: il->instance = `%s';", il->instance);
1211 }
1212 else
1213 {
1214 value_t val = csnmp_value_list_to_value (vb, DS_TYPE_COUNTER,
1215 /* scale = */ 1.0, /* shift = */ 0.0, hd->name, dd->name);
1216 ssnprintf (il->instance, sizeof (il->instance),
1217 "%llu", val.counter);
1218 }
1220 /* TODO: Debugging output */
1222 if (*head == NULL)
1223 *head = il;
1224 else
1225 (*tail)->next = il;
1226 *tail = il;
1228 return (0);
1229 } /* int csnmp_instance_list_add */
1231 static int csnmp_dispatch_table (host_definition_t *host, data_definition_t *data,
1232 csnmp_list_instances_t *instance_list,
1233 csnmp_table_values_t **value_table)
1234 {
1235 const data_set_t *ds;
1236 value_list_t vl = VALUE_LIST_INIT;
1238 csnmp_list_instances_t *instance_list_ptr;
1239 csnmp_table_values_t **value_table_ptr;
1241 size_t i;
1242 _Bool have_more;
1243 oid_t current_suffix;
1245 ds = plugin_get_ds (data->type);
1246 if (!ds)
1247 {
1248 ERROR ("snmp plugin: DataSet `%s' not defined.", data->type);
1249 return (-1);
1250 }
1251 assert (ds->ds_num == data->values_len);
1252 assert (data->values_len > 0);
1254 instance_list_ptr = instance_list;
1256 value_table_ptr = calloc (data->values_len, sizeof (*value_table_ptr));
1257 if (value_table_ptr == NULL)
1258 return (-1);
1259 for (i = 0; i < data->values_len; i++)
1260 value_table_ptr[i] = value_table[i];
1262 vl.values_len = data->values_len;
1263 vl.values = malloc (sizeof (*vl.values) * vl.values_len);
1264 if (vl.values == NULL)
1265 {
1266 ERROR ("snmp plugin: malloc failed.");
1267 sfree (value_table_ptr);
1268 return (-1);
1269 }
1271 sstrncpy (vl.host, host->name, sizeof (vl.host));
1272 sstrncpy (vl.plugin, "snmp", sizeof (vl.plugin));
1274 vl.interval = host->interval;
1276 have_more = 1;
1277 while (have_more)
1278 {
1279 _Bool suffix_skipped = 0;
1281 /* Determine next suffix to handle. */
1282 if (instance_list != NULL)
1283 {
1284 if (instance_list_ptr == NULL)
1285 {
1286 have_more = 0;
1287 continue;
1288 }
1290 memcpy (¤t_suffix, &instance_list_ptr->suffix, sizeof (current_suffix));
1291 }
1292 else /* no instance configured */
1293 {
1294 csnmp_table_values_t *ptr = value_table_ptr[0];
1295 if (ptr == NULL)
1296 {
1297 have_more = 0;
1298 continue;
1299 }
1301 memcpy (¤t_suffix, &ptr->suffix, sizeof (current_suffix));
1302 }
1304 /* Update all the value_table_ptr to point at the entry with the same
1305 * trailing partial OID */
1306 for (i = 0; i < data->values_len; i++)
1307 {
1308 while ((value_table_ptr[i] != NULL)
1309 && (csnmp_oid_compare (&value_table_ptr[i]->suffix, ¤t_suffix) < 0))
1310 value_table_ptr[i] = value_table_ptr[i]->next;
1312 if (value_table_ptr[i] == NULL)
1313 {
1314 have_more = 0;
1315 break;
1316 }
1317 else if (csnmp_oid_compare (&value_table_ptr[i]->suffix, ¤t_suffix) > 0)
1318 {
1319 /* This suffix is missing in the subtree. Indicate this with the
1320 * "suffix_skipped" flag and try the next instance / suffix. */
1321 suffix_skipped = 1;
1322 break;
1323 }
1324 } /* for (i = 0; i < columns; i++) */
1326 if (!have_more)
1327 break;
1329 /* Matching the values failed. Start from the beginning again. */
1330 if (suffix_skipped)
1331 {
1332 if (instance_list != NULL)
1333 instance_list_ptr = instance_list_ptr->next;
1334 else
1335 value_table_ptr[0] = value_table_ptr[0]->next;
1337 continue;
1338 }
1340 /* if we reach this line, all value_table_ptr[i] are non-NULL and are set
1341 * to the same subid. instance_list_ptr is either NULL or points to the
1342 * same subid, too. */
1343 #if COLLECT_DEBUG
1344 for (i = 1; i < data->values_len; i++)
1345 {
1346 assert (value_table_ptr[i] != NULL);
1347 assert (csnmp_oid_compare (&value_table_ptr[i-1]->suffix,
1348 &value_table_ptr[i]->suffix) == 0);
1349 }
1350 assert ((instance_list_ptr == NULL)
1351 || (csnmp_oid_compare (&instance_list_ptr->suffix,
1352 &value_table_ptr[0]->suffix) == 0));
1353 #endif
1355 sstrncpy (vl.type, data->type, sizeof (vl.type));
1357 {
1358 char temp[DATA_MAX_NAME_LEN];
1360 if (instance_list_ptr == NULL)
1361 csnmp_oid_to_string (temp, sizeof (temp), ¤t_suffix);
1362 else
1363 sstrncpy (temp, instance_list_ptr->instance, sizeof (temp));
1365 if (data->instance_prefix == NULL)
1366 sstrncpy (vl.type_instance, temp, sizeof (vl.type_instance));
1367 else
1368 ssnprintf (vl.type_instance, sizeof (vl.type_instance), "%s%s",
1369 data->instance_prefix, temp);
1370 }
1372 for (i = 0; i < data->values_len; i++)
1373 vl.values[i] = value_table_ptr[i]->value;
1375 /* If we get here `vl.type_instance' and all `vl.values' have been set */
1376 plugin_dispatch_values (&vl);
1378 if (instance_list != NULL)
1379 instance_list_ptr = instance_list_ptr->next;
1380 else
1381 value_table_ptr[0] = value_table_ptr[0]->next;
1382 } /* while (have_more) */
1384 sfree (vl.values);
1385 sfree (value_table_ptr);
1387 return (0);
1388 } /* int csnmp_dispatch_table */
1390 static int csnmp_read_table (host_definition_t *host, data_definition_t *data)
1391 {
1392 struct snmp_pdu *req;
1393 struct snmp_pdu *res = NULL;
1394 struct variable_list *vb;
1396 const data_set_t *ds;
1398 size_t oid_list_len = data->values_len + 1;
1399 /* Holds the last OID returned by the device. We use this in the GETNEXT
1400 * request to proceed. */
1401 oid_t oid_list[oid_list_len];
1402 /* Set to false when an OID has left its subtree so we don't re-request it
1403 * again. */
1404 _Bool oid_list_todo[oid_list_len];
1406 int status;
1407 size_t i;
1409 /* `value_list_head' and `value_list_tail' implement a linked list for each
1410 * value. `instance_list_head' and `instance_list_tail' implement a linked list of
1411 * instance names. This is used to jump gaps in the table. */
1412 csnmp_list_instances_t *instance_list_head;
1413 csnmp_list_instances_t *instance_list_tail;
1414 csnmp_table_values_t **value_list_head;
1415 csnmp_table_values_t **value_list_tail;
1417 DEBUG ("snmp plugin: csnmp_read_table (host = %s, data = %s)",
1418 host->name, data->name);
1420 if (host->sess_handle == NULL)
1421 {
1422 DEBUG ("snmp plugin: csnmp_read_table: host->sess_handle == NULL");
1423 return (-1);
1424 }
1426 ds = plugin_get_ds (data->type);
1427 if (!ds)
1428 {
1429 ERROR ("snmp plugin: DataSet `%s' not defined.", data->type);
1430 return (-1);
1431 }
1433 if (ds->ds_num != data->values_len)
1434 {
1435 ERROR ("snmp plugin: DataSet `%s' requires %zu values, but config talks about %zu",
1436 data->type, ds->ds_num, data->values_len);
1437 return (-1);
1438 }
1439 assert (data->values_len > 0);
1441 /* We need a copy of all the OIDs, because GETNEXT will destroy them. */
1442 memcpy (oid_list, data->values, data->values_len * sizeof (oid_t));
1443 if (data->instance.oid.oid_len > 0)
1444 memcpy (oid_list + data->values_len, &data->instance.oid, sizeof (oid_t));
1445 else /* no InstanceFrom option specified. */
1446 oid_list_len--;
1448 for (i = 0; i < oid_list_len; i++)
1449 oid_list_todo[i] = 1;
1451 /* We're going to construct n linked lists, one for each "value".
1452 * value_list_head will contain pointers to the heads of these linked lists,
1453 * value_list_tail will contain pointers to the tail of the lists. */
1454 value_list_head = calloc (data->values_len, sizeof (*value_list_head));
1455 value_list_tail = calloc (data->values_len, sizeof (*value_list_tail));
1456 if ((value_list_head == NULL) || (value_list_tail == NULL))
1457 {
1458 ERROR ("snmp plugin: csnmp_read_table: calloc failed.");
1459 sfree (value_list_head);
1460 sfree (value_list_tail);
1461 return (-1);
1462 }
1464 instance_list_head = NULL;
1465 instance_list_tail = NULL;
1467 status = 0;
1468 while (status == 0)
1469 {
1470 int oid_list_todo_num;
1472 req = snmp_pdu_create (SNMP_MSG_GETNEXT);
1473 if (req == NULL)
1474 {
1475 ERROR ("snmp plugin: snmp_pdu_create failed.");
1476 status = -1;
1477 break;
1478 }
1480 oid_list_todo_num = 0;
1481 for (i = 0; i < oid_list_len; i++)
1482 {
1483 /* Do not rerequest already finished OIDs */
1484 if (!oid_list_todo[i])
1485 continue;
1486 oid_list_todo_num++;
1487 snmp_add_null_var (req, oid_list[i].oid, oid_list[i].oid_len);
1488 }
1490 if (oid_list_todo_num == 0)
1491 {
1492 /* The request is still empty - so we are finished */
1493 DEBUG ("snmp plugin: all variables have left their subtree");
1494 status = 0;
1495 break;
1496 }
1498 res = NULL;
1499 status = snmp_sess_synch_response (host->sess_handle, req, &res);
1500 if ((status != STAT_SUCCESS) || (res == NULL))
1501 {
1502 char *errstr = NULL;
1504 snmp_sess_error (host->sess_handle, NULL, NULL, &errstr);
1506 c_complain (LOG_ERR, &host->complaint,
1507 "snmp plugin: host %s: snmp_sess_synch_response failed: %s",
1508 host->name, (errstr == NULL) ? "Unknown problem" : errstr);
1510 if (res != NULL)
1511 snmp_free_pdu (res);
1512 res = NULL;
1514 /* snmp_synch_response already freed our PDU */
1515 req = NULL;
1516 sfree (errstr);
1517 csnmp_host_close_session (host);
1519 status = -1;
1520 break;
1521 }
1523 status = 0;
1524 assert (res != NULL);
1525 c_release (LOG_INFO, &host->complaint,
1526 "snmp plugin: host %s: snmp_sess_synch_response successful.",
1527 host->name);
1529 vb = res->variables;
1530 if (vb == NULL)
1531 {
1532 status = -1;
1533 break;
1534 }
1536 for (vb = res->variables, i = 0; (vb != NULL); vb = vb->next_variable, i++)
1537 {
1538 /* Calculate value index from todo list */
1539 while ((i < oid_list_len) && !oid_list_todo[i])
1540 i++;
1542 /* An instance is configured and the res variable we process is the
1543 * instance value (last index) */
1544 if ((data->instance.oid.oid_len > 0) && (i == data->values_len))
1545 {
1546 if ((vb->type == SNMP_ENDOFMIBVIEW)
1547 || (snmp_oid_ncompare (data->instance.oid.oid,
1548 data->instance.oid.oid_len,
1549 vb->name, vb->name_length,
1550 data->instance.oid.oid_len) != 0))
1551 {
1552 DEBUG ("snmp plugin: host = %s; data = %s; Instance left its subtree.",
1553 host->name, data->name);
1554 oid_list_todo[i] = 0;
1555 continue;
1556 }
1558 /* Allocate a new `csnmp_list_instances_t', insert the instance name and
1559 * add it to the list */
1560 if (csnmp_instance_list_add (&instance_list_head, &instance_list_tail,
1561 res, host, data) != 0)
1562 {
1563 ERROR ("snmp plugin: host %s: csnmp_instance_list_add failed.",
1564 host->name);
1565 status = -1;
1566 break;
1567 }
1568 }
1569 else /* The variable we are processing is a normal value */
1570 {
1571 csnmp_table_values_t *vt;
1572 oid_t vb_name;
1573 oid_t suffix;
1574 int ret;
1576 csnmp_oid_init (&vb_name, vb->name, vb->name_length);
1578 /* Calculate the current suffix. This is later used to check that the
1579 * suffix is increasing. This also checks if we left the subtree */
1580 ret = csnmp_oid_suffix (&suffix, &vb_name, data->values + i);
1581 if (ret != 0)
1582 {
1583 DEBUG ("snmp plugin: host = %s; data = %s; i = %zu; "
1584 "Value probably left its subtree.",
1585 host->name, data->name, i);
1586 oid_list_todo[i] = 0;
1587 continue;
1588 }
1590 /* Make sure the OIDs returned by the agent are increasing. Otherwise our
1591 * table matching algorithm will get confused. */
1592 if ((value_list_tail[i] != NULL)
1593 && (csnmp_oid_compare (&suffix, &value_list_tail[i]->suffix) <= 0))
1594 {
1595 DEBUG ("snmp plugin: host = %s; data = %s; i = %zu; "
1596 "Suffix is not increasing.",
1597 host->name, data->name, i);
1598 oid_list_todo[i] = 0;
1599 continue;
1600 }
1602 vt = calloc (1, sizeof (*vt));
1603 if (vt == NULL)
1604 {
1605 ERROR ("snmp plugin: calloc failed.");
1606 status = -1;
1607 break;
1608 }
1610 vt->value = csnmp_value_list_to_value (vb, ds->ds[i].type,
1611 data->scale, data->shift, host->name, data->name);
1612 memcpy (&vt->suffix, &suffix, sizeof (vt->suffix));
1613 vt->next = NULL;
1615 if (value_list_tail[i] == NULL)
1616 value_list_head[i] = vt;
1617 else
1618 value_list_tail[i]->next = vt;
1619 value_list_tail[i] = vt;
1620 }
1622 /* Copy OID to oid_list[i] */
1623 memcpy (oid_list[i].oid, vb->name, sizeof (oid) * vb->name_length);
1624 oid_list[i].oid_len = vb->name_length;
1626 } /* for (vb = res->variables ...) */
1628 if (res != NULL)
1629 snmp_free_pdu (res);
1630 res = NULL;
1631 } /* while (status == 0) */
1633 if (res != NULL)
1634 snmp_free_pdu (res);
1635 res = NULL;
1637 if (req != NULL)
1638 snmp_free_pdu (req);
1639 req = NULL;
1641 if (status == 0)
1642 csnmp_dispatch_table (host, data, instance_list_head, value_list_head);
1644 /* Free all allocated variables here */
1645 while (instance_list_head != NULL)
1646 {
1647 csnmp_list_instances_t *next = instance_list_head->next;
1648 sfree (instance_list_head);
1649 instance_list_head = next;
1650 }
1652 for (i = 0; i < data->values_len; i++)
1653 {
1654 while (value_list_head[i] != NULL)
1655 {
1656 csnmp_table_values_t *next = value_list_head[i]->next;
1657 sfree (value_list_head[i]);
1658 value_list_head[i] = next;
1659 }
1660 }
1662 sfree (value_list_head);
1663 sfree (value_list_tail);
1665 return (0);
1666 } /* int csnmp_read_table */
1668 static int csnmp_read_value (host_definition_t *host, data_definition_t *data)
1669 {
1670 struct snmp_pdu *req;
1671 struct snmp_pdu *res = NULL;
1672 struct variable_list *vb;
1674 const data_set_t *ds;
1675 value_list_t vl = VALUE_LIST_INIT;
1677 int status;
1678 size_t i;
1680 DEBUG ("snmp plugin: csnmp_read_value (host = %s, data = %s)",
1681 host->name, data->name);
1683 if (host->sess_handle == NULL)
1684 {
1685 DEBUG ("snmp plugin: csnmp_read_value: host->sess_handle == NULL");
1686 return (-1);
1687 }
1689 ds = plugin_get_ds (data->type);
1690 if (!ds)
1691 {
1692 ERROR ("snmp plugin: DataSet `%s' not defined.", data->type);
1693 return (-1);
1694 }
1696 if (ds->ds_num != data->values_len)
1697 {
1698 ERROR ("snmp plugin: DataSet `%s' requires %zu values, but config talks about %zu",
1699 data->type, ds->ds_num, data->values_len);
1700 return (-1);
1701 }
1703 vl.values_len = ds->ds_num;
1704 vl.values = malloc (sizeof (*vl.values) * vl.values_len);
1705 if (vl.values == NULL)
1706 return (-1);
1707 for (i = 0; i < vl.values_len; i++)
1708 {
1709 if (ds->ds[i].type == DS_TYPE_COUNTER)
1710 vl.values[i].counter = 0;
1711 else
1712 vl.values[i].gauge = NAN;
1713 }
1715 sstrncpy (vl.host, host->name, sizeof (vl.host));
1716 sstrncpy (vl.plugin, "snmp", sizeof (vl.plugin));
1717 sstrncpy (vl.type, data->type, sizeof (vl.type));
1718 sstrncpy (vl.type_instance, data->instance.string, sizeof (vl.type_instance));
1720 vl.interval = host->interval;
1722 req = snmp_pdu_create (SNMP_MSG_GET);
1723 if (req == NULL)
1724 {
1725 ERROR ("snmp plugin: snmp_pdu_create failed.");
1726 sfree (vl.values);
1727 return (-1);
1728 }
1730 for (i = 0; i < data->values_len; i++)
1731 snmp_add_null_var (req, data->values[i].oid, data->values[i].oid_len);
1733 status = snmp_sess_synch_response (host->sess_handle, req, &res);
1735 if ((status != STAT_SUCCESS) || (res == NULL))
1736 {
1737 char *errstr = NULL;
1739 snmp_sess_error (host->sess_handle, NULL, NULL, &errstr);
1740 ERROR ("snmp plugin: host %s: snmp_sess_synch_response failed: %s",
1741 host->name, (errstr == NULL) ? "Unknown problem" : errstr);
1743 if (res != NULL)
1744 snmp_free_pdu (res);
1746 sfree (errstr);
1747 sfree (vl.values);
1748 csnmp_host_close_session (host);
1750 return (-1);
1751 }
1754 for (vb = res->variables; vb != NULL; vb = vb->next_variable)
1755 {
1756 #if COLLECT_DEBUG
1757 char buffer[1024];
1758 snprint_variable (buffer, sizeof (buffer),
1759 vb->name, vb->name_length, vb);
1760 DEBUG ("snmp plugin: Got this variable: %s", buffer);
1761 #endif /* COLLECT_DEBUG */
1763 for (i = 0; i < data->values_len; i++)
1764 if (snmp_oid_compare (data->values[i].oid, data->values[i].oid_len,
1765 vb->name, vb->name_length) == 0)
1766 vl.values[i] = csnmp_value_list_to_value (vb, ds->ds[i].type,
1767 data->scale, data->shift, host->name, data->name);
1768 } /* for (res->variables) */
1770 snmp_free_pdu (res);
1772 DEBUG ("snmp plugin: -> plugin_dispatch_values (&vl);");
1773 plugin_dispatch_values (&vl);
1774 sfree (vl.values);
1776 return (0);
1777 } /* int csnmp_read_value */
1779 static int csnmp_read_host (user_data_t *ud)
1780 {
1781 host_definition_t *host;
1782 int status;
1783 int success;
1784 int i;
1786 host = ud->data;
1788 if (host->interval == 0)
1789 host->interval = plugin_get_interval ();
1791 if (host->sess_handle == NULL)
1792 csnmp_host_open_session (host);
1794 if (host->sess_handle == NULL)
1795 return (-1);
1797 success = 0;
1798 for (i = 0; i < host->data_list_len; i++)
1799 {
1800 data_definition_t *data = host->data_list[i];
1802 if (data->is_table)
1803 status = csnmp_read_table (host, data);
1804 else
1805 status = csnmp_read_value (host, data);
1807 if (status == 0)
1808 success++;
1809 }
1811 if (success == 0)
1812 return (-1);
1814 return (0);
1815 } /* int csnmp_read_host */
1817 static int csnmp_init (void)
1818 {
1819 call_snmp_init_once ();
1821 return (0);
1822 } /* int csnmp_init */
1824 static int csnmp_shutdown (void)
1825 {
1826 data_definition_t *data_this;
1827 data_definition_t *data_next;
1829 /* When we get here, the read threads have been stopped and all the
1830 * `host_definition_t' will be freed. */
1831 DEBUG ("snmp plugin: Destroying all data definitions.");
1833 data_this = data_head;
1834 data_head = NULL;
1835 while (data_this != NULL)
1836 {
1837 data_next = data_this->next;
1839 sfree (data_this->name);
1840 sfree (data_this->type);
1841 sfree (data_this->values);
1842 sfree (data_this->ignores);
1843 sfree (data_this);
1845 data_this = data_next;
1846 }
1848 return (0);
1849 } /* int csnmp_shutdown */
1851 void module_register (void)
1852 {
1853 plugin_register_complex_config ("snmp", csnmp_config);
1854 plugin_register_init ("snmp", csnmp_init);
1855 plugin_register_shutdown ("snmp", csnmp_shutdown);
1856 } /* void module_register */
1858 /*
1859 * vim: shiftwidth=2 softtabstop=2 tabstop=8 fdm=marker
1860 */