1 /**
2 * collectd - src/snmp.c
3 * Copyright (C) 2007-2012 Florian octo Forster
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; only version 2 of the License is applicable.
8 *
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17 *
18 * Authors:
19 * Florian octo Forster <octo at collectd.org>
20 **/
22 #include "collectd.h"
23 #include "common.h"
24 #include "plugin.h"
25 #include "utils_complain.h"
27 #include <pthread.h>
29 #include <net-snmp/net-snmp-config.h>
30 #include <net-snmp/net-snmp-includes.h>
32 /*
33 * Private data structes
34 */
35 struct oid_s
36 {
37 oid oid[MAX_OID_LEN];
38 size_t oid_len;
39 };
40 typedef struct oid_s oid_t;
42 union instance_u
43 {
44 char string[DATA_MAX_NAME_LEN];
45 oid_t oid;
46 };
47 typedef union instance_u instance_t;
49 struct data_definition_s
50 {
51 char *name; /* used to reference this from the `Collect' option */
52 char *type; /* used to find the data_set */
53 int is_table;
54 instance_t instance;
55 char *instance_prefix;
56 oid_t *values;
57 int values_len;
58 double scale;
59 double shift;
60 struct data_definition_s *next;
61 };
62 typedef struct data_definition_s data_definition_t;
64 struct host_definition_s
65 {
66 char *name;
67 char *address;
68 char *community;
69 int version;
70 void *sess_handle;
71 c_complain_t complaint;
72 uint32_t interval;
73 data_definition_t **data_list;
74 int data_list_len;
75 };
76 typedef struct host_definition_s host_definition_t;
78 /* These two types are used to cache values in `csnmp_read_table' to handle
79 * gaps in tables. */
80 struct csnmp_list_instances_s
81 {
82 oid_t suffix;
83 char instance[DATA_MAX_NAME_LEN];
84 struct csnmp_list_instances_s *next;
85 };
86 typedef struct csnmp_list_instances_s csnmp_list_instances_t;
88 struct csnmp_table_values_s
89 {
90 oid_t suffix;
91 value_t value;
92 struct csnmp_table_values_s *next;
93 };
94 typedef struct csnmp_table_values_s csnmp_table_values_t;
96 /*
97 * Private variables
98 */
99 static data_definition_t *data_head = NULL;
101 /*
102 * Prototypes
103 */
104 static int csnmp_read_host (user_data_t *ud);
106 /*
107 * Private functions
108 */
109 static void csnmp_oid_init (oid_t *dst, oid const *src, size_t n)
110 {
111 assert (n <= STATIC_ARRAY_LEN (dst->oid));
112 memcpy (dst->oid, src, sizeof (*src) * n);
113 dst->oid_len = n;
114 }
116 static int csnmp_oid_compare (oid_t const *left, oid_t const *right)
117 {
118 return (snmp_oid_compare (left->oid, left->oid_len,
119 right->oid, right->oid_len));
120 }
122 static int csnmp_oid_suffix (oid_t *dst, oid_t const *src,
123 oid_t const *root)
124 {
125 /* Make sure "src" is in "root"s subtree. */
126 if (src->oid_len <= root->oid_len)
127 return (EINVAL);
128 if (snmp_oid_ncompare (root->oid, root->oid_len,
129 src->oid, src->oid_len,
130 /* n = */ root->oid_len) != 0)
131 return (EINVAL);
133 memset (dst, 0, sizeof (*dst));
134 dst->oid_len = src->oid_len - root->oid_len;
135 memcpy (dst->oid, &src->oid[root->oid_len],
136 dst->oid_len * sizeof (dst->oid[0]));
137 return (0);
138 }
140 static int csnmp_oid_to_string (char *buffer, size_t buffer_size,
141 oid_t const *o)
142 {
143 char oid_str[MAX_OID_LEN][16];
144 char *oid_str_ptr[MAX_OID_LEN];
145 size_t i;
147 for (i = 0; i < o->oid_len; i++)
148 {
149 ssnprintf (oid_str[i], sizeof (oid_str[i]), "%lu", (unsigned long) o->oid[i]);
150 oid_str_ptr[i] = oid_str[i];
151 }
153 return (strjoin (buffer, buffer_size,
154 oid_str_ptr, o->oid_len, /* separator = */ "."));
155 }
157 static void csnmp_host_close_session (host_definition_t *host) /* {{{ */
158 {
159 if (host->sess_handle == NULL)
160 return;
162 snmp_sess_close (host->sess_handle);
163 host->sess_handle = NULL;
164 } /* }}} void csnmp_host_close_session */
166 static void csnmp_host_definition_destroy (void *arg) /* {{{ */
167 {
168 host_definition_t *hd;
170 hd = arg;
172 if (hd == NULL)
173 return;
175 if (hd->name != NULL)
176 {
177 DEBUG ("snmp plugin: Destroying host definition for host `%s'.",
178 hd->name);
179 }
181 csnmp_host_close_session (hd);
183 sfree (hd->name);
184 sfree (hd->address);
185 sfree (hd->community);
186 sfree (hd->data_list);
188 sfree (hd);
189 } /* }}} void csnmp_host_definition_destroy */
191 /* Many functions to handle the configuration. {{{ */
192 /* First there are many functions which do configuration stuff. It's a big
193 * bloated and messy, I'm afraid. */
195 /*
196 * Callgraph for the config stuff:
197 * csnmp_config
198 * +-> call_snmp_init_once
199 * +-> csnmp_config_add_data
200 * ! +-> csnmp_config_add_data_type
201 * ! +-> csnmp_config_add_data_table
202 * ! +-> csnmp_config_add_data_instance
203 * ! +-> csnmp_config_add_data_instance_prefix
204 * ! +-> csnmp_config_add_data_values
205 * +-> csnmp_config_add_host
206 * +-> csnmp_config_add_host_address
207 * +-> csnmp_config_add_host_community
208 * +-> csnmp_config_add_host_version
209 * +-> csnmp_config_add_host_collect
210 * +-> csnmp_config_add_host_interval
211 */
212 static void call_snmp_init_once (void)
213 {
214 static int have_init = 0;
216 if (have_init == 0)
217 init_snmp (PACKAGE_NAME);
218 have_init = 1;
219 } /* void call_snmp_init_once */
221 static int csnmp_config_add_data_type (data_definition_t *dd, oconfig_item_t *ci)
222 {
223 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
224 {
225 WARNING ("snmp plugin: `Type' needs exactly one string argument.");
226 return (-1);
227 }
229 sfree (dd->type);
230 dd->type = strdup (ci->values[0].value.string);
231 if (dd->type == NULL)
232 return (-1);
234 return (0);
235 } /* int csnmp_config_add_data_type */
237 static int csnmp_config_add_data_table (data_definition_t *dd, oconfig_item_t *ci)
238 {
239 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_BOOLEAN))
240 {
241 WARNING ("snmp plugin: `Table' needs exactly one boolean argument.");
242 return (-1);
243 }
245 dd->is_table = ci->values[0].value.boolean ? 1 : 0;
247 return (0);
248 } /* int csnmp_config_add_data_table */
250 static int csnmp_config_add_data_instance (data_definition_t *dd, oconfig_item_t *ci)
251 {
252 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
253 {
254 WARNING ("snmp plugin: `Instance' needs exactly one string argument.");
255 return (-1);
256 }
258 if (dd->is_table)
259 {
260 /* Instance is an OID */
261 dd->instance.oid.oid_len = MAX_OID_LEN;
263 if (!read_objid (ci->values[0].value.string,
264 dd->instance.oid.oid, &dd->instance.oid.oid_len))
265 {
266 ERROR ("snmp plugin: read_objid (%s) failed.",
267 ci->values[0].value.string);
268 return (-1);
269 }
270 }
271 else
272 {
273 /* Instance is a simple string */
274 sstrncpy (dd->instance.string, ci->values[0].value.string,
275 sizeof (dd->instance.string));
276 }
278 return (0);
279 } /* int csnmp_config_add_data_instance */
281 static int csnmp_config_add_data_instance_prefix (data_definition_t *dd,
282 oconfig_item_t *ci)
283 {
284 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
285 {
286 WARNING ("snmp plugin: `InstancePrefix' needs exactly one string argument.");
287 return (-1);
288 }
290 if (!dd->is_table)
291 {
292 WARNING ("snmp plugin: data %s: InstancePrefix is ignored when `Table' "
293 "is set to `false'.", dd->name);
294 return (-1);
295 }
297 sfree (dd->instance_prefix);
298 dd->instance_prefix = strdup (ci->values[0].value.string);
299 if (dd->instance_prefix == NULL)
300 return (-1);
302 return (0);
303 } /* int csnmp_config_add_data_instance_prefix */
305 static int csnmp_config_add_data_values (data_definition_t *dd, oconfig_item_t *ci)
306 {
307 int i;
309 if (ci->values_num < 1)
310 {
311 WARNING ("snmp plugin: `Values' needs at least one argument.");
312 return (-1);
313 }
315 for (i = 0; i < ci->values_num; i++)
316 if (ci->values[i].type != OCONFIG_TYPE_STRING)
317 {
318 WARNING ("snmp plugin: `Values' needs only string argument.");
319 return (-1);
320 }
322 sfree (dd->values);
323 dd->values_len = 0;
324 dd->values = (oid_t *) malloc (sizeof (oid_t) * ci->values_num);
325 if (dd->values == NULL)
326 return (-1);
327 dd->values_len = ci->values_num;
329 for (i = 0; i < ci->values_num; i++)
330 {
331 dd->values[i].oid_len = MAX_OID_LEN;
333 if (NULL == snmp_parse_oid (ci->values[i].value.string,
334 dd->values[i].oid, &dd->values[i].oid_len))
335 {
336 ERROR ("snmp plugin: snmp_parse_oid (%s) failed.",
337 ci->values[i].value.string);
338 free (dd->values);
339 dd->values = NULL;
340 dd->values_len = 0;
341 return (-1);
342 }
343 }
345 return (0);
346 } /* int csnmp_config_add_data_instance */
348 static int csnmp_config_add_data_shift (data_definition_t *dd, oconfig_item_t *ci)
349 {
350 if ((ci->values_num != 1)
351 || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
352 {
353 WARNING ("snmp plugin: The `Shift' config option needs exactly one number argument.");
354 return (-1);
355 }
357 dd->shift = ci->values[0].value.number;
359 return (0);
360 } /* int csnmp_config_add_data_shift */
362 static int csnmp_config_add_data_scale (data_definition_t *dd, oconfig_item_t *ci)
363 {
364 if ((ci->values_num != 1)
365 || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
366 {
367 WARNING ("snmp plugin: The `Scale' config option needs exactly one number argument.");
368 return (-1);
369 }
371 dd->scale = ci->values[0].value.number;
373 return (0);
374 } /* int csnmp_config_add_data_scale */
376 static int csnmp_config_add_data (oconfig_item_t *ci)
377 {
378 data_definition_t *dd;
379 int status = 0;
380 int i;
382 if ((ci->values_num != 1)
383 || (ci->values[0].type != OCONFIG_TYPE_STRING))
384 {
385 WARNING ("snmp plugin: The `Data' config option needs exactly one string argument.");
386 return (-1);
387 }
389 dd = (data_definition_t *) malloc (sizeof (data_definition_t));
390 if (dd == NULL)
391 return (-1);
392 memset (dd, '\0', sizeof (data_definition_t));
394 dd->name = strdup (ci->values[0].value.string);
395 if (dd->name == NULL)
396 {
397 free (dd);
398 return (-1);
399 }
400 dd->scale = 1.0;
401 dd->shift = 0.0;
403 for (i = 0; i < ci->children_num; i++)
404 {
405 oconfig_item_t *option = ci->children + i;
406 status = 0;
408 if (strcasecmp ("Type", option->key) == 0)
409 status = csnmp_config_add_data_type (dd, option);
410 else if (strcasecmp ("Table", option->key) == 0)
411 status = csnmp_config_add_data_table (dd, option);
412 else if (strcasecmp ("Instance", option->key) == 0)
413 status = csnmp_config_add_data_instance (dd, option);
414 else if (strcasecmp ("InstancePrefix", option->key) == 0)
415 status = csnmp_config_add_data_instance_prefix (dd, option);
416 else if (strcasecmp ("Values", option->key) == 0)
417 status = csnmp_config_add_data_values (dd, option);
418 else if (strcasecmp ("Shift", option->key) == 0)
419 status = csnmp_config_add_data_shift (dd, option);
420 else if (strcasecmp ("Scale", option->key) == 0)
421 status = csnmp_config_add_data_scale (dd, option);
422 else
423 {
424 WARNING ("snmp plugin: Option `%s' not allowed here.", option->key);
425 status = -1;
426 }
428 if (status != 0)
429 break;
430 } /* for (ci->children) */
432 while (status == 0)
433 {
434 if (dd->type == NULL)
435 {
436 WARNING ("snmp plugin: `Type' not given for data `%s'", dd->name);
437 status = -1;
438 break;
439 }
440 if (dd->values == NULL)
441 {
442 WARNING ("snmp plugin: No `Value' given for data `%s'", dd->name);
443 status = -1;
444 break;
445 }
447 break;
448 } /* while (status == 0) */
450 if (status != 0)
451 {
452 sfree (dd->name);
453 sfree (dd->instance_prefix);
454 sfree (dd->values);
455 sfree (dd);
456 return (-1);
457 }
459 DEBUG ("snmp plugin: dd = { name = %s, type = %s, is_table = %s, values_len = %i }",
460 dd->name, dd->type, (dd->is_table != 0) ? "true" : "false", dd->values_len);
462 if (data_head == NULL)
463 data_head = dd;
464 else
465 {
466 data_definition_t *last;
467 last = data_head;
468 while (last->next != NULL)
469 last = last->next;
470 last->next = dd;
471 }
473 return (0);
474 } /* int csnmp_config_add_data */
476 static int csnmp_config_add_host_address (host_definition_t *hd, oconfig_item_t *ci)
477 {
478 if ((ci->values_num != 1)
479 || (ci->values[0].type != OCONFIG_TYPE_STRING))
480 {
481 WARNING ("snmp plugin: The `Address' config option needs exactly one string argument.");
482 return (-1);
483 }
485 if (hd->address == NULL)
486 free (hd->address);
488 hd->address = strdup (ci->values[0].value.string);
489 if (hd->address == NULL)
490 return (-1);
492 DEBUG ("snmp plugin: host = %s; host->address = %s;",
493 hd->name, hd->address);
495 return (0);
496 } /* int csnmp_config_add_host_address */
498 static int csnmp_config_add_host_community (host_definition_t *hd, oconfig_item_t *ci)
499 {
500 if ((ci->values_num != 1)
501 || (ci->values[0].type != OCONFIG_TYPE_STRING))
502 {
503 WARNING ("snmp plugin: The `Community' config option needs exactly one string argument.");
504 return (-1);
505 }
507 if (hd->community == NULL)
508 free (hd->community);
510 hd->community = strdup (ci->values[0].value.string);
511 if (hd->community == NULL)
512 return (-1);
514 DEBUG ("snmp plugin: host = %s; host->community = %s;",
515 hd->name, hd->community);
517 return (0);
518 } /* int csnmp_config_add_host_community */
520 static int csnmp_config_add_host_version (host_definition_t *hd, oconfig_item_t *ci)
521 {
522 int version;
524 if ((ci->values_num != 1)
525 || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
526 {
527 WARNING ("snmp plugin: The `Version' config option needs exactly one number argument.");
528 return (-1);
529 }
531 version = (int) ci->values[0].value.number;
532 if ((version != 1) && (version != 2))
533 {
534 WARNING ("snmp plugin: `Version' must either be `1' or `2'.");
535 return (-1);
536 }
538 hd->version = version;
540 return (0);
541 } /* int csnmp_config_add_host_address */
543 static int csnmp_config_add_host_collect (host_definition_t *host,
544 oconfig_item_t *ci)
545 {
546 data_definition_t *data;
547 data_definition_t **data_list;
548 int data_list_len;
549 int i;
551 if (ci->values_num < 1)
552 {
553 WARNING ("snmp plugin: `Collect' needs at least one argument.");
554 return (-1);
555 }
557 for (i = 0; i < ci->values_num; i++)
558 if (ci->values[i].type != OCONFIG_TYPE_STRING)
559 {
560 WARNING ("snmp plugin: All arguments to `Collect' must be strings.");
561 return (-1);
562 }
564 data_list_len = host->data_list_len + ci->values_num;
565 data_list = (data_definition_t **) realloc (host->data_list,
566 sizeof (data_definition_t *) * data_list_len);
567 if (data_list == NULL)
568 return (-1);
569 host->data_list = data_list;
571 for (i = 0; i < ci->values_num; i++)
572 {
573 for (data = data_head; data != NULL; data = data->next)
574 if (strcasecmp (ci->values[i].value.string, data->name) == 0)
575 break;
577 if (data == NULL)
578 {
579 WARNING ("snmp plugin: No such data configured: `%s'",
580 ci->values[i].value.string);
581 continue;
582 }
584 DEBUG ("snmp plugin: Collect: host = %s, data[%i] = %s;",
585 host->name, host->data_list_len, data->name);
587 host->data_list[host->data_list_len] = data;
588 host->data_list_len++;
589 } /* for (values_num) */
591 return (0);
592 } /* int csnmp_config_add_host_collect */
594 static int csnmp_config_add_host_interval (host_definition_t *hd, oconfig_item_t *ci)
595 {
596 if ((ci->values_num != 1)
597 || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
598 {
599 WARNING ("snmp plugin: The `Interval' config option needs exactly one number argument.");
600 return (-1);
601 }
603 hd->interval = ci->values[0].value.number >= 0
604 ? (uint32_t) ci->values[0].value.number
605 : 0;
607 return (0);
608 } /* int csnmp_config_add_host_interval */
610 static int csnmp_config_add_host (oconfig_item_t *ci)
611 {
612 host_definition_t *hd;
613 int status = 0;
614 int i;
616 /* Registration stuff. */
617 char cb_name[DATA_MAX_NAME_LEN];
618 user_data_t cb_data;
619 struct timespec cb_interval;
621 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
622 {
623 WARNING ("snmp plugin: `Host' needs exactly one string argument.");
624 return (-1);
625 }
627 hd = (host_definition_t *) malloc (sizeof (host_definition_t));
628 if (hd == NULL)
629 return (-1);
630 memset (hd, '\0', sizeof (host_definition_t));
631 hd->version = 2;
632 C_COMPLAIN_INIT (&hd->complaint);
634 hd->name = strdup (ci->values[0].value.string);
635 if (hd->name == NULL)
636 {
637 free (hd);
638 return (-1);
639 }
641 hd->sess_handle = NULL;
642 hd->interval = 0;
644 for (i = 0; i < ci->children_num; i++)
645 {
646 oconfig_item_t *option = ci->children + i;
647 status = 0;
649 if (strcasecmp ("Address", option->key) == 0)
650 status = csnmp_config_add_host_address (hd, option);
651 else if (strcasecmp ("Community", option->key) == 0)
652 status = csnmp_config_add_host_community (hd, option);
653 else if (strcasecmp ("Version", option->key) == 0)
654 status = csnmp_config_add_host_version (hd, option);
655 else if (strcasecmp ("Collect", option->key) == 0)
656 csnmp_config_add_host_collect (hd, option);
657 else if (strcasecmp ("Interval", option->key) == 0)
658 csnmp_config_add_host_interval (hd, option);
659 else
660 {
661 WARNING ("snmp plugin: csnmp_config_add_host: Option `%s' not allowed here.", option->key);
662 status = -1;
663 }
665 if (status != 0)
666 break;
667 } /* for (ci->children) */
669 while (status == 0)
670 {
671 if (hd->address == NULL)
672 {
673 WARNING ("snmp plugin: `Address' not given for host `%s'", hd->name);
674 status = -1;
675 break;
676 }
677 if (hd->community == NULL)
678 {
679 WARNING ("snmp plugin: `Community' not given for host `%s'", hd->name);
680 status = -1;
681 break;
682 }
684 break;
685 } /* while (status == 0) */
687 if (status != 0)
688 {
689 csnmp_host_definition_destroy (hd);
690 return (-1);
691 }
693 DEBUG ("snmp plugin: hd = { name = %s, address = %s, community = %s, version = %i }",
694 hd->name, hd->address, hd->community, hd->version);
696 ssnprintf (cb_name, sizeof (cb_name), "snmp-%s", hd->name);
698 memset (&cb_data, 0, sizeof (cb_data));
699 cb_data.data = hd;
700 cb_data.free_func = csnmp_host_definition_destroy;
702 memset (&cb_interval, 0, sizeof (cb_interval));
703 if (hd->interval != 0)
704 cb_interval.tv_sec = (time_t) hd->interval;
706 status = plugin_register_complex_read (/* group = */ NULL, cb_name,
707 csnmp_read_host, /* interval = */ &cb_interval,
708 /* user_data = */ &cb_data);
709 if (status != 0)
710 {
711 ERROR ("snmp plugin: Registering complex read function failed.");
712 csnmp_host_definition_destroy (hd);
713 return (-1);
714 }
716 return (0);
717 } /* int csnmp_config_add_host */
719 static int csnmp_config (oconfig_item_t *ci)
720 {
721 int i;
723 call_snmp_init_once ();
725 for (i = 0; i < ci->children_num; i++)
726 {
727 oconfig_item_t *child = ci->children + i;
728 if (strcasecmp ("Data", child->key) == 0)
729 csnmp_config_add_data (child);
730 else if (strcasecmp ("Host", child->key) == 0)
731 csnmp_config_add_host (child);
732 else
733 {
734 WARNING ("snmp plugin: Ignoring unknown config option `%s'.", child->key);
735 }
736 } /* for (ci->children) */
738 return (0);
739 } /* int csnmp_config */
741 /* }}} End of the config stuff. Now the interesting part begins */
743 static void csnmp_host_open_session (host_definition_t *host)
744 {
745 struct snmp_session sess;
747 if (host->sess_handle != NULL)
748 csnmp_host_close_session (host);
750 snmp_sess_init (&sess);
751 sess.peername = host->address;
752 sess.community = (u_char *) host->community;
753 sess.community_len = strlen (host->community);
754 sess.version = (host->version == 1) ? SNMP_VERSION_1 : SNMP_VERSION_2c;
756 /* snmp_sess_open will copy the `struct snmp_session *'. */
757 host->sess_handle = snmp_sess_open (&sess);
759 if (host->sess_handle == NULL)
760 {
761 char *errstr = NULL;
763 snmp_error (&sess, NULL, NULL, &errstr);
765 ERROR ("snmp plugin: host %s: snmp_sess_open failed: %s",
766 host->name, (errstr == NULL) ? "Unknown problem" : errstr);
767 sfree (errstr);
768 }
769 } /* void csnmp_host_open_session */
771 /* TODO: Check if negative values wrap around. Problem: negative temperatures. */
772 static value_t csnmp_value_list_to_value (struct variable_list *vl, int type,
773 double scale, double shift)
774 {
775 value_t ret;
776 uint64_t tmp_unsigned = 0;
777 int64_t tmp_signed = 0;
778 _Bool defined = 1;
779 /* Set to true when the original SNMP type appears to have been signed. */
780 _Bool prefer_signed = 0;
782 if ((vl->type == ASN_INTEGER)
783 || (vl->type == ASN_UINTEGER)
784 || (vl->type == ASN_COUNTER)
785 #ifdef ASN_TIMETICKS
786 || (vl->type == ASN_TIMETICKS)
787 #endif
788 || (vl->type == ASN_GAUGE))
789 {
790 tmp_unsigned = (uint32_t) *vl->val.integer;
791 tmp_signed = (int32_t) *vl->val.integer;
793 if ((vl->type == ASN_INTEGER)
794 || (vl->type == ASN_GAUGE))
795 prefer_signed = 1;
797 DEBUG ("snmp plugin: Parsed int32 value is %"PRIu64".", tmp_unsigned);
798 }
799 else if (vl->type == ASN_COUNTER64)
800 {
801 tmp_unsigned = (uint32_t) vl->val.counter64->high;
802 tmp_unsigned = tmp_unsigned << 32;
803 tmp_unsigned += (uint32_t) vl->val.counter64->low;
804 tmp_signed = (int64_t) tmp_unsigned;
805 DEBUG ("snmp plugin: Parsed int64 value is %"PRIu64".", tmp_unsigned);
806 }
807 else if (vl->type == ASN_OCTET_STR)
808 {
809 /* We'll handle this later.. */
810 }
811 else
812 {
813 char oid_buffer[1024];
815 memset (oid_buffer, 0, sizeof (oid_buffer));
816 snprint_objid (oid_buffer, sizeof (oid_buffer) - 1,
817 vl->name, vl->name_length);
819 #ifdef ASN_NULL
820 if (vl->type == ASN_NULL)
821 INFO ("snmp plugin: OID \"%s\" is undefined (type ASN_NULL)",
822 oid_buffer);
823 else
824 #endif
825 WARNING ("snmp plugin: I don't know the ASN type \"%i\" (OID: %s)",
826 (int) vl->type, oid_buffer);
828 defined = 0;
829 }
831 if (vl->type == ASN_OCTET_STR)
832 {
833 int status = -1;
835 if (vl->val.string != NULL)
836 {
837 char string[64];
838 size_t string_length;
840 string_length = sizeof (string) - 1;
841 if (vl->val_len < string_length)
842 string_length = vl->val_len;
844 /* The strings we get from the Net-SNMP library may not be null
845 * terminated. That is why we're using `memcpy' here and not `strcpy'.
846 * `string_length' is set to `vl->val_len' which holds the length of the
847 * string. -octo */
848 memcpy (string, vl->val.string, string_length);
849 string[string_length] = 0;
851 status = parse_value (string, &ret, type);
852 if (status != 0)
853 {
854 ERROR ("snmp plugin: csnmp_value_list_to_value: Parsing string as %s failed: %s",
855 DS_TYPE_TO_STRING (type), string);
856 }
857 }
859 if (status != 0)
860 {
861 switch (type)
862 {
863 case DS_TYPE_COUNTER:
864 case DS_TYPE_DERIVE:
865 case DS_TYPE_ABSOLUTE:
866 memset (&ret, 0, sizeof (ret));
867 break;
869 case DS_TYPE_GAUGE:
870 ret.gauge = NAN;
871 break;
873 default:
874 ERROR ("snmp plugin: csnmp_value_list_to_value: Unknown "
875 "data source type: %i.", type);
876 ret.gauge = NAN;
877 }
878 }
879 } /* if (vl->type == ASN_OCTET_STR) */
880 else if (type == DS_TYPE_COUNTER)
881 {
882 ret.counter = tmp_unsigned;
883 }
884 else if (type == DS_TYPE_GAUGE)
885 {
886 if (!defined)
887 ret.gauge = NAN;
888 else if (prefer_signed)
889 ret.gauge = (scale * tmp_signed) + shift;
890 else
891 ret.gauge = (scale * tmp_unsigned) + shift;
892 }
893 else if (type == DS_TYPE_DERIVE)
894 {
895 if (prefer_signed)
896 ret.derive = (derive_t) tmp_signed;
897 else
898 ret.derive = (derive_t) tmp_unsigned;
899 }
900 else if (type == DS_TYPE_ABSOLUTE)
901 {
902 ret.absolute = (absolute_t) tmp_unsigned;
903 }
904 else
905 {
906 ERROR ("snmp plugin: csnmp_value_list_to_value: Unknown data source "
907 "type: %i.", type);
908 ret.gauge = NAN;
909 }
911 return (ret);
912 } /* value_t csnmp_value_list_to_value */
914 /* Returns true if all OIDs have left their subtree */
915 static int csnmp_check_res_left_subtree (const host_definition_t *host,
916 const data_definition_t *data,
917 struct snmp_pdu *res)
918 {
919 struct variable_list *vb;
920 int num_checked;
921 int num_left_subtree;
922 int i;
924 vb = res->variables;
925 if (vb == NULL)
926 return (-1);
928 num_checked = 0;
929 num_left_subtree = 0;
931 /* check all the variables and count how many have left their subtree */
932 for (vb = res->variables, i = 0;
933 (vb != NULL) && (i < data->values_len);
934 vb = vb->next_variable, i++)
935 {
936 num_checked++;
938 if ((vb->type == SNMP_ENDOFMIBVIEW)
939 || (snmp_oid_ncompare (data->values[i].oid,
940 data->values[i].oid_len,
941 vb->name, vb->name_length,
942 data->values[i].oid_len) != 0))
943 num_left_subtree++;
944 }
946 /* check if enough variables have been returned */
947 if (i < data->values_len)
948 {
949 ERROR ("snmp plugin: host %s: Expected %i variables, but got only %i",
950 host->name, data->values_len, i);
951 return (-1);
952 }
954 if (data->instance.oid.oid_len > 0)
955 {
956 if (vb == NULL)
957 {
958 ERROR ("snmp plugin: host %s: Expected one more variable for "
959 "the instance..", host->name);
960 return (-1);
961 }
963 num_checked++;
964 if (snmp_oid_ncompare (data->instance.oid.oid,
965 data->instance.oid.oid_len,
966 vb->name, vb->name_length,
967 data->instance.oid.oid_len) != 0)
968 num_left_subtree++;
969 }
971 DEBUG ("snmp plugin: csnmp_check_res_left_subtree: %i of %i variables have "
972 "left their subtree",
973 num_left_subtree, num_checked);
974 if (num_left_subtree >= num_checked)
975 return (1);
976 return (0);
977 } /* int csnmp_check_res_left_subtree */
979 static int csnmp_strvbcopy_hexstring (char *dst, /* {{{ */
980 const struct variable_list *vb, size_t dst_size)
981 {
982 char *buffer_ptr;
983 size_t buffer_free;
984 size_t i;
986 buffer_ptr = dst;
987 buffer_free = dst_size;
989 for (i = 0; i < vb->val_len; i++)
990 {
991 int status;
993 status = snprintf (buffer_ptr, buffer_free,
994 (i == 0) ? "%02x" : ":%02x", (unsigned int) vb->val.bitstring[i]);
996 if (status >= buffer_free)
997 {
998 buffer_ptr += (buffer_free - 1);
999 *buffer_ptr = 0;
1000 return (dst_size + (buffer_free - status));
1001 }
1002 else /* if (status < buffer_free) */
1003 {
1004 buffer_ptr += status;
1005 buffer_free -= status;
1006 }
1007 }
1009 return ((int) (dst_size - buffer_free));
1010 } /* }}} int csnmp_strvbcopy_hexstring */
1012 static int csnmp_strvbcopy (char *dst, /* {{{ */
1013 const struct variable_list *vb, size_t dst_size)
1014 {
1015 char *src;
1016 size_t num_chars;
1017 size_t i;
1019 if (vb->type == ASN_OCTET_STR)
1020 src = (char *) vb->val.string;
1021 else if (vb->type == ASN_BIT_STR)
1022 src = (char *) vb->val.bitstring;
1023 else
1024 {
1025 dst[0] = 0;
1026 return (EINVAL);
1027 }
1029 num_chars = dst_size - 1;
1030 if (num_chars > vb->val_len)
1031 num_chars = vb->val_len;
1033 for (i = 0; i < num_chars; i++)
1034 {
1035 /* Check for control characters. */
1036 if ((unsigned char)src[i] < 32)
1037 return (csnmp_strvbcopy_hexstring (dst, vb, dst_size));
1038 dst[i] = src[i];
1039 }
1040 dst[num_chars] = 0;
1042 return ((int) vb->val_len);
1043 } /* }}} int csnmp_strvbcopy */
1045 static int csnmp_instance_list_add (csnmp_list_instances_t **head,
1046 csnmp_list_instances_t **tail,
1047 struct snmp_pdu const *res,
1048 oid_t const *root)
1049 {
1050 csnmp_list_instances_t *il;
1051 struct variable_list *vb;
1052 oid_t vb_name;
1053 int status;
1055 /* Set vb on the last variable */
1056 for (vb = res->variables;
1057 (vb != NULL) && (vb->next_variable != NULL);
1058 vb = vb->next_variable)
1059 /* do nothing */;
1060 if (vb == NULL)
1061 return (-1);
1063 csnmp_oid_init (&vb_name, vb->name, vb->name_length);
1065 il = malloc (sizeof (*il));
1066 if (il == NULL)
1067 {
1068 ERROR ("snmp plugin: malloc failed.");
1069 return (-1);
1070 }
1071 memset (il, 0, sizeof (*il));
1072 il->next = NULL;
1074 status = csnmp_oid_suffix (&il->suffix, &vb_name, root);
1075 if (status != 0)
1076 {
1077 sfree (il);
1078 return (status);
1079 }
1081 /* Get instance name */
1082 if ((vb->type == ASN_OCTET_STR) || (vb->type == ASN_BIT_STR))
1083 {
1084 char *ptr;
1086 csnmp_strvbcopy (il->instance, vb, sizeof (il->instance));
1088 for (ptr = il->instance; *ptr != '\0'; ptr++)
1089 {
1090 if ((*ptr > 0) && (*ptr < 32))
1091 *ptr = ' ';
1092 else if (*ptr == '/')
1093 *ptr = '_';
1094 }
1095 DEBUG ("snmp plugin: il->instance = `%s';", il->instance);
1096 }
1097 else
1098 {
1099 value_t val = csnmp_value_list_to_value (vb, DS_TYPE_COUNTER, 1.0, 0.0);
1100 ssnprintf (il->instance, sizeof (il->instance),
1101 "%llu", val.counter);
1102 }
1104 /* TODO: Debugging output */
1106 if (*head == NULL)
1107 *head = il;
1108 else
1109 (*tail)->next = il;
1110 *tail = il;
1112 return (0);
1113 } /* int csnmp_instance_list_add */
1115 static int csnmp_dispatch_table (host_definition_t *host, data_definition_t *data,
1116 csnmp_list_instances_t *instance_list,
1117 csnmp_table_values_t **value_table)
1118 {
1119 const data_set_t *ds;
1120 value_list_t vl = VALUE_LIST_INIT;
1122 csnmp_list_instances_t *instance_list_ptr;
1123 csnmp_table_values_t **value_table_ptr;
1125 int i;
1126 _Bool have_more;
1127 oid_t current_suffix;
1129 ds = plugin_get_ds (data->type);
1130 if (!ds)
1131 {
1132 ERROR ("snmp plugin: DataSet `%s' not defined.", data->type);
1133 return (-1);
1134 }
1135 assert (ds->ds_num == data->values_len);
1137 instance_list_ptr = instance_list;
1139 value_table_ptr = malloc (sizeof (*value_table_ptr) * data->values_len);
1140 if (value_table_ptr == NULL)
1141 return (-1);
1142 for (i = 0; i < data->values_len; i++)
1143 value_table_ptr[i] = value_table[i];
1145 vl.values_len = ds->ds_num;
1146 vl.values = malloc (sizeof (*vl.values) * vl.values_len);
1147 if (vl.values == NULL)
1148 {
1149 ERROR ("snmp plugin: malloc failed.");
1150 sfree (value_table_ptr);
1151 return (-1);
1152 }
1154 sstrncpy (vl.host, host->name, sizeof (vl.host));
1155 sstrncpy (vl.plugin, "snmp", sizeof (vl.plugin));
1157 vl.interval = host->interval;
1159 have_more = 1;
1160 memset (¤t_suffix, 0, sizeof (current_suffix));
1161 while (have_more)
1162 {
1163 _Bool suffix_skipped = 0;
1165 /* Determine next suffix to handle. */
1166 if (instance_list != NULL)
1167 {
1168 if (instance_list_ptr == NULL)
1169 {
1170 have_more = 0;
1171 continue;
1172 }
1174 memcpy (¤t_suffix, &instance_list_ptr->suffix, sizeof (current_suffix));
1175 }
1176 else /* no instance configured */
1177 {
1178 csnmp_table_values_t *ptr = value_table_ptr[0];
1179 if (ptr == NULL)
1180 {
1181 have_more = 0;
1182 continue;
1183 }
1185 memcpy (¤t_suffix, &ptr->suffix, sizeof (current_suffix));
1186 }
1188 /* Update all the value_table_ptr to point at the entry with the same
1189 * trailing partial OID */
1190 for (i = 0; i < data->values_len; i++)
1191 {
1192 while ((value_table_ptr[i] != NULL)
1193 && (csnmp_oid_compare (&value_table_ptr[i]->suffix, ¤t_suffix) < 0))
1194 value_table_ptr[i] = value_table_ptr[i]->next;
1196 if (value_table_ptr[i] == NULL)
1197 {
1198 have_more = 0;
1199 break;
1200 }
1201 else if (csnmp_oid_compare (&value_table_ptr[i]->suffix, ¤t_suffix) > 0)
1202 {
1203 /* This suffix is missing in the subtree. Indicate this with the
1204 * "suffix_skipped" flag and try the next instance / suffix. */
1205 suffix_skipped = 1;
1206 break;
1207 }
1208 } /* for (i = 0; i < columns; i++) */
1210 if (!have_more)
1211 break;
1213 /* Matching the values failed. Start from the beginning again. */
1214 if (suffix_skipped)
1215 {
1216 if (instance_list != NULL)
1217 instance_list_ptr = instance_list_ptr->next;
1218 else
1219 value_table_ptr[0] = value_table_ptr[0]->next;
1221 continue;
1222 }
1224 /* if we reach this line, all value_table_ptr[i] are non-NULL and are set
1225 * to the same subid. instance_list_ptr is either NULL or points to the
1226 * same subid, too. */
1227 #if COLLECT_DEBUG
1228 for (i = 1; i < data->values_len; i++)
1229 {
1230 assert (value_table_ptr[i] != NULL);
1231 assert (csnmp_oid_compare (&value_table_ptr[i-1]->suffix,
1232 &value_table_ptr[i]->suffix) == 0);
1233 }
1234 assert ((instance_list_ptr == NULL)
1235 || (csnmp_oid_compare (&instance_list_ptr->suffix,
1236 &value_table_ptr[0]->suffix) == 0));
1237 #endif
1239 sstrncpy (vl.type, data->type, sizeof (vl.type));
1241 {
1242 char temp[DATA_MAX_NAME_LEN];
1244 if (instance_list_ptr == NULL)
1245 csnmp_oid_to_string (temp, sizeof (temp), ¤t_suffix);
1246 else
1247 sstrncpy (temp, instance_list_ptr->instance, sizeof (temp));
1249 if (data->instance_prefix == NULL)
1250 sstrncpy (vl.type_instance, temp, sizeof (vl.type_instance));
1251 else
1252 ssnprintf (vl.type_instance, sizeof (vl.type_instance), "%s%s",
1253 data->instance_prefix, temp);
1254 }
1256 for (i = 0; i < data->values_len; i++)
1257 vl.values[i] = value_table_ptr[i]->value;
1259 /* If we get here `vl.type_instance' and all `vl.values' have been set */
1260 plugin_dispatch_values (&vl);
1262 if (instance_list != NULL)
1263 instance_list_ptr = instance_list_ptr->next;
1264 else
1265 value_table_ptr[0] = value_table_ptr[0]->next;
1266 } /* while (have_more) */
1268 sfree (vl.values);
1269 sfree (value_table_ptr);
1271 return (0);
1272 } /* int csnmp_dispatch_table */
1274 static int csnmp_read_table (host_definition_t *host, data_definition_t *data)
1275 {
1276 struct snmp_pdu *req;
1277 struct snmp_pdu *res;
1278 struct variable_list *vb;
1280 const data_set_t *ds;
1281 oid_t *oid_list;
1282 uint32_t oid_list_len;
1284 int status;
1285 int i;
1287 /* `value_list_head' and `value_list_tail' implement a linked list for each
1288 * value. `instance_list_head' and `instance_list_tail' implement a linked list of
1289 * instance names. This is used to jump gaps in the table. */
1290 csnmp_list_instances_t *instance_list_head;
1291 csnmp_list_instances_t *instance_list_tail;
1292 csnmp_table_values_t **value_list_head;
1293 csnmp_table_values_t **value_list_tail;
1295 DEBUG ("snmp plugin: csnmp_read_table (host = %s, data = %s)",
1296 host->name, data->name);
1298 if (host->sess_handle == NULL)
1299 {
1300 DEBUG ("snmp plugin: csnmp_read_table: host->sess_handle == NULL");
1301 return (-1);
1302 }
1304 ds = plugin_get_ds (data->type);
1305 if (!ds)
1306 {
1307 ERROR ("snmp plugin: DataSet `%s' not defined.", data->type);
1308 return (-1);
1309 }
1311 if (ds->ds_num != data->values_len)
1312 {
1313 ERROR ("snmp plugin: DataSet `%s' requires %i values, but config talks about %i",
1314 data->type, ds->ds_num, data->values_len);
1315 return (-1);
1316 }
1318 /* We need a copy of all the OIDs, because GETNEXT will destroy them. */
1319 oid_list_len = data->values_len + 1;
1320 oid_list = (oid_t *) malloc (sizeof (oid_t) * (oid_list_len));
1321 if (oid_list == NULL)
1322 {
1323 ERROR ("snmp plugin: csnmp_read_table: malloc failed.");
1324 return (-1);
1325 }
1326 memcpy (oid_list, data->values, data->values_len * sizeof (oid_t));
1327 if (data->instance.oid.oid_len > 0)
1328 memcpy (oid_list + data->values_len, &data->instance.oid, sizeof (oid_t));
1329 else
1330 oid_list_len--;
1332 /* We're going to construct n linked lists, one for each "value".
1333 * value_list_head will contain pointers to the heads of these linked lists,
1334 * value_list_tail will contain pointers to the tail of the lists. */
1335 value_list_head = calloc (data->values_len, sizeof (*value_list_head));
1336 value_list_tail = calloc (data->values_len, sizeof (*value_list_tail));
1337 if ((value_list_head == NULL) || (value_list_tail == NULL))
1338 {
1339 ERROR ("snmp plugin: csnmp_read_table: calloc failed.");
1340 sfree (oid_list);
1341 sfree (value_list_head);
1342 sfree (value_list_tail);
1343 return (-1);
1344 }
1346 instance_list_head = NULL;
1347 instance_list_tail = NULL;
1349 status = 0;
1350 while (status == 0)
1351 {
1352 req = snmp_pdu_create (SNMP_MSG_GETNEXT);
1353 if (req == NULL)
1354 {
1355 ERROR ("snmp plugin: snmp_pdu_create failed.");
1356 status = -1;
1357 break;
1358 }
1360 for (i = 0; (uint32_t) i < oid_list_len; i++)
1361 snmp_add_null_var (req, oid_list[i].oid, oid_list[i].oid_len);
1363 res = NULL;
1364 status = snmp_sess_synch_response (host->sess_handle, req, &res);
1366 if ((status != STAT_SUCCESS) || (res == NULL))
1367 {
1368 char *errstr = NULL;
1370 snmp_sess_error (host->sess_handle, NULL, NULL, &errstr);
1372 c_complain (LOG_ERR, &host->complaint,
1373 "snmp plugin: host %s: snmp_sess_synch_response failed: %s",
1374 host->name, (errstr == NULL) ? "Unknown problem" : errstr);
1376 if (res != NULL)
1377 snmp_free_pdu (res);
1378 res = NULL;
1380 sfree (errstr);
1381 csnmp_host_close_session (host);
1383 status = -1;
1384 break;
1385 }
1386 status = 0;
1387 assert (res != NULL);
1388 c_release (LOG_INFO, &host->complaint,
1389 "snmp plugin: host %s: snmp_sess_synch_response successful.",
1390 host->name);
1392 vb = res->variables;
1393 if (vb == NULL)
1394 {
1395 status = -1;
1396 break;
1397 }
1399 /* Check if all values (and possibly the instance) have left their
1400 * subtree */
1401 if (csnmp_check_res_left_subtree (host, data, res) != 0)
1402 {
1403 status = 0;
1404 break;
1405 }
1407 /* Copy the OID of the value used as instance to oid_list, if an instance
1408 * is configured. */
1409 if (data->instance.oid.oid_len > 0)
1410 {
1411 /* Allocate a new `csnmp_list_instances_t', insert the instance name and
1412 * add it to the list */
1413 if (csnmp_instance_list_add (&instance_list_head, &instance_list_tail,
1414 res, &data->instance.oid) != 0)
1415 {
1416 ERROR ("snmp plugin: csnmp_instance_list_add failed.");
1417 status = -1;
1418 break;
1419 }
1421 /* The instance OID is added to the list of OIDs to GET from the
1422 * snmp agent last, so set vb on the last variable returned and copy
1423 * that OID. */
1424 for (vb = res->variables;
1425 (vb != NULL) && (vb->next_variable != NULL);
1426 vb = vb->next_variable)
1427 /* do nothing */;
1428 assert (vb != NULL);
1430 /* Copy the OID of the instance value to oid_list[data->values_len].
1431 * "oid_list" is used for the next GETNEXT request. */
1432 memcpy (oid_list[data->values_len].oid, vb->name,
1433 sizeof (oid) * vb->name_length);
1434 oid_list[data->values_len].oid_len = vb->name_length;
1435 }
1437 /* Iterate over all the (non-instance) values returned by the agent. The
1438 * (i < value_len) check will make sure we're not handling the instance OID
1439 * twice. */
1440 for (vb = res->variables, i = 0;
1441 (vb != NULL) && (i < data->values_len);
1442 vb = vb->next_variable, i++)
1443 {
1444 csnmp_table_values_t *vt;
1445 oid_t vb_name;
1446 oid_t suffix;
1448 csnmp_oid_init (&vb_name, vb->name, vb->name_length);
1450 /* Calculate the current suffix. This is later used to check that the
1451 * suffix is increasing. This also checks if we left the subtree */
1452 status = csnmp_oid_suffix (&suffix, &vb_name, data->values + i);
1453 if (status != 0)
1454 {
1455 DEBUG ("snmp plugin: host = %s; data = %s; Value %i failed. "
1456 "It probably left its subtree.",
1457 host->name, data->name, i);
1458 continue;
1459 }
1461 /* Make sure the OIDs returned by the agent are increasing. Otherwise our
1462 * table matching algorithm will get confused. */
1463 if ((value_list_tail[i] != NULL)
1464 && (csnmp_oid_compare (&suffix, &value_list_tail[i]->suffix) <= 0))
1465 {
1466 DEBUG ("snmp plugin: host = %s; data = %s; i = %i; "
1467 "Suffix is not increasing.",
1468 host->name, data->name, i);
1469 continue;
1470 }
1472 vt = malloc (sizeof (*vt));
1473 if (vt == NULL)
1474 {
1475 ERROR ("snmp plugin: malloc failed.");
1476 status = -1;
1477 break;
1478 }
1479 memset (vt, 0, sizeof (*vt));
1481 vt->value = csnmp_value_list_to_value (vb, ds->ds[i].type,
1482 data->scale, data->shift);
1483 memcpy (&vt->suffix, &suffix, sizeof (vt->suffix));
1484 vt->next = NULL;
1486 if (value_list_tail[i] == NULL)
1487 value_list_head[i] = vt;
1488 else
1489 value_list_tail[i]->next = vt;
1490 value_list_tail[i] = vt;
1492 /* Copy OID to oid_list[i + 1] */
1493 memcpy (oid_list[i].oid, vb->name, sizeof (oid) * vb->name_length);
1494 oid_list[i].oid_len = vb->name_length;
1495 } /* for (i = data->values_len) */
1497 if (res != NULL)
1498 snmp_free_pdu (res);
1499 res = NULL;
1500 } /* while (status == 0) */
1502 if (res != NULL)
1503 snmp_free_pdu (res);
1504 res = NULL;
1506 if (status == 0)
1507 csnmp_dispatch_table (host, data, instance_list_head, value_list_head);
1509 /* Free all allocated variables here */
1510 while (instance_list_head != NULL)
1511 {
1512 csnmp_list_instances_t *next = instance_list_head->next;
1513 sfree (instance_list_head);
1514 instance_list_head = next;
1515 }
1517 for (i = 0; i < data->values_len; i++)
1518 {
1519 while (value_list_head[i] != NULL)
1520 {
1521 csnmp_table_values_t *next = value_list_head[i]->next;
1522 sfree (value_list_head[i]);
1523 value_list_head[i] = next;
1524 }
1525 }
1527 sfree (value_list_head);
1528 sfree (value_list_tail);
1529 sfree (oid_list);
1531 return (0);
1532 } /* int csnmp_read_table */
1534 static int csnmp_read_value (host_definition_t *host, data_definition_t *data)
1535 {
1536 struct snmp_pdu *req;
1537 struct snmp_pdu *res;
1538 struct variable_list *vb;
1540 const data_set_t *ds;
1541 value_list_t vl = VALUE_LIST_INIT;
1543 int status;
1544 int i;
1546 DEBUG ("snmp plugin: csnmp_read_value (host = %s, data = %s)",
1547 host->name, data->name);
1549 if (host->sess_handle == NULL)
1550 {
1551 DEBUG ("snmp plugin: csnmp_read_table: host->sess_handle == NULL");
1552 return (-1);
1553 }
1555 ds = plugin_get_ds (data->type);
1556 if (!ds)
1557 {
1558 ERROR ("snmp plugin: DataSet `%s' not defined.", data->type);
1559 return (-1);
1560 }
1562 if (ds->ds_num != data->values_len)
1563 {
1564 ERROR ("snmp plugin: DataSet `%s' requires %i values, but config talks about %i",
1565 data->type, ds->ds_num, data->values_len);
1566 return (-1);
1567 }
1569 vl.values_len = ds->ds_num;
1570 vl.values = (value_t *) malloc (sizeof (value_t) * vl.values_len);
1571 if (vl.values == NULL)
1572 return (-1);
1573 for (i = 0; i < vl.values_len; i++)
1574 {
1575 if (ds->ds[i].type == DS_TYPE_COUNTER)
1576 vl.values[i].counter = 0;
1577 else
1578 vl.values[i].gauge = NAN;
1579 }
1581 sstrncpy (vl.host, host->name, sizeof (vl.host));
1582 sstrncpy (vl.plugin, "snmp", sizeof (vl.plugin));
1583 sstrncpy (vl.type, data->type, sizeof (vl.type));
1584 sstrncpy (vl.type_instance, data->instance.string, sizeof (vl.type_instance));
1586 vl.interval = host->interval;
1588 req = snmp_pdu_create (SNMP_MSG_GET);
1589 if (req == NULL)
1590 {
1591 ERROR ("snmp plugin: snmp_pdu_create failed.");
1592 sfree (vl.values);
1593 return (-1);
1594 }
1596 for (i = 0; i < data->values_len; i++)
1597 snmp_add_null_var (req, data->values[i].oid, data->values[i].oid_len);
1599 res = NULL;
1600 status = snmp_sess_synch_response (host->sess_handle, req, &res);
1602 if ((status != STAT_SUCCESS) || (res == NULL))
1603 {
1604 char *errstr = NULL;
1606 snmp_sess_error (host->sess_handle, NULL, NULL, &errstr);
1607 ERROR ("snmp plugin: host %s: snmp_sess_synch_response failed: %s",
1608 host->name, (errstr == NULL) ? "Unknown problem" : errstr);
1610 if (res != NULL)
1611 snmp_free_pdu (res);
1612 res = NULL;
1614 sfree (errstr);
1615 csnmp_host_close_session (host);
1617 return (-1);
1618 }
1621 for (vb = res->variables; vb != NULL; vb = vb->next_variable)
1622 {
1623 #if COLLECT_DEBUG
1624 char buffer[1024];
1625 snprint_variable (buffer, sizeof (buffer),
1626 vb->name, vb->name_length, vb);
1627 DEBUG ("snmp plugin: Got this variable: %s", buffer);
1628 #endif /* COLLECT_DEBUG */
1630 for (i = 0; i < data->values_len; i++)
1631 if (snmp_oid_compare (data->values[i].oid, data->values[i].oid_len,
1632 vb->name, vb->name_length) == 0)
1633 vl.values[i] = csnmp_value_list_to_value (vb, ds->ds[i].type,
1634 data->scale, data->shift);
1635 } /* for (res->variables) */
1637 if (res != NULL)
1638 snmp_free_pdu (res);
1639 res = NULL;
1641 DEBUG ("snmp plugin: -> plugin_dispatch_values (&vl);");
1642 plugin_dispatch_values (&vl);
1643 sfree (vl.values);
1645 return (0);
1646 } /* int csnmp_read_value */
1648 static int csnmp_read_host (user_data_t *ud)
1649 {
1650 host_definition_t *host;
1651 time_t time_start;
1652 time_t time_end;
1653 int status;
1654 int success;
1655 int i;
1657 host = ud->data;
1659 if (host->interval == 0)
1660 host->interval = interval_g;
1662 time_start = time (NULL);
1663 DEBUG ("snmp plugin: csnmp_read_host (%s) started at %u;", host->name,
1664 (unsigned int) time_start);
1666 if (host->sess_handle == NULL)
1667 csnmp_host_open_session (host);
1669 if (host->sess_handle == NULL)
1670 return (-1);
1672 success = 0;
1673 for (i = 0; i < host->data_list_len; i++)
1674 {
1675 data_definition_t *data = host->data_list[i];
1677 if (data->is_table)
1678 status = csnmp_read_table (host, data);
1679 else
1680 status = csnmp_read_value (host, data);
1682 if (status == 0)
1683 success++;
1684 }
1686 time_end = time (NULL);
1687 DEBUG ("snmp plugin: csnmp_read_host (%s) finished at %u;", host->name,
1688 (unsigned int) time_end);
1689 if ((uint32_t) (time_end - time_start) > host->interval)
1690 {
1691 WARNING ("snmp plugin: Host `%s' should be queried every %"PRIu32
1692 " seconds, but reading all values takes %u seconds.",
1693 host->name, host->interval, (unsigned int) (time_end - time_start));
1694 }
1696 if (success == 0)
1697 return (-1);
1699 return (0);
1700 } /* int csnmp_read_host */
1702 static int csnmp_init (void)
1703 {
1704 call_snmp_init_once ();
1706 return (0);
1707 } /* int csnmp_init */
1709 static int csnmp_shutdown (void)
1710 {
1711 data_definition_t *data_this;
1712 data_definition_t *data_next;
1714 /* When we get here, the read threads have been stopped and all the
1715 * `host_definition_t' will be freed. */
1716 DEBUG ("snmp plugin: Destroying all data definitions.");
1718 data_this = data_head;
1719 data_head = NULL;
1720 while (data_this != NULL)
1721 {
1722 data_next = data_this->next;
1724 sfree (data_this->name);
1725 sfree (data_this->type);
1726 sfree (data_this->values);
1727 sfree (data_this);
1729 data_this = data_next;
1730 }
1732 return (0);
1733 } /* int csnmp_shutdown */
1735 void module_register (void)
1736 {
1737 plugin_register_complex_config ("snmp", csnmp_config);
1738 plugin_register_init ("snmp", csnmp_init);
1739 plugin_register_shutdown ("snmp", csnmp_shutdown);
1740 } /* void module_register */
1742 /*
1743 * vim: shiftwidth=2 softtabstop=2 tabstop=8 fdm=marker
1744 */