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 cdtime_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_SIZE (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 */
211 static void call_snmp_init_once (void)
212 {
213 static int have_init = 0;
215 if (have_init == 0)
216 init_snmp (PACKAGE_NAME);
217 have_init = 1;
218 } /* void call_snmp_init_once */
220 static int csnmp_config_add_data_type (data_definition_t *dd, oconfig_item_t *ci)
221 {
222 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
223 {
224 WARNING ("snmp plugin: `Type' needs exactly one string argument.");
225 return (-1);
226 }
228 sfree (dd->type);
229 dd->type = strdup (ci->values[0].value.string);
230 if (dd->type == NULL)
231 return (-1);
233 return (0);
234 } /* int csnmp_config_add_data_type */
236 static int csnmp_config_add_data_table (data_definition_t *dd, oconfig_item_t *ci)
237 {
238 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_BOOLEAN))
239 {
240 WARNING ("snmp plugin: `Table' needs exactly one boolean argument.");
241 return (-1);
242 }
244 dd->is_table = ci->values[0].value.boolean ? 1 : 0;
246 return (0);
247 } /* int csnmp_config_add_data_table */
249 static int csnmp_config_add_data_instance (data_definition_t *dd, oconfig_item_t *ci)
250 {
251 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
252 {
253 WARNING ("snmp plugin: `Instance' needs exactly one string argument.");
254 return (-1);
255 }
257 if (dd->is_table)
258 {
259 /* Instance is an OID */
260 dd->instance.oid.oid_len = MAX_OID_LEN;
262 if (!read_objid (ci->values[0].value.string,
263 dd->instance.oid.oid, &dd->instance.oid.oid_len))
264 {
265 ERROR ("snmp plugin: read_objid (%s) failed.",
266 ci->values[0].value.string);
267 return (-1);
268 }
269 }
270 else
271 {
272 /* Instance is a simple string */
273 sstrncpy (dd->instance.string, ci->values[0].value.string,
274 sizeof (dd->instance.string));
275 }
277 return (0);
278 } /* int csnmp_config_add_data_instance */
280 static int csnmp_config_add_data_instance_prefix (data_definition_t *dd,
281 oconfig_item_t *ci)
282 {
283 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
284 {
285 WARNING ("snmp plugin: `InstancePrefix' needs exactly one string argument.");
286 return (-1);
287 }
289 if (!dd->is_table)
290 {
291 WARNING ("snmp plugin: data %s: InstancePrefix is ignored when `Table' "
292 "is set to `false'.", dd->name);
293 return (-1);
294 }
296 sfree (dd->instance_prefix);
297 dd->instance_prefix = strdup (ci->values[0].value.string);
298 if (dd->instance_prefix == NULL)
299 return (-1);
301 return (0);
302 } /* int csnmp_config_add_data_instance_prefix */
304 static int csnmp_config_add_data_values (data_definition_t *dd, oconfig_item_t *ci)
305 {
306 int i;
308 if (ci->values_num < 1)
309 {
310 WARNING ("snmp plugin: `Values' needs at least one argument.");
311 return (-1);
312 }
314 for (i = 0; i < ci->values_num; i++)
315 if (ci->values[i].type != OCONFIG_TYPE_STRING)
316 {
317 WARNING ("snmp plugin: `Values' needs only string argument.");
318 return (-1);
319 }
321 sfree (dd->values);
322 dd->values_len = 0;
323 dd->values = (oid_t *) malloc (sizeof (oid_t) * ci->values_num);
324 if (dd->values == NULL)
325 return (-1);
326 dd->values_len = ci->values_num;
328 for (i = 0; i < ci->values_num; i++)
329 {
330 dd->values[i].oid_len = MAX_OID_LEN;
332 if (NULL == snmp_parse_oid (ci->values[i].value.string,
333 dd->values[i].oid, &dd->values[i].oid_len))
334 {
335 ERROR ("snmp plugin: snmp_parse_oid (%s) failed.",
336 ci->values[i].value.string);
337 free (dd->values);
338 dd->values = NULL;
339 dd->values_len = 0;
340 return (-1);
341 }
342 }
344 return (0);
345 } /* int csnmp_config_add_data_instance */
347 static int csnmp_config_add_data_shift (data_definition_t *dd, oconfig_item_t *ci)
348 {
349 if ((ci->values_num != 1)
350 || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
351 {
352 WARNING ("snmp plugin: The `Shift' config option needs exactly one number argument.");
353 return (-1);
354 }
356 dd->shift = ci->values[0].value.number;
358 return (0);
359 } /* int csnmp_config_add_data_shift */
361 static int csnmp_config_add_data_scale (data_definition_t *dd, oconfig_item_t *ci)
362 {
363 if ((ci->values_num != 1)
364 || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
365 {
366 WARNING ("snmp plugin: The `Scale' config option needs exactly one number argument.");
367 return (-1);
368 }
370 dd->scale = ci->values[0].value.number;
372 return (0);
373 } /* int csnmp_config_add_data_scale */
375 static int csnmp_config_add_data (oconfig_item_t *ci)
376 {
377 data_definition_t *dd;
378 int status = 0;
379 int i;
381 if ((ci->values_num != 1)
382 || (ci->values[0].type != OCONFIG_TYPE_STRING))
383 {
384 WARNING ("snmp plugin: The `Data' config option needs exactly one string argument.");
385 return (-1);
386 }
388 dd = (data_definition_t *) malloc (sizeof (data_definition_t));
389 if (dd == NULL)
390 return (-1);
391 memset (dd, '\0', sizeof (data_definition_t));
393 dd->name = strdup (ci->values[0].value.string);
394 if (dd->name == NULL)
395 {
396 free (dd);
397 return (-1);
398 }
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 = csnmp_config_add_data_type (dd, option);
409 else if (strcasecmp ("Table", option->key) == 0)
410 status = csnmp_config_add_data_table (dd, option);
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 = csnmp_config_add_data_shift (dd, option);
419 else if (strcasecmp ("Scale", option->key) == 0)
420 status = csnmp_config_add_data_scale (dd, option);
421 else
422 {
423 WARNING ("snmp plugin: Option `%s' not allowed here.", option->key);
424 status = -1;
425 }
427 if (status != 0)
428 break;
429 } /* for (ci->children) */
431 while (status == 0)
432 {
433 if (dd->type == NULL)
434 {
435 WARNING ("snmp plugin: `Type' not given for data `%s'", dd->name);
436 status = -1;
437 break;
438 }
439 if (dd->values == NULL)
440 {
441 WARNING ("snmp plugin: No `Value' given for data `%s'", dd->name);
442 status = -1;
443 break;
444 }
446 break;
447 } /* while (status == 0) */
449 if (status != 0)
450 {
451 sfree (dd->name);
452 sfree (dd->instance_prefix);
453 sfree (dd->values);
454 sfree (dd);
455 return (-1);
456 }
458 DEBUG ("snmp plugin: dd = { name = %s, type = %s, is_table = %s, values_len = %i }",
459 dd->name, dd->type, (dd->is_table != 0) ? "true" : "false", dd->values_len);
461 if (data_head == NULL)
462 data_head = dd;
463 else
464 {
465 data_definition_t *last;
466 last = data_head;
467 while (last->next != NULL)
468 last = last->next;
469 last->next = dd;
470 }
472 return (0);
473 } /* int csnmp_config_add_data */
475 static int csnmp_config_add_host_address (host_definition_t *hd, oconfig_item_t *ci)
476 {
477 if ((ci->values_num != 1)
478 || (ci->values[0].type != OCONFIG_TYPE_STRING))
479 {
480 WARNING ("snmp plugin: The `Address' config option needs exactly one string argument.");
481 return (-1);
482 }
484 if (hd->address == NULL)
485 free (hd->address);
487 hd->address = strdup (ci->values[0].value.string);
488 if (hd->address == NULL)
489 return (-1);
491 DEBUG ("snmp plugin: host = %s; host->address = %s;",
492 hd->name, hd->address);
494 return (0);
495 } /* int csnmp_config_add_host_address */
497 static int csnmp_config_add_host_community (host_definition_t *hd, oconfig_item_t *ci)
498 {
499 if ((ci->values_num != 1)
500 || (ci->values[0].type != OCONFIG_TYPE_STRING))
501 {
502 WARNING ("snmp plugin: The `Community' config option needs exactly one string argument.");
503 return (-1);
504 }
506 if (hd->community == NULL)
507 free (hd->community);
509 hd->community = strdup (ci->values[0].value.string);
510 if (hd->community == NULL)
511 return (-1);
513 DEBUG ("snmp plugin: host = %s; host->community = %s;",
514 hd->name, hd->community);
516 return (0);
517 } /* int csnmp_config_add_host_community */
519 static int csnmp_config_add_host_version (host_definition_t *hd, oconfig_item_t *ci)
520 {
521 int version;
523 if ((ci->values_num != 1)
524 || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
525 {
526 WARNING ("snmp plugin: The `Version' config option needs exactly one number argument.");
527 return (-1);
528 }
530 version = (int) ci->values[0].value.number;
531 if ((version != 1) && (version != 2))
532 {
533 WARNING ("snmp plugin: `Version' must either be `1' or `2'.");
534 return (-1);
535 }
537 hd->version = version;
539 return (0);
540 } /* int csnmp_config_add_host_address */
542 static int csnmp_config_add_host_collect (host_definition_t *host,
543 oconfig_item_t *ci)
544 {
545 data_definition_t *data;
546 data_definition_t **data_list;
547 int data_list_len;
548 int i;
550 if (ci->values_num < 1)
551 {
552 WARNING ("snmp plugin: `Collect' needs at least one argument.");
553 return (-1);
554 }
556 for (i = 0; i < ci->values_num; i++)
557 if (ci->values[i].type != OCONFIG_TYPE_STRING)
558 {
559 WARNING ("snmp plugin: All arguments to `Collect' must be strings.");
560 return (-1);
561 }
563 data_list_len = host->data_list_len + ci->values_num;
564 data_list = (data_definition_t **) realloc (host->data_list,
565 sizeof (data_definition_t *) * data_list_len);
566 if (data_list == NULL)
567 return (-1);
568 host->data_list = data_list;
570 for (i = 0; i < ci->values_num; i++)
571 {
572 for (data = data_head; data != NULL; data = data->next)
573 if (strcasecmp (ci->values[i].value.string, data->name) == 0)
574 break;
576 if (data == NULL)
577 {
578 WARNING ("snmp plugin: No such data configured: `%s'",
579 ci->values[i].value.string);
580 continue;
581 }
583 DEBUG ("snmp plugin: Collect: host = %s, data[%i] = %s;",
584 host->name, host->data_list_len, data->name);
586 host->data_list[host->data_list_len] = data;
587 host->data_list_len++;
588 } /* for (values_num) */
590 return (0);
591 } /* int csnmp_config_add_host_collect */
593 static int csnmp_config_add_host (oconfig_item_t *ci)
594 {
595 host_definition_t *hd;
596 int status = 0;
597 int i;
599 /* Registration stuff. */
600 char cb_name[DATA_MAX_NAME_LEN];
601 user_data_t cb_data;
602 struct timespec cb_interval;
604 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
605 {
606 WARNING ("snmp plugin: `Host' needs exactly one string argument.");
607 return (-1);
608 }
610 hd = (host_definition_t *) malloc (sizeof (host_definition_t));
611 if (hd == NULL)
612 return (-1);
613 memset (hd, '\0', sizeof (host_definition_t));
614 hd->version = 2;
615 C_COMPLAIN_INIT (&hd->complaint);
617 hd->name = strdup (ci->values[0].value.string);
618 if (hd->name == NULL)
619 {
620 free (hd);
621 return (-1);
622 }
624 hd->sess_handle = NULL;
625 hd->interval = 0;
627 for (i = 0; i < ci->children_num; i++)
628 {
629 oconfig_item_t *option = ci->children + i;
630 status = 0;
632 if (strcasecmp ("Address", option->key) == 0)
633 status = csnmp_config_add_host_address (hd, option);
634 else if (strcasecmp ("Community", option->key) == 0)
635 status = csnmp_config_add_host_community (hd, option);
636 else if (strcasecmp ("Version", option->key) == 0)
637 status = csnmp_config_add_host_version (hd, option);
638 else if (strcasecmp ("Collect", option->key) == 0)
639 csnmp_config_add_host_collect (hd, option);
640 else if (strcasecmp ("Interval", option->key) == 0)
641 cf_util_get_cdtime (option, &hd->interval);
642 else
643 {
644 WARNING ("snmp plugin: csnmp_config_add_host: Option `%s' not allowed here.", option->key);
645 status = -1;
646 }
648 if (status != 0)
649 break;
650 } /* for (ci->children) */
652 while (status == 0)
653 {
654 if (hd->address == NULL)
655 {
656 WARNING ("snmp plugin: `Address' not given for host `%s'", hd->name);
657 status = -1;
658 break;
659 }
660 if (hd->community == NULL)
661 {
662 WARNING ("snmp plugin: `Community' not given for host `%s'", hd->name);
663 status = -1;
664 break;
665 }
667 break;
668 } /* while (status == 0) */
670 if (status != 0)
671 {
672 csnmp_host_definition_destroy (hd);
673 return (-1);
674 }
676 DEBUG ("snmp plugin: hd = { name = %s, address = %s, community = %s, version = %i }",
677 hd->name, hd->address, hd->community, hd->version);
679 ssnprintf (cb_name, sizeof (cb_name), "snmp-%s", hd->name);
681 memset (&cb_data, 0, sizeof (cb_data));
682 cb_data.data = hd;
683 cb_data.free_func = csnmp_host_definition_destroy;
685 CDTIME_T_TO_TIMESPEC (hd->interval, &cb_interval);
687 status = plugin_register_complex_read (/* group = */ NULL, cb_name,
688 csnmp_read_host, /* interval = */ &cb_interval,
689 /* user_data = */ &cb_data);
690 if (status != 0)
691 {
692 ERROR ("snmp plugin: Registering complex read function failed.");
693 csnmp_host_definition_destroy (hd);
694 return (-1);
695 }
697 return (0);
698 } /* int csnmp_config_add_host */
700 static int csnmp_config (oconfig_item_t *ci)
701 {
702 int i;
704 call_snmp_init_once ();
706 for (i = 0; i < ci->children_num; i++)
707 {
708 oconfig_item_t *child = ci->children + i;
709 if (strcasecmp ("Data", child->key) == 0)
710 csnmp_config_add_data (child);
711 else if (strcasecmp ("Host", child->key) == 0)
712 csnmp_config_add_host (child);
713 else
714 {
715 WARNING ("snmp plugin: Ignoring unknown config option `%s'.", child->key);
716 }
717 } /* for (ci->children) */
719 return (0);
720 } /* int csnmp_config */
722 /* }}} End of the config stuff. Now the interesting part begins */
724 static void csnmp_host_open_session (host_definition_t *host)
725 {
726 struct snmp_session sess;
728 if (host->sess_handle != NULL)
729 csnmp_host_close_session (host);
731 snmp_sess_init (&sess);
732 sess.peername = host->address;
733 sess.community = (u_char *) host->community;
734 sess.community_len = strlen (host->community);
735 sess.version = (host->version == 1) ? SNMP_VERSION_1 : SNMP_VERSION_2c;
737 /* snmp_sess_open will copy the `struct snmp_session *'. */
738 host->sess_handle = snmp_sess_open (&sess);
740 if (host->sess_handle == NULL)
741 {
742 char *errstr = NULL;
744 snmp_error (&sess, NULL, NULL, &errstr);
746 ERROR ("snmp plugin: host %s: snmp_sess_open failed: %s",
747 host->name, (errstr == NULL) ? "Unknown problem" : errstr);
748 sfree (errstr);
749 }
750 } /* void csnmp_host_open_session */
752 /* TODO: Check if negative values wrap around. Problem: negative temperatures. */
753 static value_t csnmp_value_list_to_value (struct variable_list *vl, int type,
754 double scale, double shift,
755 const char *host_name, const char *data_name)
756 {
757 value_t ret;
758 uint64_t tmp_unsigned = 0;
759 int64_t tmp_signed = 0;
760 _Bool defined = 1;
761 /* Set to true when the original SNMP type appears to have been signed. */
762 _Bool prefer_signed = 0;
764 if ((vl->type == ASN_INTEGER)
765 || (vl->type == ASN_UINTEGER)
766 || (vl->type == ASN_COUNTER)
767 #ifdef ASN_TIMETICKS
768 || (vl->type == ASN_TIMETICKS)
769 #endif
770 || (vl->type == ASN_GAUGE))
771 {
772 tmp_unsigned = (uint32_t) *vl->val.integer;
773 tmp_signed = (int32_t) *vl->val.integer;
775 if ((vl->type == ASN_INTEGER)
776 || (vl->type == ASN_GAUGE))
777 prefer_signed = 1;
779 DEBUG ("snmp plugin: Parsed int32 value is %"PRIu64".", tmp_unsigned);
780 }
781 else if (vl->type == ASN_COUNTER64)
782 {
783 tmp_unsigned = (uint32_t) vl->val.counter64->high;
784 tmp_unsigned = tmp_unsigned << 32;
785 tmp_unsigned += (uint32_t) vl->val.counter64->low;
786 tmp_signed = (int64_t) tmp_unsigned;
787 DEBUG ("snmp plugin: Parsed int64 value is %"PRIu64".", tmp_unsigned);
788 }
789 else if (vl->type == ASN_OCTET_STR)
790 {
791 /* We'll handle this later.. */
792 }
793 else
794 {
795 char oid_buffer[1024];
797 memset (oid_buffer, 0, sizeof (oid_buffer));
798 snprint_objid (oid_buffer, sizeof (oid_buffer) - 1,
799 vl->name, vl->name_length);
801 #ifdef ASN_NULL
802 if (vl->type == ASN_NULL)
803 INFO ("snmp plugin: OID \"%s\" is undefined (type ASN_NULL)",
804 oid_buffer);
805 else
806 #endif
807 WARNING ("snmp plugin: I don't know the ASN type #%i "
808 "(OID: \"%s\", data block \"%s\", host block \"%s\")",
809 (int) vl->type, oid_buffer,
810 (data_name != NULL) ? data_name : "UNKNOWN",
811 (host_name != NULL) ? host_name : "UNKNOWN");
813 defined = 0;
814 }
816 if (vl->type == ASN_OCTET_STR)
817 {
818 int status = -1;
820 if (vl->val.string != NULL)
821 {
822 char string[64];
823 size_t string_length;
825 string_length = sizeof (string) - 1;
826 if (vl->val_len < string_length)
827 string_length = vl->val_len;
829 /* The strings we get from the Net-SNMP library may not be null
830 * terminated. That is why we're using `memcpy' here and not `strcpy'.
831 * `string_length' is set to `vl->val_len' which holds the length of the
832 * string. -octo */
833 memcpy (string, vl->val.string, string_length);
834 string[string_length] = 0;
836 status = parse_value (string, &ret, type);
837 if (status != 0)
838 {
839 ERROR ("snmp plugin: csnmp_value_list_to_value: Parsing string as %s failed: %s",
840 DS_TYPE_TO_STRING (type), string);
841 }
842 }
844 if (status != 0)
845 {
846 switch (type)
847 {
848 case DS_TYPE_COUNTER:
849 case DS_TYPE_DERIVE:
850 case DS_TYPE_ABSOLUTE:
851 memset (&ret, 0, sizeof (ret));
852 break;
854 case DS_TYPE_GAUGE:
855 ret.gauge = NAN;
856 break;
858 default:
859 ERROR ("snmp plugin: csnmp_value_list_to_value: Unknown "
860 "data source type: %i.", type);
861 ret.gauge = NAN;
862 }
863 }
864 } /* if (vl->type == ASN_OCTET_STR) */
865 else if (type == DS_TYPE_COUNTER)
866 {
867 ret.counter = tmp_unsigned;
868 }
869 else if (type == DS_TYPE_GAUGE)
870 {
871 if (!defined)
872 ret.gauge = NAN;
873 else if (prefer_signed)
874 ret.gauge = (scale * tmp_signed) + shift;
875 else
876 ret.gauge = (scale * tmp_unsigned) + shift;
877 }
878 else if (type == DS_TYPE_DERIVE)
879 {
880 if (prefer_signed)
881 ret.derive = (derive_t) tmp_signed;
882 else
883 ret.derive = (derive_t) tmp_unsigned;
884 }
885 else if (type == DS_TYPE_ABSOLUTE)
886 {
887 ret.absolute = (absolute_t) tmp_unsigned;
888 }
889 else
890 {
891 ERROR ("snmp plugin: csnmp_value_list_to_value: Unknown data source "
892 "type: %i.", type);
893 ret.gauge = NAN;
894 }
896 return (ret);
897 } /* value_t csnmp_value_list_to_value */
899 /* Returns true if all OIDs have left their subtree */
900 static int csnmp_check_res_left_subtree (const host_definition_t *host,
901 const data_definition_t *data,
902 struct snmp_pdu *res)
903 {
904 struct variable_list *vb;
905 int num_checked;
906 int num_left_subtree;
907 int i;
909 vb = res->variables;
910 if (vb == NULL)
911 return (-1);
913 num_checked = 0;
914 num_left_subtree = 0;
916 /* check all the variables and count how many have left their subtree */
917 for (vb = res->variables, i = 0;
918 (vb != NULL) && (i < data->values_len);
919 vb = vb->next_variable, i++)
920 {
921 num_checked++;
923 if ((vb->type == SNMP_ENDOFMIBVIEW)
924 || (snmp_oid_ncompare (data->values[i].oid,
925 data->values[i].oid_len,
926 vb->name, vb->name_length,
927 data->values[i].oid_len) != 0))
928 num_left_subtree++;
929 }
931 /* check if enough variables have been returned */
932 if (i < data->values_len)
933 {
934 ERROR ("snmp plugin: host %s: Expected %i variables, but got only %i",
935 host->name, data->values_len, i);
936 return (-1);
937 }
939 if (data->instance.oid.oid_len > 0)
940 {
941 if (vb == NULL)
942 {
943 ERROR ("snmp plugin: host %s: Expected one more variable for "
944 "the instance..", host->name);
945 return (-1);
946 }
948 num_checked++;
949 if (snmp_oid_ncompare (data->instance.oid.oid,
950 data->instance.oid.oid_len,
951 vb->name, vb->name_length,
952 data->instance.oid.oid_len) != 0)
953 num_left_subtree++;
954 }
956 DEBUG ("snmp plugin: csnmp_check_res_left_subtree: %i of %i variables have "
957 "left their subtree",
958 num_left_subtree, num_checked);
959 if (num_left_subtree >= num_checked)
960 return (1);
961 return (0);
962 } /* int csnmp_check_res_left_subtree */
964 static int csnmp_strvbcopy_hexstring (char *dst, /* {{{ */
965 const struct variable_list *vb, size_t dst_size)
966 {
967 char *buffer_ptr;
968 size_t buffer_free;
969 size_t i;
971 buffer_ptr = dst;
972 buffer_free = dst_size;
974 for (i = 0; i < vb->val_len; i++)
975 {
976 int status;
978 status = snprintf (buffer_ptr, buffer_free,
979 (i == 0) ? "%02x" : ":%02x", (unsigned int) vb->val.bitstring[i]);
981 if (status >= buffer_free)
982 {
983 buffer_ptr += (buffer_free - 1);
984 *buffer_ptr = 0;
985 return (dst_size + (buffer_free - status));
986 }
987 else /* if (status < buffer_free) */
988 {
989 buffer_ptr += status;
990 buffer_free -= status;
991 }
992 }
994 return ((int) (dst_size - buffer_free));
995 } /* }}} int csnmp_strvbcopy_hexstring */
997 static int csnmp_strvbcopy (char *dst, /* {{{ */
998 const struct variable_list *vb, size_t dst_size)
999 {
1000 char *src;
1001 size_t num_chars;
1002 size_t i;
1004 if (vb->type == ASN_OCTET_STR)
1005 src = (char *) vb->val.string;
1006 else if (vb->type == ASN_BIT_STR)
1007 src = (char *) vb->val.bitstring;
1008 else
1009 {
1010 dst[0] = 0;
1011 return (EINVAL);
1012 }
1014 num_chars = dst_size - 1;
1015 if (num_chars > vb->val_len)
1016 num_chars = vb->val_len;
1018 for (i = 0; i < num_chars; i++)
1019 {
1020 /* Check for control characters. */
1021 if ((unsigned char)src[i] < 32)
1022 return (csnmp_strvbcopy_hexstring (dst, vb, dst_size));
1023 dst[i] = src[i];
1024 }
1025 dst[num_chars] = 0;
1027 return ((int) vb->val_len);
1028 } /* }}} int csnmp_strvbcopy */
1030 static int csnmp_instance_list_add (csnmp_list_instances_t **head,
1031 csnmp_list_instances_t **tail,
1032 const struct snmp_pdu *res,
1033 const host_definition_t *hd, const data_definition_t *dd)
1034 {
1035 csnmp_list_instances_t *il;
1036 struct variable_list *vb;
1037 oid_t vb_name;
1038 int status;
1040 /* Set vb on the last variable */
1041 for (vb = res->variables;
1042 (vb != NULL) && (vb->next_variable != NULL);
1043 vb = vb->next_variable)
1044 /* do nothing */;
1045 if (vb == NULL)
1046 return (-1);
1048 csnmp_oid_init (&vb_name, vb->name, vb->name_length);
1050 il = malloc (sizeof (*il));
1051 if (il == NULL)
1052 {
1053 ERROR ("snmp plugin: malloc failed.");
1054 return (-1);
1055 }
1056 memset (il, 0, sizeof (*il));
1057 il->next = NULL;
1059 status = csnmp_oid_suffix (&il->suffix, &vb_name, &dd->instance.oid);
1060 if (status != 0)
1061 {
1062 sfree (il);
1063 return (status);
1064 }
1066 /* Get instance name */
1067 if ((vb->type == ASN_OCTET_STR) || (vb->type == ASN_BIT_STR))
1068 {
1069 char *ptr;
1071 csnmp_strvbcopy (il->instance, vb, sizeof (il->instance));
1073 for (ptr = il->instance; *ptr != '\0'; ptr++)
1074 {
1075 if ((*ptr > 0) && (*ptr < 32))
1076 *ptr = ' ';
1077 else if (*ptr == '/')
1078 *ptr = '_';
1079 }
1080 DEBUG ("snmp plugin: il->instance = `%s';", il->instance);
1081 }
1082 else
1083 {
1084 value_t val = csnmp_value_list_to_value (vb, DS_TYPE_COUNTER,
1085 /* scale = */ 1.0, /* shift = */ 0.0, hd->name, dd->name);
1086 ssnprintf (il->instance, sizeof (il->instance),
1087 "%llu", val.counter);
1088 }
1090 /* TODO: Debugging output */
1092 if (*head == NULL)
1093 *head = il;
1094 else
1095 (*tail)->next = il;
1096 *tail = il;
1098 return (0);
1099 } /* int csnmp_instance_list_add */
1101 static int csnmp_dispatch_table (host_definition_t *host, data_definition_t *data,
1102 csnmp_list_instances_t *instance_list,
1103 csnmp_table_values_t **value_table)
1104 {
1105 const data_set_t *ds;
1106 value_list_t vl = VALUE_LIST_INIT;
1108 csnmp_list_instances_t *instance_list_ptr;
1109 csnmp_table_values_t **value_table_ptr;
1111 int i;
1112 _Bool have_more;
1113 oid_t current_suffix;
1115 ds = plugin_get_ds (data->type);
1116 if (!ds)
1117 {
1118 ERROR ("snmp plugin: DataSet `%s' not defined.", data->type);
1119 return (-1);
1120 }
1121 assert (ds->ds_num == data->values_len);
1123 instance_list_ptr = instance_list;
1125 value_table_ptr = malloc (sizeof (*value_table_ptr) * data->values_len);
1126 if (value_table_ptr == NULL)
1127 return (-1);
1128 for (i = 0; i < data->values_len; i++)
1129 value_table_ptr[i] = value_table[i];
1131 vl.values_len = ds->ds_num;
1132 vl.values = malloc (sizeof (*vl.values) * vl.values_len);
1133 if (vl.values == NULL)
1134 {
1135 ERROR ("snmp plugin: malloc failed.");
1136 sfree (value_table_ptr);
1137 return (-1);
1138 }
1140 sstrncpy (vl.host, host->name, sizeof (vl.host));
1141 sstrncpy (vl.plugin, "snmp", sizeof (vl.plugin));
1143 vl.interval = host->interval;
1145 have_more = 1;
1146 memset (¤t_suffix, 0, sizeof (current_suffix));
1147 while (have_more)
1148 {
1149 _Bool suffix_skipped = 0;
1151 /* Determine next suffix to handle. */
1152 if (instance_list != NULL)
1153 {
1154 if (instance_list_ptr == NULL)
1155 {
1156 have_more = 0;
1157 continue;
1158 }
1160 memcpy (¤t_suffix, &instance_list_ptr->suffix, sizeof (current_suffix));
1161 }
1162 else /* no instance configured */
1163 {
1164 csnmp_table_values_t *ptr = value_table_ptr[0];
1165 if (ptr == NULL)
1166 {
1167 have_more = 0;
1168 continue;
1169 }
1171 memcpy (¤t_suffix, &ptr->suffix, sizeof (current_suffix));
1172 }
1174 /* Update all the value_table_ptr to point at the entry with the same
1175 * trailing partial OID */
1176 for (i = 0; i < data->values_len; i++)
1177 {
1178 while ((value_table_ptr[i] != NULL)
1179 && (csnmp_oid_compare (&value_table_ptr[i]->suffix, ¤t_suffix) < 0))
1180 value_table_ptr[i] = value_table_ptr[i]->next;
1182 if (value_table_ptr[i] == NULL)
1183 {
1184 have_more = 0;
1185 break;
1186 }
1187 else if (csnmp_oid_compare (&value_table_ptr[i]->suffix, ¤t_suffix) > 0)
1188 {
1189 /* This suffix is missing in the subtree. Indicate this with the
1190 * "suffix_skipped" flag and try the next instance / suffix. */
1191 suffix_skipped = 1;
1192 break;
1193 }
1194 } /* for (i = 0; i < columns; i++) */
1196 if (!have_more)
1197 break;
1199 /* Matching the values failed. Start from the beginning again. */
1200 if (suffix_skipped)
1201 {
1202 if (instance_list != NULL)
1203 instance_list_ptr = instance_list_ptr->next;
1204 else
1205 value_table_ptr[0] = value_table_ptr[0]->next;
1207 continue;
1208 }
1210 /* if we reach this line, all value_table_ptr[i] are non-NULL and are set
1211 * to the same subid. instance_list_ptr is either NULL or points to the
1212 * same subid, too. */
1213 #if COLLECT_DEBUG
1214 for (i = 1; i < data->values_len; i++)
1215 {
1216 assert (value_table_ptr[i] != NULL);
1217 assert (csnmp_oid_compare (&value_table_ptr[i-1]->suffix,
1218 &value_table_ptr[i]->suffix) == 0);
1219 }
1220 assert ((instance_list_ptr == NULL)
1221 || (csnmp_oid_compare (&instance_list_ptr->suffix,
1222 &value_table_ptr[0]->suffix) == 0));
1223 #endif
1225 sstrncpy (vl.type, data->type, sizeof (vl.type));
1227 {
1228 char temp[DATA_MAX_NAME_LEN];
1230 if (instance_list_ptr == NULL)
1231 csnmp_oid_to_string (temp, sizeof (temp), ¤t_suffix);
1232 else
1233 sstrncpy (temp, instance_list_ptr->instance, sizeof (temp));
1235 if (data->instance_prefix == NULL)
1236 sstrncpy (vl.type_instance, temp, sizeof (vl.type_instance));
1237 else
1238 ssnprintf (vl.type_instance, sizeof (vl.type_instance), "%s%s",
1239 data->instance_prefix, temp);
1240 }
1242 for (i = 0; i < data->values_len; i++)
1243 vl.values[i] = value_table_ptr[i]->value;
1245 /* If we get here `vl.type_instance' and all `vl.values' have been set */
1246 plugin_dispatch_values (&vl);
1248 if (instance_list != NULL)
1249 instance_list_ptr = instance_list_ptr->next;
1250 else
1251 value_table_ptr[0] = value_table_ptr[0]->next;
1252 } /* while (have_more) */
1254 sfree (vl.values);
1255 sfree (value_table_ptr);
1257 return (0);
1258 } /* int csnmp_dispatch_table */
1260 static int csnmp_read_table (host_definition_t *host, data_definition_t *data)
1261 {
1262 struct snmp_pdu *req;
1263 struct snmp_pdu *res;
1264 struct variable_list *vb;
1266 const data_set_t *ds;
1267 oid_t *oid_list;
1268 uint32_t oid_list_len;
1270 int status;
1271 int i;
1273 /* `value_list_head' and `value_list_tail' implement a linked list for each
1274 * value. `instance_list_head' and `instance_list_tail' implement a linked list of
1275 * instance names. This is used to jump gaps in the table. */
1276 csnmp_list_instances_t *instance_list_head;
1277 csnmp_list_instances_t *instance_list_tail;
1278 csnmp_table_values_t **value_list_head;
1279 csnmp_table_values_t **value_list_tail;
1281 DEBUG ("snmp plugin: csnmp_read_table (host = %s, data = %s)",
1282 host->name, data->name);
1284 if (host->sess_handle == NULL)
1285 {
1286 DEBUG ("snmp plugin: csnmp_read_table: host->sess_handle == NULL");
1287 return (-1);
1288 }
1290 ds = plugin_get_ds (data->type);
1291 if (!ds)
1292 {
1293 ERROR ("snmp plugin: DataSet `%s' not defined.", data->type);
1294 return (-1);
1295 }
1297 if (ds->ds_num != data->values_len)
1298 {
1299 ERROR ("snmp plugin: DataSet `%s' requires %i values, but config talks about %i",
1300 data->type, ds->ds_num, data->values_len);
1301 return (-1);
1302 }
1304 /* We need a copy of all the OIDs, because GETNEXT will destroy them. */
1305 oid_list_len = data->values_len + 1;
1306 oid_list = (oid_t *) malloc (sizeof (oid_t) * (oid_list_len));
1307 if (oid_list == NULL)
1308 {
1309 ERROR ("snmp plugin: csnmp_read_table: malloc failed.");
1310 return (-1);
1311 }
1312 memcpy (oid_list, data->values, data->values_len * sizeof (oid_t));
1313 if (data->instance.oid.oid_len > 0)
1314 memcpy (oid_list + data->values_len, &data->instance.oid, sizeof (oid_t));
1315 else
1316 oid_list_len--;
1318 /* We're going to construct n linked lists, one for each "value".
1319 * value_list_head will contain pointers to the heads of these linked lists,
1320 * value_list_tail will contain pointers to the tail of the lists. */
1321 value_list_head = calloc (data->values_len, sizeof (*value_list_head));
1322 value_list_tail = calloc (data->values_len, sizeof (*value_list_tail));
1323 if ((value_list_head == NULL) || (value_list_tail == NULL))
1324 {
1325 ERROR ("snmp plugin: csnmp_read_table: calloc failed.");
1326 sfree (oid_list);
1327 sfree (value_list_head);
1328 sfree (value_list_tail);
1329 return (-1);
1330 }
1332 instance_list_head = NULL;
1333 instance_list_tail = NULL;
1335 status = 0;
1336 while (status == 0)
1337 {
1338 req = snmp_pdu_create (SNMP_MSG_GETNEXT);
1339 if (req == NULL)
1340 {
1341 ERROR ("snmp plugin: snmp_pdu_create failed.");
1342 status = -1;
1343 break;
1344 }
1346 for (i = 0; (uint32_t) i < oid_list_len; i++)
1347 snmp_add_null_var (req, oid_list[i].oid, oid_list[i].oid_len);
1349 res = NULL;
1350 status = snmp_sess_synch_response (host->sess_handle, req, &res);
1352 if ((status != STAT_SUCCESS) || (res == NULL))
1353 {
1354 char *errstr = NULL;
1356 snmp_sess_error (host->sess_handle, NULL, NULL, &errstr);
1358 c_complain (LOG_ERR, &host->complaint,
1359 "snmp plugin: host %s: snmp_sess_synch_response failed: %s",
1360 host->name, (errstr == NULL) ? "Unknown problem" : errstr);
1362 if (res != NULL)
1363 snmp_free_pdu (res);
1364 res = NULL;
1366 sfree (errstr);
1367 csnmp_host_close_session (host);
1369 status = -1;
1370 break;
1371 }
1372 status = 0;
1373 assert (res != NULL);
1374 c_release (LOG_INFO, &host->complaint,
1375 "snmp plugin: host %s: snmp_sess_synch_response successful.",
1376 host->name);
1378 vb = res->variables;
1379 if (vb == NULL)
1380 {
1381 status = -1;
1382 break;
1383 }
1385 /* Check if all values (and possibly the instance) have left their
1386 * subtree */
1387 if (csnmp_check_res_left_subtree (host, data, res) != 0)
1388 {
1389 status = 0;
1390 break;
1391 }
1393 /* Copy the OID of the value used as instance to oid_list, if an instance
1394 * is configured. */
1395 if (data->instance.oid.oid_len > 0)
1396 {
1397 /* Allocate a new `csnmp_list_instances_t', insert the instance name and
1398 * add it to the list */
1399 if (csnmp_instance_list_add (&instance_list_head, &instance_list_tail,
1400 res, host, data) != 0)
1401 {
1402 ERROR ("snmp plugin: csnmp_instance_list_add failed.");
1403 status = -1;
1404 break;
1405 }
1407 /* The instance OID is added to the list of OIDs to GET from the
1408 * snmp agent last, so set vb on the last variable returned and copy
1409 * that OID. */
1410 for (vb = res->variables;
1411 (vb != NULL) && (vb->next_variable != NULL);
1412 vb = vb->next_variable)
1413 /* do nothing */;
1414 assert (vb != NULL);
1416 /* Copy the OID of the instance value to oid_list[data->values_len].
1417 * "oid_list" is used for the next GETNEXT request. */
1418 memcpy (oid_list[data->values_len].oid, vb->name,
1419 sizeof (oid) * vb->name_length);
1420 oid_list[data->values_len].oid_len = vb->name_length;
1421 }
1423 /* Iterate over all the (non-instance) values returned by the agent. The
1424 * (i < value_len) check will make sure we're not handling the instance OID
1425 * twice. */
1426 for (vb = res->variables, i = 0;
1427 (vb != NULL) && (i < data->values_len);
1428 vb = vb->next_variable, i++)
1429 {
1430 csnmp_table_values_t *vt;
1431 oid_t vb_name;
1432 oid_t suffix;
1434 csnmp_oid_init (&vb_name, vb->name, vb->name_length);
1436 /* Calculate the current suffix. This is later used to check that the
1437 * suffix is increasing. This also checks if we left the subtree */
1438 status = csnmp_oid_suffix (&suffix, &vb_name, data->values + i);
1439 if (status != 0)
1440 {
1441 DEBUG ("snmp plugin: host = %s; data = %s; Value %i failed. "
1442 "It probably left its subtree.",
1443 host->name, data->name, i);
1444 continue;
1445 }
1447 /* Make sure the OIDs returned by the agent are increasing. Otherwise our
1448 * table matching algorithm will get confused. */
1449 if ((value_list_tail[i] != NULL)
1450 && (csnmp_oid_compare (&suffix, &value_list_tail[i]->suffix) <= 0))
1451 {
1452 DEBUG ("snmp plugin: host = %s; data = %s; i = %i; "
1453 "Suffix is not increasing.",
1454 host->name, data->name, i);
1455 continue;
1456 }
1458 vt = malloc (sizeof (*vt));
1459 if (vt == NULL)
1460 {
1461 ERROR ("snmp plugin: malloc failed.");
1462 status = -1;
1463 break;
1464 }
1465 memset (vt, 0, sizeof (*vt));
1467 vt->value = csnmp_value_list_to_value (vb, ds->ds[i].type,
1468 data->scale, data->shift, host->name, data->name);
1469 memcpy (&vt->suffix, &suffix, sizeof (vt->suffix));
1470 vt->next = NULL;
1472 if (value_list_tail[i] == NULL)
1473 value_list_head[i] = vt;
1474 else
1475 value_list_tail[i]->next = vt;
1476 value_list_tail[i] = vt;
1478 /* Copy OID to oid_list[i + 1] */
1479 memcpy (oid_list[i].oid, vb->name, sizeof (oid) * vb->name_length);
1480 oid_list[i].oid_len = vb->name_length;
1481 } /* for (i = data->values_len) */
1483 if (res != NULL)
1484 snmp_free_pdu (res);
1485 res = NULL;
1486 } /* while (status == 0) */
1488 if (res != NULL)
1489 snmp_free_pdu (res);
1490 res = NULL;
1492 if (status == 0)
1493 csnmp_dispatch_table (host, data, instance_list_head, value_list_head);
1495 /* Free all allocated variables here */
1496 while (instance_list_head != NULL)
1497 {
1498 csnmp_list_instances_t *next = instance_list_head->next;
1499 sfree (instance_list_head);
1500 instance_list_head = next;
1501 }
1503 for (i = 0; i < data->values_len; i++)
1504 {
1505 while (value_list_head[i] != NULL)
1506 {
1507 csnmp_table_values_t *next = value_list_head[i]->next;
1508 sfree (value_list_head[i]);
1509 value_list_head[i] = next;
1510 }
1511 }
1513 sfree (value_list_head);
1514 sfree (value_list_tail);
1515 sfree (oid_list);
1517 return (0);
1518 } /* int csnmp_read_table */
1520 static int csnmp_read_value (host_definition_t *host, data_definition_t *data)
1521 {
1522 struct snmp_pdu *req;
1523 struct snmp_pdu *res;
1524 struct variable_list *vb;
1526 const data_set_t *ds;
1527 value_list_t vl = VALUE_LIST_INIT;
1529 int status;
1530 int i;
1532 DEBUG ("snmp plugin: csnmp_read_value (host = %s, data = %s)",
1533 host->name, data->name);
1535 if (host->sess_handle == NULL)
1536 {
1537 DEBUG ("snmp plugin: csnmp_read_table: host->sess_handle == NULL");
1538 return (-1);
1539 }
1541 ds = plugin_get_ds (data->type);
1542 if (!ds)
1543 {
1544 ERROR ("snmp plugin: DataSet `%s' not defined.", data->type);
1545 return (-1);
1546 }
1548 if (ds->ds_num != data->values_len)
1549 {
1550 ERROR ("snmp plugin: DataSet `%s' requires %i values, but config talks about %i",
1551 data->type, ds->ds_num, data->values_len);
1552 return (-1);
1553 }
1555 vl.values_len = ds->ds_num;
1556 vl.values = (value_t *) malloc (sizeof (value_t) * vl.values_len);
1557 if (vl.values == NULL)
1558 return (-1);
1559 for (i = 0; i < vl.values_len; i++)
1560 {
1561 if (ds->ds[i].type == DS_TYPE_COUNTER)
1562 vl.values[i].counter = 0;
1563 else
1564 vl.values[i].gauge = NAN;
1565 }
1567 sstrncpy (vl.host, host->name, sizeof (vl.host));
1568 sstrncpy (vl.plugin, "snmp", sizeof (vl.plugin));
1569 sstrncpy (vl.type, data->type, sizeof (vl.type));
1570 sstrncpy (vl.type_instance, data->instance.string, sizeof (vl.type_instance));
1572 vl.interval = host->interval;
1574 req = snmp_pdu_create (SNMP_MSG_GET);
1575 if (req == NULL)
1576 {
1577 ERROR ("snmp plugin: snmp_pdu_create failed.");
1578 sfree (vl.values);
1579 return (-1);
1580 }
1582 for (i = 0; i < data->values_len; i++)
1583 snmp_add_null_var (req, data->values[i].oid, data->values[i].oid_len);
1585 res = NULL;
1586 status = snmp_sess_synch_response (host->sess_handle, req, &res);
1588 if ((status != STAT_SUCCESS) || (res == NULL))
1589 {
1590 char *errstr = NULL;
1592 snmp_sess_error (host->sess_handle, NULL, NULL, &errstr);
1593 ERROR ("snmp plugin: host %s: snmp_sess_synch_response failed: %s",
1594 host->name, (errstr == NULL) ? "Unknown problem" : errstr);
1596 if (res != NULL)
1597 snmp_free_pdu (res);
1598 res = NULL;
1600 sfree (errstr);
1601 csnmp_host_close_session (host);
1603 return (-1);
1604 }
1607 for (vb = res->variables; vb != NULL; vb = vb->next_variable)
1608 {
1609 #if COLLECT_DEBUG
1610 char buffer[1024];
1611 snprint_variable (buffer, sizeof (buffer),
1612 vb->name, vb->name_length, vb);
1613 DEBUG ("snmp plugin: Got this variable: %s", buffer);
1614 #endif /* COLLECT_DEBUG */
1616 for (i = 0; i < data->values_len; i++)
1617 if (snmp_oid_compare (data->values[i].oid, data->values[i].oid_len,
1618 vb->name, vb->name_length) == 0)
1619 vl.values[i] = csnmp_value_list_to_value (vb, ds->ds[i].type,
1620 data->scale, data->shift, host->name, data->name);
1621 } /* for (res->variables) */
1623 if (res != NULL)
1624 snmp_free_pdu (res);
1625 res = NULL;
1627 DEBUG ("snmp plugin: -> plugin_dispatch_values (&vl);");
1628 plugin_dispatch_values (&vl);
1629 sfree (vl.values);
1631 return (0);
1632 } /* int csnmp_read_value */
1634 static int csnmp_read_host (user_data_t *ud)
1635 {
1636 host_definition_t *host;
1637 cdtime_t time_start;
1638 cdtime_t time_end;
1639 int status;
1640 int success;
1641 int i;
1643 host = ud->data;
1645 if (host->interval == 0)
1646 host->interval = plugin_get_interval ();
1648 time_start = cdtime ();
1650 if (host->sess_handle == NULL)
1651 csnmp_host_open_session (host);
1653 if (host->sess_handle == NULL)
1654 return (-1);
1656 success = 0;
1657 for (i = 0; i < host->data_list_len; i++)
1658 {
1659 data_definition_t *data = host->data_list[i];
1661 if (data->is_table)
1662 status = csnmp_read_table (host, data);
1663 else
1664 status = csnmp_read_value (host, data);
1666 if (status == 0)
1667 success++;
1668 }
1670 time_end = cdtime ();
1671 if ((time_end - time_start) > host->interval)
1672 {
1673 WARNING ("snmp plugin: Host `%s' should be queried every %.3f "
1674 "seconds, but reading all values takes %.3f seconds.",
1675 host->name,
1676 CDTIME_T_TO_DOUBLE (host->interval),
1677 CDTIME_T_TO_DOUBLE (time_end - time_start));
1678 }
1680 if (success == 0)
1681 return (-1);
1683 return (0);
1684 } /* int csnmp_read_host */
1686 static int csnmp_init (void)
1687 {
1688 call_snmp_init_once ();
1690 return (0);
1691 } /* int csnmp_init */
1693 static int csnmp_shutdown (void)
1694 {
1695 data_definition_t *data_this;
1696 data_definition_t *data_next;
1698 /* When we get here, the read threads have been stopped and all the
1699 * `host_definition_t' will be freed. */
1700 DEBUG ("snmp plugin: Destroying all data definitions.");
1702 data_this = data_head;
1703 data_head = NULL;
1704 while (data_this != NULL)
1705 {
1706 data_next = data_this->next;
1708 sfree (data_this->name);
1709 sfree (data_this->type);
1710 sfree (data_this->values);
1711 sfree (data_this);
1713 data_this = data_next;
1714 }
1716 return (0);
1717 } /* int csnmp_shutdown */
1719 void module_register (void)
1720 {
1721 plugin_register_complex_config ("snmp", csnmp_config);
1722 plugin_register_init ("snmp", csnmp_init);
1723 plugin_register_shutdown ("snmp", csnmp_shutdown);
1724 } /* void module_register */
1726 /*
1727 * vim: shiftwidth=2 softtabstop=2 tabstop=8 fdm=marker
1728 */