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 <pthread.h>
34 #include <net-snmp/net-snmp-config.h>
35 #include <net-snmp/net-snmp-includes.h>
37 #include <fnmatch.h>
39 /*
40 * Private data structes
41 */
42 struct oid_s
43 {
44 oid oid[MAX_OID_LEN];
45 size_t oid_len;
46 };
47 typedef struct oid_s oid_t;
49 union instance_u
50 {
51 char string[DATA_MAX_NAME_LEN];
52 oid_t oid;
53 };
54 typedef union instance_u instance_t;
56 struct data_definition_s
57 {
58 char *name; /* used to reference this from the `Collect' option */
59 char *type; /* used to find the data_set */
60 _Bool is_table;
61 instance_t instance;
62 char *instance_prefix;
63 oid_t *values;
64 int values_len;
65 double scale;
66 double shift;
67 struct data_definition_s *next;
68 char **ignores;
69 size_t ignores_len;
70 int invert_match;
71 };
72 typedef struct data_definition_s data_definition_t;
74 struct host_definition_s
75 {
76 char *name;
77 char *address;
78 int version;
80 /* snmpv1/2 options */
81 char *community;
83 /* snmpv3 security options */
84 char *username;
85 oid *auth_protocol;
86 size_t auth_protocol_len;
87 char *auth_passphrase;
88 oid *priv_protocol;
89 size_t priv_protocol_len;
90 char *priv_passphrase;
91 int security_level;
92 char *context;
94 void *sess_handle;
95 c_complain_t complaint;
96 cdtime_t interval;
97 data_definition_t **data_list;
98 int data_list_len;
99 };
100 typedef struct host_definition_s host_definition_t;
102 /* These two types are used to cache values in `csnmp_read_table' to handle
103 * gaps in tables. */
104 struct csnmp_list_instances_s
105 {
106 oid_t suffix;
107 char instance[DATA_MAX_NAME_LEN];
108 struct csnmp_list_instances_s *next;
109 };
110 typedef struct csnmp_list_instances_s csnmp_list_instances_t;
112 struct csnmp_table_values_s
113 {
114 oid_t suffix;
115 value_t value;
116 struct csnmp_table_values_s *next;
117 };
118 typedef struct csnmp_table_values_s csnmp_table_values_t;
120 /*
121 * Private variables
122 */
123 static data_definition_t *data_head = NULL;
125 /*
126 * Prototypes
127 */
128 static int csnmp_read_host (user_data_t *ud);
130 /*
131 * Private functions
132 */
133 static void csnmp_oid_init (oid_t *dst, oid const *src, size_t n)
134 {
135 assert (n <= STATIC_ARRAY_SIZE (dst->oid));
136 memcpy (dst->oid, src, sizeof (*src) * n);
137 dst->oid_len = n;
138 }
140 static int csnmp_oid_compare (oid_t const *left, oid_t const *right)
141 {
142 return (snmp_oid_compare (left->oid, left->oid_len,
143 right->oid, right->oid_len));
144 }
146 static int csnmp_oid_suffix (oid_t *dst, oid_t const *src,
147 oid_t const *root)
148 {
149 /* Make sure "src" is in "root"s subtree. */
150 if (src->oid_len <= root->oid_len)
151 return (EINVAL);
152 if (snmp_oid_ncompare (root->oid, root->oid_len,
153 src->oid, src->oid_len,
154 /* n = */ root->oid_len) != 0)
155 return (EINVAL);
157 memset (dst, 0, sizeof (*dst));
158 dst->oid_len = src->oid_len - root->oid_len;
159 memcpy (dst->oid, &src->oid[root->oid_len],
160 dst->oid_len * sizeof (dst->oid[0]));
161 return (0);
162 }
164 static int csnmp_oid_to_string (char *buffer, size_t buffer_size,
165 oid_t const *o)
166 {
167 char oid_str[MAX_OID_LEN][16];
168 char *oid_str_ptr[MAX_OID_LEN];
169 size_t i;
171 for (i = 0; i < o->oid_len; i++)
172 {
173 ssnprintf (oid_str[i], sizeof (oid_str[i]), "%lu", (unsigned long) o->oid[i]);
174 oid_str_ptr[i] = oid_str[i];
175 }
177 return (strjoin (buffer, buffer_size,
178 oid_str_ptr, o->oid_len, /* separator = */ "."));
179 }
181 static void csnmp_host_close_session (host_definition_t *host) /* {{{ */
182 {
183 if (host->sess_handle == NULL)
184 return;
186 snmp_sess_close (host->sess_handle);
187 host->sess_handle = NULL;
188 } /* }}} void csnmp_host_close_session */
190 static void csnmp_host_definition_destroy (void *arg) /* {{{ */
191 {
192 host_definition_t *hd;
194 hd = arg;
196 if (hd == NULL)
197 return;
199 if (hd->name != NULL)
200 {
201 DEBUG ("snmp plugin: Destroying host definition for host `%s'.",
202 hd->name);
203 }
205 csnmp_host_close_session (hd);
207 sfree (hd->name);
208 sfree (hd->address);
209 sfree (hd->community);
210 sfree (hd->username);
211 sfree (hd->auth_passphrase);
212 sfree (hd->priv_passphrase);
213 sfree (hd->context);
214 sfree (hd->data_list);
216 sfree (hd);
217 } /* }}} void csnmp_host_definition_destroy */
219 /* Many functions to handle the configuration. {{{ */
220 /* First there are many functions which do configuration stuff. It's a big
221 * bloated and messy, I'm afraid. */
223 /*
224 * Callgraph for the config stuff:
225 * csnmp_config
226 * +-> call_snmp_init_once
227 * +-> csnmp_config_add_data
228 * ! +-> csnmp_config_add_data_instance
229 * ! +-> csnmp_config_add_data_instance_prefix
230 * ! +-> csnmp_config_add_data_values
231 * +-> csnmp_config_add_host
232 * +-> csnmp_config_add_host_version
233 * +-> csnmp_config_add_host_collect
234 * +-> csnmp_config_add_host_auth_protocol
235 * +-> csnmp_config_add_host_priv_protocol
236 * +-> csnmp_config_add_host_security_level
237 */
238 static void call_snmp_init_once (void)
239 {
240 static int have_init = 0;
242 if (have_init == 0)
243 init_snmp (PACKAGE_NAME);
244 have_init = 1;
245 } /* void call_snmp_init_once */
247 static int csnmp_config_add_data_instance (data_definition_t *dd, oconfig_item_t *ci)
248 {
249 char buffer[DATA_MAX_NAME_LEN];
250 int status;
252 status = cf_util_get_string_buffer(ci, buffer, sizeof(buffer));
253 if (status != 0)
254 return status;
256 if (dd->is_table)
257 {
258 /* Instance is an OID */
259 dd->instance.oid.oid_len = MAX_OID_LEN;
261 if (!read_objid (buffer,
262 dd->instance.oid.oid, &dd->instance.oid.oid_len))
263 {
264 ERROR ("snmp plugin: read_objid (%s) failed.", buffer);
265 return (-1);
266 }
267 }
268 else
269 {
270 /* Instance is a simple string */
271 sstrncpy (dd->instance.string, buffer,
272 sizeof (dd->instance.string));
273 }
275 return (0);
276 } /* int csnmp_config_add_data_instance */
278 static int csnmp_config_add_data_instance_prefix (data_definition_t *dd,
279 oconfig_item_t *ci)
280 {
281 int status;
283 if (!dd->is_table)
284 {
285 WARNING ("snmp plugin: data %s: InstancePrefix is ignored when `Table' "
286 "is set to `false'.", dd->name);
287 return (-1);
288 }
290 status = cf_util_get_string(ci, &dd->instance_prefix);
291 return status;
292 } /* int csnmp_config_add_data_instance_prefix */
294 static int csnmp_config_add_data_values (data_definition_t *dd, oconfig_item_t *ci)
295 {
296 int i;
298 if (ci->values_num < 1)
299 {
300 WARNING ("snmp plugin: `Values' needs at least one argument.");
301 return (-1);
302 }
304 for (i = 0; i < ci->values_num; i++)
305 if (ci->values[i].type != OCONFIG_TYPE_STRING)
306 {
307 WARNING ("snmp plugin: `Values' needs only string argument.");
308 return (-1);
309 }
311 sfree (dd->values);
312 dd->values_len = 0;
313 dd->values = (oid_t *) malloc (sizeof (oid_t) * ci->values_num);
314 if (dd->values == NULL)
315 return (-1);
316 dd->values_len = ci->values_num;
318 for (i = 0; i < ci->values_num; i++)
319 {
320 dd->values[i].oid_len = MAX_OID_LEN;
322 if (NULL == snmp_parse_oid (ci->values[i].value.string,
323 dd->values[i].oid, &dd->values[i].oid_len))
324 {
325 ERROR ("snmp plugin: snmp_parse_oid (%s) failed.",
326 ci->values[i].value.string);
327 free (dd->values);
328 dd->values = NULL;
329 dd->values_len = 0;
330 return (-1);
331 }
332 }
334 return (0);
335 } /* int csnmp_config_add_data_instance */
337 static int csnmp_config_add_data_blacklist(data_definition_t *dd, oconfig_item_t *ci)
338 {
339 int i;
341 if (ci->values_num < 1)
342 return (0);
344 for (i = 0; i < ci->values_num; i++)
345 {
346 if (ci->values[i].type != OCONFIG_TYPE_STRING)
347 {
348 WARNING ("snmp plugin: `Ignore' needs only string argument.");
349 return (-1);
350 }
351 }
353 dd->ignores_len = 0;
354 dd->ignores = NULL;
356 for (i = 0; i < ci->values_num; ++i)
357 {
358 if (strarray_add(&(dd->ignores), &(dd->ignores_len), ci->values[i].value.string) != 0)
359 {
360 ERROR("snmp plugin: Can't allocate memory");
361 strarray_free(dd->ignores, dd->ignores_len);
362 return (ENOMEM);
363 }
364 }
365 return 0;
366 } /* int csnmp_config_add_data_blacklist */
368 static int csnmp_config_add_data_blacklist_match_inverted(data_definition_t *dd, oconfig_item_t *ci)
369 {
370 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_BOOLEAN))
371 {
372 WARNING ("snmp plugin: `InvertMatch' needs exactly one boolean argument.");
373 return (-1);
374 }
376 dd->invert_match = ci->values[0].value.boolean ? 1 : 0;
378 return (0);
379 } /* int csnmp_config_add_data_blacklist_match_inverted */
381 static int csnmp_config_add_data (oconfig_item_t *ci)
382 {
383 data_definition_t *dd;
384 int status = 0;
385 int i;
387 dd = (data_definition_t *) malloc (sizeof (data_definition_t));
388 if (dd == NULL)
389 return (-1);
390 memset (dd, '\0', sizeof (data_definition_t));
392 status = cf_util_get_string(ci, &dd->name);
393 if (status != 0)
394 {
395 free (dd);
396 return (-1);
397 }
399 dd->scale = 1.0;
400 dd->shift = 0.0;
402 for (i = 0; i < ci->children_num; i++)
403 {
404 oconfig_item_t *option = ci->children + i;
405 status = 0;
407 if (strcasecmp ("Type", option->key) == 0)
408 status = cf_util_get_string(option, &dd->type);
409 else if (strcasecmp ("Table", option->key) == 0)
410 status = cf_util_get_boolean(option, &dd->is_table);
411 else if (strcasecmp ("Instance", option->key) == 0)
412 status = csnmp_config_add_data_instance (dd, option);
413 else if (strcasecmp ("InstancePrefix", option->key) == 0)
414 status = csnmp_config_add_data_instance_prefix (dd, option);
415 else if (strcasecmp ("Values", option->key) == 0)
416 status = csnmp_config_add_data_values (dd, option);
417 else if (strcasecmp ("Shift", option->key) == 0)
418 status = cf_util_get_double(option, &dd->shift);
419 else if (strcasecmp ("Scale", option->key) == 0)
420 status = cf_util_get_double(option, &dd->scale);
421 else if (strcasecmp ("Ignore", option->key) == 0)
422 status = csnmp_config_add_data_blacklist(dd, option);
423 else if (strcasecmp ("InvertMatch", option->key) == 0)
424 status = csnmp_config_add_data_blacklist_match_inverted(dd, option);
425 else
426 {
427 WARNING ("snmp plugin: Option `%s' not allowed here.", option->key);
428 status = -1;
429 }
431 if (status != 0)
432 break;
433 } /* for (ci->children) */
435 while (status == 0)
436 {
437 if (dd->type == NULL)
438 {
439 WARNING ("snmp plugin: `Type' not given for data `%s'", dd->name);
440 status = -1;
441 break;
442 }
443 if (dd->values == NULL)
444 {
445 WARNING ("snmp plugin: No `Value' given for data `%s'", dd->name);
446 status = -1;
447 break;
448 }
450 break;
451 } /* while (status == 0) */
453 if (status != 0)
454 {
455 sfree (dd->name);
456 sfree (dd->instance_prefix);
457 sfree (dd->values);
458 sfree (dd->ignores);
459 sfree (dd);
460 return (-1);
461 }
463 DEBUG ("snmp plugin: dd = { name = %s, type = %s, is_table = %s, values_len = %i }",
464 dd->name, dd->type, (dd->is_table != 0) ? "true" : "false", dd->values_len);
466 if (data_head == NULL)
467 data_head = dd;
468 else
469 {
470 data_definition_t *last;
471 last = data_head;
472 while (last->next != NULL)
473 last = last->next;
474 last->next = dd;
475 }
477 return (0);
478 } /* int csnmp_config_add_data */
480 static int csnmp_config_add_host_version (host_definition_t *hd, oconfig_item_t *ci)
481 {
482 int version;
484 if ((ci->values_num != 1)
485 || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
486 {
487 WARNING ("snmp plugin: The `Version' config option needs exactly one number argument.");
488 return (-1);
489 }
491 version = (int) ci->values[0].value.number;
492 if ((version < 1) || (version > 3))
493 {
494 WARNING ("snmp plugin: `Version' must either be `1', `2', or `3'.");
495 return (-1);
496 }
498 hd->version = version;
500 return (0);
501 } /* int csnmp_config_add_host_address */
503 static int csnmp_config_add_host_collect (host_definition_t *host,
504 oconfig_item_t *ci)
505 {
506 data_definition_t *data;
507 data_definition_t **data_list;
508 int data_list_len;
509 int i;
511 if (ci->values_num < 1)
512 {
513 WARNING ("snmp plugin: `Collect' needs at least one argument.");
514 return (-1);
515 }
517 for (i = 0; i < ci->values_num; i++)
518 if (ci->values[i].type != OCONFIG_TYPE_STRING)
519 {
520 WARNING ("snmp plugin: All arguments to `Collect' must be strings.");
521 return (-1);
522 }
524 data_list_len = host->data_list_len + ci->values_num;
525 data_list = (data_definition_t **) realloc (host->data_list,
526 sizeof (data_definition_t *) * data_list_len);
527 if (data_list == NULL)
528 return (-1);
529 host->data_list = data_list;
531 for (i = 0; i < ci->values_num; i++)
532 {
533 for (data = data_head; data != NULL; data = data->next)
534 if (strcasecmp (ci->values[i].value.string, data->name) == 0)
535 break;
537 if (data == NULL)
538 {
539 WARNING ("snmp plugin: No such data configured: `%s'",
540 ci->values[i].value.string);
541 continue;
542 }
544 DEBUG ("snmp plugin: Collect: host = %s, data[%i] = %s;",
545 host->name, host->data_list_len, data->name);
547 host->data_list[host->data_list_len] = data;
548 host->data_list_len++;
549 } /* for (values_num) */
551 return (0);
552 } /* int csnmp_config_add_host_collect */
554 static int csnmp_config_add_host_auth_protocol (host_definition_t *hd, oconfig_item_t *ci)
555 {
556 char buffer[4];
557 int status;
559 status = cf_util_get_string_buffer(ci, buffer, sizeof(buffer));
560 if (status != 0)
561 return status;
563 if (strcasecmp("MD5", buffer) == 0) {
564 hd->auth_protocol = usmHMACMD5AuthProtocol;
565 hd->auth_protocol_len = sizeof(usmHMACMD5AuthProtocol)/sizeof(oid);
566 }
567 else if (strcasecmp("SHA", buffer) == 0) {
568 hd->auth_protocol = usmHMACSHA1AuthProtocol;
569 hd->auth_protocol_len = sizeof(usmHMACSHA1AuthProtocol)/sizeof(oid);
570 }
571 else
572 {
573 WARNING ("snmp plugin: The `AuthProtocol' config option must be `MD5' or `SHA'.");
574 return (-1);
575 }
577 DEBUG ("snmp plugin: host = %s; host->auth_protocol = %s;",
578 hd->name, hd->auth_protocol == usmHMACMD5AuthProtocol ? "MD5" : "SHA");
580 return (0);
581 } /* int csnmp_config_add_host_auth_protocol */
583 static int csnmp_config_add_host_priv_protocol (host_definition_t *hd, oconfig_item_t *ci)
584 {
585 char buffer[4];
586 int status;
588 status = cf_util_get_string_buffer(ci, buffer, sizeof(buffer));
589 if (status != 0)
590 return status;
592 if (strcasecmp("AES", buffer) == 0)
593 {
594 hd->priv_protocol = usmAESPrivProtocol;
595 hd->priv_protocol_len = sizeof(usmAESPrivProtocol)/sizeof(oid);
596 }
597 else if (strcasecmp("DES", buffer) == 0) {
598 hd->priv_protocol = usmDESPrivProtocol;
599 hd->priv_protocol_len = sizeof(usmDESPrivProtocol)/sizeof(oid);
600 }
601 else
602 {
603 WARNING ("snmp plugin: The `PrivProtocol' config option must be `AES' or `DES'.");
604 return (-1);
605 }
607 DEBUG ("snmp plugin: host = %s; host->priv_protocol = %s;",
608 hd->name, hd->priv_protocol == usmAESPrivProtocol ? "AES" : "DES");
610 return (0);
611 } /* int csnmp_config_add_host_priv_protocol */
613 static int csnmp_config_add_host_security_level (host_definition_t *hd, oconfig_item_t *ci)
614 {
615 char buffer[16];
616 int status;
618 status = cf_util_get_string_buffer(ci, buffer, sizeof(buffer));
619 if (status != 0)
620 return status;
622 if (strcasecmp("noAuthNoPriv", buffer) == 0)
623 hd->security_level = SNMP_SEC_LEVEL_NOAUTH;
624 else if (strcasecmp("authNoPriv", buffer) == 0)
625 hd->security_level = SNMP_SEC_LEVEL_AUTHNOPRIV;
626 else if (strcasecmp("authPriv", buffer) == 0)
627 hd->security_level = SNMP_SEC_LEVEL_AUTHPRIV;
628 else
629 {
630 WARNING ("snmp plugin: The `SecurityLevel' config option must be `noAuthNoPriv', `authNoPriv', or `authPriv'.");
631 return (-1);
632 }
634 DEBUG ("snmp plugin: host = %s; host->security_level = %d;",
635 hd->name, hd->security_level);
637 return (0);
638 } /* int csnmp_config_add_host_security_level */
640 static int csnmp_config_add_host (oconfig_item_t *ci)
641 {
642 host_definition_t *hd;
643 int status = 0;
644 int i;
646 /* Registration stuff. */
647 char cb_name[DATA_MAX_NAME_LEN];
648 user_data_t cb_data;
649 struct timespec cb_interval;
651 hd = (host_definition_t *) malloc (sizeof (host_definition_t));
652 if (hd == NULL)
653 return (-1);
654 memset (hd, '\0', sizeof (host_definition_t));
655 hd->version = 2;
656 C_COMPLAIN_INIT (&hd->complaint);
658 status = cf_util_get_string(ci, &hd->name);
659 if (status != 0)
660 return status;
662 hd->sess_handle = NULL;
663 hd->interval = 0;
665 for (i = 0; i < ci->children_num; i++)
666 {
667 oconfig_item_t *option = ci->children + i;
668 status = 0;
670 if (strcasecmp ("Address", option->key) == 0)
671 status = cf_util_get_string(option, &hd->address);
672 else if (strcasecmp ("Community", option->key) == 0)
673 status = cf_util_get_string(option, &hd->community);
674 else if (strcasecmp ("Version", option->key) == 0)
675 status = csnmp_config_add_host_version (hd, option);
676 else if (strcasecmp ("Collect", option->key) == 0)
677 csnmp_config_add_host_collect (hd, option);
678 else if (strcasecmp ("Interval", option->key) == 0)
679 cf_util_get_cdtime (option, &hd->interval);
680 else if (strcasecmp ("Username", option->key) == 0)
681 status = cf_util_get_string(option, &hd->username);
682 else if (strcasecmp ("AuthProtocol", option->key) == 0)
683 status = csnmp_config_add_host_auth_protocol (hd, option);
684 else if (strcasecmp ("PrivacyProtocol", option->key) == 0)
685 status = csnmp_config_add_host_priv_protocol (hd, option);
686 else if (strcasecmp ("AuthPassphrase", option->key) == 0)
687 status = cf_util_get_string(option, &hd->auth_passphrase);
688 else if (strcasecmp ("PrivacyPassphrase", option->key) == 0)
689 status = cf_util_get_string(option, &hd->priv_passphrase);
690 else if (strcasecmp ("SecurityLevel", option->key) == 0)
691 status = csnmp_config_add_host_security_level (hd, option);
692 else if (strcasecmp ("Context", option->key) == 0)
693 status = cf_util_get_string(option, &hd->context);
694 else
695 {
696 WARNING ("snmp plugin: csnmp_config_add_host: Option `%s' not allowed here.", option->key);
697 status = -1;
698 }
700 if (status != 0)
701 break;
702 } /* for (ci->children) */
704 while (status == 0)
705 {
706 if (hd->address == NULL)
707 {
708 WARNING ("snmp plugin: `Address' not given for host `%s'", hd->name);
709 status = -1;
710 break;
711 }
712 if (hd->community == NULL && hd->version < 3)
713 {
714 WARNING ("snmp plugin: `Community' not given for host `%s'", hd->name);
715 status = -1;
716 break;
717 }
718 if (hd->version == 3)
719 {
720 if (hd->username == NULL)
721 {
722 WARNING ("snmp plugin: `Username' not given for host `%s'", hd->name);
723 status = -1;
724 break;
725 }
726 if (hd->security_level == 0)
727 {
728 WARNING ("snmp plugin: `SecurityLevel' not given for host `%s'", hd->name);
729 status = -1;
730 break;
731 }
732 if (hd->security_level == SNMP_SEC_LEVEL_AUTHNOPRIV || hd->security_level == SNMP_SEC_LEVEL_AUTHPRIV)
733 {
734 if (hd->auth_protocol == NULL)
735 {
736 WARNING ("snmp plugin: `AuthProtocol' not given for host `%s'", hd->name);
737 status = -1;
738 break;
739 }
740 if (hd->auth_passphrase == NULL)
741 {
742 WARNING ("snmp plugin: `AuthPassphrase' not given for host `%s'", hd->name);
743 status = -1;
744 break;
745 }
746 }
747 if (hd->security_level == SNMP_SEC_LEVEL_AUTHPRIV)
748 {
749 if (hd->priv_protocol == NULL)
750 {
751 WARNING ("snmp plugin: `PrivacyProtocol' not given for host `%s'", hd->name);
752 status = -1;
753 break;
754 }
755 if (hd->priv_passphrase == NULL)
756 {
757 WARNING ("snmp plugin: `PrivacyPassphrase' not given for host `%s'", hd->name);
758 status = -1;
759 break;
760 }
761 }
762 }
764 break;
765 } /* while (status == 0) */
767 if (status != 0)
768 {
769 csnmp_host_definition_destroy (hd);
770 return (-1);
771 }
773 DEBUG ("snmp plugin: hd = { name = %s, address = %s, community = %s, version = %i }",
774 hd->name, hd->address, hd->community, hd->version);
776 ssnprintf (cb_name, sizeof (cb_name), "snmp-%s", hd->name);
778 memset (&cb_data, 0, sizeof (cb_data));
779 cb_data.data = hd;
780 cb_data.free_func = csnmp_host_definition_destroy;
782 CDTIME_T_TO_TIMESPEC (hd->interval, &cb_interval);
784 status = plugin_register_complex_read (/* group = */ NULL, cb_name,
785 csnmp_read_host, /* interval = */ &cb_interval,
786 /* user_data = */ &cb_data);
787 if (status != 0)
788 {
789 ERROR ("snmp plugin: Registering complex read function failed.");
790 csnmp_host_definition_destroy (hd);
791 return (-1);
792 }
794 return (0);
795 } /* int csnmp_config_add_host */
797 static int csnmp_config (oconfig_item_t *ci)
798 {
799 int i;
801 call_snmp_init_once ();
803 for (i = 0; i < ci->children_num; i++)
804 {
805 oconfig_item_t *child = ci->children + i;
806 if (strcasecmp ("Data", child->key) == 0)
807 csnmp_config_add_data (child);
808 else if (strcasecmp ("Host", child->key) == 0)
809 csnmp_config_add_host (child);
810 else
811 {
812 WARNING ("snmp plugin: Ignoring unknown config option `%s'.", child->key);
813 }
814 } /* for (ci->children) */
816 return (0);
817 } /* int csnmp_config */
819 /* }}} End of the config stuff. Now the interesting part begins */
821 static void csnmp_host_open_session (host_definition_t *host)
822 {
823 struct snmp_session sess;
824 int error;
826 if (host->sess_handle != NULL)
827 csnmp_host_close_session (host);
829 snmp_sess_init (&sess);
830 sess.peername = host->address;
831 switch (host->version)
832 {
833 case 1:
834 sess.version = SNMP_VERSION_1;
835 break;
836 case 3:
837 sess.version = SNMP_VERSION_3;
838 break;
839 default:
840 sess.version = SNMP_VERSION_2c;
841 break;
842 }
844 if (host->version == 3)
845 {
846 sess.securityName = host->username;
847 sess.securityNameLen = strlen (host->username);
848 sess.securityLevel = host->security_level;
850 if (sess.securityLevel == SNMP_SEC_LEVEL_AUTHNOPRIV || sess.securityLevel == SNMP_SEC_LEVEL_AUTHPRIV)
851 {
852 sess.securityAuthProto = host->auth_protocol;
853 sess.securityAuthProtoLen = host->auth_protocol_len;
854 sess.securityAuthKeyLen = USM_AUTH_KU_LEN;
855 error = generate_Ku (sess.securityAuthProto,
856 sess.securityAuthProtoLen,
857 (u_char *) host->auth_passphrase,
858 strlen(host->auth_passphrase),
859 sess.securityAuthKey,
860 &sess.securityAuthKeyLen);
861 if (error != SNMPERR_SUCCESS) {
862 ERROR ("snmp plugin: host %s: Error generating Ku from auth_passphrase. (Error %d)", host->name, error);
863 }
864 }
866 if (sess.securityLevel == SNMP_SEC_LEVEL_AUTHPRIV)
867 {
868 sess.securityPrivProto = host->priv_protocol;
869 sess.securityPrivProtoLen = host->priv_protocol_len;
870 sess.securityPrivKeyLen = USM_PRIV_KU_LEN;
871 error = generate_Ku (sess.securityAuthProto,
872 sess.securityAuthProtoLen,
873 (u_char *) host->priv_passphrase,
874 strlen(host->priv_passphrase),
875 sess.securityPrivKey,
876 &sess.securityPrivKeyLen);
877 if (error != SNMPERR_SUCCESS) {
878 ERROR ("snmp plugin: host %s: Error generating Ku from priv_passphrase. (Error %d)", host->name, error);
879 }
880 }
882 if (host->context != NULL)
883 {
884 sess.contextName = host->context;
885 sess.contextNameLen = strlen (host->context);
886 }
887 }
888 else /* SNMPv1/2 "authenticates" with community string */
889 {
890 sess.community = (u_char *) host->community;
891 sess.community_len = strlen (host->community);
892 }
894 /* snmp_sess_open will copy the `struct snmp_session *'. */
895 host->sess_handle = snmp_sess_open (&sess);
897 if (host->sess_handle == NULL)
898 {
899 char *errstr = NULL;
901 snmp_error (&sess, NULL, NULL, &errstr);
903 ERROR ("snmp plugin: host %s: snmp_sess_open failed: %s",
904 host->name, (errstr == NULL) ? "Unknown problem" : errstr);
905 sfree (errstr);
906 }
907 } /* void csnmp_host_open_session */
909 /* TODO: Check if negative values wrap around. Problem: negative temperatures. */
910 static value_t csnmp_value_list_to_value (struct variable_list *vl, int type,
911 double scale, double shift,
912 const char *host_name, const char *data_name)
913 {
914 value_t ret;
915 uint64_t tmp_unsigned = 0;
916 int64_t tmp_signed = 0;
917 _Bool defined = 1;
918 /* Set to true when the original SNMP type appears to have been signed. */
919 _Bool prefer_signed = 0;
921 if ((vl->type == ASN_INTEGER)
922 || (vl->type == ASN_UINTEGER)
923 || (vl->type == ASN_COUNTER)
924 #ifdef ASN_TIMETICKS
925 || (vl->type == ASN_TIMETICKS)
926 #endif
927 || (vl->type == ASN_GAUGE))
928 {
929 tmp_unsigned = (uint32_t) *vl->val.integer;
930 tmp_signed = (int32_t) *vl->val.integer;
932 if ((vl->type == ASN_INTEGER)
933 || (vl->type == ASN_GAUGE))
934 prefer_signed = 1;
936 DEBUG ("snmp plugin: Parsed int32 value is %"PRIu64".", tmp_unsigned);
937 }
938 else if (vl->type == ASN_COUNTER64)
939 {
940 tmp_unsigned = (uint32_t) vl->val.counter64->high;
941 tmp_unsigned = tmp_unsigned << 32;
942 tmp_unsigned += (uint32_t) vl->val.counter64->low;
943 tmp_signed = (int64_t) tmp_unsigned;
944 DEBUG ("snmp plugin: Parsed int64 value is %"PRIu64".", tmp_unsigned);
945 }
946 else if (vl->type == ASN_OCTET_STR)
947 {
948 /* We'll handle this later.. */
949 }
950 else
951 {
952 char oid_buffer[1024];
954 memset (oid_buffer, 0, sizeof (oid_buffer));
955 snprint_objid (oid_buffer, sizeof (oid_buffer) - 1,
956 vl->name, vl->name_length);
958 #ifdef ASN_NULL
959 if (vl->type == ASN_NULL)
960 INFO ("snmp plugin: OID \"%s\" is undefined (type ASN_NULL)",
961 oid_buffer);
962 else
963 #endif
964 WARNING ("snmp plugin: I don't know the ASN type #%i "
965 "(OID: \"%s\", data block \"%s\", host block \"%s\")",
966 (int) vl->type, oid_buffer,
967 (data_name != NULL) ? data_name : "UNKNOWN",
968 (host_name != NULL) ? host_name : "UNKNOWN");
970 defined = 0;
971 }
973 if (vl->type == ASN_OCTET_STR)
974 {
975 int status = -1;
977 if (vl->val.string != NULL)
978 {
979 char string[64];
980 size_t string_length;
982 string_length = sizeof (string) - 1;
983 if (vl->val_len < string_length)
984 string_length = vl->val_len;
986 /* The strings we get from the Net-SNMP library may not be null
987 * terminated. That is why we're using `memcpy' here and not `strcpy'.
988 * `string_length' is set to `vl->val_len' which holds the length of the
989 * string. -octo */
990 memcpy (string, vl->val.string, string_length);
991 string[string_length] = 0;
993 status = parse_value (string, &ret, type);
994 if (status != 0)
995 {
996 ERROR ("snmp plugin: host %s: csnmp_value_list_to_value: Parsing string as %s failed: %s",
997 (host_name != NULL) ? host_name : "UNKNOWN",
998 DS_TYPE_TO_STRING (type), string);
999 }
1000 }
1002 if (status != 0)
1003 {
1004 switch (type)
1005 {
1006 case DS_TYPE_COUNTER:
1007 case DS_TYPE_DERIVE:
1008 case DS_TYPE_ABSOLUTE:
1009 memset (&ret, 0, sizeof (ret));
1010 break;
1012 case DS_TYPE_GAUGE:
1013 ret.gauge = NAN;
1014 break;
1016 default:
1017 ERROR ("snmp plugin: csnmp_value_list_to_value: Unknown "
1018 "data source type: %i.", type);
1019 ret.gauge = NAN;
1020 }
1021 }
1022 } /* if (vl->type == ASN_OCTET_STR) */
1023 else if (type == DS_TYPE_COUNTER)
1024 {
1025 ret.counter = tmp_unsigned;
1026 }
1027 else if (type == DS_TYPE_GAUGE)
1028 {
1029 if (!defined)
1030 ret.gauge = NAN;
1031 else if (prefer_signed)
1032 ret.gauge = (scale * tmp_signed) + shift;
1033 else
1034 ret.gauge = (scale * tmp_unsigned) + shift;
1035 }
1036 else if (type == DS_TYPE_DERIVE)
1037 {
1038 if (prefer_signed)
1039 ret.derive = (derive_t) tmp_signed;
1040 else
1041 ret.derive = (derive_t) tmp_unsigned;
1042 }
1043 else if (type == DS_TYPE_ABSOLUTE)
1044 {
1045 ret.absolute = (absolute_t) tmp_unsigned;
1046 }
1047 else
1048 {
1049 ERROR ("snmp plugin: csnmp_value_list_to_value: Unknown data source "
1050 "type: %i.", type);
1051 ret.gauge = NAN;
1052 }
1054 return (ret);
1055 } /* value_t csnmp_value_list_to_value */
1057 static int csnmp_strvbcopy_hexstring (char *dst, /* {{{ */
1058 const struct variable_list *vb, size_t dst_size)
1059 {
1060 char *buffer_ptr;
1061 size_t buffer_free;
1062 size_t i;
1064 buffer_ptr = dst;
1065 buffer_free = dst_size;
1067 for (i = 0; i < vb->val_len; i++)
1068 {
1069 int status;
1071 status = snprintf (buffer_ptr, buffer_free,
1072 (i == 0) ? "%02x" : ":%02x", (unsigned int) vb->val.bitstring[i]);
1074 if (status >= buffer_free)
1075 {
1076 buffer_ptr += (buffer_free - 1);
1077 *buffer_ptr = 0;
1078 return (dst_size + (buffer_free - status));
1079 }
1080 else /* if (status < buffer_free) */
1081 {
1082 buffer_ptr += status;
1083 buffer_free -= status;
1084 }
1085 }
1087 return ((int) (dst_size - buffer_free));
1088 } /* }}} int csnmp_strvbcopy_hexstring */
1090 static int csnmp_strvbcopy (char *dst, /* {{{ */
1091 const struct variable_list *vb, size_t dst_size)
1092 {
1093 char *src;
1094 size_t num_chars;
1095 size_t i;
1097 if (vb->type == ASN_OCTET_STR)
1098 src = (char *) vb->val.string;
1099 else if (vb->type == ASN_BIT_STR)
1100 src = (char *) vb->val.bitstring;
1101 else
1102 {
1103 dst[0] = 0;
1104 return (EINVAL);
1105 }
1107 num_chars = dst_size - 1;
1108 if (num_chars > vb->val_len)
1109 num_chars = vb->val_len;
1111 for (i = 0; i < num_chars; i++)
1112 {
1113 /* Check for control characters. */
1114 if ((unsigned char)src[i] < 32)
1115 return (csnmp_strvbcopy_hexstring (dst, vb, dst_size));
1116 dst[i] = src[i];
1117 }
1118 dst[num_chars] = 0;
1120 return ((int) vb->val_len);
1121 } /* }}} int csnmp_strvbcopy */
1123 static int csnmp_instance_list_add (csnmp_list_instances_t **head,
1124 csnmp_list_instances_t **tail,
1125 const struct snmp_pdu *res,
1126 const host_definition_t *hd, const data_definition_t *dd)
1127 {
1128 csnmp_list_instances_t *il;
1129 struct variable_list *vb;
1130 oid_t vb_name;
1131 int status;
1132 uint32_t i;
1133 uint32_t is_matched;
1135 /* Set vb on the last variable */
1136 for (vb = res->variables;
1137 (vb != NULL) && (vb->next_variable != NULL);
1138 vb = vb->next_variable)
1139 /* do nothing */;
1140 if (vb == NULL)
1141 return (-1);
1143 csnmp_oid_init (&vb_name, vb->name, vb->name_length);
1145 il = malloc (sizeof (*il));
1146 if (il == NULL)
1147 {
1148 ERROR ("snmp plugin: malloc failed.");
1149 return (-1);
1150 }
1151 memset (il, 0, sizeof (*il));
1152 il->next = NULL;
1154 status = csnmp_oid_suffix (&il->suffix, &vb_name, &dd->instance.oid);
1155 if (status != 0)
1156 {
1157 sfree (il);
1158 return (status);
1159 }
1161 /* Get instance name */
1162 if ((vb->type == ASN_OCTET_STR) || (vb->type == ASN_BIT_STR))
1163 {
1164 char *ptr;
1166 csnmp_strvbcopy (il->instance, vb, sizeof (il->instance));
1167 is_matched = 0;
1168 for (i = 0; i < dd->ignores_len; i++)
1169 {
1170 status = fnmatch(dd->ignores[i], il->instance, 0);
1171 if (status == 0)
1172 {
1173 if (dd->invert_match == 0)
1174 {
1175 sfree(il);
1176 return 0;
1177 }
1178 else
1179 {
1180 is_matched = 1;
1181 break;
1182 }
1183 }
1184 }
1185 if (dd->invert_match != 0 && is_matched == 0)
1186 {
1187 sfree(il);
1188 return 0;
1189 }
1190 for (ptr = il->instance; *ptr != '\0'; ptr++)
1191 {
1192 if ((*ptr > 0) && (*ptr < 32))
1193 *ptr = ' ';
1194 else if (*ptr == '/')
1195 *ptr = '_';
1196 }
1197 DEBUG ("snmp plugin: il->instance = `%s';", il->instance);
1198 }
1199 else
1200 {
1201 value_t val = csnmp_value_list_to_value (vb, DS_TYPE_COUNTER,
1202 /* scale = */ 1.0, /* shift = */ 0.0, hd->name, dd->name);
1203 ssnprintf (il->instance, sizeof (il->instance),
1204 "%llu", val.counter);
1205 }
1207 /* TODO: Debugging output */
1209 if (*head == NULL)
1210 *head = il;
1211 else
1212 (*tail)->next = il;
1213 *tail = il;
1215 return (0);
1216 } /* int csnmp_instance_list_add */
1218 static int csnmp_dispatch_table (host_definition_t *host, data_definition_t *data,
1219 csnmp_list_instances_t *instance_list,
1220 csnmp_table_values_t **value_table)
1221 {
1222 const data_set_t *ds;
1223 value_list_t vl = VALUE_LIST_INIT;
1225 csnmp_list_instances_t *instance_list_ptr;
1226 csnmp_table_values_t **value_table_ptr;
1228 int i;
1229 _Bool have_more;
1230 oid_t current_suffix;
1232 ds = plugin_get_ds (data->type);
1233 if (!ds)
1234 {
1235 ERROR ("snmp plugin: DataSet `%s' not defined.", data->type);
1236 return (-1);
1237 }
1238 assert (ds->ds_num == data->values_len);
1240 instance_list_ptr = instance_list;
1242 value_table_ptr = malloc (sizeof (*value_table_ptr) * data->values_len);
1243 if (value_table_ptr == NULL)
1244 return (-1);
1245 for (i = 0; i < data->values_len; i++)
1246 value_table_ptr[i] = value_table[i];
1248 vl.values_len = ds->ds_num;
1249 vl.values = malloc (sizeof (*vl.values) * vl.values_len);
1250 if (vl.values == NULL)
1251 {
1252 ERROR ("snmp plugin: malloc failed.");
1253 sfree (value_table_ptr);
1254 return (-1);
1255 }
1257 sstrncpy (vl.host, host->name, sizeof (vl.host));
1258 sstrncpy (vl.plugin, "snmp", sizeof (vl.plugin));
1260 vl.interval = host->interval;
1262 have_more = 1;
1263 memset (¤t_suffix, 0, sizeof (current_suffix));
1264 while (have_more)
1265 {
1266 _Bool suffix_skipped = 0;
1268 /* Determine next suffix to handle. */
1269 if (instance_list != NULL)
1270 {
1271 if (instance_list_ptr == NULL)
1272 {
1273 have_more = 0;
1274 continue;
1275 }
1277 memcpy (¤t_suffix, &instance_list_ptr->suffix, sizeof (current_suffix));
1278 }
1279 else /* no instance configured */
1280 {
1281 csnmp_table_values_t *ptr = value_table_ptr[0];
1282 if (ptr == NULL)
1283 {
1284 have_more = 0;
1285 continue;
1286 }
1288 memcpy (¤t_suffix, &ptr->suffix, sizeof (current_suffix));
1289 }
1291 /* Update all the value_table_ptr to point at the entry with the same
1292 * trailing partial OID */
1293 for (i = 0; i < data->values_len; i++)
1294 {
1295 while ((value_table_ptr[i] != NULL)
1296 && (csnmp_oid_compare (&value_table_ptr[i]->suffix, ¤t_suffix) < 0))
1297 value_table_ptr[i] = value_table_ptr[i]->next;
1299 if (value_table_ptr[i] == NULL)
1300 {
1301 have_more = 0;
1302 break;
1303 }
1304 else if (csnmp_oid_compare (&value_table_ptr[i]->suffix, ¤t_suffix) > 0)
1305 {
1306 /* This suffix is missing in the subtree. Indicate this with the
1307 * "suffix_skipped" flag and try the next instance / suffix. */
1308 suffix_skipped = 1;
1309 break;
1310 }
1311 } /* for (i = 0; i < columns; i++) */
1313 if (!have_more)
1314 break;
1316 /* Matching the values failed. Start from the beginning again. */
1317 if (suffix_skipped)
1318 {
1319 if (instance_list != NULL)
1320 instance_list_ptr = instance_list_ptr->next;
1321 else
1322 value_table_ptr[0] = value_table_ptr[0]->next;
1324 continue;
1325 }
1327 /* if we reach this line, all value_table_ptr[i] are non-NULL and are set
1328 * to the same subid. instance_list_ptr is either NULL or points to the
1329 * same subid, too. */
1330 #if COLLECT_DEBUG
1331 for (i = 1; i < data->values_len; i++)
1332 {
1333 assert (value_table_ptr[i] != NULL);
1334 assert (csnmp_oid_compare (&value_table_ptr[i-1]->suffix,
1335 &value_table_ptr[i]->suffix) == 0);
1336 }
1337 assert ((instance_list_ptr == NULL)
1338 || (csnmp_oid_compare (&instance_list_ptr->suffix,
1339 &value_table_ptr[0]->suffix) == 0));
1340 #endif
1342 sstrncpy (vl.type, data->type, sizeof (vl.type));
1344 {
1345 char temp[DATA_MAX_NAME_LEN];
1347 if (instance_list_ptr == NULL)
1348 csnmp_oid_to_string (temp, sizeof (temp), ¤t_suffix);
1349 else
1350 sstrncpy (temp, instance_list_ptr->instance, sizeof (temp));
1352 if (data->instance_prefix == NULL)
1353 sstrncpy (vl.type_instance, temp, sizeof (vl.type_instance));
1354 else
1355 ssnprintf (vl.type_instance, sizeof (vl.type_instance), "%s%s",
1356 data->instance_prefix, temp);
1357 }
1359 for (i = 0; i < data->values_len; i++)
1360 vl.values[i] = value_table_ptr[i]->value;
1362 /* If we get here `vl.type_instance' and all `vl.values' have been set */
1363 plugin_dispatch_values (&vl);
1365 if (instance_list != NULL)
1366 instance_list_ptr = instance_list_ptr->next;
1367 else
1368 value_table_ptr[0] = value_table_ptr[0]->next;
1369 } /* while (have_more) */
1371 sfree (vl.values);
1372 sfree (value_table_ptr);
1374 return (0);
1375 } /* int csnmp_dispatch_table */
1377 static int csnmp_read_table (host_definition_t *host, data_definition_t *data)
1378 {
1379 struct snmp_pdu *req;
1380 struct snmp_pdu *res;
1381 struct variable_list *vb;
1383 const data_set_t *ds;
1385 uint32_t oid_list_len = (uint32_t) (data->values_len + 1);
1386 /* Holds the last OID returned by the device. We use this in the GETNEXT
1387 * request to proceed. */
1388 oid_t oid_list[oid_list_len];
1389 /* Set to false when an OID has left its subtree so we don't re-request it
1390 * again. */
1391 _Bool oid_list_todo[oid_list_len];
1393 int status;
1394 int i;
1395 uint32_t j;
1397 /* `value_list_head' and `value_list_tail' implement a linked list for each
1398 * value. `instance_list_head' and `instance_list_tail' implement a linked list of
1399 * instance names. This is used to jump gaps in the table. */
1400 csnmp_list_instances_t *instance_list_head;
1401 csnmp_list_instances_t *instance_list_tail;
1402 csnmp_table_values_t **value_list_head;
1403 csnmp_table_values_t **value_list_tail;
1405 DEBUG ("snmp plugin: csnmp_read_table (host = %s, data = %s)",
1406 host->name, data->name);
1408 if (host->sess_handle == NULL)
1409 {
1410 DEBUG ("snmp plugin: csnmp_read_table: host->sess_handle == NULL");
1411 return (-1);
1412 }
1414 ds = plugin_get_ds (data->type);
1415 if (!ds)
1416 {
1417 ERROR ("snmp plugin: DataSet `%s' not defined.", data->type);
1418 return (-1);
1419 }
1421 if (ds->ds_num != data->values_len)
1422 {
1423 ERROR ("snmp plugin: DataSet `%s' requires %i values, but config talks about %i",
1424 data->type, ds->ds_num, data->values_len);
1425 return (-1);
1426 }
1428 /* We need a copy of all the OIDs, because GETNEXT will destroy them. */
1429 memcpy (oid_list, data->values, data->values_len * sizeof (oid_t));
1430 if (data->instance.oid.oid_len > 0)
1431 memcpy (oid_list + data->values_len, &data->instance.oid, sizeof (oid_t));
1432 else /* no InstanceFrom option specified. */
1433 oid_list_len--;
1435 for (j = 0; j < oid_list_len; j++)
1436 oid_list_todo[j] = 1;
1438 /* We're going to construct n linked lists, one for each "value".
1439 * value_list_head will contain pointers to the heads of these linked lists,
1440 * value_list_tail will contain pointers to the tail of the lists. */
1441 value_list_head = calloc (data->values_len, sizeof (*value_list_head));
1442 value_list_tail = calloc (data->values_len, sizeof (*value_list_tail));
1443 if ((value_list_head == NULL) || (value_list_tail == NULL))
1444 {
1445 ERROR ("snmp plugin: csnmp_read_table: calloc failed.");
1446 sfree (value_list_head);
1447 sfree (value_list_tail);
1448 return (-1);
1449 }
1451 instance_list_head = NULL;
1452 instance_list_tail = NULL;
1454 status = 0;
1455 while (status == 0)
1456 {
1457 int oid_list_todo_num;
1459 req = snmp_pdu_create (SNMP_MSG_GETNEXT);
1460 if (req == NULL)
1461 {
1462 ERROR ("snmp plugin: snmp_pdu_create failed.");
1463 status = -1;
1464 break;
1465 }
1467 oid_list_todo_num = 0;
1468 for (j = 0; j < oid_list_len; j++)
1469 {
1470 /* Do not rerequest already finished OIDs */
1471 if (!oid_list_todo[j])
1472 continue;
1473 oid_list_todo_num++;
1474 snmp_add_null_var (req, oid_list[j].oid, oid_list[j].oid_len);
1475 }
1477 if (oid_list_todo_num == 0)
1478 {
1479 /* The request is still empty - so we are finished */
1480 DEBUG ("snmp plugin: all variables have left their subtree");
1481 status = 0;
1482 break;
1483 }
1485 res = NULL;
1486 status = snmp_sess_synch_response (host->sess_handle, req, &res);
1487 if ((status != STAT_SUCCESS) || (res == NULL))
1488 {
1489 char *errstr = NULL;
1491 snmp_sess_error (host->sess_handle, NULL, NULL, &errstr);
1493 c_complain (LOG_ERR, &host->complaint,
1494 "snmp plugin: host %s: snmp_sess_synch_response failed: %s",
1495 host->name, (errstr == NULL) ? "Unknown problem" : errstr);
1497 if (res != NULL)
1498 snmp_free_pdu (res);
1499 res = NULL;
1501 /* snmp_synch_response already freed our PDU */
1502 req = NULL;
1503 sfree (errstr);
1504 csnmp_host_close_session (host);
1506 status = -1;
1507 break;
1508 }
1510 status = 0;
1511 assert (res != NULL);
1512 c_release (LOG_INFO, &host->complaint,
1513 "snmp plugin: host %s: snmp_sess_synch_response successful.",
1514 host->name);
1516 vb = res->variables;
1517 if (vb == NULL)
1518 {
1519 status = -1;
1520 break;
1521 }
1523 for (vb = res->variables, i = 0; (vb != NULL); vb = vb->next_variable, i++)
1524 {
1525 /* Calculate value index from todo list */
1526 while (!oid_list_todo[i] && (i < oid_list_len))
1527 i++;
1529 /* An instance is configured and the res variable we process is the
1530 * instance value (last index) */
1531 if ((data->instance.oid.oid_len > 0) && (i == data->values_len))
1532 {
1533 if ((vb->type == SNMP_ENDOFMIBVIEW)
1534 || (snmp_oid_ncompare (data->instance.oid.oid,
1535 data->instance.oid.oid_len,
1536 vb->name, vb->name_length,
1537 data->instance.oid.oid_len) != 0))
1538 {
1539 DEBUG ("snmp plugin: host = %s; data = %s; Instance left its subtree.",
1540 host->name, data->name);
1541 oid_list_todo[i] = 0;
1542 continue;
1543 }
1545 /* Allocate a new `csnmp_list_instances_t', insert the instance name and
1546 * add it to the list */
1547 if (csnmp_instance_list_add (&instance_list_head, &instance_list_tail,
1548 res, host, data) != 0)
1549 {
1550 ERROR ("snmp plugin: host %s: csnmp_instance_list_add failed.",
1551 host->name);
1552 status = -1;
1553 break;
1554 }
1555 }
1556 else /* The variable we are processing is a normal value */
1557 {
1558 csnmp_table_values_t *vt;
1559 oid_t vb_name;
1560 oid_t suffix;
1561 int ret;
1563 csnmp_oid_init (&vb_name, vb->name, vb->name_length);
1565 /* Calculate the current suffix. This is later used to check that the
1566 * suffix is increasing. This also checks if we left the subtree */
1567 ret = csnmp_oid_suffix (&suffix, &vb_name, data->values + i);
1568 if (ret != 0)
1569 {
1570 DEBUG ("snmp plugin: host = %s; data = %s; i = %i; "
1571 "Value probably left its subtree.",
1572 host->name, data->name, i);
1573 oid_list_todo[i] = 0;
1574 continue;
1575 }
1577 /* Make sure the OIDs returned by the agent are increasing. Otherwise our
1578 * table matching algorithm will get confused. */
1579 if ((value_list_tail[i] != NULL)
1580 && (csnmp_oid_compare (&suffix, &value_list_tail[i]->suffix) <= 0))
1581 {
1582 DEBUG ("snmp plugin: host = %s; data = %s; i = %i; "
1583 "Suffix is not increasing.",
1584 host->name, data->name, i);
1585 oid_list_todo[i] = 0;
1586 continue;
1587 }
1589 vt = malloc (sizeof (*vt));
1590 if (vt == NULL)
1591 {
1592 ERROR ("snmp plugin: malloc failed.");
1593 status = -1;
1594 break;
1595 }
1596 memset (vt, 0, sizeof (*vt));
1598 vt->value = csnmp_value_list_to_value (vb, ds->ds[i].type,
1599 data->scale, data->shift, host->name, data->name);
1600 memcpy (&vt->suffix, &suffix, sizeof (vt->suffix));
1601 vt->next = NULL;
1603 if (value_list_tail[i] == NULL)
1604 value_list_head[i] = vt;
1605 else
1606 value_list_tail[i]->next = vt;
1607 value_list_tail[i] = vt;
1608 }
1610 /* Copy OID to oid_list[i] */
1611 memcpy (oid_list[i].oid, vb->name, sizeof (oid) * vb->name_length);
1612 oid_list[i].oid_len = vb->name_length;
1614 } /* for (vb = res->variables ...) */
1616 if (res != NULL)
1617 snmp_free_pdu (res);
1618 res = NULL;
1619 } /* while (status == 0) */
1621 if (res != NULL)
1622 snmp_free_pdu (res);
1623 res = NULL;
1625 if (req != NULL)
1626 snmp_free_pdu (req);
1627 req = NULL;
1629 if (status == 0)
1630 csnmp_dispatch_table (host, data, instance_list_head, value_list_head);
1632 /* Free all allocated variables here */
1633 while (instance_list_head != NULL)
1634 {
1635 csnmp_list_instances_t *next = instance_list_head->next;
1636 sfree (instance_list_head);
1637 instance_list_head = next;
1638 }
1640 for (i = 0; i < data->values_len; i++)
1641 {
1642 while (value_list_head[i] != NULL)
1643 {
1644 csnmp_table_values_t *next = value_list_head[i]->next;
1645 sfree (value_list_head[i]);
1646 value_list_head[i] = next;
1647 }
1648 }
1650 sfree (value_list_head);
1651 sfree (value_list_tail);
1653 return (0);
1654 } /* int csnmp_read_table */
1656 static int csnmp_read_value (host_definition_t *host, data_definition_t *data)
1657 {
1658 struct snmp_pdu *req;
1659 struct snmp_pdu *res;
1660 struct variable_list *vb;
1662 const data_set_t *ds;
1663 value_list_t vl = VALUE_LIST_INIT;
1665 int status;
1666 int i;
1668 DEBUG ("snmp plugin: csnmp_read_value (host = %s, data = %s)",
1669 host->name, data->name);
1671 if (host->sess_handle == NULL)
1672 {
1673 DEBUG ("snmp plugin: csnmp_read_table: host->sess_handle == NULL");
1674 return (-1);
1675 }
1677 ds = plugin_get_ds (data->type);
1678 if (!ds)
1679 {
1680 ERROR ("snmp plugin: DataSet `%s' not defined.", data->type);
1681 return (-1);
1682 }
1684 if (ds->ds_num != data->values_len)
1685 {
1686 ERROR ("snmp plugin: DataSet `%s' requires %i values, but config talks about %i",
1687 data->type, ds->ds_num, data->values_len);
1688 return (-1);
1689 }
1691 vl.values_len = ds->ds_num;
1692 vl.values = (value_t *) malloc (sizeof (value_t) * vl.values_len);
1693 if (vl.values == NULL)
1694 return (-1);
1695 for (i = 0; i < vl.values_len; i++)
1696 {
1697 if (ds->ds[i].type == DS_TYPE_COUNTER)
1698 vl.values[i].counter = 0;
1699 else
1700 vl.values[i].gauge = NAN;
1701 }
1703 sstrncpy (vl.host, host->name, sizeof (vl.host));
1704 sstrncpy (vl.plugin, "snmp", sizeof (vl.plugin));
1705 sstrncpy (vl.type, data->type, sizeof (vl.type));
1706 sstrncpy (vl.type_instance, data->instance.string, sizeof (vl.type_instance));
1708 vl.interval = host->interval;
1710 req = snmp_pdu_create (SNMP_MSG_GET);
1711 if (req == NULL)
1712 {
1713 ERROR ("snmp plugin: snmp_pdu_create failed.");
1714 sfree (vl.values);
1715 return (-1);
1716 }
1718 for (i = 0; i < data->values_len; i++)
1719 snmp_add_null_var (req, data->values[i].oid, data->values[i].oid_len);
1721 res = NULL;
1722 status = snmp_sess_synch_response (host->sess_handle, req, &res);
1724 if ((status != STAT_SUCCESS) || (res == NULL))
1725 {
1726 char *errstr = NULL;
1728 snmp_sess_error (host->sess_handle, NULL, NULL, &errstr);
1729 ERROR ("snmp plugin: host %s: snmp_sess_synch_response failed: %s",
1730 host->name, (errstr == NULL) ? "Unknown problem" : errstr);
1732 if (res != NULL)
1733 snmp_free_pdu (res);
1734 res = NULL;
1736 sfree (errstr);
1737 csnmp_host_close_session (host);
1739 return (-1);
1740 }
1743 for (vb = res->variables; vb != NULL; vb = vb->next_variable)
1744 {
1745 #if COLLECT_DEBUG
1746 char buffer[1024];
1747 snprint_variable (buffer, sizeof (buffer),
1748 vb->name, vb->name_length, vb);
1749 DEBUG ("snmp plugin: Got this variable: %s", buffer);
1750 #endif /* COLLECT_DEBUG */
1752 for (i = 0; i < data->values_len; i++)
1753 if (snmp_oid_compare (data->values[i].oid, data->values[i].oid_len,
1754 vb->name, vb->name_length) == 0)
1755 vl.values[i] = csnmp_value_list_to_value (vb, ds->ds[i].type,
1756 data->scale, data->shift, host->name, data->name);
1757 } /* for (res->variables) */
1759 if (res != NULL)
1760 snmp_free_pdu (res);
1761 res = NULL;
1763 DEBUG ("snmp plugin: -> plugin_dispatch_values (&vl);");
1764 plugin_dispatch_values (&vl);
1765 sfree (vl.values);
1767 return (0);
1768 } /* int csnmp_read_value */
1770 static int csnmp_read_host (user_data_t *ud)
1771 {
1772 host_definition_t *host;
1773 cdtime_t time_start;
1774 cdtime_t time_end;
1775 int status;
1776 int success;
1777 int i;
1779 host = ud->data;
1781 if (host->interval == 0)
1782 host->interval = plugin_get_interval ();
1784 time_start = cdtime ();
1786 if (host->sess_handle == NULL)
1787 csnmp_host_open_session (host);
1789 if (host->sess_handle == NULL)
1790 return (-1);
1792 success = 0;
1793 for (i = 0; i < host->data_list_len; i++)
1794 {
1795 data_definition_t *data = host->data_list[i];
1797 if (data->is_table)
1798 status = csnmp_read_table (host, data);
1799 else
1800 status = csnmp_read_value (host, data);
1802 if (status == 0)
1803 success++;
1804 }
1806 time_end = cdtime ();
1807 if ((time_end - time_start) > host->interval)
1808 {
1809 WARNING ("snmp plugin: Host `%s' should be queried every %.3f "
1810 "seconds, but reading all values takes %.3f seconds.",
1811 host->name,
1812 CDTIME_T_TO_DOUBLE (host->interval),
1813 CDTIME_T_TO_DOUBLE (time_end - time_start));
1814 }
1816 if (success == 0)
1817 return (-1);
1819 return (0);
1820 } /* int csnmp_read_host */
1822 static int csnmp_init (void)
1823 {
1824 call_snmp_init_once ();
1826 return (0);
1827 } /* int csnmp_init */
1829 static int csnmp_shutdown (void)
1830 {
1831 data_definition_t *data_this;
1832 data_definition_t *data_next;
1834 /* When we get here, the read threads have been stopped and all the
1835 * `host_definition_t' will be freed. */
1836 DEBUG ("snmp plugin: Destroying all data definitions.");
1838 data_this = data_head;
1839 data_head = NULL;
1840 while (data_this != NULL)
1841 {
1842 data_next = data_this->next;
1844 sfree (data_this->name);
1845 sfree (data_this->type);
1846 sfree (data_this->values);
1847 sfree (data_this->ignores);
1848 sfree (data_this);
1850 data_this = data_next;
1851 }
1853 return (0);
1854 } /* int csnmp_shutdown */
1856 void module_register (void)
1857 {
1858 plugin_register_complex_config ("snmp", csnmp_config);
1859 plugin_register_init ("snmp", csnmp_init);
1860 plugin_register_shutdown ("snmp", csnmp_shutdown);
1861 } /* void module_register */
1863 /*
1864 * vim: shiftwidth=2 softtabstop=2 tabstop=8 fdm=marker
1865 */