1 /**
2 * collectd - src/snmp.c
3 * Copyright (C) 2007 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 verplant.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 subid;
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 subid;
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_host_close_session (host_definition_t *host) /* {{{ */
110 {
111 if (host->sess_handle == NULL)
112 return;
114 snmp_sess_close (host->sess_handle);
115 host->sess_handle = NULL;
116 } /* }}} void csnmp_host_close_session */
118 static void csnmp_host_definition_destroy (void *arg) /* {{{ */
119 {
120 host_definition_t *hd;
122 hd = arg;
124 if (hd == NULL)
125 return;
127 if (hd->name != NULL)
128 {
129 DEBUG ("snmp plugin: Destroying host definition for host `%s'.",
130 hd->name);
131 }
133 csnmp_host_close_session (hd);
135 sfree (hd->name);
136 sfree (hd->address);
137 sfree (hd->community);
138 sfree (hd->data_list);
140 sfree (hd);
141 } /* }}} void csnmp_host_definition_destroy */
143 /* Many functions to handle the configuration. {{{ */
144 /* First there are many functions which do configuration stuff. It's a big
145 * bloated and messy, I'm afraid. */
147 /*
148 * Callgraph for the config stuff:
149 * csnmp_config
150 * +-> call_snmp_init_once
151 * +-> csnmp_config_add_data
152 * ! +-> csnmp_config_add_data_type
153 * ! +-> csnmp_config_add_data_table
154 * ! +-> csnmp_config_add_data_instance
155 * ! +-> csnmp_config_add_data_instance_prefix
156 * ! +-> csnmp_config_add_data_values
157 * +-> csnmp_config_add_host
158 * +-> csnmp_config_add_host_address
159 * +-> csnmp_config_add_host_community
160 * +-> csnmp_config_add_host_version
161 * +-> csnmp_config_add_host_collect
162 */
163 static void call_snmp_init_once (void)
164 {
165 static int have_init = 0;
167 if (have_init == 0)
168 init_snmp (PACKAGE_NAME);
169 have_init = 1;
170 } /* void call_snmp_init_once */
172 static int csnmp_config_add_data_type (data_definition_t *dd, oconfig_item_t *ci)
173 {
174 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
175 {
176 WARNING ("snmp plugin: `Type' needs exactly one string argument.");
177 return (-1);
178 }
180 sfree (dd->type);
181 dd->type = strdup (ci->values[0].value.string);
182 if (dd->type == NULL)
183 return (-1);
185 return (0);
186 } /* int csnmp_config_add_data_type */
188 static int csnmp_config_add_data_table (data_definition_t *dd, oconfig_item_t *ci)
189 {
190 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_BOOLEAN))
191 {
192 WARNING ("snmp plugin: `Table' needs exactly one boolean argument.");
193 return (-1);
194 }
196 dd->is_table = ci->values[0].value.boolean ? 1 : 0;
198 return (0);
199 } /* int csnmp_config_add_data_table */
201 static int csnmp_config_add_data_instance (data_definition_t *dd, oconfig_item_t *ci)
202 {
203 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
204 {
205 WARNING ("snmp plugin: `Instance' needs exactly one string argument.");
206 return (-1);
207 }
209 if (dd->is_table)
210 {
211 /* Instance is an OID */
212 dd->instance.oid.oid_len = MAX_OID_LEN;
214 if (!read_objid (ci->values[0].value.string,
215 dd->instance.oid.oid, &dd->instance.oid.oid_len))
216 {
217 ERROR ("snmp plugin: read_objid (%s) failed.",
218 ci->values[0].value.string);
219 return (-1);
220 }
221 }
222 else
223 {
224 /* Instance is a simple string */
225 sstrncpy (dd->instance.string, ci->values[0].value.string,
226 sizeof (dd->instance.string));
227 }
229 return (0);
230 } /* int csnmp_config_add_data_instance */
232 static int csnmp_config_add_data_instance_prefix (data_definition_t *dd,
233 oconfig_item_t *ci)
234 {
235 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
236 {
237 WARNING ("snmp plugin: `InstancePrefix' needs exactly one string argument.");
238 return (-1);
239 }
241 if (!dd->is_table)
242 {
243 WARNING ("snmp plugin: data %s: InstancePrefix is ignored when `Table' "
244 "is set to `false'.", dd->name);
245 return (-1);
246 }
248 sfree (dd->instance_prefix);
249 dd->instance_prefix = strdup (ci->values[0].value.string);
250 if (dd->instance_prefix == NULL)
251 return (-1);
253 return (0);
254 } /* int csnmp_config_add_data_instance_prefix */
256 static int csnmp_config_add_data_values (data_definition_t *dd, oconfig_item_t *ci)
257 {
258 int i;
260 if (ci->values_num < 1)
261 {
262 WARNING ("snmp plugin: `Values' needs at least one argument.");
263 return (-1);
264 }
266 for (i = 0; i < ci->values_num; i++)
267 if (ci->values[i].type != OCONFIG_TYPE_STRING)
268 {
269 WARNING ("snmp plugin: `Values' needs only string argument.");
270 return (-1);
271 }
273 sfree (dd->values);
274 dd->values_len = 0;
275 dd->values = (oid_t *) malloc (sizeof (oid_t) * ci->values_num);
276 if (dd->values == NULL)
277 return (-1);
278 dd->values_len = ci->values_num;
280 for (i = 0; i < ci->values_num; i++)
281 {
282 dd->values[i].oid_len = MAX_OID_LEN;
284 if (NULL == snmp_parse_oid (ci->values[i].value.string,
285 dd->values[i].oid, &dd->values[i].oid_len))
286 {
287 ERROR ("snmp plugin: snmp_parse_oid (%s) failed.",
288 ci->values[i].value.string);
289 free (dd->values);
290 dd->values = NULL;
291 dd->values_len = 0;
292 return (-1);
293 }
294 }
296 return (0);
297 } /* int csnmp_config_add_data_instance */
299 static int csnmp_config_add_data_shift (data_definition_t *dd, oconfig_item_t *ci)
300 {
301 if ((ci->values_num != 1)
302 || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
303 {
304 WARNING ("snmp plugin: The `Scale' config option needs exactly one number argument.");
305 return (-1);
306 }
308 dd->shift = ci->values[0].value.number;
310 return (0);
311 } /* int csnmp_config_add_data_shift */
313 static int csnmp_config_add_data_scale (data_definition_t *dd, oconfig_item_t *ci)
314 {
315 if ((ci->values_num != 1)
316 || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
317 {
318 WARNING ("snmp plugin: The `Scale' config option needs exactly one number argument.");
319 return (-1);
320 }
322 dd->scale = ci->values[0].value.number;
324 return (0);
325 } /* int csnmp_config_add_data_scale */
327 static int csnmp_config_add_data (oconfig_item_t *ci)
328 {
329 data_definition_t *dd;
330 int status = 0;
331 int i;
333 if ((ci->values_num != 1)
334 || (ci->values[0].type != OCONFIG_TYPE_STRING))
335 {
336 WARNING ("snmp plugin: The `Data' config option needs exactly one string argument.");
337 return (-1);
338 }
340 dd = (data_definition_t *) malloc (sizeof (data_definition_t));
341 if (dd == NULL)
342 return (-1);
343 memset (dd, '\0', sizeof (data_definition_t));
345 dd->name = strdup (ci->values[0].value.string);
346 if (dd->name == NULL)
347 {
348 free (dd);
349 return (-1);
350 }
351 dd->scale = 1.0;
352 dd->shift = 0.0;
354 for (i = 0; i < ci->children_num; i++)
355 {
356 oconfig_item_t *option = ci->children + i;
357 status = 0;
359 if (strcasecmp ("Type", option->key) == 0)
360 status = csnmp_config_add_data_type (dd, option);
361 else if (strcasecmp ("Table", option->key) == 0)
362 status = csnmp_config_add_data_table (dd, option);
363 else if (strcasecmp ("Instance", option->key) == 0)
364 status = csnmp_config_add_data_instance (dd, option);
365 else if (strcasecmp ("InstancePrefix", option->key) == 0)
366 status = csnmp_config_add_data_instance_prefix (dd, option);
367 else if (strcasecmp ("Values", option->key) == 0)
368 status = csnmp_config_add_data_values (dd, option);
369 else if (strcasecmp ("Shift", option->key) == 0)
370 status = csnmp_config_add_data_shift (dd, option);
371 else if (strcasecmp ("Scale", option->key) == 0)
372 status = csnmp_config_add_data_scale (dd, option);
373 else
374 {
375 WARNING ("snmp plugin: Option `%s' not allowed here.", option->key);
376 status = -1;
377 }
379 if (status != 0)
380 break;
381 } /* for (ci->children) */
383 while (status == 0)
384 {
385 if (dd->type == NULL)
386 {
387 WARNING ("snmp plugin: `Type' not given for data `%s'", dd->name);
388 status = -1;
389 break;
390 }
391 if (dd->values == NULL)
392 {
393 WARNING ("snmp plugin: No `Value' given for data `%s'", dd->name);
394 status = -1;
395 break;
396 }
398 break;
399 } /* while (status == 0) */
401 if (status != 0)
402 {
403 sfree (dd->name);
404 sfree (dd->instance_prefix);
405 sfree (dd->values);
406 sfree (dd);
407 return (-1);
408 }
410 DEBUG ("snmp plugin: dd = { name = %s, type = %s, is_table = %s, values_len = %i }",
411 dd->name, dd->type, (dd->is_table != 0) ? "true" : "false", dd->values_len);
413 if (data_head == NULL)
414 data_head = dd;
415 else
416 {
417 data_definition_t *last;
418 last = data_head;
419 while (last->next != NULL)
420 last = last->next;
421 last->next = dd;
422 }
424 return (0);
425 } /* int csnmp_config_add_data */
427 static int csnmp_config_add_host_address (host_definition_t *hd, oconfig_item_t *ci)
428 {
429 if ((ci->values_num != 1)
430 || (ci->values[0].type != OCONFIG_TYPE_STRING))
431 {
432 WARNING ("snmp plugin: The `Address' config option needs exactly one string argument.");
433 return (-1);
434 }
436 if (hd->address == NULL)
437 free (hd->address);
439 hd->address = strdup (ci->values[0].value.string);
440 if (hd->address == NULL)
441 return (-1);
443 DEBUG ("snmp plugin: host = %s; host->address = %s;",
444 hd->name, hd->address);
446 return (0);
447 } /* int csnmp_config_add_host_address */
449 static int csnmp_config_add_host_community (host_definition_t *hd, oconfig_item_t *ci)
450 {
451 if ((ci->values_num != 1)
452 || (ci->values[0].type != OCONFIG_TYPE_STRING))
453 {
454 WARNING ("snmp plugin: The `Community' config option needs exactly one string argument.");
455 return (-1);
456 }
458 if (hd->community == NULL)
459 free (hd->community);
461 hd->community = strdup (ci->values[0].value.string);
462 if (hd->community == NULL)
463 return (-1);
465 DEBUG ("snmp plugin: host = %s; host->community = %s;",
466 hd->name, hd->community);
468 return (0);
469 } /* int csnmp_config_add_host_community */
471 static int csnmp_config_add_host_version (host_definition_t *hd, oconfig_item_t *ci)
472 {
473 int version;
475 if ((ci->values_num != 1)
476 || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
477 {
478 WARNING ("snmp plugin: The `Version' config option needs exactly one number argument.");
479 return (-1);
480 }
482 version = (int) ci->values[0].value.number;
483 if ((version != 1) && (version != 2))
484 {
485 WARNING ("snmp plugin: `Version' must either be `1' or `2'.");
486 return (-1);
487 }
489 hd->version = version;
491 return (0);
492 } /* int csnmp_config_add_host_address */
494 static int csnmp_config_add_host_collect (host_definition_t *host,
495 oconfig_item_t *ci)
496 {
497 data_definition_t *data;
498 data_definition_t **data_list;
499 int data_list_len;
500 int i;
502 if (ci->values_num < 1)
503 {
504 WARNING ("snmp plugin: `Collect' needs at least one argument.");
505 return (-1);
506 }
508 for (i = 0; i < ci->values_num; i++)
509 if (ci->values[i].type != OCONFIG_TYPE_STRING)
510 {
511 WARNING ("snmp plugin: All arguments to `Collect' must be strings.");
512 return (-1);
513 }
515 data_list_len = host->data_list_len + ci->values_num;
516 data_list = (data_definition_t **) realloc (host->data_list,
517 sizeof (data_definition_t *) * data_list_len);
518 if (data_list == NULL)
519 return (-1);
520 host->data_list = data_list;
522 for (i = 0; i < ci->values_num; i++)
523 {
524 for (data = data_head; data != NULL; data = data->next)
525 if (strcasecmp (ci->values[i].value.string, data->name) == 0)
526 break;
528 if (data == NULL)
529 {
530 WARNING ("snmp plugin: No such data configured: `%s'",
531 ci->values[i].value.string);
532 continue;
533 }
535 DEBUG ("snmp plugin: Collect: host = %s, data[%i] = %s;",
536 host->name, host->data_list_len, data->name);
538 host->data_list[host->data_list_len] = data;
539 host->data_list_len++;
540 } /* for (values_num) */
542 return (0);
543 } /* int csnmp_config_add_host_collect */
545 static int csnmp_config_add_host (oconfig_item_t *ci)
546 {
547 host_definition_t *hd;
548 int status = 0;
549 int i;
551 /* Registration stuff. */
552 char cb_name[DATA_MAX_NAME_LEN];
553 user_data_t cb_data;
554 struct timespec cb_interval;
556 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
557 {
558 WARNING ("snmp plugin: `Host' needs exactly one string argument.");
559 return (-1);
560 }
562 hd = (host_definition_t *) malloc (sizeof (host_definition_t));
563 if (hd == NULL)
564 return (-1);
565 memset (hd, '\0', sizeof (host_definition_t));
566 hd->version = 2;
567 C_COMPLAIN_INIT (&hd->complaint);
569 hd->name = strdup (ci->values[0].value.string);
570 if (hd->name == NULL)
571 {
572 free (hd);
573 return (-1);
574 }
576 hd->sess_handle = NULL;
577 hd->interval = 0;
579 for (i = 0; i < ci->children_num; i++)
580 {
581 oconfig_item_t *option = ci->children + i;
582 status = 0;
584 if (strcasecmp ("Address", option->key) == 0)
585 status = csnmp_config_add_host_address (hd, option);
586 else if (strcasecmp ("Community", option->key) == 0)
587 status = csnmp_config_add_host_community (hd, option);
588 else if (strcasecmp ("Version", option->key) == 0)
589 status = csnmp_config_add_host_version (hd, option);
590 else if (strcasecmp ("Collect", option->key) == 0)
591 csnmp_config_add_host_collect (hd, option);
592 else if (strcasecmp ("Interval", option->key) == 0)
593 cf_util_get_cdtime (option, &hd->interval);
594 else
595 {
596 WARNING ("snmp plugin: csnmp_config_add_host: Option `%s' not allowed here.", option->key);
597 status = -1;
598 }
600 if (status != 0)
601 break;
602 } /* for (ci->children) */
604 while (status == 0)
605 {
606 if (hd->address == NULL)
607 {
608 WARNING ("snmp plugin: `Address' not given for host `%s'", hd->name);
609 status = -1;
610 break;
611 }
612 if (hd->community == NULL)
613 {
614 WARNING ("snmp plugin: `Community' not given for host `%s'", hd->name);
615 status = -1;
616 break;
617 }
619 break;
620 } /* while (status == 0) */
622 if (status != 0)
623 {
624 csnmp_host_definition_destroy (hd);
625 return (-1);
626 }
628 DEBUG ("snmp plugin: hd = { name = %s, address = %s, community = %s, version = %i }",
629 hd->name, hd->address, hd->community, hd->version);
631 ssnprintf (cb_name, sizeof (cb_name), "snmp-%s", hd->name);
633 memset (&cb_data, 0, sizeof (cb_data));
634 cb_data.data = hd;
635 cb_data.free_func = csnmp_host_definition_destroy;
637 CDTIME_T_TO_TIMESPEC (hd->interval, &cb_interval);
639 status = plugin_register_complex_read (/* group = */ NULL, cb_name,
640 csnmp_read_host, /* interval = */ &cb_interval,
641 /* user_data = */ &cb_data);
642 if (status != 0)
643 {
644 ERROR ("snmp plugin: Registering complex read function failed.");
645 csnmp_host_definition_destroy (hd);
646 return (-1);
647 }
649 return (0);
650 } /* int csnmp_config_add_host */
652 static int csnmp_config (oconfig_item_t *ci)
653 {
654 int i;
656 call_snmp_init_once ();
658 for (i = 0; i < ci->children_num; i++)
659 {
660 oconfig_item_t *child = ci->children + i;
661 if (strcasecmp ("Data", child->key) == 0)
662 csnmp_config_add_data (child);
663 else if (strcasecmp ("Host", child->key) == 0)
664 csnmp_config_add_host (child);
665 else
666 {
667 WARNING ("snmp plugin: Ignoring unknown config option `%s'.", child->key);
668 }
669 } /* for (ci->children) */
671 return (0);
672 } /* int csnmp_config */
674 /* }}} End of the config stuff. Now the interesting part begins */
676 static void csnmp_host_open_session (host_definition_t *host)
677 {
678 struct snmp_session sess;
680 if (host->sess_handle != NULL)
681 csnmp_host_close_session (host);
683 snmp_sess_init (&sess);
684 sess.peername = host->address;
685 sess.community = (u_char *) host->community;
686 sess.community_len = strlen (host->community);
687 sess.version = (host->version == 1) ? SNMP_VERSION_1 : SNMP_VERSION_2c;
689 /* snmp_sess_open will copy the `struct snmp_session *'. */
690 host->sess_handle = snmp_sess_open (&sess);
692 if (host->sess_handle == NULL)
693 {
694 char *errstr = NULL;
696 snmp_error (&sess, NULL, NULL, &errstr);
698 ERROR ("snmp plugin: host %s: snmp_sess_open failed: %s",
699 host->name, (errstr == NULL) ? "Unknown problem" : errstr);
700 sfree (errstr);
701 }
702 } /* void csnmp_host_open_session */
704 /* TODO: Check if negative values wrap around. Problem: negative temperatures. */
705 static value_t csnmp_value_list_to_value (struct variable_list *vl, int type,
706 double scale, double shift,
707 const char *host_name, const char *data_name)
708 {
709 value_t ret;
710 uint64_t tmp_unsigned = 0;
711 int64_t tmp_signed = 0;
712 _Bool defined = 1;
713 /* Set to true when the original SNMP type appears to have been signed. */
714 _Bool prefer_signed = 0;
716 if ((vl->type == ASN_INTEGER)
717 || (vl->type == ASN_UINTEGER)
718 || (vl->type == ASN_COUNTER)
719 #ifdef ASN_TIMETICKS
720 || (vl->type == ASN_TIMETICKS)
721 #endif
722 || (vl->type == ASN_GAUGE))
723 {
724 tmp_unsigned = (uint32_t) *vl->val.integer;
725 tmp_signed = (int32_t) *vl->val.integer;
727 if ((vl->type == ASN_INTEGER)
728 || (vl->type == ASN_GAUGE))
729 prefer_signed = 1;
731 DEBUG ("snmp plugin: Parsed int32 value is %"PRIu64".", tmp_unsigned);
732 }
733 else if (vl->type == ASN_COUNTER64)
734 {
735 tmp_unsigned = (uint32_t) vl->val.counter64->high;
736 tmp_unsigned = tmp_unsigned << 32;
737 tmp_unsigned += (uint32_t) vl->val.counter64->low;
738 tmp_signed = (int64_t) tmp_unsigned;
739 DEBUG ("snmp plugin: Parsed int64 value is %"PRIu64".", tmp_unsigned);
740 }
741 else if (vl->type == ASN_OCTET_STR)
742 {
743 /* We'll handle this later.. */
744 }
745 else
746 {
747 char oid_buffer[1024];
749 memset (oid_buffer, 0, sizeof (oid_buffer));
750 snprint_objid (oid_buffer, sizeof (oid_buffer) - 1,
751 vl->name, vl->name_length);
753 #ifdef ASN_NULL
754 if (vl->type == ASN_NULL)
755 INFO ("snmp plugin: OID \"%s\" is undefined (type ASN_NULL)",
756 oid_buffer);
757 else
758 #endif
759 WARNING ("snmp plugin: I don't know the ASN type #%i "
760 "(OID: \"%s\", data block \"%s\", host block \"%s\")",
761 (int) vl->type, oid_buffer,
762 (data_name != NULL) ? data_name : "UNKNOWN",
763 (host_name != NULL) ? host_name : "UNKNOWN");
765 defined = 0;
766 }
768 if (vl->type == ASN_OCTET_STR)
769 {
770 int status = -1;
772 if (vl->val.string != NULL)
773 {
774 char string[64];
775 size_t string_length;
777 string_length = sizeof (string) - 1;
778 if (vl->val_len < string_length)
779 string_length = vl->val_len;
781 /* The strings we get from the Net-SNMP library may not be null
782 * terminated. That is why we're using `memcpy' here and not `strcpy'.
783 * `string_length' is set to `vl->val_len' which holds the length of the
784 * string. -octo */
785 memcpy (string, vl->val.string, string_length);
786 string[string_length] = 0;
788 status = parse_value (string, &ret, type);
789 if (status != 0)
790 {
791 ERROR ("snmp plugin: csnmp_value_list_to_value: Parsing string as %s failed: %s",
792 DS_TYPE_TO_STRING (type), string);
793 }
794 }
796 if (status != 0)
797 {
798 switch (type)
799 {
800 case DS_TYPE_COUNTER:
801 case DS_TYPE_DERIVE:
802 case DS_TYPE_ABSOLUTE:
803 memset (&ret, 0, sizeof (ret));
804 break;
806 case DS_TYPE_GAUGE:
807 ret.gauge = NAN;
808 break;
810 default:
811 ERROR ("snmp plugin: csnmp_value_list_to_value: Unknown "
812 "data source type: %i.", type);
813 ret.gauge = NAN;
814 }
815 }
816 } /* if (vl->type == ASN_OCTET_STR) */
817 else if (type == DS_TYPE_COUNTER)
818 {
819 ret.counter = tmp_unsigned;
820 }
821 else if (type == DS_TYPE_GAUGE)
822 {
823 if (!defined)
824 ret.gauge = NAN;
825 else if (prefer_signed)
826 ret.gauge = (scale * tmp_signed) + shift;
827 else
828 ret.gauge = (scale * tmp_unsigned) + shift;
829 }
830 else if (type == DS_TYPE_DERIVE)
831 {
832 if (prefer_signed)
833 ret.derive = (derive_t) tmp_signed;
834 else
835 ret.derive = (derive_t) tmp_unsigned;
836 }
837 else if (type == DS_TYPE_ABSOLUTE)
838 {
839 ret.absolute = (absolute_t) tmp_unsigned;
840 }
841 else
842 {
843 ERROR ("snmp plugin: csnmp_value_list_to_value: Unknown data source "
844 "type: %i.", type);
845 ret.gauge = NAN;
846 }
848 return (ret);
849 } /* value_t csnmp_value_list_to_value */
851 /* Returns true if all OIDs have left their subtree */
852 static int csnmp_check_res_left_subtree (const host_definition_t *host,
853 const data_definition_t *data,
854 struct snmp_pdu *res)
855 {
856 struct variable_list *vb;
857 int num_checked;
858 int num_left_subtree;
859 int i;
861 vb = res->variables;
862 if (vb == NULL)
863 return (-1);
865 num_checked = 0;
866 num_left_subtree = 0;
868 /* check all the variables and count how many have left their subtree */
869 for (vb = res->variables, i = 0;
870 (vb != NULL) && (i < data->values_len);
871 vb = vb->next_variable, i++)
872 {
873 num_checked++;
874 if (snmp_oid_ncompare (data->values[i].oid,
875 data->values[i].oid_len,
876 vb->name, vb->name_length,
877 data->values[i].oid_len) != 0)
878 num_left_subtree++;
879 }
881 /* check if enough variables have been returned */
882 if (i < data->values_len)
883 {
884 ERROR ("snmp plugin: host %s: Expected %i variables, but got only %i",
885 host->name, data->values_len, i);
886 return (-1);
887 }
889 if (data->instance.oid.oid_len > 0)
890 {
891 if (vb == NULL)
892 {
893 ERROR ("snmp plugin: host %s: Expected one more variable for "
894 "the instance..", host->name);
895 return (-1);
896 }
898 num_checked++;
899 if (snmp_oid_ncompare (data->instance.oid.oid,
900 data->instance.oid.oid_len,
901 vb->name, vb->name_length,
902 data->instance.oid.oid_len) != 0)
903 num_left_subtree++;
904 }
906 DEBUG ("snmp plugin: csnmp_check_res_left_subtree: %i of %i variables have "
907 "left their subtree",
908 num_left_subtree, num_checked);
909 if (num_left_subtree >= num_checked)
910 return (1);
911 return (0);
912 } /* int csnmp_check_res_left_subtree */
914 static int csnmp_strvbcopy_hexstring (char *dst, /* {{{ */
915 const struct variable_list *vb, size_t dst_size)
916 {
917 char *buffer_ptr;
918 size_t buffer_free;
919 size_t i;
921 buffer_ptr = dst;
922 buffer_free = dst_size;
924 for (i = 0; i < vb->val_len; i++)
925 {
926 int status;
928 status = snprintf (buffer_ptr, buffer_free,
929 (i == 0) ? "%02x" : ":%02x", (unsigned int) vb->val.bitstring[i]);
931 if (status >= buffer_free)
932 {
933 buffer_ptr += (buffer_free - 1);
934 *buffer_ptr = 0;
935 return (dst_size + (buffer_free - status));
936 }
937 else /* if (status < buffer_free) */
938 {
939 buffer_ptr += status;
940 buffer_free -= status;
941 }
942 }
944 return ((int) (dst_size - buffer_free));
945 } /* }}} int csnmp_strvbcopy_hexstring */
947 static int csnmp_strvbcopy (char *dst, /* {{{ */
948 const struct variable_list *vb, size_t dst_size)
949 {
950 char *src;
951 size_t num_chars;
952 size_t i;
954 if (vb->type == ASN_OCTET_STR)
955 src = (char *) vb->val.string;
956 else if (vb->type == ASN_BIT_STR)
957 src = (char *) vb->val.bitstring;
958 else
959 {
960 dst[0] = 0;
961 return (EINVAL);
962 }
964 num_chars = dst_size - 1;
965 if (num_chars > vb->val_len)
966 num_chars = vb->val_len;
968 for (i = 0; i < num_chars; i++)
969 {
970 /* Check for control characters. */
971 if ((unsigned char)src[i] < 32)
972 return (csnmp_strvbcopy_hexstring (dst, vb, dst_size));
973 dst[i] = src[i];
974 }
975 dst[num_chars] = 0;
977 return ((int) vb->val_len);
978 } /* }}} int csnmp_strvbcopy */
980 static int csnmp_instance_list_add (csnmp_list_instances_t **head,
981 csnmp_list_instances_t **tail,
982 const struct snmp_pdu *res,
983 const host_definition_t *hd, const data_definition_t *dd)
984 {
985 csnmp_list_instances_t *il;
986 struct variable_list *vb;
988 /* Set vb on the last variable */
989 for (vb = res->variables;
990 (vb != NULL) && (vb->next_variable != NULL);
991 vb = vb->next_variable)
992 /* do nothing */;
993 if (vb == NULL)
994 return (-1);
996 il = (csnmp_list_instances_t *) malloc (sizeof (csnmp_list_instances_t));
997 if (il == NULL)
998 {
999 ERROR ("snmp plugin: malloc failed.");
1000 return (-1);
1001 }
1002 il->subid = vb->name[vb->name_length - 1];
1003 il->next = NULL;
1005 /* Get instance name */
1006 if ((vb->type == ASN_OCTET_STR) || (vb->type == ASN_BIT_STR))
1007 {
1008 char *ptr;
1010 csnmp_strvbcopy (il->instance, vb, sizeof (il->instance));
1012 for (ptr = il->instance; *ptr != '\0'; ptr++)
1013 {
1014 if ((*ptr > 0) && (*ptr < 32))
1015 *ptr = ' ';
1016 else if (*ptr == '/')
1017 *ptr = '_';
1018 }
1019 DEBUG ("snmp plugin: il->instance = `%s';", il->instance);
1020 }
1021 else
1022 {
1023 value_t val = csnmp_value_list_to_value (vb, DS_TYPE_COUNTER,
1024 /* scale = */ 1.0, /* shift = */ 0.0, hd->name, dd->name);
1025 ssnprintf (il->instance, sizeof (il->instance),
1026 "%llu", val.counter);
1027 }
1029 /* TODO: Debugging output */
1031 if (*head == NULL)
1032 *head = il;
1033 else
1034 (*tail)->next = il;
1035 *tail = il;
1037 return (0);
1038 } /* int csnmp_instance_list_add */
1040 static int csnmp_dispatch_table (host_definition_t *host, data_definition_t *data,
1041 csnmp_list_instances_t *instance_list,
1042 csnmp_table_values_t **value_table)
1043 {
1044 const data_set_t *ds;
1045 value_list_t vl = VALUE_LIST_INIT;
1047 csnmp_list_instances_t *instance_list_ptr;
1048 csnmp_table_values_t **value_table_ptr;
1050 int i;
1051 oid subid;
1052 int have_more;
1054 ds = plugin_get_ds (data->type);
1055 if (!ds)
1056 {
1057 ERROR ("snmp plugin: DataSet `%s' not defined.", data->type);
1058 return (-1);
1059 }
1060 assert (ds->ds_num == data->values_len);
1062 instance_list_ptr = instance_list;
1064 value_table_ptr = (csnmp_table_values_t **) malloc (sizeof (csnmp_table_values_t *)
1065 * data->values_len);
1066 if (value_table_ptr == NULL)
1067 return (-1);
1068 for (i = 0; i < data->values_len; i++)
1069 value_table_ptr[i] = value_table[i];
1071 vl.values_len = ds->ds_num;
1072 vl.values = (value_t *) malloc (sizeof (value_t) * vl.values_len);
1073 if (vl.values == NULL)
1074 {
1075 ERROR ("snmp plugin: malloc failed.");
1076 sfree (value_table_ptr);
1077 return (-1);
1078 }
1080 sstrncpy (vl.host, host->name, sizeof (vl.host));
1081 sstrncpy (vl.plugin, "snmp", sizeof (vl.plugin));
1083 vl.interval = host->interval;
1085 subid = 0;
1086 have_more = 1;
1088 while (have_more != 0)
1089 {
1090 if (instance_list != NULL)
1091 {
1092 while ((instance_list_ptr != NULL)
1093 && (instance_list_ptr->subid < subid))
1094 instance_list_ptr = instance_list_ptr->next;
1096 if (instance_list_ptr == NULL)
1097 {
1098 have_more = 0;
1099 continue;
1100 }
1101 else if (instance_list_ptr->subid > subid)
1102 {
1103 subid = instance_list_ptr->subid;
1104 continue;
1105 }
1106 } /* if (instance_list != NULL) */
1108 for (i = 0; i < data->values_len; i++)
1109 {
1110 while ((value_table_ptr[i] != NULL)
1111 && (value_table_ptr[i]->subid < subid))
1112 value_table_ptr[i] = value_table_ptr[i]->next;
1114 if (value_table_ptr[i] == NULL)
1115 {
1116 have_more = 0;
1117 break;
1118 }
1119 else if (value_table_ptr[i]->subid > subid)
1120 {
1121 subid = value_table_ptr[i]->subid;
1122 break;
1123 }
1124 } /* for (i = 0; i < columns; i++) */
1125 /* The subid has been increased - start scanning from the beginning
1126 * again.. */
1127 if (i < data->values_len)
1128 continue;
1130 /* if we reach this line, all value_table_ptr[i] are non-NULL and are set
1131 * to the same subid. instance_list_ptr is either NULL or points to the
1132 * same subid, too. */
1133 #if COLLECT_DEBUG
1134 for (i = 1; i < data->values_len; i++)
1135 {
1136 assert (value_table_ptr[i] != NULL);
1137 assert (value_table_ptr[i-1]->subid == value_table_ptr[i]->subid);
1138 }
1139 assert ((instance_list_ptr == NULL)
1140 || (instance_list_ptr->subid == value_table_ptr[0]->subid));
1141 #endif
1143 sstrncpy (vl.type, data->type, sizeof (vl.type));
1145 {
1146 char temp[DATA_MAX_NAME_LEN];
1148 if (instance_list_ptr == NULL)
1149 ssnprintf (temp, sizeof (temp), "%"PRIu32, (uint32_t) subid);
1150 else
1151 sstrncpy (temp, instance_list_ptr->instance, sizeof (temp));
1153 if (data->instance_prefix == NULL)
1154 sstrncpy (vl.type_instance, temp, sizeof (vl.type_instance));
1155 else
1156 ssnprintf (vl.type_instance, sizeof (vl.type_instance), "%s%s",
1157 data->instance_prefix, temp);
1158 }
1160 for (i = 0; i < data->values_len; i++)
1161 vl.values[i] = value_table_ptr[i]->value;
1163 /* If we get here `vl.type_instance' and all `vl.values' have been set */
1164 plugin_dispatch_values (&vl);
1166 subid++;
1167 } /* while (have_more != 0) */
1169 sfree (vl.values);
1170 sfree (value_table_ptr);
1172 return (0);
1173 } /* int csnmp_dispatch_table */
1175 static int csnmp_read_table (host_definition_t *host, data_definition_t *data)
1176 {
1177 struct snmp_pdu *req;
1178 struct snmp_pdu *res;
1179 struct variable_list *vb;
1181 const data_set_t *ds;
1182 oid_t *oid_list;
1183 uint32_t oid_list_len;
1185 int status;
1186 int i;
1188 /* `value_table' and `value_table_ptr' implement a linked list for each
1189 * value. `instance_list' and `instance_list_ptr' implement a linked list of
1190 * instance names. This is used to jump gaps in the table. */
1191 csnmp_list_instances_t *instance_list;
1192 csnmp_list_instances_t *instance_list_ptr;
1193 csnmp_table_values_t **value_table;
1194 csnmp_table_values_t **value_table_ptr;
1196 DEBUG ("snmp plugin: csnmp_read_table (host = %s, data = %s)",
1197 host->name, data->name);
1199 if (host->sess_handle == NULL)
1200 {
1201 DEBUG ("snmp plugin: csnmp_read_table: host->sess_handle == NULL");
1202 return (-1);
1203 }
1205 ds = plugin_get_ds (data->type);
1206 if (!ds)
1207 {
1208 ERROR ("snmp plugin: DataSet `%s' not defined.", data->type);
1209 return (-1);
1210 }
1212 if (ds->ds_num != data->values_len)
1213 {
1214 ERROR ("snmp plugin: DataSet `%s' requires %i values, but config talks about %i",
1215 data->type, ds->ds_num, data->values_len);
1216 return (-1);
1217 }
1219 /* We need a copy of all the OIDs, because GETNEXT will destroy them. */
1220 oid_list_len = data->values_len + 1;
1221 oid_list = (oid_t *) malloc (sizeof (oid_t) * (oid_list_len));
1222 if (oid_list == NULL)
1223 {
1224 ERROR ("snmp plugin: csnmp_read_table: malloc failed.");
1225 return (-1);
1226 }
1227 memcpy (oid_list, data->values, data->values_len * sizeof (oid_t));
1228 if (data->instance.oid.oid_len > 0)
1229 memcpy (oid_list + data->values_len, &data->instance.oid, sizeof (oid_t));
1230 else
1231 oid_list_len--;
1233 /* Allocate the `value_table' */
1234 value_table = (csnmp_table_values_t **) malloc (sizeof (csnmp_table_values_t *)
1235 * 2 * data->values_len);
1236 if (value_table == NULL)
1237 {
1238 ERROR ("snmp plugin: csnmp_read_table: malloc failed.");
1239 sfree (oid_list);
1240 return (-1);
1241 }
1242 memset (value_table, '\0', sizeof (csnmp_table_values_t *) * 2 * data->values_len);
1243 value_table_ptr = value_table + data->values_len;
1245 instance_list = NULL;
1246 instance_list_ptr = NULL;
1248 status = 0;
1249 while (status == 0)
1250 {
1251 req = snmp_pdu_create (SNMP_MSG_GETNEXT);
1252 if (req == NULL)
1253 {
1254 ERROR ("snmp plugin: snmp_pdu_create failed.");
1255 status = -1;
1256 break;
1257 }
1259 for (i = 0; (uint32_t) i < oid_list_len; i++)
1260 snmp_add_null_var (req, oid_list[i].oid, oid_list[i].oid_len);
1262 res = NULL;
1263 status = snmp_sess_synch_response (host->sess_handle, req, &res);
1265 if ((status != STAT_SUCCESS) || (res == NULL))
1266 {
1267 char *errstr = NULL;
1269 snmp_sess_error (host->sess_handle, NULL, NULL, &errstr);
1271 c_complain (LOG_ERR, &host->complaint,
1272 "snmp plugin: host %s: snmp_sess_synch_response failed: %s",
1273 host->name, (errstr == NULL) ? "Unknown problem" : errstr);
1275 if (res != NULL)
1276 snmp_free_pdu (res);
1277 res = NULL;
1279 sfree (errstr);
1280 csnmp_host_close_session (host);
1282 status = -1;
1283 break;
1284 }
1285 status = 0;
1286 assert (res != NULL);
1287 c_release (LOG_INFO, &host->complaint,
1288 "snmp plugin: host %s: snmp_sess_synch_response successful.",
1289 host->name);
1291 vb = res->variables;
1292 if (vb == NULL)
1293 {
1294 status = -1;
1295 break;
1296 }
1298 /* Check if all values (and possibly the instance) have left their
1299 * subtree */
1300 if (csnmp_check_res_left_subtree (host, data, res) != 0)
1301 {
1302 status = 0;
1303 break;
1304 }
1306 /* if an instance-OID is configured.. */
1307 if (data->instance.oid.oid_len > 0)
1308 {
1309 /* Allocate a new `csnmp_list_instances_t', insert the instance name and
1310 * add it to the list */
1311 if (csnmp_instance_list_add (&instance_list, &instance_list_ptr,
1312 res, host, data) != 0)
1313 {
1314 ERROR ("snmp plugin: csnmp_instance_list_add failed.");
1315 status = -1;
1316 break;
1317 }
1319 /* Set vb on the last variable */
1320 for (vb = res->variables;
1321 (vb != NULL) && (vb->next_variable != NULL);
1322 vb = vb->next_variable)
1323 /* do nothing */;
1324 assert (vb != NULL);
1326 /* Copy OID to oid_list[data->values_len] */
1327 memcpy (oid_list[data->values_len].oid, vb->name,
1328 sizeof (oid) * vb->name_length);
1329 oid_list[data->values_len].oid_len = vb->name_length;
1330 }
1332 for (vb = res->variables, i = 0;
1333 (vb != NULL) && (i < data->values_len);
1334 vb = vb->next_variable, i++)
1335 {
1336 csnmp_table_values_t *vt;
1338 /* Check if we left the subtree */
1339 if (snmp_oid_ncompare (data->values[i].oid,
1340 data->values[i].oid_len,
1341 vb->name, vb->name_length,
1342 data->values[i].oid_len) != 0)
1343 {
1344 DEBUG ("snmp plugin: host = %s; data = %s; Value %i left its subtree.",
1345 host->name, data->name, i);
1346 continue;
1347 }
1349 if ((value_table_ptr[i] != NULL)
1350 && (vb->name[vb->name_length - 1] <= value_table_ptr[i]->subid))
1351 {
1352 DEBUG ("snmp plugin: host = %s; data = %s; i = %i; "
1353 "SUBID is not increasing.",
1354 host->name, data->name, i);
1355 continue;
1356 }
1358 vt = (csnmp_table_values_t *) malloc (sizeof (csnmp_table_values_t));
1359 if (vt == NULL)
1360 {
1361 ERROR ("snmp plugin: malloc failed.");
1362 status = -1;
1363 break;
1364 }
1366 vt->subid = vb->name[vb->name_length - 1];
1367 vt->value = csnmp_value_list_to_value (vb, ds->ds[i].type,
1368 data->scale, data->shift, host->name, data->name);
1369 vt->next = NULL;
1371 if (value_table_ptr[i] == NULL)
1372 value_table[i] = vt;
1373 else
1374 value_table_ptr[i]->next = vt;
1375 value_table_ptr[i] = vt;
1377 /* Copy OID to oid_list[i + 1] */
1378 memcpy (oid_list[i].oid, vb->name, sizeof (oid) * vb->name_length);
1379 oid_list[i].oid_len = vb->name_length;
1380 } /* for (i = data->values_len) */
1382 if (res != NULL)
1383 snmp_free_pdu (res);
1384 res = NULL;
1385 } /* while (status == 0) */
1387 if (res != NULL)
1388 snmp_free_pdu (res);
1389 res = NULL;
1391 if (status == 0)
1392 csnmp_dispatch_table (host, data, instance_list, value_table);
1394 /* Free all allocated variables here */
1395 while (instance_list != NULL)
1396 {
1397 instance_list_ptr = instance_list->next;
1398 sfree (instance_list);
1399 instance_list = instance_list_ptr;
1400 }
1402 for (i = 0; i < data->values_len; i++)
1403 {
1404 csnmp_table_values_t *tmp;
1405 while (value_table[i] != NULL)
1406 {
1407 tmp = value_table[i]->next;
1408 sfree (value_table[i]);
1409 value_table[i] = tmp;
1410 }
1411 }
1413 sfree (value_table);
1414 sfree (oid_list);
1416 return (0);
1417 } /* int csnmp_read_table */
1419 static int csnmp_read_value (host_definition_t *host, data_definition_t *data)
1420 {
1421 struct snmp_pdu *req;
1422 struct snmp_pdu *res;
1423 struct variable_list *vb;
1425 const data_set_t *ds;
1426 value_list_t vl = VALUE_LIST_INIT;
1428 int status;
1429 int i;
1431 DEBUG ("snmp plugin: csnmp_read_value (host = %s, data = %s)",
1432 host->name, data->name);
1434 if (host->sess_handle == NULL)
1435 {
1436 DEBUG ("snmp plugin: csnmp_read_table: host->sess_handle == NULL");
1437 return (-1);
1438 }
1440 ds = plugin_get_ds (data->type);
1441 if (!ds)
1442 {
1443 ERROR ("snmp plugin: DataSet `%s' not defined.", data->type);
1444 return (-1);
1445 }
1447 if (ds->ds_num != data->values_len)
1448 {
1449 ERROR ("snmp plugin: DataSet `%s' requires %i values, but config talks about %i",
1450 data->type, ds->ds_num, data->values_len);
1451 return (-1);
1452 }
1454 vl.values_len = ds->ds_num;
1455 vl.values = (value_t *) malloc (sizeof (value_t) * vl.values_len);
1456 if (vl.values == NULL)
1457 return (-1);
1458 for (i = 0; i < vl.values_len; i++)
1459 {
1460 if (ds->ds[i].type == DS_TYPE_COUNTER)
1461 vl.values[i].counter = 0;
1462 else
1463 vl.values[i].gauge = NAN;
1464 }
1466 sstrncpy (vl.host, host->name, sizeof (vl.host));
1467 sstrncpy (vl.plugin, "snmp", sizeof (vl.plugin));
1468 sstrncpy (vl.type, data->type, sizeof (vl.type));
1469 sstrncpy (vl.type_instance, data->instance.string, sizeof (vl.type_instance));
1471 vl.interval = host->interval;
1473 req = snmp_pdu_create (SNMP_MSG_GET);
1474 if (req == NULL)
1475 {
1476 ERROR ("snmp plugin: snmp_pdu_create failed.");
1477 sfree (vl.values);
1478 return (-1);
1479 }
1481 for (i = 0; i < data->values_len; i++)
1482 snmp_add_null_var (req, data->values[i].oid, data->values[i].oid_len);
1484 res = NULL;
1485 status = snmp_sess_synch_response (host->sess_handle, req, &res);
1487 if ((status != STAT_SUCCESS) || (res == NULL))
1488 {
1489 char *errstr = NULL;
1491 snmp_sess_error (host->sess_handle, NULL, NULL, &errstr);
1492 ERROR ("snmp plugin: host %s: snmp_sess_synch_response failed: %s",
1493 host->name, (errstr == NULL) ? "Unknown problem" : errstr);
1495 if (res != NULL)
1496 snmp_free_pdu (res);
1497 res = NULL;
1499 sfree (errstr);
1500 csnmp_host_close_session (host);
1502 return (-1);
1503 }
1506 for (vb = res->variables; vb != NULL; vb = vb->next_variable)
1507 {
1508 #if COLLECT_DEBUG
1509 char buffer[1024];
1510 snprint_variable (buffer, sizeof (buffer),
1511 vb->name, vb->name_length, vb);
1512 DEBUG ("snmp plugin: Got this variable: %s", buffer);
1513 #endif /* COLLECT_DEBUG */
1515 for (i = 0; i < data->values_len; i++)
1516 if (snmp_oid_compare (data->values[i].oid, data->values[i].oid_len,
1517 vb->name, vb->name_length) == 0)
1518 vl.values[i] = csnmp_value_list_to_value (vb, ds->ds[i].type,
1519 data->scale, data->shift, host->name, data->name);
1520 } /* for (res->variables) */
1522 if (res != NULL)
1523 snmp_free_pdu (res);
1524 res = NULL;
1526 DEBUG ("snmp plugin: -> plugin_dispatch_values (&vl);");
1527 plugin_dispatch_values (&vl);
1528 sfree (vl.values);
1530 return (0);
1531 } /* int csnmp_read_value */
1533 static int csnmp_read_host (user_data_t *ud)
1534 {
1535 host_definition_t *host;
1536 cdtime_t time_start;
1537 cdtime_t time_end;
1538 int status;
1539 int success;
1540 int i;
1542 host = ud->data;
1544 if (host->interval == 0)
1545 host->interval = interval_g;
1547 time_start = cdtime ();
1549 if (host->sess_handle == NULL)
1550 csnmp_host_open_session (host);
1552 if (host->sess_handle == NULL)
1553 return (-1);
1555 success = 0;
1556 for (i = 0; i < host->data_list_len; i++)
1557 {
1558 data_definition_t *data = host->data_list[i];
1560 if (data->is_table)
1561 status = csnmp_read_table (host, data);
1562 else
1563 status = csnmp_read_value (host, data);
1565 if (status == 0)
1566 success++;
1567 }
1569 time_end = cdtime ();
1570 if ((time_end - time_start) > host->interval)
1571 {
1572 WARNING ("snmp plugin: Host `%s' should be queried every %.3f "
1573 "seconds, but reading all values takes %.3f seconds.",
1574 host->name,
1575 CDTIME_T_TO_DOUBLE (host->interval),
1576 CDTIME_T_TO_DOUBLE (time_end - time_start));
1577 }
1579 if (success == 0)
1580 return (-1);
1582 return (0);
1583 } /* int csnmp_read_host */
1585 static int csnmp_init (void)
1586 {
1587 call_snmp_init_once ();
1589 return (0);
1590 } /* int csnmp_init */
1592 static int csnmp_shutdown (void)
1593 {
1594 data_definition_t *data_this;
1595 data_definition_t *data_next;
1597 /* When we get here, the read threads have been stopped and all the
1598 * `host_definition_t' will be freed. */
1599 DEBUG ("snmp plugin: Destroying all data definitions.");
1601 data_this = data_head;
1602 data_head = NULL;
1603 while (data_this != NULL)
1604 {
1605 data_next = data_this->next;
1607 sfree (data_this->name);
1608 sfree (data_this->type);
1609 sfree (data_this->values);
1610 sfree (data_this);
1612 data_this = data_next;
1613 }
1615 return (0);
1616 } /* int csnmp_shutdown */
1618 void module_register (void)
1619 {
1620 plugin_register_complex_config ("snmp", csnmp_config);
1621 plugin_register_init ("snmp", csnmp_init);
1622 plugin_register_shutdown ("snmp", csnmp_shutdown);
1623 } /* void module_register */
1625 /*
1626 * vim: shiftwidth=2 softtabstop=2 tabstop=8 fdm=marker
1627 */