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;
650 hd = (host_definition_t *) malloc (sizeof (host_definition_t));
651 if (hd == NULL)
652 return (-1);
653 memset (hd, '\0', sizeof (host_definition_t));
654 hd->version = 2;
655 C_COMPLAIN_INIT (&hd->complaint);
657 status = cf_util_get_string(ci, &hd->name);
658 if (status != 0)
659 return status;
661 hd->sess_handle = NULL;
662 hd->interval = 0;
664 for (i = 0; i < ci->children_num; i++)
665 {
666 oconfig_item_t *option = ci->children + i;
667 status = 0;
669 if (strcasecmp ("Address", option->key) == 0)
670 status = cf_util_get_string(option, &hd->address);
671 else if (strcasecmp ("Community", option->key) == 0)
672 status = cf_util_get_string(option, &hd->community);
673 else if (strcasecmp ("Version", option->key) == 0)
674 status = csnmp_config_add_host_version (hd, option);
675 else if (strcasecmp ("Collect", option->key) == 0)
676 csnmp_config_add_host_collect (hd, option);
677 else if (strcasecmp ("Interval", option->key) == 0)
678 cf_util_get_cdtime (option, &hd->interval);
679 else if (strcasecmp ("Username", option->key) == 0)
680 status = cf_util_get_string(option, &hd->username);
681 else if (strcasecmp ("AuthProtocol", option->key) == 0)
682 status = csnmp_config_add_host_auth_protocol (hd, option);
683 else if (strcasecmp ("PrivacyProtocol", option->key) == 0)
684 status = csnmp_config_add_host_priv_protocol (hd, option);
685 else if (strcasecmp ("AuthPassphrase", option->key) == 0)
686 status = cf_util_get_string(option, &hd->auth_passphrase);
687 else if (strcasecmp ("PrivacyPassphrase", option->key) == 0)
688 status = cf_util_get_string(option, &hd->priv_passphrase);
689 else if (strcasecmp ("SecurityLevel", option->key) == 0)
690 status = csnmp_config_add_host_security_level (hd, option);
691 else if (strcasecmp ("Context", option->key) == 0)
692 status = cf_util_get_string(option, &hd->context);
693 else
694 {
695 WARNING ("snmp plugin: csnmp_config_add_host: Option `%s' not allowed here.", option->key);
696 status = -1;
697 }
699 if (status != 0)
700 break;
701 } /* for (ci->children) */
703 while (status == 0)
704 {
705 if (hd->address == NULL)
706 {
707 WARNING ("snmp plugin: `Address' not given for host `%s'", hd->name);
708 status = -1;
709 break;
710 }
711 if (hd->community == NULL && hd->version < 3)
712 {
713 WARNING ("snmp plugin: `Community' not given for host `%s'", hd->name);
714 status = -1;
715 break;
716 }
717 if (hd->version == 3)
718 {
719 if (hd->username == NULL)
720 {
721 WARNING ("snmp plugin: `Username' not given for host `%s'", hd->name);
722 status = -1;
723 break;
724 }
725 if (hd->security_level == 0)
726 {
727 WARNING ("snmp plugin: `SecurityLevel' not given for host `%s'", hd->name);
728 status = -1;
729 break;
730 }
731 if (hd->security_level == SNMP_SEC_LEVEL_AUTHNOPRIV || hd->security_level == SNMP_SEC_LEVEL_AUTHPRIV)
732 {
733 if (hd->auth_protocol == NULL)
734 {
735 WARNING ("snmp plugin: `AuthProtocol' not given for host `%s'", hd->name);
736 status = -1;
737 break;
738 }
739 if (hd->auth_passphrase == NULL)
740 {
741 WARNING ("snmp plugin: `AuthPassphrase' not given for host `%s'", hd->name);
742 status = -1;
743 break;
744 }
745 }
746 if (hd->security_level == SNMP_SEC_LEVEL_AUTHPRIV)
747 {
748 if (hd->priv_protocol == NULL)
749 {
750 WARNING ("snmp plugin: `PrivacyProtocol' not given for host `%s'", hd->name);
751 status = -1;
752 break;
753 }
754 if (hd->priv_passphrase == NULL)
755 {
756 WARNING ("snmp plugin: `PrivacyPassphrase' not given for host `%s'", hd->name);
757 status = -1;
758 break;
759 }
760 }
761 }
763 break;
764 } /* while (status == 0) */
766 if (status != 0)
767 {
768 csnmp_host_definition_destroy (hd);
769 return (-1);
770 }
772 DEBUG ("snmp plugin: hd = { name = %s, address = %s, community = %s, version = %i }",
773 hd->name, hd->address, hd->community, hd->version);
775 ssnprintf (cb_name, sizeof (cb_name), "snmp-%s", hd->name);
777 memset (&cb_data, 0, sizeof (cb_data));
778 cb_data.data = hd;
779 cb_data.free_func = csnmp_host_definition_destroy;
781 status = plugin_register_complex_read (/* group = */ NULL, cb_name,
782 csnmp_read_host, hd->interval, /* user_data = */ &cb_data);
783 if (status != 0)
784 {
785 ERROR ("snmp plugin: Registering complex read function failed.");
786 csnmp_host_definition_destroy (hd);
787 return (-1);
788 }
790 return (0);
791 } /* int csnmp_config_add_host */
793 static int csnmp_config (oconfig_item_t *ci)
794 {
795 int i;
797 call_snmp_init_once ();
799 for (i = 0; i < ci->children_num; i++)
800 {
801 oconfig_item_t *child = ci->children + i;
802 if (strcasecmp ("Data", child->key) == 0)
803 csnmp_config_add_data (child);
804 else if (strcasecmp ("Host", child->key) == 0)
805 csnmp_config_add_host (child);
806 else
807 {
808 WARNING ("snmp plugin: Ignoring unknown config option `%s'.", child->key);
809 }
810 } /* for (ci->children) */
812 return (0);
813 } /* int csnmp_config */
815 /* }}} End of the config stuff. Now the interesting part begins */
817 static void csnmp_host_open_session (host_definition_t *host)
818 {
819 struct snmp_session sess;
820 int error;
822 if (host->sess_handle != NULL)
823 csnmp_host_close_session (host);
825 snmp_sess_init (&sess);
826 sess.peername = host->address;
827 switch (host->version)
828 {
829 case 1:
830 sess.version = SNMP_VERSION_1;
831 break;
832 case 3:
833 sess.version = SNMP_VERSION_3;
834 break;
835 default:
836 sess.version = SNMP_VERSION_2c;
837 break;
838 }
840 if (host->version == 3)
841 {
842 sess.securityName = host->username;
843 sess.securityNameLen = strlen (host->username);
844 sess.securityLevel = host->security_level;
846 if (sess.securityLevel == SNMP_SEC_LEVEL_AUTHNOPRIV || sess.securityLevel == SNMP_SEC_LEVEL_AUTHPRIV)
847 {
848 sess.securityAuthProto = host->auth_protocol;
849 sess.securityAuthProtoLen = host->auth_protocol_len;
850 sess.securityAuthKeyLen = USM_AUTH_KU_LEN;
851 error = generate_Ku (sess.securityAuthProto,
852 sess.securityAuthProtoLen,
853 (u_char *) host->auth_passphrase,
854 strlen(host->auth_passphrase),
855 sess.securityAuthKey,
856 &sess.securityAuthKeyLen);
857 if (error != SNMPERR_SUCCESS) {
858 ERROR ("snmp plugin: host %s: Error generating Ku from auth_passphrase. (Error %d)", host->name, error);
859 }
860 }
862 if (sess.securityLevel == SNMP_SEC_LEVEL_AUTHPRIV)
863 {
864 sess.securityPrivProto = host->priv_protocol;
865 sess.securityPrivProtoLen = host->priv_protocol_len;
866 sess.securityPrivKeyLen = USM_PRIV_KU_LEN;
867 error = generate_Ku (sess.securityAuthProto,
868 sess.securityAuthProtoLen,
869 (u_char *) host->priv_passphrase,
870 strlen(host->priv_passphrase),
871 sess.securityPrivKey,
872 &sess.securityPrivKeyLen);
873 if (error != SNMPERR_SUCCESS) {
874 ERROR ("snmp plugin: host %s: Error generating Ku from priv_passphrase. (Error %d)", host->name, error);
875 }
876 }
878 if (host->context != NULL)
879 {
880 sess.contextName = host->context;
881 sess.contextNameLen = strlen (host->context);
882 }
883 }
884 else /* SNMPv1/2 "authenticates" with community string */
885 {
886 sess.community = (u_char *) host->community;
887 sess.community_len = strlen (host->community);
888 }
890 /* snmp_sess_open will copy the `struct snmp_session *'. */
891 host->sess_handle = snmp_sess_open (&sess);
893 if (host->sess_handle == NULL)
894 {
895 char *errstr = NULL;
897 snmp_error (&sess, NULL, NULL, &errstr);
899 ERROR ("snmp plugin: host %s: snmp_sess_open failed: %s",
900 host->name, (errstr == NULL) ? "Unknown problem" : errstr);
901 sfree (errstr);
902 }
903 } /* void csnmp_host_open_session */
905 /* TODO: Check if negative values wrap around. Problem: negative temperatures. */
906 static value_t csnmp_value_list_to_value (struct variable_list *vl, int type,
907 double scale, double shift,
908 const char *host_name, const char *data_name)
909 {
910 value_t ret;
911 uint64_t tmp_unsigned = 0;
912 int64_t tmp_signed = 0;
913 _Bool defined = 1;
914 /* Set to true when the original SNMP type appears to have been signed. */
915 _Bool prefer_signed = 0;
917 if ((vl->type == ASN_INTEGER)
918 || (vl->type == ASN_UINTEGER)
919 || (vl->type == ASN_COUNTER)
920 #ifdef ASN_TIMETICKS
921 || (vl->type == ASN_TIMETICKS)
922 #endif
923 || (vl->type == ASN_GAUGE))
924 {
925 tmp_unsigned = (uint32_t) *vl->val.integer;
926 tmp_signed = (int32_t) *vl->val.integer;
928 if ((vl->type == ASN_INTEGER)
929 || (vl->type == ASN_GAUGE))
930 prefer_signed = 1;
932 DEBUG ("snmp plugin: Parsed int32 value is %"PRIu64".", tmp_unsigned);
933 }
934 else if (vl->type == ASN_COUNTER64)
935 {
936 tmp_unsigned = (uint32_t) vl->val.counter64->high;
937 tmp_unsigned = tmp_unsigned << 32;
938 tmp_unsigned += (uint32_t) vl->val.counter64->low;
939 tmp_signed = (int64_t) tmp_unsigned;
940 DEBUG ("snmp plugin: Parsed int64 value is %"PRIu64".", tmp_unsigned);
941 }
942 else if (vl->type == ASN_OCTET_STR)
943 {
944 /* We'll handle this later.. */
945 }
946 else
947 {
948 char oid_buffer[1024];
950 memset (oid_buffer, 0, sizeof (oid_buffer));
951 snprint_objid (oid_buffer, sizeof (oid_buffer) - 1,
952 vl->name, vl->name_length);
954 #ifdef ASN_NULL
955 if (vl->type == ASN_NULL)
956 INFO ("snmp plugin: OID \"%s\" is undefined (type ASN_NULL)",
957 oid_buffer);
958 else
959 #endif
960 WARNING ("snmp plugin: I don't know the ASN type #%i "
961 "(OID: \"%s\", data block \"%s\", host block \"%s\")",
962 (int) vl->type, oid_buffer,
963 (data_name != NULL) ? data_name : "UNKNOWN",
964 (host_name != NULL) ? host_name : "UNKNOWN");
966 defined = 0;
967 }
969 if (vl->type == ASN_OCTET_STR)
970 {
971 int status = -1;
973 if (vl->val.string != NULL)
974 {
975 char string[64];
976 size_t string_length;
978 string_length = sizeof (string) - 1;
979 if (vl->val_len < string_length)
980 string_length = vl->val_len;
982 /* The strings we get from the Net-SNMP library may not be null
983 * terminated. That is why we're using `memcpy' here and not `strcpy'.
984 * `string_length' is set to `vl->val_len' which holds the length of the
985 * string. -octo */
986 memcpy (string, vl->val.string, string_length);
987 string[string_length] = 0;
989 status = parse_value (string, &ret, type);
990 if (status != 0)
991 {
992 ERROR ("snmp plugin: host %s: csnmp_value_list_to_value: Parsing string as %s failed: %s",
993 (host_name != NULL) ? host_name : "UNKNOWN",
994 DS_TYPE_TO_STRING (type), string);
995 }
996 }
998 if (status != 0)
999 {
1000 switch (type)
1001 {
1002 case DS_TYPE_COUNTER:
1003 case DS_TYPE_DERIVE:
1004 case DS_TYPE_ABSOLUTE:
1005 memset (&ret, 0, sizeof (ret));
1006 break;
1008 case DS_TYPE_GAUGE:
1009 ret.gauge = NAN;
1010 break;
1012 default:
1013 ERROR ("snmp plugin: csnmp_value_list_to_value: Unknown "
1014 "data source type: %i.", type);
1015 ret.gauge = NAN;
1016 }
1017 }
1018 } /* if (vl->type == ASN_OCTET_STR) */
1019 else if (type == DS_TYPE_COUNTER)
1020 {
1021 ret.counter = tmp_unsigned;
1022 }
1023 else if (type == DS_TYPE_GAUGE)
1024 {
1025 if (!defined)
1026 ret.gauge = NAN;
1027 else if (prefer_signed)
1028 ret.gauge = (scale * tmp_signed) + shift;
1029 else
1030 ret.gauge = (scale * tmp_unsigned) + shift;
1031 }
1032 else if (type == DS_TYPE_DERIVE)
1033 {
1034 if (prefer_signed)
1035 ret.derive = (derive_t) tmp_signed;
1036 else
1037 ret.derive = (derive_t) tmp_unsigned;
1038 }
1039 else if (type == DS_TYPE_ABSOLUTE)
1040 {
1041 ret.absolute = (absolute_t) tmp_unsigned;
1042 }
1043 else
1044 {
1045 ERROR ("snmp plugin: csnmp_value_list_to_value: Unknown data source "
1046 "type: %i.", type);
1047 ret.gauge = NAN;
1048 }
1050 return (ret);
1051 } /* value_t csnmp_value_list_to_value */
1053 static int csnmp_strvbcopy_hexstring (char *dst, /* {{{ */
1054 const struct variable_list *vb, size_t dst_size)
1055 {
1056 char *buffer_ptr;
1057 size_t buffer_free;
1058 size_t i;
1060 buffer_ptr = dst;
1061 buffer_free = dst_size;
1063 for (i = 0; i < vb->val_len; i++)
1064 {
1065 int status;
1067 status = snprintf (buffer_ptr, buffer_free,
1068 (i == 0) ? "%02x" : ":%02x", (unsigned int) vb->val.bitstring[i]);
1070 if (status >= buffer_free)
1071 {
1072 buffer_ptr += (buffer_free - 1);
1073 *buffer_ptr = 0;
1074 return (dst_size + (buffer_free - status));
1075 }
1076 else /* if (status < buffer_free) */
1077 {
1078 buffer_ptr += status;
1079 buffer_free -= status;
1080 }
1081 }
1083 return ((int) (dst_size - buffer_free));
1084 } /* }}} int csnmp_strvbcopy_hexstring */
1086 static int csnmp_strvbcopy (char *dst, /* {{{ */
1087 const struct variable_list *vb, size_t dst_size)
1088 {
1089 char *src;
1090 size_t num_chars;
1091 size_t i;
1093 if (vb->type == ASN_OCTET_STR)
1094 src = (char *) vb->val.string;
1095 else if (vb->type == ASN_BIT_STR)
1096 src = (char *) vb->val.bitstring;
1097 else
1098 {
1099 dst[0] = 0;
1100 return (EINVAL);
1101 }
1103 num_chars = dst_size - 1;
1104 if (num_chars > vb->val_len)
1105 num_chars = vb->val_len;
1107 for (i = 0; i < num_chars; i++)
1108 {
1109 /* Check for control characters. */
1110 if ((unsigned char)src[i] < 32)
1111 return (csnmp_strvbcopy_hexstring (dst, vb, dst_size));
1112 dst[i] = src[i];
1113 }
1114 dst[num_chars] = 0;
1116 return ((int) vb->val_len);
1117 } /* }}} int csnmp_strvbcopy */
1119 static int csnmp_instance_list_add (csnmp_list_instances_t **head,
1120 csnmp_list_instances_t **tail,
1121 const struct snmp_pdu *res,
1122 const host_definition_t *hd, const data_definition_t *dd)
1123 {
1124 csnmp_list_instances_t *il;
1125 struct variable_list *vb;
1126 oid_t vb_name;
1127 int status;
1128 uint32_t i;
1129 uint32_t is_matched;
1131 /* Set vb on the last variable */
1132 for (vb = res->variables;
1133 (vb != NULL) && (vb->next_variable != NULL);
1134 vb = vb->next_variable)
1135 /* do nothing */;
1136 if (vb == NULL)
1137 return (-1);
1139 csnmp_oid_init (&vb_name, vb->name, vb->name_length);
1141 il = malloc (sizeof (*il));
1142 if (il == NULL)
1143 {
1144 ERROR ("snmp plugin: malloc failed.");
1145 return (-1);
1146 }
1147 memset (il, 0, sizeof (*il));
1148 il->next = NULL;
1150 status = csnmp_oid_suffix (&il->suffix, &vb_name, &dd->instance.oid);
1151 if (status != 0)
1152 {
1153 sfree (il);
1154 return (status);
1155 }
1157 /* Get instance name */
1158 if ((vb->type == ASN_OCTET_STR) || (vb->type == ASN_BIT_STR))
1159 {
1160 char *ptr;
1162 csnmp_strvbcopy (il->instance, vb, sizeof (il->instance));
1163 is_matched = 0;
1164 for (i = 0; i < dd->ignores_len; i++)
1165 {
1166 status = fnmatch(dd->ignores[i], il->instance, 0);
1167 if (status == 0)
1168 {
1169 if (dd->invert_match == 0)
1170 {
1171 sfree(il);
1172 return 0;
1173 }
1174 else
1175 {
1176 is_matched = 1;
1177 break;
1178 }
1179 }
1180 }
1181 if (dd->invert_match != 0 && is_matched == 0)
1182 {
1183 sfree(il);
1184 return 0;
1185 }
1186 for (ptr = il->instance; *ptr != '\0'; ptr++)
1187 {
1188 if ((*ptr > 0) && (*ptr < 32))
1189 *ptr = ' ';
1190 else if (*ptr == '/')
1191 *ptr = '_';
1192 }
1193 DEBUG ("snmp plugin: il->instance = `%s';", il->instance);
1194 }
1195 else
1196 {
1197 value_t val = csnmp_value_list_to_value (vb, DS_TYPE_COUNTER,
1198 /* scale = */ 1.0, /* shift = */ 0.0, hd->name, dd->name);
1199 ssnprintf (il->instance, sizeof (il->instance),
1200 "%llu", val.counter);
1201 }
1203 /* TODO: Debugging output */
1205 if (*head == NULL)
1206 *head = il;
1207 else
1208 (*tail)->next = il;
1209 *tail = il;
1211 return (0);
1212 } /* int csnmp_instance_list_add */
1214 static int csnmp_dispatch_table (host_definition_t *host, data_definition_t *data,
1215 csnmp_list_instances_t *instance_list,
1216 csnmp_table_values_t **value_table)
1217 {
1218 const data_set_t *ds;
1219 value_list_t vl = VALUE_LIST_INIT;
1221 csnmp_list_instances_t *instance_list_ptr;
1222 csnmp_table_values_t **value_table_ptr;
1224 int i;
1225 _Bool have_more;
1226 oid_t current_suffix;
1228 ds = plugin_get_ds (data->type);
1229 if (!ds)
1230 {
1231 ERROR ("snmp plugin: DataSet `%s' not defined.", data->type);
1232 return (-1);
1233 }
1234 assert (ds->ds_num == data->values_len);
1236 instance_list_ptr = instance_list;
1238 value_table_ptr = malloc (sizeof (*value_table_ptr) * data->values_len);
1239 if (value_table_ptr == NULL)
1240 return (-1);
1241 for (i = 0; i < data->values_len; i++)
1242 value_table_ptr[i] = value_table[i];
1244 vl.values_len = ds->ds_num;
1245 vl.values = malloc (sizeof (*vl.values) * vl.values_len);
1246 if (vl.values == NULL)
1247 {
1248 ERROR ("snmp plugin: malloc failed.");
1249 sfree (value_table_ptr);
1250 return (-1);
1251 }
1253 sstrncpy (vl.host, host->name, sizeof (vl.host));
1254 sstrncpy (vl.plugin, "snmp", sizeof (vl.plugin));
1256 vl.interval = host->interval;
1258 have_more = 1;
1259 memset (¤t_suffix, 0, sizeof (current_suffix));
1260 while (have_more)
1261 {
1262 _Bool suffix_skipped = 0;
1264 /* Determine next suffix to handle. */
1265 if (instance_list != NULL)
1266 {
1267 if (instance_list_ptr == NULL)
1268 {
1269 have_more = 0;
1270 continue;
1271 }
1273 memcpy (¤t_suffix, &instance_list_ptr->suffix, sizeof (current_suffix));
1274 }
1275 else /* no instance configured */
1276 {
1277 csnmp_table_values_t *ptr = value_table_ptr[0];
1278 if (ptr == NULL)
1279 {
1280 have_more = 0;
1281 continue;
1282 }
1284 memcpy (¤t_suffix, &ptr->suffix, sizeof (current_suffix));
1285 }
1287 /* Update all the value_table_ptr to point at the entry with the same
1288 * trailing partial OID */
1289 for (i = 0; i < data->values_len; i++)
1290 {
1291 while ((value_table_ptr[i] != NULL)
1292 && (csnmp_oid_compare (&value_table_ptr[i]->suffix, ¤t_suffix) < 0))
1293 value_table_ptr[i] = value_table_ptr[i]->next;
1295 if (value_table_ptr[i] == NULL)
1296 {
1297 have_more = 0;
1298 break;
1299 }
1300 else if (csnmp_oid_compare (&value_table_ptr[i]->suffix, ¤t_suffix) > 0)
1301 {
1302 /* This suffix is missing in the subtree. Indicate this with the
1303 * "suffix_skipped" flag and try the next instance / suffix. */
1304 suffix_skipped = 1;
1305 break;
1306 }
1307 } /* for (i = 0; i < columns; i++) */
1309 if (!have_more)
1310 break;
1312 /* Matching the values failed. Start from the beginning again. */
1313 if (suffix_skipped)
1314 {
1315 if (instance_list != NULL)
1316 instance_list_ptr = instance_list_ptr->next;
1317 else
1318 value_table_ptr[0] = value_table_ptr[0]->next;
1320 continue;
1321 }
1323 /* if we reach this line, all value_table_ptr[i] are non-NULL and are set
1324 * to the same subid. instance_list_ptr is either NULL or points to the
1325 * same subid, too. */
1326 #if COLLECT_DEBUG
1327 for (i = 1; i < data->values_len; i++)
1328 {
1329 assert (value_table_ptr[i] != NULL);
1330 assert (csnmp_oid_compare (&value_table_ptr[i-1]->suffix,
1331 &value_table_ptr[i]->suffix) == 0);
1332 }
1333 assert ((instance_list_ptr == NULL)
1334 || (csnmp_oid_compare (&instance_list_ptr->suffix,
1335 &value_table_ptr[0]->suffix) == 0));
1336 #endif
1338 sstrncpy (vl.type, data->type, sizeof (vl.type));
1340 {
1341 char temp[DATA_MAX_NAME_LEN];
1343 if (instance_list_ptr == NULL)
1344 csnmp_oid_to_string (temp, sizeof (temp), ¤t_suffix);
1345 else
1346 sstrncpy (temp, instance_list_ptr->instance, sizeof (temp));
1348 if (data->instance_prefix == NULL)
1349 sstrncpy (vl.type_instance, temp, sizeof (vl.type_instance));
1350 else
1351 ssnprintf (vl.type_instance, sizeof (vl.type_instance), "%s%s",
1352 data->instance_prefix, temp);
1353 }
1355 for (i = 0; i < data->values_len; i++)
1356 vl.values[i] = value_table_ptr[i]->value;
1358 /* If we get here `vl.type_instance' and all `vl.values' have been set */
1359 plugin_dispatch_values (&vl);
1361 if (instance_list != NULL)
1362 instance_list_ptr = instance_list_ptr->next;
1363 else
1364 value_table_ptr[0] = value_table_ptr[0]->next;
1365 } /* while (have_more) */
1367 sfree (vl.values);
1368 sfree (value_table_ptr);
1370 return (0);
1371 } /* int csnmp_dispatch_table */
1373 static int csnmp_read_table (host_definition_t *host, data_definition_t *data)
1374 {
1375 struct snmp_pdu *req;
1376 struct snmp_pdu *res;
1377 struct variable_list *vb;
1379 const data_set_t *ds;
1381 uint32_t oid_list_len = (uint32_t) (data->values_len + 1);
1382 /* Holds the last OID returned by the device. We use this in the GETNEXT
1383 * request to proceed. */
1384 oid_t oid_list[oid_list_len];
1385 /* Set to false when an OID has left its subtree so we don't re-request it
1386 * again. */
1387 _Bool oid_list_todo[oid_list_len];
1389 int status;
1390 int i;
1391 uint32_t j;
1393 /* `value_list_head' and `value_list_tail' implement a linked list for each
1394 * value. `instance_list_head' and `instance_list_tail' implement a linked list of
1395 * instance names. This is used to jump gaps in the table. */
1396 csnmp_list_instances_t *instance_list_head;
1397 csnmp_list_instances_t *instance_list_tail;
1398 csnmp_table_values_t **value_list_head;
1399 csnmp_table_values_t **value_list_tail;
1401 DEBUG ("snmp plugin: csnmp_read_table (host = %s, data = %s)",
1402 host->name, data->name);
1404 if (host->sess_handle == NULL)
1405 {
1406 DEBUG ("snmp plugin: csnmp_read_table: host->sess_handle == NULL");
1407 return (-1);
1408 }
1410 ds = plugin_get_ds (data->type);
1411 if (!ds)
1412 {
1413 ERROR ("snmp plugin: DataSet `%s' not defined.", data->type);
1414 return (-1);
1415 }
1417 if (ds->ds_num != data->values_len)
1418 {
1419 ERROR ("snmp plugin: DataSet `%s' requires %i values, but config talks about %i",
1420 data->type, ds->ds_num, data->values_len);
1421 return (-1);
1422 }
1424 /* We need a copy of all the OIDs, because GETNEXT will destroy them. */
1425 memcpy (oid_list, data->values, data->values_len * sizeof (oid_t));
1426 if (data->instance.oid.oid_len > 0)
1427 memcpy (oid_list + data->values_len, &data->instance.oid, sizeof (oid_t));
1428 else /* no InstanceFrom option specified. */
1429 oid_list_len--;
1431 for (j = 0; j < oid_list_len; j++)
1432 oid_list_todo[j] = 1;
1434 /* We're going to construct n linked lists, one for each "value".
1435 * value_list_head will contain pointers to the heads of these linked lists,
1436 * value_list_tail will contain pointers to the tail of the lists. */
1437 value_list_head = calloc (data->values_len, sizeof (*value_list_head));
1438 value_list_tail = calloc (data->values_len, sizeof (*value_list_tail));
1439 if ((value_list_head == NULL) || (value_list_tail == NULL))
1440 {
1441 ERROR ("snmp plugin: csnmp_read_table: calloc failed.");
1442 sfree (value_list_head);
1443 sfree (value_list_tail);
1444 return (-1);
1445 }
1447 instance_list_head = NULL;
1448 instance_list_tail = NULL;
1450 status = 0;
1451 while (status == 0)
1452 {
1453 int oid_list_todo_num;
1455 req = snmp_pdu_create (SNMP_MSG_GETNEXT);
1456 if (req == NULL)
1457 {
1458 ERROR ("snmp plugin: snmp_pdu_create failed.");
1459 status = -1;
1460 break;
1461 }
1463 oid_list_todo_num = 0;
1464 for (j = 0; j < oid_list_len; j++)
1465 {
1466 /* Do not rerequest already finished OIDs */
1467 if (!oid_list_todo[j])
1468 continue;
1469 oid_list_todo_num++;
1470 snmp_add_null_var (req, oid_list[j].oid, oid_list[j].oid_len);
1471 }
1473 if (oid_list_todo_num == 0)
1474 {
1475 /* The request is still empty - so we are finished */
1476 DEBUG ("snmp plugin: all variables have left their subtree");
1477 status = 0;
1478 break;
1479 }
1481 res = NULL;
1482 status = snmp_sess_synch_response (host->sess_handle, req, &res);
1483 if ((status != STAT_SUCCESS) || (res == NULL))
1484 {
1485 char *errstr = NULL;
1487 snmp_sess_error (host->sess_handle, NULL, NULL, &errstr);
1489 c_complain (LOG_ERR, &host->complaint,
1490 "snmp plugin: host %s: snmp_sess_synch_response failed: %s",
1491 host->name, (errstr == NULL) ? "Unknown problem" : errstr);
1493 if (res != NULL)
1494 snmp_free_pdu (res);
1495 res = NULL;
1497 /* snmp_synch_response already freed our PDU */
1498 req = NULL;
1499 sfree (errstr);
1500 csnmp_host_close_session (host);
1502 status = -1;
1503 break;
1504 }
1506 status = 0;
1507 assert (res != NULL);
1508 c_release (LOG_INFO, &host->complaint,
1509 "snmp plugin: host %s: snmp_sess_synch_response successful.",
1510 host->name);
1512 vb = res->variables;
1513 if (vb == NULL)
1514 {
1515 status = -1;
1516 break;
1517 }
1519 for (vb = res->variables, i = 0; (vb != NULL); vb = vb->next_variable, i++)
1520 {
1521 /* Calculate value index from todo list */
1522 while (!oid_list_todo[i] && (i < oid_list_len))
1523 i++;
1525 /* An instance is configured and the res variable we process is the
1526 * instance value (last index) */
1527 if ((data->instance.oid.oid_len > 0) && (i == data->values_len))
1528 {
1529 if ((vb->type == SNMP_ENDOFMIBVIEW)
1530 || (snmp_oid_ncompare (data->instance.oid.oid,
1531 data->instance.oid.oid_len,
1532 vb->name, vb->name_length,
1533 data->instance.oid.oid_len) != 0))
1534 {
1535 DEBUG ("snmp plugin: host = %s; data = %s; Instance left its subtree.",
1536 host->name, data->name);
1537 oid_list_todo[i] = 0;
1538 continue;
1539 }
1541 /* Allocate a new `csnmp_list_instances_t', insert the instance name and
1542 * add it to the list */
1543 if (csnmp_instance_list_add (&instance_list_head, &instance_list_tail,
1544 res, host, data) != 0)
1545 {
1546 ERROR ("snmp plugin: host %s: csnmp_instance_list_add failed.",
1547 host->name);
1548 status = -1;
1549 break;
1550 }
1551 }
1552 else /* The variable we are processing is a normal value */
1553 {
1554 csnmp_table_values_t *vt;
1555 oid_t vb_name;
1556 oid_t suffix;
1557 int ret;
1559 csnmp_oid_init (&vb_name, vb->name, vb->name_length);
1561 /* Calculate the current suffix. This is later used to check that the
1562 * suffix is increasing. This also checks if we left the subtree */
1563 ret = csnmp_oid_suffix (&suffix, &vb_name, data->values + i);
1564 if (ret != 0)
1565 {
1566 DEBUG ("snmp plugin: host = %s; data = %s; i = %i; "
1567 "Value probably left its subtree.",
1568 host->name, data->name, i);
1569 oid_list_todo[i] = 0;
1570 continue;
1571 }
1573 /* Make sure the OIDs returned by the agent are increasing. Otherwise our
1574 * table matching algorithm will get confused. */
1575 if ((value_list_tail[i] != NULL)
1576 && (csnmp_oid_compare (&suffix, &value_list_tail[i]->suffix) <= 0))
1577 {
1578 DEBUG ("snmp plugin: host = %s; data = %s; i = %i; "
1579 "Suffix is not increasing.",
1580 host->name, data->name, i);
1581 oid_list_todo[i] = 0;
1582 continue;
1583 }
1585 vt = malloc (sizeof (*vt));
1586 if (vt == NULL)
1587 {
1588 ERROR ("snmp plugin: malloc failed.");
1589 status = -1;
1590 break;
1591 }
1592 memset (vt, 0, sizeof (*vt));
1594 vt->value = csnmp_value_list_to_value (vb, ds->ds[i].type,
1595 data->scale, data->shift, host->name, data->name);
1596 memcpy (&vt->suffix, &suffix, sizeof (vt->suffix));
1597 vt->next = NULL;
1599 if (value_list_tail[i] == NULL)
1600 value_list_head[i] = vt;
1601 else
1602 value_list_tail[i]->next = vt;
1603 value_list_tail[i] = vt;
1604 }
1606 /* Copy OID to oid_list[i] */
1607 memcpy (oid_list[i].oid, vb->name, sizeof (oid) * vb->name_length);
1608 oid_list[i].oid_len = vb->name_length;
1610 } /* for (vb = res->variables ...) */
1612 if (res != NULL)
1613 snmp_free_pdu (res);
1614 res = NULL;
1615 } /* while (status == 0) */
1617 if (res != NULL)
1618 snmp_free_pdu (res);
1619 res = NULL;
1621 if (req != NULL)
1622 snmp_free_pdu (req);
1623 req = NULL;
1625 if (status == 0)
1626 csnmp_dispatch_table (host, data, instance_list_head, value_list_head);
1628 /* Free all allocated variables here */
1629 while (instance_list_head != NULL)
1630 {
1631 csnmp_list_instances_t *next = instance_list_head->next;
1632 sfree (instance_list_head);
1633 instance_list_head = next;
1634 }
1636 for (i = 0; i < data->values_len; i++)
1637 {
1638 while (value_list_head[i] != NULL)
1639 {
1640 csnmp_table_values_t *next = value_list_head[i]->next;
1641 sfree (value_list_head[i]);
1642 value_list_head[i] = next;
1643 }
1644 }
1646 sfree (value_list_head);
1647 sfree (value_list_tail);
1649 return (0);
1650 } /* int csnmp_read_table */
1652 static int csnmp_read_value (host_definition_t *host, data_definition_t *data)
1653 {
1654 struct snmp_pdu *req;
1655 struct snmp_pdu *res;
1656 struct variable_list *vb;
1658 const data_set_t *ds;
1659 value_list_t vl = VALUE_LIST_INIT;
1661 int status;
1662 int i;
1664 DEBUG ("snmp plugin: csnmp_read_value (host = %s, data = %s)",
1665 host->name, data->name);
1667 if (host->sess_handle == NULL)
1668 {
1669 DEBUG ("snmp plugin: csnmp_read_table: host->sess_handle == NULL");
1670 return (-1);
1671 }
1673 ds = plugin_get_ds (data->type);
1674 if (!ds)
1675 {
1676 ERROR ("snmp plugin: DataSet `%s' not defined.", data->type);
1677 return (-1);
1678 }
1680 if (ds->ds_num != data->values_len)
1681 {
1682 ERROR ("snmp plugin: DataSet `%s' requires %i values, but config talks about %i",
1683 data->type, ds->ds_num, data->values_len);
1684 return (-1);
1685 }
1687 vl.values_len = ds->ds_num;
1688 vl.values = (value_t *) malloc (sizeof (value_t) * vl.values_len);
1689 if (vl.values == NULL)
1690 return (-1);
1691 for (i = 0; i < vl.values_len; i++)
1692 {
1693 if (ds->ds[i].type == DS_TYPE_COUNTER)
1694 vl.values[i].counter = 0;
1695 else
1696 vl.values[i].gauge = NAN;
1697 }
1699 sstrncpy (vl.host, host->name, sizeof (vl.host));
1700 sstrncpy (vl.plugin, "snmp", sizeof (vl.plugin));
1701 sstrncpy (vl.type, data->type, sizeof (vl.type));
1702 sstrncpy (vl.type_instance, data->instance.string, sizeof (vl.type_instance));
1704 vl.interval = host->interval;
1706 req = snmp_pdu_create (SNMP_MSG_GET);
1707 if (req == NULL)
1708 {
1709 ERROR ("snmp plugin: snmp_pdu_create failed.");
1710 sfree (vl.values);
1711 return (-1);
1712 }
1714 for (i = 0; i < data->values_len; i++)
1715 snmp_add_null_var (req, data->values[i].oid, data->values[i].oid_len);
1717 res = NULL;
1718 status = snmp_sess_synch_response (host->sess_handle, req, &res);
1720 if ((status != STAT_SUCCESS) || (res == NULL))
1721 {
1722 char *errstr = NULL;
1724 snmp_sess_error (host->sess_handle, NULL, NULL, &errstr);
1725 ERROR ("snmp plugin: host %s: snmp_sess_synch_response failed: %s",
1726 host->name, (errstr == NULL) ? "Unknown problem" : errstr);
1728 if (res != NULL)
1729 snmp_free_pdu (res);
1730 res = NULL;
1732 sfree (errstr);
1733 csnmp_host_close_session (host);
1735 return (-1);
1736 }
1739 for (vb = res->variables; vb != NULL; vb = vb->next_variable)
1740 {
1741 #if COLLECT_DEBUG
1742 char buffer[1024];
1743 snprint_variable (buffer, sizeof (buffer),
1744 vb->name, vb->name_length, vb);
1745 DEBUG ("snmp plugin: Got this variable: %s", buffer);
1746 #endif /* COLLECT_DEBUG */
1748 for (i = 0; i < data->values_len; i++)
1749 if (snmp_oid_compare (data->values[i].oid, data->values[i].oid_len,
1750 vb->name, vb->name_length) == 0)
1751 vl.values[i] = csnmp_value_list_to_value (vb, ds->ds[i].type,
1752 data->scale, data->shift, host->name, data->name);
1753 } /* for (res->variables) */
1755 if (res != NULL)
1756 snmp_free_pdu (res);
1757 res = NULL;
1759 DEBUG ("snmp plugin: -> plugin_dispatch_values (&vl);");
1760 plugin_dispatch_values (&vl);
1761 sfree (vl.values);
1763 return (0);
1764 } /* int csnmp_read_value */
1766 static int csnmp_read_host (user_data_t *ud)
1767 {
1768 host_definition_t *host;
1769 cdtime_t time_start;
1770 cdtime_t time_end;
1771 int status;
1772 int success;
1773 int i;
1775 host = ud->data;
1777 if (host->interval == 0)
1778 host->interval = plugin_get_interval ();
1780 time_start = cdtime ();
1782 if (host->sess_handle == NULL)
1783 csnmp_host_open_session (host);
1785 if (host->sess_handle == NULL)
1786 return (-1);
1788 success = 0;
1789 for (i = 0; i < host->data_list_len; i++)
1790 {
1791 data_definition_t *data = host->data_list[i];
1793 if (data->is_table)
1794 status = csnmp_read_table (host, data);
1795 else
1796 status = csnmp_read_value (host, data);
1798 if (status == 0)
1799 success++;
1800 }
1802 time_end = cdtime ();
1803 if ((time_end - time_start) > host->interval)
1804 {
1805 WARNING ("snmp plugin: Host `%s' should be queried every %.3f "
1806 "seconds, but reading all values takes %.3f seconds.",
1807 host->name,
1808 CDTIME_T_TO_DOUBLE (host->interval),
1809 CDTIME_T_TO_DOUBLE (time_end - time_start));
1810 }
1812 if (success == 0)
1813 return (-1);
1815 return (0);
1816 } /* int csnmp_read_host */
1818 static int csnmp_init (void)
1819 {
1820 call_snmp_init_once ();
1822 return (0);
1823 } /* int csnmp_init */
1825 static int csnmp_shutdown (void)
1826 {
1827 data_definition_t *data_this;
1828 data_definition_t *data_next;
1830 /* When we get here, the read threads have been stopped and all the
1831 * `host_definition_t' will be freed. */
1832 DEBUG ("snmp plugin: Destroying all data definitions.");
1834 data_this = data_head;
1835 data_head = NULL;
1836 while (data_this != NULL)
1837 {
1838 data_next = data_this->next;
1840 sfree (data_this->name);
1841 sfree (data_this->type);
1842 sfree (data_this->values);
1843 sfree (data_this->ignores);
1844 sfree (data_this);
1846 data_this = data_next;
1847 }
1849 return (0);
1850 } /* int csnmp_shutdown */
1852 void module_register (void)
1853 {
1854 plugin_register_complex_config ("snmp", csnmp_config);
1855 plugin_register_init ("snmp", csnmp_init);
1856 plugin_register_shutdown ("snmp", csnmp_shutdown);
1857 } /* void module_register */
1859 /*
1860 * vim: shiftwidth=2 softtabstop=2 tabstop=8 fdm=marker
1861 */