6a5bb8b964802af92ffad9d600e1255e2ed3a4d8
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;
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 memset (&cb_data, 0, sizeof (cb_data));
776 cb_data.data = hd;
777 cb_data.free_func = csnmp_host_definition_destroy;
779 status = plugin_register_complex_read (/* group = */ NULL, cb_name,
780 csnmp_read_host, hd->interval, /* user_data = */ &cb_data);
781 if (status != 0)
782 {
783 ERROR ("snmp plugin: Registering complex read function failed.");
784 csnmp_host_definition_destroy (hd);
785 return (-1);
786 }
788 return (0);
789 } /* int csnmp_config_add_host */
791 static int csnmp_config (oconfig_item_t *ci)
792 {
793 int i;
795 call_snmp_init_once ();
797 for (i = 0; i < ci->children_num; i++)
798 {
799 oconfig_item_t *child = ci->children + i;
800 if (strcasecmp ("Data", child->key) == 0)
801 csnmp_config_add_data (child);
802 else if (strcasecmp ("Host", child->key) == 0)
803 csnmp_config_add_host (child);
804 else
805 {
806 WARNING ("snmp plugin: Ignoring unknown config option `%s'.", child->key);
807 }
808 } /* for (ci->children) */
810 return (0);
811 } /* int csnmp_config */
813 /* }}} End of the config stuff. Now the interesting part begins */
815 static void csnmp_host_open_session (host_definition_t *host)
816 {
817 struct snmp_session sess;
818 int error;
820 if (host->sess_handle != NULL)
821 csnmp_host_close_session (host);
823 snmp_sess_init (&sess);
824 sess.peername = host->address;
825 switch (host->version)
826 {
827 case 1:
828 sess.version = SNMP_VERSION_1;
829 break;
830 case 3:
831 sess.version = SNMP_VERSION_3;
832 break;
833 default:
834 sess.version = SNMP_VERSION_2c;
835 break;
836 }
838 if (host->version == 3)
839 {
840 sess.securityName = host->username;
841 sess.securityNameLen = strlen (host->username);
842 sess.securityLevel = host->security_level;
844 if (sess.securityLevel == SNMP_SEC_LEVEL_AUTHNOPRIV || sess.securityLevel == SNMP_SEC_LEVEL_AUTHPRIV)
845 {
846 sess.securityAuthProto = host->auth_protocol;
847 sess.securityAuthProtoLen = host->auth_protocol_len;
848 sess.securityAuthKeyLen = USM_AUTH_KU_LEN;
849 error = generate_Ku (sess.securityAuthProto,
850 sess.securityAuthProtoLen,
851 (u_char *) host->auth_passphrase,
852 strlen(host->auth_passphrase),
853 sess.securityAuthKey,
854 &sess.securityAuthKeyLen);
855 if (error != SNMPERR_SUCCESS) {
856 ERROR ("snmp plugin: host %s: Error generating Ku from auth_passphrase. (Error %d)", host->name, error);
857 }
858 }
860 if (sess.securityLevel == SNMP_SEC_LEVEL_AUTHPRIV)
861 {
862 sess.securityPrivProto = host->priv_protocol;
863 sess.securityPrivProtoLen = host->priv_protocol_len;
864 sess.securityPrivKeyLen = USM_PRIV_KU_LEN;
865 error = generate_Ku (sess.securityAuthProto,
866 sess.securityAuthProtoLen,
867 (u_char *) host->priv_passphrase,
868 strlen(host->priv_passphrase),
869 sess.securityPrivKey,
870 &sess.securityPrivKeyLen);
871 if (error != SNMPERR_SUCCESS) {
872 ERROR ("snmp plugin: host %s: Error generating Ku from priv_passphrase. (Error %d)", host->name, error);
873 }
874 }
876 if (host->context != NULL)
877 {
878 sess.contextName = host->context;
879 sess.contextNameLen = strlen (host->context);
880 }
881 }
882 else /* SNMPv1/2 "authenticates" with community string */
883 {
884 sess.community = (u_char *) host->community;
885 sess.community_len = strlen (host->community);
886 }
888 /* snmp_sess_open will copy the `struct snmp_session *'. */
889 host->sess_handle = snmp_sess_open (&sess);
891 if (host->sess_handle == NULL)
892 {
893 char *errstr = NULL;
895 snmp_error (&sess, NULL, NULL, &errstr);
897 ERROR ("snmp plugin: host %s: snmp_sess_open failed: %s",
898 host->name, (errstr == NULL) ? "Unknown problem" : errstr);
899 sfree (errstr);
900 }
901 } /* void csnmp_host_open_session */
903 /* TODO: Check if negative values wrap around. Problem: negative temperatures. */
904 static value_t csnmp_value_list_to_value (struct variable_list *vl, int type,
905 double scale, double shift,
906 const char *host_name, const char *data_name)
907 {
908 value_t ret;
909 uint64_t tmp_unsigned = 0;
910 int64_t tmp_signed = 0;
911 _Bool defined = 1;
912 /* Set to true when the original SNMP type appears to have been signed. */
913 _Bool prefer_signed = 0;
915 if ((vl->type == ASN_INTEGER)
916 || (vl->type == ASN_UINTEGER)
917 || (vl->type == ASN_COUNTER)
918 #ifdef ASN_TIMETICKS
919 || (vl->type == ASN_TIMETICKS)
920 #endif
921 || (vl->type == ASN_GAUGE))
922 {
923 tmp_unsigned = (uint32_t) *vl->val.integer;
924 tmp_signed = (int32_t) *vl->val.integer;
926 if (vl->type == ASN_INTEGER)
927 prefer_signed = 1;
929 DEBUG ("snmp plugin: Parsed int32 value is %"PRIu64".", tmp_unsigned);
930 }
931 else if (vl->type == ASN_COUNTER64)
932 {
933 tmp_unsigned = (uint32_t) vl->val.counter64->high;
934 tmp_unsigned = tmp_unsigned << 32;
935 tmp_unsigned += (uint32_t) vl->val.counter64->low;
936 tmp_signed = (int64_t) tmp_unsigned;
937 DEBUG ("snmp plugin: Parsed int64 value is %"PRIu64".", tmp_unsigned);
938 }
939 else if (vl->type == ASN_OCTET_STR)
940 {
941 /* We'll handle this later.. */
942 }
943 else
944 {
945 char oid_buffer[1024];
947 memset (oid_buffer, 0, sizeof (oid_buffer));
948 snprint_objid (oid_buffer, sizeof (oid_buffer) - 1,
949 vl->name, vl->name_length);
951 #ifdef ASN_NULL
952 if (vl->type == ASN_NULL)
953 INFO ("snmp plugin: OID \"%s\" is undefined (type ASN_NULL)",
954 oid_buffer);
955 else
956 #endif
957 WARNING ("snmp plugin: I don't know the ASN type #%i "
958 "(OID: \"%s\", data block \"%s\", host block \"%s\")",
959 (int) vl->type, oid_buffer,
960 (data_name != NULL) ? data_name : "UNKNOWN",
961 (host_name != NULL) ? host_name : "UNKNOWN");
963 defined = 0;
964 }
966 if (vl->type == ASN_OCTET_STR)
967 {
968 int status = -1;
970 if (vl->val.string != NULL)
971 {
972 char string[64];
973 size_t string_length;
975 string_length = sizeof (string) - 1;
976 if (vl->val_len < string_length)
977 string_length = vl->val_len;
979 /* The strings we get from the Net-SNMP library may not be null
980 * terminated. That is why we're using `memcpy' here and not `strcpy'.
981 * `string_length' is set to `vl->val_len' which holds the length of the
982 * string. -octo */
983 memcpy (string, vl->val.string, string_length);
984 string[string_length] = 0;
986 status = parse_value (string, &ret, type);
987 if (status != 0)
988 {
989 ERROR ("snmp plugin: host %s: csnmp_value_list_to_value: Parsing string as %s failed: %s",
990 (host_name != NULL) ? host_name : "UNKNOWN",
991 DS_TYPE_TO_STRING (type), string);
992 }
993 }
995 if (status != 0)
996 {
997 switch (type)
998 {
999 case DS_TYPE_COUNTER:
1000 case DS_TYPE_DERIVE:
1001 case DS_TYPE_ABSOLUTE:
1002 memset (&ret, 0, sizeof (ret));
1003 break;
1005 case DS_TYPE_GAUGE:
1006 ret.gauge = NAN;
1007 break;
1009 default:
1010 ERROR ("snmp plugin: csnmp_value_list_to_value: Unknown "
1011 "data source type: %i.", type);
1012 ret.gauge = NAN;
1013 }
1014 }
1015 } /* if (vl->type == ASN_OCTET_STR) */
1016 else if (type == DS_TYPE_COUNTER)
1017 {
1018 ret.counter = tmp_unsigned;
1019 }
1020 else if (type == DS_TYPE_GAUGE)
1021 {
1022 if (!defined)
1023 ret.gauge = NAN;
1024 else if (prefer_signed)
1025 ret.gauge = (scale * tmp_signed) + shift;
1026 else
1027 ret.gauge = (scale * tmp_unsigned) + shift;
1028 }
1029 else if (type == DS_TYPE_DERIVE)
1030 {
1031 if (prefer_signed)
1032 ret.derive = (derive_t) tmp_signed;
1033 else
1034 ret.derive = (derive_t) tmp_unsigned;
1035 }
1036 else if (type == DS_TYPE_ABSOLUTE)
1037 {
1038 ret.absolute = (absolute_t) tmp_unsigned;
1039 }
1040 else
1041 {
1042 ERROR ("snmp plugin: csnmp_value_list_to_value: Unknown data source "
1043 "type: %i.", type);
1044 ret.gauge = NAN;
1045 }
1047 return (ret);
1048 } /* value_t csnmp_value_list_to_value */
1050 /* csnmp_strvbcopy_hexstring converts the bit string contained in "vb" to a hex
1051 * representation and writes it to dst. Returns zero on success and ENOMEM if
1052 * dst is not large enough to hold the string. dst is guaranteed to be
1053 * nul-terminated. */
1054 static int csnmp_strvbcopy_hexstring (char *dst, /* {{{ */
1055 const struct variable_list *vb, size_t dst_size)
1056 {
1057 char *buffer_ptr;
1058 size_t buffer_free;
1059 size_t i;
1061 dst[0] = 0;
1063 buffer_ptr = dst;
1064 buffer_free = dst_size;
1066 for (i = 0; i < vb->val_len; i++)
1067 {
1068 int status;
1070 status = snprintf (buffer_ptr, buffer_free,
1071 (i == 0) ? "%02x" : ":%02x", (unsigned int) vb->val.bitstring[i]);
1072 assert (status >= 0);
1074 if (((size_t) status) >= buffer_free) /* truncated */
1075 {
1076 dst[dst_size - 1] = 0;
1077 return ENOMEM;
1078 }
1079 else /* if (status < buffer_free) */
1080 {
1081 buffer_ptr += (size_t) status;
1082 buffer_free -= (size_t) status;
1083 }
1084 }
1086 return 0;
1087 } /* }}} int csnmp_strvbcopy_hexstring */
1089 /* csnmp_strvbcopy copies the octet string or bit string contained in vb to
1090 * dst. If non-printable characters are detected, it will switch to a hex
1091 * representation of the string. Returns zero on success, EINVAL if vb does not
1092 * contain a string and ENOMEM if dst is not large enough to contain the
1093 * string. */
1094 static int csnmp_strvbcopy (char *dst, /* {{{ */
1095 const struct variable_list *vb, size_t dst_size)
1096 {
1097 char *src;
1098 size_t num_chars;
1099 size_t i;
1101 if (vb->type == ASN_OCTET_STR)
1102 src = (char *) vb->val.string;
1103 else if (vb->type == ASN_BIT_STR)
1104 src = (char *) vb->val.bitstring;
1105 else if (vb->type == ASN_IPADDRESS)
1106 {
1107 return ssnprintf (dst, dst_size, "%"PRIu8".%"PRIu8".%"PRIu8".%"PRIu8"",
1108 (uint8_t) vb->val.string[0],
1109 (uint8_t) vb->val.string[1],
1110 (uint8_t) vb->val.string[2],
1111 (uint8_t) vb->val.string[3]);
1112 }
1113 else
1114 {
1115 dst[0] = 0;
1116 return (EINVAL);
1117 }
1119 num_chars = dst_size - 1;
1120 if (num_chars > vb->val_len)
1121 num_chars = vb->val_len;
1123 for (i = 0; i < num_chars; i++)
1124 {
1125 /* Check for control characters. */
1126 if ((unsigned char)src[i] < 32)
1127 return (csnmp_strvbcopy_hexstring (dst, vb, dst_size));
1128 dst[i] = src[i];
1129 }
1130 dst[num_chars] = 0;
1131 dst[dst_size - 1] = 0;
1133 if (dst_size <= vb->val_len)
1134 return ENOMEM;
1136 return 0;
1137 } /* }}} int csnmp_strvbcopy */
1139 static int csnmp_instance_list_add (csnmp_list_instances_t **head,
1140 csnmp_list_instances_t **tail,
1141 const struct snmp_pdu *res,
1142 const host_definition_t *hd, const data_definition_t *dd)
1143 {
1144 csnmp_list_instances_t *il;
1145 struct variable_list *vb;
1146 oid_t vb_name;
1147 int status;
1148 uint32_t i;
1149 uint32_t is_matched;
1151 /* Set vb on the last variable */
1152 for (vb = res->variables;
1153 (vb != NULL) && (vb->next_variable != NULL);
1154 vb = vb->next_variable)
1155 /* do nothing */;
1156 if (vb == NULL)
1157 return (-1);
1159 csnmp_oid_init (&vb_name, vb->name, vb->name_length);
1161 il = calloc (1, sizeof (*il));
1162 if (il == NULL)
1163 {
1164 ERROR ("snmp plugin: calloc failed.");
1165 return (-1);
1166 }
1167 il->next = NULL;
1169 status = csnmp_oid_suffix (&il->suffix, &vb_name, &dd->instance.oid);
1170 if (status != 0)
1171 {
1172 sfree (il);
1173 return (status);
1174 }
1176 /* Get instance name */
1177 if ((vb->type == ASN_OCTET_STR) || (vb->type == ASN_BIT_STR) || (vb->type == ASN_IPADDRESS))
1178 {
1179 char *ptr;
1181 csnmp_strvbcopy (il->instance, vb, sizeof (il->instance));
1182 is_matched = 0;
1183 for (i = 0; i < dd->ignores_len; i++)
1184 {
1185 status = fnmatch(dd->ignores[i], il->instance, 0);
1186 if (status == 0)
1187 {
1188 if (dd->invert_match == 0)
1189 {
1190 sfree(il);
1191 return 0;
1192 }
1193 else
1194 {
1195 is_matched = 1;
1196 break;
1197 }
1198 }
1199 }
1200 if (dd->invert_match != 0 && is_matched == 0)
1201 {
1202 sfree(il);
1203 return 0;
1204 }
1205 for (ptr = il->instance; *ptr != '\0'; ptr++)
1206 {
1207 if ((*ptr > 0) && (*ptr < 32))
1208 *ptr = ' ';
1209 else if (*ptr == '/')
1210 *ptr = '_';
1211 }
1212 DEBUG ("snmp plugin: il->instance = `%s';", il->instance);
1213 }
1214 else
1215 {
1216 value_t val = csnmp_value_list_to_value (vb, DS_TYPE_COUNTER,
1217 /* scale = */ 1.0, /* shift = */ 0.0, hd->name, dd->name);
1218 ssnprintf (il->instance, sizeof (il->instance),
1219 "%llu", val.counter);
1220 }
1222 /* TODO: Debugging output */
1224 if (*head == NULL)
1225 *head = il;
1226 else
1227 (*tail)->next = il;
1228 *tail = il;
1230 return (0);
1231 } /* int csnmp_instance_list_add */
1233 static int csnmp_dispatch_table (host_definition_t *host, data_definition_t *data,
1234 csnmp_list_instances_t *instance_list,
1235 csnmp_table_values_t **value_table)
1236 {
1237 const data_set_t *ds;
1238 value_list_t vl = VALUE_LIST_INIT;
1240 csnmp_list_instances_t *instance_list_ptr;
1241 csnmp_table_values_t **value_table_ptr;
1243 size_t i;
1244 _Bool have_more;
1245 oid_t current_suffix;
1247 ds = plugin_get_ds (data->type);
1248 if (!ds)
1249 {
1250 ERROR ("snmp plugin: DataSet `%s' not defined.", data->type);
1251 return (-1);
1252 }
1253 assert (ds->ds_num == data->values_len);
1254 assert (data->values_len > 0);
1256 instance_list_ptr = instance_list;
1258 value_table_ptr = calloc (data->values_len, sizeof (*value_table_ptr));
1259 if (value_table_ptr == NULL)
1260 return (-1);
1261 for (i = 0; i < data->values_len; i++)
1262 value_table_ptr[i] = value_table[i];
1264 vl.values_len = data->values_len;
1265 vl.values = malloc (sizeof (*vl.values) * vl.values_len);
1266 if (vl.values == NULL)
1267 {
1268 ERROR ("snmp plugin: malloc failed.");
1269 sfree (value_table_ptr);
1270 return (-1);
1271 }
1273 sstrncpy (vl.host, host->name, sizeof (vl.host));
1274 sstrncpy (vl.plugin, "snmp", sizeof (vl.plugin));
1276 vl.interval = host->interval;
1278 have_more = 1;
1279 memset (¤t_suffix, 0, sizeof (current_suffix));
1280 while (have_more)
1281 {
1282 _Bool suffix_skipped = 0;
1284 /* Determine next suffix to handle. */
1285 if (instance_list != NULL)
1286 {
1287 if (instance_list_ptr == NULL)
1288 {
1289 have_more = 0;
1290 continue;
1291 }
1293 memcpy (¤t_suffix, &instance_list_ptr->suffix, sizeof (current_suffix));
1294 }
1295 else /* no instance configured */
1296 {
1297 csnmp_table_values_t *ptr = value_table_ptr[0];
1298 if (ptr == NULL)
1299 {
1300 have_more = 0;
1301 continue;
1302 }
1304 memcpy (¤t_suffix, &ptr->suffix, sizeof (current_suffix));
1305 }
1307 /* Update all the value_table_ptr to point at the entry with the same
1308 * trailing partial OID */
1309 for (i = 0; i < data->values_len; i++)
1310 {
1311 while ((value_table_ptr[i] != NULL)
1312 && (csnmp_oid_compare (&value_table_ptr[i]->suffix, ¤t_suffix) < 0))
1313 value_table_ptr[i] = value_table_ptr[i]->next;
1315 if (value_table_ptr[i] == NULL)
1316 {
1317 have_more = 0;
1318 break;
1319 }
1320 else if (csnmp_oid_compare (&value_table_ptr[i]->suffix, ¤t_suffix) > 0)
1321 {
1322 /* This suffix is missing in the subtree. Indicate this with the
1323 * "suffix_skipped" flag and try the next instance / suffix. */
1324 suffix_skipped = 1;
1325 break;
1326 }
1327 } /* for (i = 0; i < columns; i++) */
1329 if (!have_more)
1330 break;
1332 /* Matching the values failed. Start from the beginning again. */
1333 if (suffix_skipped)
1334 {
1335 if (instance_list != NULL)
1336 instance_list_ptr = instance_list_ptr->next;
1337 else
1338 value_table_ptr[0] = value_table_ptr[0]->next;
1340 continue;
1341 }
1343 /* if we reach this line, all value_table_ptr[i] are non-NULL and are set
1344 * to the same subid. instance_list_ptr is either NULL or points to the
1345 * same subid, too. */
1346 #if COLLECT_DEBUG
1347 for (i = 1; i < data->values_len; i++)
1348 {
1349 assert (value_table_ptr[i] != NULL);
1350 assert (csnmp_oid_compare (&value_table_ptr[i-1]->suffix,
1351 &value_table_ptr[i]->suffix) == 0);
1352 }
1353 assert ((instance_list_ptr == NULL)
1354 || (csnmp_oid_compare (&instance_list_ptr->suffix,
1355 &value_table_ptr[0]->suffix) == 0));
1356 #endif
1358 sstrncpy (vl.type, data->type, sizeof (vl.type));
1360 {
1361 char temp[DATA_MAX_NAME_LEN];
1363 if (instance_list_ptr == NULL)
1364 csnmp_oid_to_string (temp, sizeof (temp), ¤t_suffix);
1365 else
1366 sstrncpy (temp, instance_list_ptr->instance, sizeof (temp));
1368 if (data->instance_prefix == NULL)
1369 sstrncpy (vl.type_instance, temp, sizeof (vl.type_instance));
1370 else
1371 ssnprintf (vl.type_instance, sizeof (vl.type_instance), "%s%s",
1372 data->instance_prefix, temp);
1373 }
1375 for (i = 0; i < data->values_len; i++)
1376 vl.values[i] = value_table_ptr[i]->value;
1378 /* If we get here `vl.type_instance' and all `vl.values' have been set */
1379 plugin_dispatch_values (&vl);
1381 if (instance_list != NULL)
1382 instance_list_ptr = instance_list_ptr->next;
1383 else
1384 value_table_ptr[0] = value_table_ptr[0]->next;
1385 } /* while (have_more) */
1387 sfree (vl.values);
1388 sfree (value_table_ptr);
1390 return (0);
1391 } /* int csnmp_dispatch_table */
1393 static int csnmp_read_table (host_definition_t *host, data_definition_t *data)
1394 {
1395 struct snmp_pdu *req;
1396 struct snmp_pdu *res = NULL;
1397 struct variable_list *vb;
1399 const data_set_t *ds;
1401 size_t oid_list_len = data->values_len + 1;
1402 /* Holds the last OID returned by the device. We use this in the GETNEXT
1403 * request to proceed. */
1404 oid_t oid_list[oid_list_len];
1405 /* Set to false when an OID has left its subtree so we don't re-request it
1406 * again. */
1407 _Bool oid_list_todo[oid_list_len];
1409 int status;
1410 size_t i;
1412 /* `value_list_head' and `value_list_tail' implement a linked list for each
1413 * value. `instance_list_head' and `instance_list_tail' implement a linked list of
1414 * instance names. This is used to jump gaps in the table. */
1415 csnmp_list_instances_t *instance_list_head;
1416 csnmp_list_instances_t *instance_list_tail;
1417 csnmp_table_values_t **value_list_head;
1418 csnmp_table_values_t **value_list_tail;
1420 DEBUG ("snmp plugin: csnmp_read_table (host = %s, data = %s)",
1421 host->name, data->name);
1423 if (host->sess_handle == NULL)
1424 {
1425 DEBUG ("snmp plugin: csnmp_read_table: host->sess_handle == NULL");
1426 return (-1);
1427 }
1429 ds = plugin_get_ds (data->type);
1430 if (!ds)
1431 {
1432 ERROR ("snmp plugin: DataSet `%s' not defined.", data->type);
1433 return (-1);
1434 }
1436 if (ds->ds_num != data->values_len)
1437 {
1438 ERROR ("snmp plugin: DataSet `%s' requires %zu values, but config talks about %zu",
1439 data->type, ds->ds_num, data->values_len);
1440 return (-1);
1441 }
1442 assert (data->values_len > 0);
1444 /* We need a copy of all the OIDs, because GETNEXT will destroy them. */
1445 memcpy (oid_list, data->values, data->values_len * sizeof (oid_t));
1446 if (data->instance.oid.oid_len > 0)
1447 memcpy (oid_list + data->values_len, &data->instance.oid, sizeof (oid_t));
1448 else /* no InstanceFrom option specified. */
1449 oid_list_len--;
1451 for (i = 0; i < oid_list_len; i++)
1452 oid_list_todo[i] = 1;
1454 /* We're going to construct n linked lists, one for each "value".
1455 * value_list_head will contain pointers to the heads of these linked lists,
1456 * value_list_tail will contain pointers to the tail of the lists. */
1457 value_list_head = calloc (data->values_len, sizeof (*value_list_head));
1458 value_list_tail = calloc (data->values_len, sizeof (*value_list_tail));
1459 if ((value_list_head == NULL) || (value_list_tail == NULL))
1460 {
1461 ERROR ("snmp plugin: csnmp_read_table: calloc failed.");
1462 sfree (value_list_head);
1463 sfree (value_list_tail);
1464 return (-1);
1465 }
1467 instance_list_head = NULL;
1468 instance_list_tail = NULL;
1470 status = 0;
1471 while (status == 0)
1472 {
1473 int oid_list_todo_num;
1475 req = snmp_pdu_create (SNMP_MSG_GETNEXT);
1476 if (req == NULL)
1477 {
1478 ERROR ("snmp plugin: snmp_pdu_create failed.");
1479 status = -1;
1480 break;
1481 }
1483 oid_list_todo_num = 0;
1484 for (i = 0; i < oid_list_len; i++)
1485 {
1486 /* Do not rerequest already finished OIDs */
1487 if (!oid_list_todo[i])
1488 continue;
1489 oid_list_todo_num++;
1490 snmp_add_null_var (req, oid_list[i].oid, oid_list[i].oid_len);
1491 }
1493 if (oid_list_todo_num == 0)
1494 {
1495 /* The request is still empty - so we are finished */
1496 DEBUG ("snmp plugin: all variables have left their subtree");
1497 status = 0;
1498 break;
1499 }
1501 res = NULL;
1502 status = snmp_sess_synch_response (host->sess_handle, req, &res);
1503 if ((status != STAT_SUCCESS) || (res == NULL))
1504 {
1505 char *errstr = NULL;
1507 snmp_sess_error (host->sess_handle, NULL, NULL, &errstr);
1509 c_complain (LOG_ERR, &host->complaint,
1510 "snmp plugin: host %s: snmp_sess_synch_response failed: %s",
1511 host->name, (errstr == NULL) ? "Unknown problem" : errstr);
1513 if (res != NULL)
1514 snmp_free_pdu (res);
1515 res = NULL;
1517 /* snmp_synch_response already freed our PDU */
1518 req = NULL;
1519 sfree (errstr);
1520 csnmp_host_close_session (host);
1522 status = -1;
1523 break;
1524 }
1526 status = 0;
1527 assert (res != NULL);
1528 c_release (LOG_INFO, &host->complaint,
1529 "snmp plugin: host %s: snmp_sess_synch_response successful.",
1530 host->name);
1532 vb = res->variables;
1533 if (vb == NULL)
1534 {
1535 status = -1;
1536 break;
1537 }
1539 for (vb = res->variables, i = 0; (vb != NULL); vb = vb->next_variable, i++)
1540 {
1541 /* Calculate value index from todo list */
1542 while ((i < oid_list_len) && !oid_list_todo[i])
1543 i++;
1545 /* An instance is configured and the res variable we process is the
1546 * instance value (last index) */
1547 if ((data->instance.oid.oid_len > 0) && (i == data->values_len))
1548 {
1549 if ((vb->type == SNMP_ENDOFMIBVIEW)
1550 || (snmp_oid_ncompare (data->instance.oid.oid,
1551 data->instance.oid.oid_len,
1552 vb->name, vb->name_length,
1553 data->instance.oid.oid_len) != 0))
1554 {
1555 DEBUG ("snmp plugin: host = %s; data = %s; Instance left its subtree.",
1556 host->name, data->name);
1557 oid_list_todo[i] = 0;
1558 continue;
1559 }
1561 /* Allocate a new `csnmp_list_instances_t', insert the instance name and
1562 * add it to the list */
1563 if (csnmp_instance_list_add (&instance_list_head, &instance_list_tail,
1564 res, host, data) != 0)
1565 {
1566 ERROR ("snmp plugin: host %s: csnmp_instance_list_add failed.",
1567 host->name);
1568 status = -1;
1569 break;
1570 }
1571 }
1572 else /* The variable we are processing is a normal value */
1573 {
1574 csnmp_table_values_t *vt;
1575 oid_t vb_name;
1576 oid_t suffix;
1577 int ret;
1579 csnmp_oid_init (&vb_name, vb->name, vb->name_length);
1581 /* Calculate the current suffix. This is later used to check that the
1582 * suffix is increasing. This also checks if we left the subtree */
1583 ret = csnmp_oid_suffix (&suffix, &vb_name, data->values + i);
1584 if (ret != 0)
1585 {
1586 DEBUG ("snmp plugin: host = %s; data = %s; i = %zu; "
1587 "Value probably left its subtree.",
1588 host->name, data->name, i);
1589 oid_list_todo[i] = 0;
1590 continue;
1591 }
1593 /* Make sure the OIDs returned by the agent are increasing. Otherwise our
1594 * table matching algorithm will get confused. */
1595 if ((value_list_tail[i] != NULL)
1596 && (csnmp_oid_compare (&suffix, &value_list_tail[i]->suffix) <= 0))
1597 {
1598 DEBUG ("snmp plugin: host = %s; data = %s; i = %zu; "
1599 "Suffix is not increasing.",
1600 host->name, data->name, i);
1601 oid_list_todo[i] = 0;
1602 continue;
1603 }
1605 vt = calloc (1, sizeof (*vt));
1606 if (vt == NULL)
1607 {
1608 ERROR ("snmp plugin: calloc failed.");
1609 status = -1;
1610 break;
1611 }
1613 vt->value = csnmp_value_list_to_value (vb, ds->ds[i].type,
1614 data->scale, data->shift, host->name, data->name);
1615 memcpy (&vt->suffix, &suffix, sizeof (vt->suffix));
1616 vt->next = NULL;
1618 if (value_list_tail[i] == NULL)
1619 value_list_head[i] = vt;
1620 else
1621 value_list_tail[i]->next = vt;
1622 value_list_tail[i] = vt;
1623 }
1625 /* Copy OID to oid_list[i] */
1626 memcpy (oid_list[i].oid, vb->name, sizeof (oid) * vb->name_length);
1627 oid_list[i].oid_len = vb->name_length;
1629 } /* for (vb = res->variables ...) */
1631 if (res != NULL)
1632 snmp_free_pdu (res);
1633 res = NULL;
1634 } /* while (status == 0) */
1636 if (res != NULL)
1637 snmp_free_pdu (res);
1638 res = NULL;
1640 if (req != NULL)
1641 snmp_free_pdu (req);
1642 req = NULL;
1644 if (status == 0)
1645 csnmp_dispatch_table (host, data, instance_list_head, value_list_head);
1647 /* Free all allocated variables here */
1648 while (instance_list_head != NULL)
1649 {
1650 csnmp_list_instances_t *next = instance_list_head->next;
1651 sfree (instance_list_head);
1652 instance_list_head = next;
1653 }
1655 for (i = 0; i < data->values_len; i++)
1656 {
1657 while (value_list_head[i] != NULL)
1658 {
1659 csnmp_table_values_t *next = value_list_head[i]->next;
1660 sfree (value_list_head[i]);
1661 value_list_head[i] = next;
1662 }
1663 }
1665 sfree (value_list_head);
1666 sfree (value_list_tail);
1668 return (0);
1669 } /* int csnmp_read_table */
1671 static int csnmp_read_value (host_definition_t *host, data_definition_t *data)
1672 {
1673 struct snmp_pdu *req;
1674 struct snmp_pdu *res = NULL;
1675 struct variable_list *vb;
1677 const data_set_t *ds;
1678 value_list_t vl = VALUE_LIST_INIT;
1680 int status;
1681 size_t i;
1683 DEBUG ("snmp plugin: csnmp_read_value (host = %s, data = %s)",
1684 host->name, data->name);
1686 if (host->sess_handle == NULL)
1687 {
1688 DEBUG ("snmp plugin: csnmp_read_value: host->sess_handle == NULL");
1689 return (-1);
1690 }
1692 ds = plugin_get_ds (data->type);
1693 if (!ds)
1694 {
1695 ERROR ("snmp plugin: DataSet `%s' not defined.", data->type);
1696 return (-1);
1697 }
1699 if (ds->ds_num != data->values_len)
1700 {
1701 ERROR ("snmp plugin: DataSet `%s' requires %zu values, but config talks about %zu",
1702 data->type, ds->ds_num, data->values_len);
1703 return (-1);
1704 }
1706 vl.values_len = ds->ds_num;
1707 vl.values = malloc (sizeof (*vl.values) * vl.values_len);
1708 if (vl.values == NULL)
1709 return (-1);
1710 for (i = 0; i < vl.values_len; i++)
1711 {
1712 if (ds->ds[i].type == DS_TYPE_COUNTER)
1713 vl.values[i].counter = 0;
1714 else
1715 vl.values[i].gauge = NAN;
1716 }
1718 sstrncpy (vl.host, host->name, sizeof (vl.host));
1719 sstrncpy (vl.plugin, "snmp", sizeof (vl.plugin));
1720 sstrncpy (vl.type, data->type, sizeof (vl.type));
1721 sstrncpy (vl.type_instance, data->instance.string, sizeof (vl.type_instance));
1723 vl.interval = host->interval;
1725 req = snmp_pdu_create (SNMP_MSG_GET);
1726 if (req == NULL)
1727 {
1728 ERROR ("snmp plugin: snmp_pdu_create failed.");
1729 sfree (vl.values);
1730 return (-1);
1731 }
1733 for (i = 0; i < data->values_len; i++)
1734 snmp_add_null_var (req, data->values[i].oid, data->values[i].oid_len);
1736 status = snmp_sess_synch_response (host->sess_handle, req, &res);
1738 if ((status != STAT_SUCCESS) || (res == NULL))
1739 {
1740 char *errstr = NULL;
1742 snmp_sess_error (host->sess_handle, NULL, NULL, &errstr);
1743 ERROR ("snmp plugin: host %s: snmp_sess_synch_response failed: %s",
1744 host->name, (errstr == NULL) ? "Unknown problem" : errstr);
1746 if (res != NULL)
1747 snmp_free_pdu (res);
1749 sfree (errstr);
1750 sfree (vl.values);
1751 csnmp_host_close_session (host);
1753 return (-1);
1754 }
1757 for (vb = res->variables; vb != NULL; vb = vb->next_variable)
1758 {
1759 #if COLLECT_DEBUG
1760 char buffer[1024];
1761 snprint_variable (buffer, sizeof (buffer),
1762 vb->name, vb->name_length, vb);
1763 DEBUG ("snmp plugin: Got this variable: %s", buffer);
1764 #endif /* COLLECT_DEBUG */
1766 for (i = 0; i < data->values_len; i++)
1767 if (snmp_oid_compare (data->values[i].oid, data->values[i].oid_len,
1768 vb->name, vb->name_length) == 0)
1769 vl.values[i] = csnmp_value_list_to_value (vb, ds->ds[i].type,
1770 data->scale, data->shift, host->name, data->name);
1771 } /* for (res->variables) */
1773 snmp_free_pdu (res);
1775 DEBUG ("snmp plugin: -> plugin_dispatch_values (&vl);");
1776 plugin_dispatch_values (&vl);
1777 sfree (vl.values);
1779 return (0);
1780 } /* int csnmp_read_value */
1782 static int csnmp_read_host (user_data_t *ud)
1783 {
1784 host_definition_t *host;
1785 int status;
1786 int success;
1787 int i;
1789 host = ud->data;
1791 if (host->interval == 0)
1792 host->interval = plugin_get_interval ();
1794 if (host->sess_handle == NULL)
1795 csnmp_host_open_session (host);
1797 if (host->sess_handle == NULL)
1798 return (-1);
1800 success = 0;
1801 for (i = 0; i < host->data_list_len; i++)
1802 {
1803 data_definition_t *data = host->data_list[i];
1805 if (data->is_table)
1806 status = csnmp_read_table (host, data);
1807 else
1808 status = csnmp_read_value (host, data);
1810 if (status == 0)
1811 success++;
1812 }
1814 if (success == 0)
1815 return (-1);
1817 return (0);
1818 } /* int csnmp_read_host */
1820 static int csnmp_init (void)
1821 {
1822 call_snmp_init_once ();
1824 return (0);
1825 } /* int csnmp_init */
1827 static int csnmp_shutdown (void)
1828 {
1829 data_definition_t *data_this;
1830 data_definition_t *data_next;
1832 /* When we get here, the read threads have been stopped and all the
1833 * `host_definition_t' will be freed. */
1834 DEBUG ("snmp plugin: Destroying all data definitions.");
1836 data_this = data_head;
1837 data_head = NULL;
1838 while (data_this != NULL)
1839 {
1840 data_next = data_this->next;
1842 sfree (data_this->name);
1843 sfree (data_this->type);
1844 sfree (data_this->values);
1845 sfree (data_this->ignores);
1846 sfree (data_this);
1848 data_this = data_next;
1849 }
1851 return (0);
1852 } /* int csnmp_shutdown */
1854 void module_register (void)
1855 {
1856 plugin_register_complex_config ("snmp", csnmp_config);
1857 plugin_register_init ("snmp", csnmp_init);
1858 plugin_register_shutdown ("snmp", csnmp_shutdown);
1859 } /* void module_register */
1861 /*
1862 * vim: shiftwidth=2 softtabstop=2 tabstop=8 fdm=marker
1863 */