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 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 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 * +-> csnmp_config_add_host_interval
163 */
164 static void call_snmp_init_once (void)
165 {
166 static int have_init = 0;
168 if (have_init == 0)
169 init_snmp (PACKAGE_NAME);
170 have_init = 1;
171 } /* void call_snmp_init_once */
173 static int csnmp_config_add_data_type (data_definition_t *dd, oconfig_item_t *ci)
174 {
175 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
176 {
177 WARNING ("snmp plugin: `Type' needs exactly one string argument.");
178 return (-1);
179 }
181 sfree (dd->type);
182 dd->type = strdup (ci->values[0].value.string);
183 if (dd->type == NULL)
184 return (-1);
186 return (0);
187 } /* int csnmp_config_add_data_type */
189 static int csnmp_config_add_data_table (data_definition_t *dd, oconfig_item_t *ci)
190 {
191 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_BOOLEAN))
192 {
193 WARNING ("snmp plugin: `Table' needs exactly one boolean argument.");
194 return (-1);
195 }
197 dd->is_table = ci->values[0].value.boolean ? 1 : 0;
199 return (0);
200 } /* int csnmp_config_add_data_table */
202 static int csnmp_config_add_data_instance (data_definition_t *dd, oconfig_item_t *ci)
203 {
204 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
205 {
206 WARNING ("snmp plugin: `Instance' needs exactly one string argument.");
207 return (-1);
208 }
210 if (dd->is_table)
211 {
212 /* Instance is an OID */
213 dd->instance.oid.oid_len = MAX_OID_LEN;
215 if (!read_objid (ci->values[0].value.string,
216 dd->instance.oid.oid, &dd->instance.oid.oid_len))
217 {
218 ERROR ("snmp plugin: read_objid (%s) failed.",
219 ci->values[0].value.string);
220 return (-1);
221 }
222 }
223 else
224 {
225 /* Instance is a simple string */
226 sstrncpy (dd->instance.string, ci->values[0].value.string,
227 sizeof (dd->instance.string));
228 }
230 return (0);
231 } /* int csnmp_config_add_data_instance */
233 static int csnmp_config_add_data_instance_prefix (data_definition_t *dd,
234 oconfig_item_t *ci)
235 {
236 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
237 {
238 WARNING ("snmp plugin: `InstancePrefix' needs exactly one string argument.");
239 return (-1);
240 }
242 if (!dd->is_table)
243 {
244 WARNING ("snmp plugin: data %s: InstancePrefix is ignored when `Table' "
245 "is set to `false'.", dd->name);
246 return (-1);
247 }
249 sfree (dd->instance_prefix);
250 dd->instance_prefix = strdup (ci->values[0].value.string);
251 if (dd->instance_prefix == NULL)
252 return (-1);
254 return (0);
255 } /* int csnmp_config_add_data_instance_prefix */
257 static int csnmp_config_add_data_values (data_definition_t *dd, oconfig_item_t *ci)
258 {
259 int i;
261 if (ci->values_num < 1)
262 {
263 WARNING ("snmp plugin: `Values' needs at least one argument.");
264 return (-1);
265 }
267 for (i = 0; i < ci->values_num; i++)
268 if (ci->values[i].type != OCONFIG_TYPE_STRING)
269 {
270 WARNING ("snmp plugin: `Values' needs only string argument.");
271 return (-1);
272 }
274 sfree (dd->values);
275 dd->values_len = 0;
276 dd->values = (oid_t *) malloc (sizeof (oid_t) * ci->values_num);
277 if (dd->values == NULL)
278 return (-1);
279 dd->values_len = ci->values_num;
281 for (i = 0; i < ci->values_num; i++)
282 {
283 dd->values[i].oid_len = MAX_OID_LEN;
285 if (NULL == snmp_parse_oid (ci->values[i].value.string,
286 dd->values[i].oid, &dd->values[i].oid_len))
287 {
288 ERROR ("snmp plugin: snmp_parse_oid (%s) failed.",
289 ci->values[i].value.string);
290 free (dd->values);
291 dd->values = NULL;
292 dd->values_len = 0;
293 return (-1);
294 }
295 }
297 return (0);
298 } /* int csnmp_config_add_data_instance */
300 static int csnmp_config_add_data_shift (data_definition_t *dd, oconfig_item_t *ci)
301 {
302 if ((ci->values_num != 1)
303 || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
304 {
305 WARNING ("snmp plugin: The `Scale' config option needs exactly one number argument.");
306 return (-1);
307 }
309 dd->shift = ci->values[0].value.number;
311 return (0);
312 } /* int csnmp_config_add_data_shift */
314 static int csnmp_config_add_data_scale (data_definition_t *dd, oconfig_item_t *ci)
315 {
316 if ((ci->values_num != 1)
317 || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
318 {
319 WARNING ("snmp plugin: The `Scale' config option needs exactly one number argument.");
320 return (-1);
321 }
323 dd->scale = ci->values[0].value.number;
325 return (0);
326 } /* int csnmp_config_add_data_scale */
328 static int csnmp_config_add_data (oconfig_item_t *ci)
329 {
330 data_definition_t *dd;
331 int status = 0;
332 int i;
334 if ((ci->values_num != 1)
335 || (ci->values[0].type != OCONFIG_TYPE_STRING))
336 {
337 WARNING ("snmp plugin: The `Data' config option needs exactly one string argument.");
338 return (-1);
339 }
341 dd = (data_definition_t *) malloc (sizeof (data_definition_t));
342 if (dd == NULL)
343 return (-1);
344 memset (dd, '\0', sizeof (data_definition_t));
346 dd->name = strdup (ci->values[0].value.string);
347 if (dd->name == NULL)
348 {
349 free (dd);
350 return (-1);
351 }
352 dd->scale = 1.0;
353 dd->shift = 0.0;
355 for (i = 0; i < ci->children_num; i++)
356 {
357 oconfig_item_t *option = ci->children + i;
358 status = 0;
360 if (strcasecmp ("Type", option->key) == 0)
361 status = csnmp_config_add_data_type (dd, option);
362 else if (strcasecmp ("Table", option->key) == 0)
363 status = csnmp_config_add_data_table (dd, option);
364 else if (strcasecmp ("Instance", option->key) == 0)
365 status = csnmp_config_add_data_instance (dd, option);
366 else if (strcasecmp ("InstancePrefix", option->key) == 0)
367 status = csnmp_config_add_data_instance_prefix (dd, option);
368 else if (strcasecmp ("Values", option->key) == 0)
369 status = csnmp_config_add_data_values (dd, option);
370 else if (strcasecmp ("Shift", option->key) == 0)
371 status = csnmp_config_add_data_shift (dd, option);
372 else if (strcasecmp ("Scale", option->key) == 0)
373 status = csnmp_config_add_data_scale (dd, option);
374 else
375 {
376 WARNING ("snmp plugin: Option `%s' not allowed here.", option->key);
377 status = -1;
378 }
380 if (status != 0)
381 break;
382 } /* for (ci->children) */
384 while (status == 0)
385 {
386 if (dd->type == NULL)
387 {
388 WARNING ("snmp plugin: `Type' not given for data `%s'", dd->name);
389 status = -1;
390 break;
391 }
392 if (dd->values == NULL)
393 {
394 WARNING ("snmp plugin: No `Value' given for data `%s'", dd->name);
395 status = -1;
396 break;
397 }
399 break;
400 } /* while (status == 0) */
402 if (status != 0)
403 {
404 sfree (dd->name);
405 sfree (dd->instance_prefix);
406 sfree (dd->values);
407 sfree (dd);
408 return (-1);
409 }
411 DEBUG ("snmp plugin: dd = { name = %s, type = %s, is_table = %s, values_len = %i }",
412 dd->name, dd->type, (dd->is_table != 0) ? "true" : "false", dd->values_len);
414 if (data_head == NULL)
415 data_head = dd;
416 else
417 {
418 data_definition_t *last;
419 last = data_head;
420 while (last->next != NULL)
421 last = last->next;
422 last->next = dd;
423 }
425 return (0);
426 } /* int csnmp_config_add_data */
428 static int csnmp_config_add_host_address (host_definition_t *hd, oconfig_item_t *ci)
429 {
430 if ((ci->values_num != 1)
431 || (ci->values[0].type != OCONFIG_TYPE_STRING))
432 {
433 WARNING ("snmp plugin: The `Address' config option needs exactly one string argument.");
434 return (-1);
435 }
437 if (hd->address == NULL)
438 free (hd->address);
440 hd->address = strdup (ci->values[0].value.string);
441 if (hd->address == NULL)
442 return (-1);
444 DEBUG ("snmp plugin: host = %s; host->address = %s;",
445 hd->name, hd->address);
447 return (0);
448 } /* int csnmp_config_add_host_address */
450 static int csnmp_config_add_host_community (host_definition_t *hd, oconfig_item_t *ci)
451 {
452 if ((ci->values_num != 1)
453 || (ci->values[0].type != OCONFIG_TYPE_STRING))
454 {
455 WARNING ("snmp plugin: The `Community' config option needs exactly one string argument.");
456 return (-1);
457 }
459 if (hd->community == NULL)
460 free (hd->community);
462 hd->community = strdup (ci->values[0].value.string);
463 if (hd->community == NULL)
464 return (-1);
466 DEBUG ("snmp plugin: host = %s; host->community = %s;",
467 hd->name, hd->community);
469 return (0);
470 } /* int csnmp_config_add_host_community */
472 static int csnmp_config_add_host_version (host_definition_t *hd, oconfig_item_t *ci)
473 {
474 int version;
476 if ((ci->values_num != 1)
477 || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
478 {
479 WARNING ("snmp plugin: The `Version' config option needs exactly one number argument.");
480 return (-1);
481 }
483 version = (int) ci->values[0].value.number;
484 if ((version != 1) && (version != 2))
485 {
486 WARNING ("snmp plugin: `Version' must either be `1' or `2'.");
487 return (-1);
488 }
490 hd->version = version;
492 return (0);
493 } /* int csnmp_config_add_host_address */
495 static int csnmp_config_add_host_collect (host_definition_t *host,
496 oconfig_item_t *ci)
497 {
498 data_definition_t *data;
499 data_definition_t **data_list;
500 int data_list_len;
501 int i;
503 if (ci->values_num < 1)
504 {
505 WARNING ("snmp plugin: `Collect' needs at least one argument.");
506 return (-1);
507 }
509 for (i = 0; i < ci->values_num; i++)
510 if (ci->values[i].type != OCONFIG_TYPE_STRING)
511 {
512 WARNING ("snmp plugin: All arguments to `Collect' must be strings.");
513 return (-1);
514 }
516 data_list_len = host->data_list_len + ci->values_num;
517 data_list = (data_definition_t **) realloc (host->data_list,
518 sizeof (data_definition_t *) * data_list_len);
519 if (data_list == NULL)
520 return (-1);
521 host->data_list = data_list;
523 for (i = 0; i < ci->values_num; i++)
524 {
525 for (data = data_head; data != NULL; data = data->next)
526 if (strcasecmp (ci->values[i].value.string, data->name) == 0)
527 break;
529 if (data == NULL)
530 {
531 WARNING ("snmp plugin: No such data configured: `%s'",
532 ci->values[i].value.string);
533 continue;
534 }
536 DEBUG ("snmp plugin: Collect: host = %s, data[%i] = %s;",
537 host->name, host->data_list_len, data->name);
539 host->data_list[host->data_list_len] = data;
540 host->data_list_len++;
541 } /* for (values_num) */
543 return (0);
544 } /* int csnmp_config_add_host_collect */
546 static int csnmp_config_add_host_interval (host_definition_t *hd, oconfig_item_t *ci)
547 {
548 if ((ci->values_num != 1)
549 || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
550 {
551 WARNING ("snmp plugin: The `Interval' config option needs exactly one number argument.");
552 return (-1);
553 }
555 hd->interval = ci->values[0].value.number >= 0
556 ? (uint32_t) ci->values[0].value.number
557 : 0;
559 return (0);
560 } /* int csnmp_config_add_host_interval */
562 static int csnmp_config_add_host (oconfig_item_t *ci)
563 {
564 host_definition_t *hd;
565 int status = 0;
566 int i;
568 /* Registration stuff. */
569 char cb_name[DATA_MAX_NAME_LEN];
570 user_data_t cb_data;
571 struct timespec cb_interval;
573 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
574 {
575 WARNING ("snmp plugin: `Host' needs exactly one string argument.");
576 return (-1);
577 }
579 hd = (host_definition_t *) malloc (sizeof (host_definition_t));
580 if (hd == NULL)
581 return (-1);
582 memset (hd, '\0', sizeof (host_definition_t));
583 hd->version = 2;
584 C_COMPLAIN_INIT (&hd->complaint);
586 hd->name = strdup (ci->values[0].value.string);
587 if (hd->name == NULL)
588 {
589 free (hd);
590 return (-1);
591 }
593 hd->sess_handle = NULL;
594 hd->interval = 0;
596 for (i = 0; i < ci->children_num; i++)
597 {
598 oconfig_item_t *option = ci->children + i;
599 status = 0;
601 if (strcasecmp ("Address", option->key) == 0)
602 status = csnmp_config_add_host_address (hd, option);
603 else if (strcasecmp ("Community", option->key) == 0)
604 status = csnmp_config_add_host_community (hd, option);
605 else if (strcasecmp ("Version", option->key) == 0)
606 status = csnmp_config_add_host_version (hd, option);
607 else if (strcasecmp ("Collect", option->key) == 0)
608 csnmp_config_add_host_collect (hd, option);
609 else if (strcasecmp ("Interval", option->key) == 0)
610 csnmp_config_add_host_interval (hd, option);
611 else
612 {
613 WARNING ("snmp plugin: csnmp_config_add_host: Option `%s' not allowed here.", option->key);
614 status = -1;
615 }
617 if (status != 0)
618 break;
619 } /* for (ci->children) */
621 while (status == 0)
622 {
623 if (hd->address == NULL)
624 {
625 WARNING ("snmp plugin: `Address' not given for host `%s'", hd->name);
626 status = -1;
627 break;
628 }
629 if (hd->community == NULL)
630 {
631 WARNING ("snmp plugin: `Community' not given for host `%s'", hd->name);
632 status = -1;
633 break;
634 }
636 break;
637 } /* while (status == 0) */
639 if (status != 0)
640 {
641 csnmp_host_definition_destroy (hd);
642 return (-1);
643 }
645 DEBUG ("snmp plugin: hd = { name = %s, address = %s, community = %s, version = %i }",
646 hd->name, hd->address, hd->community, hd->version);
648 ssnprintf (cb_name, sizeof (cb_name), "snmp-%s", hd->name);
650 memset (&cb_data, 0, sizeof (cb_data));
651 cb_data.data = hd;
652 cb_data.free_func = csnmp_host_definition_destroy;
654 memset (&cb_interval, 0, sizeof (cb_interval));
655 if (hd->interval != 0)
656 cb_interval.tv_sec = (time_t) hd->interval;
658 status = plugin_register_complex_read (/* group = */ NULL, cb_name,
659 csnmp_read_host, /* interval = */ &cb_interval,
660 /* user_data = */ &cb_data);
661 if (status != 0)
662 {
663 ERROR ("snmp plugin: Registering complex read function failed.");
664 csnmp_host_definition_destroy (hd);
665 return (-1);
666 }
668 return (0);
669 } /* int csnmp_config_add_host */
671 static int csnmp_config (oconfig_item_t *ci)
672 {
673 int i;
675 call_snmp_init_once ();
677 for (i = 0; i < ci->children_num; i++)
678 {
679 oconfig_item_t *child = ci->children + i;
680 if (strcasecmp ("Data", child->key) == 0)
681 csnmp_config_add_data (child);
682 else if (strcasecmp ("Host", child->key) == 0)
683 csnmp_config_add_host (child);
684 else
685 {
686 WARNING ("snmp plugin: Ignoring unknown config option `%s'.", child->key);
687 }
688 } /* for (ci->children) */
690 return (0);
691 } /* int csnmp_config */
693 /* }}} End of the config stuff. Now the interesting part begins */
695 static void csnmp_host_open_session (host_definition_t *host)
696 {
697 struct snmp_session sess;
699 if (host->sess_handle != NULL)
700 csnmp_host_close_session (host);
702 snmp_sess_init (&sess);
703 sess.peername = host->address;
704 sess.community = (u_char *) host->community;
705 sess.community_len = strlen (host->community);
706 sess.version = (host->version == 1) ? SNMP_VERSION_1 : SNMP_VERSION_2c;
708 /* snmp_sess_open will copy the `struct snmp_session *'. */
709 host->sess_handle = snmp_sess_open (&sess);
711 if (host->sess_handle == NULL)
712 {
713 char *errstr = NULL;
715 snmp_error (&sess, NULL, NULL, &errstr);
717 ERROR ("snmp plugin: host %s: snmp_sess_open failed: %s",
718 host->name, (errstr == NULL) ? "Unknown problem" : errstr);
719 sfree (errstr);
720 }
721 } /* void csnmp_host_open_session */
723 /* TODO: Check if negative values wrap around. Problem: negative temperatures. */
724 static value_t csnmp_value_list_to_value (struct variable_list *vl, int type,
725 double scale, double shift)
726 {
727 value_t ret;
728 uint64_t tmp_unsigned = 0;
729 int64_t tmp_signed = 0;
730 int defined = 1;
732 if ((vl->type == ASN_INTEGER)
733 || (vl->type == ASN_UINTEGER)
734 || (vl->type == ASN_COUNTER)
735 #ifdef ASN_TIMETICKS
736 || (vl->type == ASN_TIMETICKS)
737 #endif
738 || (vl->type == ASN_GAUGE))
739 {
740 tmp_unsigned = (uint32_t) *vl->val.integer;
741 tmp_signed = (int32_t) *vl->val.integer;
742 DEBUG ("snmp plugin: Parsed int32 value is %"PRIi64".", tmp_signed);
743 }
744 else if (vl->type == ASN_COUNTER64)
745 {
746 tmp_unsigned = (uint32_t) vl->val.counter64->high;
747 tmp_unsigned = tmp_unsigned << 32;
748 tmp_unsigned += (uint32_t) vl->val.counter64->low;
749 tmp_signed = (int64_t) tmp_unsigned;
750 DEBUG ("snmp plugin: Parsed int64 value is %"PRIu64".", tmp_unsigned);
751 }
752 else if (vl->type == ASN_OCTET_STR)
753 {
754 /* We'll handle this later.. */
755 }
756 else
757 {
758 char oid_buffer[1024];
760 memset (oid_buffer, 0, sizeof (oid_buffer));
761 snprint_objid (oid_buffer, sizeof (oid_buffer) - 1,
762 vl->name, vl->name_length);
764 #ifdef ASN_NULL
765 if (vl->type == ASN_NULL)
766 INFO ("snmp plugin: OID \"%s\" is undefined (type ASN_NULL)",
767 oid_buffer);
768 else
769 #endif
770 WARNING ("snmp plugin: I don't know the ASN type \"%i\" (OID: %s)",
771 (int) vl->type, oid_buffer);
773 defined = 0;
774 }
776 if (vl->type == ASN_OCTET_STR)
777 {
778 int status = -1;
780 if (vl->val.string != NULL)
781 {
782 char string[64];
783 size_t string_length;
785 string_length = sizeof (string) - 1;
786 if (vl->val_len < string_length)
787 string_length = vl->val_len;
789 /* The strings we get from the Net-SNMP library may not be null
790 * terminated. That is why we're using `memcpy' here and not `strcpy'.
791 * `string_length' is set to `vl->val_len' which holds the length of the
792 * string. -octo */
793 memcpy (string, vl->val.string, string_length);
794 string[string_length] = 0;
796 status = parse_value (string, &ret, type);
797 if (status != 0)
798 {
799 ERROR ("snmp plugin: csnmp_value_list_to_value: Parsing string as %s failed: %s",
800 DS_TYPE_TO_STRING (type), string);
801 }
802 }
804 if (status != 0)
805 {
806 switch (type)
807 {
808 case DS_TYPE_COUNTER:
809 case DS_TYPE_DERIVE:
810 case DS_TYPE_ABSOLUTE:
811 memset (&ret, 0, sizeof (ret));
812 break;
814 case DS_TYPE_GAUGE:
815 ret.gauge = NAN;
816 break;
818 default:
819 ERROR ("snmp plugin: csnmp_value_list_to_value: Unknown "
820 "data source type: %i.", type);
821 ret.gauge = NAN;
822 }
823 }
824 } /* if (vl->type == ASN_OCTET_STR) */
825 else if (type == DS_TYPE_COUNTER)
826 {
827 ret.counter = tmp_unsigned;
828 }
829 else if (type == DS_TYPE_GAUGE)
830 {
831 ret.gauge = NAN;
832 if (defined != 0)
833 ret.gauge = (scale * tmp_signed) + shift;
834 }
835 else if (type == DS_TYPE_DERIVE)
836 ret.derive = (derive_t) tmp_signed;
837 else if (type == DS_TYPE_ABSOLUTE)
838 ret.absolute = (absolute_t) tmp_unsigned;
839 else
840 {
841 ERROR ("snmp plugin: csnmp_value_list_to_value: Unknown data source "
842 "type: %i.", type);
843 ret.gauge = NAN;
844 }
846 return (ret);
847 } /* value_t csnmp_value_list_to_value */
849 /* Returns true if all OIDs have left their subtree */
850 static int csnmp_check_res_left_subtree (const host_definition_t *host,
851 const data_definition_t *data,
852 struct snmp_pdu *res)
853 {
854 struct variable_list *vb;
855 int num_checked;
856 int num_left_subtree;
857 int i;
859 vb = res->variables;
860 if (vb == NULL)
861 return (-1);
863 num_checked = 0;
864 num_left_subtree = 0;
866 /* check all the variables and count how many have left their subtree */
867 for (vb = res->variables, i = 0;
868 (vb != NULL) && (i < data->values_len);
869 vb = vb->next_variable, i++)
870 {
871 num_checked++;
872 if (snmp_oid_ncompare (data->values[i].oid,
873 data->values[i].oid_len,
874 vb->name, vb->name_length,
875 data->values[i].oid_len) != 0)
876 num_left_subtree++;
877 }
879 /* check if enough variables have been returned */
880 if (i < data->values_len)
881 {
882 ERROR ("snmp plugin: host %s: Expected %i variables, but got only %i",
883 host->name, data->values_len, i);
884 return (-1);
885 }
887 if (data->instance.oid.oid_len > 0)
888 {
889 if (vb == NULL)
890 {
891 ERROR ("snmp plugin: host %s: Expected one more variable for "
892 "the instance..", host->name);
893 return (-1);
894 }
896 num_checked++;
897 if (snmp_oid_ncompare (data->instance.oid.oid,
898 data->instance.oid.oid_len,
899 vb->name, vb->name_length,
900 data->instance.oid.oid_len) != 0)
901 num_left_subtree++;
902 }
904 DEBUG ("snmp plugin: csnmp_check_res_left_subtree: %i of %i variables have "
905 "left their subtree",
906 num_left_subtree, num_checked);
907 if (num_left_subtree >= num_checked)
908 return (1);
909 return (0);
910 } /* int csnmp_check_res_left_subtree */
912 static int csnmp_strvbcopy_hexstring (char *dst, /* {{{ */
913 const struct variable_list *vb, size_t dst_size)
914 {
915 char *buffer_ptr;
916 size_t buffer_free;
917 size_t i;
919 buffer_ptr = dst;
920 buffer_free = dst_size;
922 for (i = 0; i < vb->val_len; i++)
923 {
924 int status;
926 status = snprintf (buffer_ptr, buffer_free,
927 (i == 0) ? "%02x" : ":%02x", (unsigned int) vb->val.bitstring[i]);
929 if (status >= buffer_free)
930 {
931 buffer_ptr += (buffer_free - 1);
932 *buffer_ptr = 0;
933 return (dst_size + (buffer_free - status));
934 }
935 else /* if (status < buffer_free) */
936 {
937 buffer_ptr += status;
938 buffer_free -= status;
939 }
940 }
942 return ((int) (dst_size - buffer_free));
943 } /* }}} int csnmp_strvbcopy_hexstring */
945 static int csnmp_strvbcopy (char *dst, /* {{{ */
946 const struct variable_list *vb, size_t dst_size)
947 {
948 char *src;
949 size_t num_chars;
950 size_t i;
952 if (vb->type == ASN_OCTET_STR)
953 src = (char *) vb->val.string;
954 else if (vb->type == ASN_BIT_STR)
955 src = (char *) vb->val.bitstring;
956 else
957 {
958 dst[0] = 0;
959 return (EINVAL);
960 }
962 num_chars = dst_size - 1;
963 if (num_chars > vb->val_len)
964 num_chars = vb->val_len;
966 for (i = 0; i < num_chars; i++)
967 {
968 /* Check for control characters. */
969 if ((unsigned char)src[i] < 32)
970 return (csnmp_strvbcopy_hexstring (dst, vb, dst_size));
971 dst[i] = src[i];
972 }
973 dst[num_chars] = 0;
975 return ((int) vb->val_len);
976 } /* }}} int csnmp_strvbcopy */
978 static int csnmp_instance_list_add (csnmp_list_instances_t **head,
979 csnmp_list_instances_t **tail,
980 const struct snmp_pdu *res)
981 {
982 csnmp_list_instances_t *il;
983 struct variable_list *vb;
985 /* Set vb on the last variable */
986 for (vb = res->variables;
987 (vb != NULL) && (vb->next_variable != NULL);
988 vb = vb->next_variable)
989 /* do nothing */;
990 if (vb == NULL)
991 return (-1);
993 il = (csnmp_list_instances_t *) malloc (sizeof (csnmp_list_instances_t));
994 if (il == NULL)
995 {
996 ERROR ("snmp plugin: malloc failed.");
997 return (-1);
998 }
999 il->subid = vb->name[vb->name_length - 1];
1000 il->next = NULL;
1002 /* Get instance name */
1003 if ((vb->type == ASN_OCTET_STR) || (vb->type == ASN_BIT_STR))
1004 {
1005 char *ptr;
1007 csnmp_strvbcopy (il->instance, vb, sizeof (il->instance));
1009 for (ptr = il->instance; *ptr != '\0'; ptr++)
1010 {
1011 if ((*ptr > 0) && (*ptr < 32))
1012 *ptr = ' ';
1013 else if (*ptr == '/')
1014 *ptr = '_';
1015 }
1016 DEBUG ("snmp plugin: il->instance = `%s';", il->instance);
1017 }
1018 else
1019 {
1020 value_t val = csnmp_value_list_to_value (vb, DS_TYPE_COUNTER, 1.0, 0.0);
1021 ssnprintf (il->instance, sizeof (il->instance),
1022 "%llu", val.counter);
1023 }
1025 /* TODO: Debugging output */
1027 if (*head == NULL)
1028 *head = il;
1029 else
1030 (*tail)->next = il;
1031 *tail = il;
1033 return (0);
1034 } /* int csnmp_instance_list_add */
1036 static int csnmp_dispatch_table (host_definition_t *host, data_definition_t *data,
1037 csnmp_list_instances_t *instance_list,
1038 csnmp_table_values_t **value_table)
1039 {
1040 const data_set_t *ds;
1041 value_list_t vl = VALUE_LIST_INIT;
1043 csnmp_list_instances_t *instance_list_ptr;
1044 csnmp_table_values_t **value_table_ptr;
1046 int i;
1047 oid subid;
1048 int have_more;
1050 ds = plugin_get_ds (data->type);
1051 if (!ds)
1052 {
1053 ERROR ("snmp plugin: DataSet `%s' not defined.", data->type);
1054 return (-1);
1055 }
1056 assert (ds->ds_num == data->values_len);
1058 instance_list_ptr = instance_list;
1060 value_table_ptr = (csnmp_table_values_t **) malloc (sizeof (csnmp_table_values_t *)
1061 * data->values_len);
1062 if (value_table_ptr == NULL)
1063 return (-1);
1064 for (i = 0; i < data->values_len; i++)
1065 value_table_ptr[i] = value_table[i];
1067 vl.values_len = ds->ds_num;
1068 vl.values = (value_t *) malloc (sizeof (value_t) * vl.values_len);
1069 if (vl.values == NULL)
1070 {
1071 ERROR ("snmp plugin: malloc failed.");
1072 sfree (value_table_ptr);
1073 return (-1);
1074 }
1076 sstrncpy (vl.host, host->name, sizeof (vl.host));
1077 sstrncpy (vl.plugin, "snmp", sizeof (vl.plugin));
1079 vl.interval = host->interval;
1081 subid = 0;
1082 have_more = 1;
1084 while (have_more != 0)
1085 {
1086 if (instance_list != NULL)
1087 {
1088 while ((instance_list_ptr != NULL)
1089 && (instance_list_ptr->subid < subid))
1090 instance_list_ptr = instance_list_ptr->next;
1092 if (instance_list_ptr == NULL)
1093 {
1094 have_more = 0;
1095 continue;
1096 }
1097 else if (instance_list_ptr->subid > subid)
1098 {
1099 subid = instance_list_ptr->subid;
1100 continue;
1101 }
1102 } /* if (instance_list != NULL) */
1104 for (i = 0; i < data->values_len; i++)
1105 {
1106 while ((value_table_ptr[i] != NULL)
1107 && (value_table_ptr[i]->subid < subid))
1108 value_table_ptr[i] = value_table_ptr[i]->next;
1110 if (value_table_ptr[i] == NULL)
1111 {
1112 have_more = 0;
1113 break;
1114 }
1115 else if (value_table_ptr[i]->subid > subid)
1116 {
1117 subid = value_table_ptr[i]->subid;
1118 break;
1119 }
1120 } /* for (i = 0; i < columns; i++) */
1121 /* The subid has been increased - start scanning from the beginning
1122 * again.. */
1123 if (i < data->values_len)
1124 continue;
1126 /* if we reach this line, all value_table_ptr[i] are non-NULL and are set
1127 * to the same subid. instance_list_ptr is either NULL or points to the
1128 * same subid, too. */
1129 #if COLLECT_DEBUG
1130 for (i = 1; i < data->values_len; i++)
1131 {
1132 assert (value_table_ptr[i] != NULL);
1133 assert (value_table_ptr[i-1]->subid == value_table_ptr[i]->subid);
1134 }
1135 assert ((instance_list_ptr == NULL)
1136 || (instance_list_ptr->subid == value_table_ptr[0]->subid));
1137 #endif
1139 sstrncpy (vl.type, data->type, sizeof (vl.type));
1141 {
1142 char temp[DATA_MAX_NAME_LEN];
1144 if (instance_list_ptr == NULL)
1145 ssnprintf (temp, sizeof (temp), "%"PRIu32, (uint32_t) subid);
1146 else
1147 sstrncpy (temp, instance_list_ptr->instance, sizeof (temp));
1149 if (data->instance_prefix == NULL)
1150 sstrncpy (vl.type_instance, temp, sizeof (vl.type_instance));
1151 else
1152 ssnprintf (vl.type_instance, sizeof (vl.type_instance), "%s%s",
1153 data->instance_prefix, temp);
1154 }
1156 for (i = 0; i < data->values_len; i++)
1157 vl.values[i] = value_table_ptr[i]->value;
1159 /* If we get here `vl.type_instance' and all `vl.values' have been set */
1160 plugin_dispatch_values (&vl);
1162 subid++;
1163 } /* while (have_more != 0) */
1165 sfree (vl.values);
1166 sfree (value_table_ptr);
1168 return (0);
1169 } /* int csnmp_dispatch_table */
1171 static int csnmp_read_table (host_definition_t *host, data_definition_t *data)
1172 {
1173 struct snmp_pdu *req;
1174 struct snmp_pdu *res;
1175 struct variable_list *vb;
1177 const data_set_t *ds;
1178 oid_t *oid_list;
1179 uint32_t oid_list_len;
1181 int status;
1182 int i;
1184 /* `value_table' and `value_table_ptr' implement a linked list for each
1185 * value. `instance_list' and `instance_list_ptr' implement a linked list of
1186 * instance names. This is used to jump gaps in the table. */
1187 csnmp_list_instances_t *instance_list;
1188 csnmp_list_instances_t *instance_list_ptr;
1189 csnmp_table_values_t **value_table;
1190 csnmp_table_values_t **value_table_ptr;
1192 DEBUG ("snmp plugin: csnmp_read_table (host = %s, data = %s)",
1193 host->name, data->name);
1195 if (host->sess_handle == NULL)
1196 {
1197 DEBUG ("snmp plugin: csnmp_read_table: host->sess_handle == NULL");
1198 return (-1);
1199 }
1201 ds = plugin_get_ds (data->type);
1202 if (!ds)
1203 {
1204 ERROR ("snmp plugin: DataSet `%s' not defined.", data->type);
1205 return (-1);
1206 }
1208 if (ds->ds_num != data->values_len)
1209 {
1210 ERROR ("snmp plugin: DataSet `%s' requires %i values, but config talks about %i",
1211 data->type, ds->ds_num, data->values_len);
1212 return (-1);
1213 }
1215 /* We need a copy of all the OIDs, because GETNEXT will destroy them. */
1216 oid_list_len = data->values_len + 1;
1217 oid_list = (oid_t *) malloc (sizeof (oid_t) * (oid_list_len));
1218 if (oid_list == NULL)
1219 {
1220 ERROR ("snmp plugin: csnmp_read_table: malloc failed.");
1221 return (-1);
1222 }
1223 memcpy (oid_list, data->values, data->values_len * sizeof (oid_t));
1224 if (data->instance.oid.oid_len > 0)
1225 memcpy (oid_list + data->values_len, &data->instance.oid, sizeof (oid_t));
1226 else
1227 oid_list_len--;
1229 /* Allocate the `value_table' */
1230 value_table = (csnmp_table_values_t **) malloc (sizeof (csnmp_table_values_t *)
1231 * 2 * data->values_len);
1232 if (value_table == NULL)
1233 {
1234 ERROR ("snmp plugin: csnmp_read_table: malloc failed.");
1235 sfree (oid_list);
1236 return (-1);
1237 }
1238 memset (value_table, '\0', sizeof (csnmp_table_values_t *) * 2 * data->values_len);
1239 value_table_ptr = value_table + data->values_len;
1241 instance_list = NULL;
1242 instance_list_ptr = NULL;
1244 status = 0;
1245 while (status == 0)
1246 {
1247 req = snmp_pdu_create (SNMP_MSG_GETNEXT);
1248 if (req == NULL)
1249 {
1250 ERROR ("snmp plugin: snmp_pdu_create failed.");
1251 status = -1;
1252 break;
1253 }
1255 for (i = 0; (uint32_t) i < oid_list_len; i++)
1256 snmp_add_null_var (req, oid_list[i].oid, oid_list[i].oid_len);
1258 res = NULL;
1259 status = snmp_sess_synch_response (host->sess_handle, req, &res);
1261 if ((status != STAT_SUCCESS) || (res == NULL))
1262 {
1263 char *errstr = NULL;
1265 snmp_sess_error (host->sess_handle, NULL, NULL, &errstr);
1267 c_complain (LOG_ERR, &host->complaint,
1268 "snmp plugin: host %s: snmp_sess_synch_response failed: %s",
1269 host->name, (errstr == NULL) ? "Unknown problem" : errstr);
1271 if (res != NULL)
1272 snmp_free_pdu (res);
1273 res = NULL;
1275 sfree (errstr);
1276 csnmp_host_close_session (host);
1278 status = -1;
1279 break;
1280 }
1281 status = 0;
1282 assert (res != NULL);
1283 c_release (LOG_INFO, &host->complaint,
1284 "snmp plugin: host %s: snmp_sess_synch_response successful.",
1285 host->name);
1287 vb = res->variables;
1288 if (vb == NULL)
1289 {
1290 status = -1;
1291 break;
1292 }
1294 /* Check if all values (and possibly the instance) have left their
1295 * subtree */
1296 if (csnmp_check_res_left_subtree (host, data, res) != 0)
1297 {
1298 status = 0;
1299 break;
1300 }
1302 /* if an instance-OID is configured.. */
1303 if (data->instance.oid.oid_len > 0)
1304 {
1305 /* Allocate a new `csnmp_list_instances_t', insert the instance name and
1306 * add it to the list */
1307 if (csnmp_instance_list_add (&instance_list, &instance_list_ptr,
1308 res) != 0)
1309 {
1310 ERROR ("snmp plugin: csnmp_instance_list_add failed.");
1311 status = -1;
1312 break;
1313 }
1315 /* Set vb on the last variable */
1316 for (vb = res->variables;
1317 (vb != NULL) && (vb->next_variable != NULL);
1318 vb = vb->next_variable)
1319 /* do nothing */;
1320 assert (vb != NULL);
1322 /* Copy OID to oid_list[data->values_len] */
1323 memcpy (oid_list[data->values_len].oid, vb->name,
1324 sizeof (oid) * vb->name_length);
1325 oid_list[data->values_len].oid_len = vb->name_length;
1326 }
1328 for (vb = res->variables, i = 0;
1329 (vb != NULL) && (i < data->values_len);
1330 vb = vb->next_variable, i++)
1331 {
1332 csnmp_table_values_t *vt;
1334 /* Check if we left the subtree */
1335 if (snmp_oid_ncompare (data->values[i].oid,
1336 data->values[i].oid_len,
1337 vb->name, vb->name_length,
1338 data->values[i].oid_len) != 0)
1339 {
1340 DEBUG ("snmp plugin: host = %s; data = %s; Value %i left its subtree.",
1341 host->name, data->name, i);
1342 continue;
1343 }
1345 if ((value_table_ptr[i] != NULL)
1346 && (vb->name[vb->name_length - 1] <= value_table_ptr[i]->subid))
1347 {
1348 DEBUG ("snmp plugin: host = %s; data = %s; i = %i; "
1349 "SUBID is not increasing.",
1350 host->name, data->name, i);
1351 continue;
1352 }
1354 vt = (csnmp_table_values_t *) malloc (sizeof (csnmp_table_values_t));
1355 if (vt == NULL)
1356 {
1357 ERROR ("snmp plugin: malloc failed.");
1358 status = -1;
1359 break;
1360 }
1362 vt->subid = vb->name[vb->name_length - 1];
1363 vt->value = csnmp_value_list_to_value (vb, ds->ds[i].type,
1364 data->scale, data->shift);
1365 vt->next = NULL;
1367 if (value_table_ptr[i] == NULL)
1368 value_table[i] = vt;
1369 else
1370 value_table_ptr[i]->next = vt;
1371 value_table_ptr[i] = vt;
1373 /* Copy OID to oid_list[i + 1] */
1374 memcpy (oid_list[i].oid, vb->name, sizeof (oid) * vb->name_length);
1375 oid_list[i].oid_len = vb->name_length;
1376 } /* for (i = data->values_len) */
1378 if (res != NULL)
1379 snmp_free_pdu (res);
1380 res = NULL;
1381 } /* while (status == 0) */
1383 if (res != NULL)
1384 snmp_free_pdu (res);
1385 res = NULL;
1387 if (status == 0)
1388 csnmp_dispatch_table (host, data, instance_list, value_table);
1390 /* Free all allocated variables here */
1391 while (instance_list != NULL)
1392 {
1393 instance_list_ptr = instance_list->next;
1394 sfree (instance_list);
1395 instance_list = instance_list_ptr;
1396 }
1398 for (i = 0; i < data->values_len; i++)
1399 {
1400 csnmp_table_values_t *tmp;
1401 while (value_table[i] != NULL)
1402 {
1403 tmp = value_table[i]->next;
1404 sfree (value_table[i]);
1405 value_table[i] = tmp;
1406 }
1407 }
1409 sfree (value_table);
1410 sfree (oid_list);
1412 return (0);
1413 } /* int csnmp_read_table */
1415 static int csnmp_read_value (host_definition_t *host, data_definition_t *data)
1416 {
1417 struct snmp_pdu *req;
1418 struct snmp_pdu *res;
1419 struct variable_list *vb;
1421 const data_set_t *ds;
1422 value_list_t vl = VALUE_LIST_INIT;
1424 int status;
1425 int i;
1427 DEBUG ("snmp plugin: csnmp_read_value (host = %s, data = %s)",
1428 host->name, data->name);
1430 if (host->sess_handle == NULL)
1431 {
1432 DEBUG ("snmp plugin: csnmp_read_table: host->sess_handle == NULL");
1433 return (-1);
1434 }
1436 ds = plugin_get_ds (data->type);
1437 if (!ds)
1438 {
1439 ERROR ("snmp plugin: DataSet `%s' not defined.", data->type);
1440 return (-1);
1441 }
1443 if (ds->ds_num != data->values_len)
1444 {
1445 ERROR ("snmp plugin: DataSet `%s' requires %i values, but config talks about %i",
1446 data->type, ds->ds_num, data->values_len);
1447 return (-1);
1448 }
1450 vl.values_len = ds->ds_num;
1451 vl.values = (value_t *) malloc (sizeof (value_t) * vl.values_len);
1452 if (vl.values == NULL)
1453 return (-1);
1454 for (i = 0; i < vl.values_len; i++)
1455 {
1456 if (ds->ds[i].type == DS_TYPE_COUNTER)
1457 vl.values[i].counter = 0;
1458 else
1459 vl.values[i].gauge = NAN;
1460 }
1462 sstrncpy (vl.host, host->name, sizeof (vl.host));
1463 sstrncpy (vl.plugin, "snmp", sizeof (vl.plugin));
1464 sstrncpy (vl.type, data->type, sizeof (vl.type));
1465 sstrncpy (vl.type_instance, data->instance.string, sizeof (vl.type_instance));
1467 vl.interval = host->interval;
1469 req = snmp_pdu_create (SNMP_MSG_GET);
1470 if (req == NULL)
1471 {
1472 ERROR ("snmp plugin: snmp_pdu_create failed.");
1473 sfree (vl.values);
1474 return (-1);
1475 }
1477 for (i = 0; i < data->values_len; i++)
1478 snmp_add_null_var (req, data->values[i].oid, data->values[i].oid_len);
1480 res = NULL;
1481 status = snmp_sess_synch_response (host->sess_handle, req, &res);
1483 if ((status != STAT_SUCCESS) || (res == NULL))
1484 {
1485 char *errstr = NULL;
1487 snmp_sess_error (host->sess_handle, NULL, NULL, &errstr);
1488 ERROR ("snmp plugin: host %s: snmp_sess_synch_response failed: %s",
1489 host->name, (errstr == NULL) ? "Unknown problem" : errstr);
1491 if (res != NULL)
1492 snmp_free_pdu (res);
1493 res = NULL;
1495 sfree (errstr);
1496 csnmp_host_close_session (host);
1498 return (-1);
1499 }
1502 for (vb = res->variables; vb != NULL; vb = vb->next_variable)
1503 {
1504 #if COLLECT_DEBUG
1505 char buffer[1024];
1506 snprint_variable (buffer, sizeof (buffer),
1507 vb->name, vb->name_length, vb);
1508 DEBUG ("snmp plugin: Got this variable: %s", buffer);
1509 #endif /* COLLECT_DEBUG */
1511 for (i = 0; i < data->values_len; i++)
1512 if (snmp_oid_compare (data->values[i].oid, data->values[i].oid_len,
1513 vb->name, vb->name_length) == 0)
1514 vl.values[i] = csnmp_value_list_to_value (vb, ds->ds[i].type,
1515 data->scale, data->shift);
1516 } /* for (res->variables) */
1518 if (res != NULL)
1519 snmp_free_pdu (res);
1520 res = NULL;
1522 DEBUG ("snmp plugin: -> plugin_dispatch_values (&vl);");
1523 plugin_dispatch_values (&vl);
1524 sfree (vl.values);
1526 return (0);
1527 } /* int csnmp_read_value */
1529 static int csnmp_read_host (user_data_t *ud)
1530 {
1531 host_definition_t *host;
1532 time_t time_start;
1533 time_t time_end;
1534 int status;
1535 int success;
1536 int i;
1538 host = ud->data;
1540 if (host->interval == 0)
1541 host->interval = interval_g;
1543 time_start = time (NULL);
1544 DEBUG ("snmp plugin: csnmp_read_host (%s) started at %u;", host->name,
1545 (unsigned int) time_start);
1547 if (host->sess_handle == NULL)
1548 csnmp_host_open_session (host);
1550 if (host->sess_handle == NULL)
1551 return (-1);
1553 success = 0;
1554 for (i = 0; i < host->data_list_len; i++)
1555 {
1556 data_definition_t *data = host->data_list[i];
1558 if (data->is_table)
1559 status = csnmp_read_table (host, data);
1560 else
1561 status = csnmp_read_value (host, data);
1563 if (status == 0)
1564 success++;
1565 }
1567 time_end = time (NULL);
1568 DEBUG ("snmp plugin: csnmp_read_host (%s) finished at %u;", host->name,
1569 (unsigned int) time_end);
1570 if ((uint32_t) (time_end - time_start) > host->interval)
1571 {
1572 WARNING ("snmp plugin: Host `%s' should be queried every %"PRIu32
1573 " seconds, but reading all values takes %u seconds.",
1574 host->name, host->interval, (unsigned int) (time_end - time_start));
1575 }
1577 if (success == 0)
1578 return (-1);
1580 return (0);
1581 } /* int csnmp_read_host */
1583 static int csnmp_init (void)
1584 {
1585 call_snmp_init_once ();
1587 return (0);
1588 } /* int csnmp_init */
1590 static int csnmp_shutdown (void)
1591 {
1592 data_definition_t *data_this;
1593 data_definition_t *data_next;
1595 /* When we get here, the read threads have been stopped and all the
1596 * `host_definition_t' will be freed. */
1597 DEBUG ("snmp plugin: Destroying all data definitions.");
1599 data_this = data_head;
1600 data_head = NULL;
1601 while (data_this != NULL)
1602 {
1603 data_next = data_this->next;
1605 sfree (data_this->name);
1606 sfree (data_this->type);
1607 sfree (data_this->values);
1608 sfree (data_this);
1610 data_this = data_next;
1611 }
1613 return (0);
1614 } /* int csnmp_shutdown */
1616 void module_register (void)
1617 {
1618 plugin_register_complex_config ("snmp", csnmp_config);
1619 plugin_register_init ("snmp", csnmp_init);
1620 plugin_register_shutdown ("snmp", csnmp_shutdown);
1621 } /* void module_register */
1623 /*
1624 * vim: shiftwidth=2 softtabstop=2 tabstop=8 fdm=marker
1625 */