Merge pull request #3269 from bplessis/feature-add-snmpbulk
authorMatthias Runge <mrunge@redhat.com>
Sat, 2 Nov 2019 21:12:06 +0000 (22:12 +0100)
committerGitHub <noreply@github.com>
Sat, 2 Nov 2019 21:12:06 +0000 (22:12 +0100)
snmp plugin: add support for SNMP Bulk Transfer

src/collectd-snmp.pod
src/collectd.conf.in
src/snmp.c

index 493f5ec..5e99ba3 100644 (file)
@@ -373,6 +373,10 @@ How long to wait for a response. The C<Net-SNMP> library default is 1 second.
 The number of times that a query should be retried after the Timeout expires.
 The C<Net-SNMP> library default is 5.
 
+=item B<BulkSize> I<Integer>
+
+Configures the size of SNMP bulk transfers. The default is 0, which disables bulk transfers altogether.
+
 =back
 
 =head1 SEE ALSO
index dcad150..63db8b1 100644 (file)
 #       Version 2
 #       Community "another_string"
 #       Collect "std_traffic" "hr_users"
+#       BulkSize 0
 #   </Host>
 #   <Host "some.ups.mydomain.org">
 #       Address "192.168.0.3"
 #      Timeout 5
 #      Retries 5
 #   </Host>
+#   <Host "highend.switch.example.org">
+#       Address "192.168.0.3"
+#       Version 2
+#       Community "another_string"
+#       Collect "std_traffic"
+#       Interval 10
+#       Timeout 10
+#       BulkSize 100
+#   </Host>
 #</Plugin>
 
 #<Plugin snmp_agent>
index aeb04fd..98da2b8 100644 (file)
@@ -99,6 +99,7 @@ struct host_definition_s {
   c_complain_t complaint;
   data_definition_t **data_list;
   int data_list_len;
+  int bulk_size;
 };
 typedef struct host_definition_s host_definition_t;
 
@@ -762,6 +763,7 @@ static int csnmp_config_add_host(oconfig_item_t *ci) {
   /* These mean that we have not set a timeout or retry value */
   hd->timeout = 0;
   hd->retries = -1;
+  hd->bulk_size = 0;
 
   for (int i = 0; i < ci->children_num; i++) {
     oconfig_item_t *option = ci->children + i;
@@ -794,6 +796,8 @@ static int csnmp_config_add_host(oconfig_item_t *ci) {
       status = csnmp_config_add_host_security_level(hd, option);
     else if (strcasecmp("Context", option->key) == 0)
       status = cf_util_get_string(option, &hd->context);
+    else if (strcasecmp("BulkSize", option->key) == 0)
+      status = cf_util_get_int(option, &hd->bulk_size);
     else {
       WARNING(
           "snmp plugin: csnmp_config_add_host: Option `%s' not allowed here.",
@@ -816,6 +820,11 @@ static int csnmp_config_add_host(oconfig_item_t *ci) {
       status = -1;
       break;
     }
+    if (hd->bulk_size > 0 && hd->version < 2) {
+      WARNING("snmp plugin: Bulk transfers is only available for SNMP v2 and "
+              "later, host '%s' is configured as version '%d'",
+              hd->name, hd->version);
+    }
     if (hd->version == 3) {
       if (hd->username == NULL) {
         WARNING("snmp plugin: `Username' not given for host `%s'", hd->name);
@@ -1655,7 +1664,14 @@ static int csnmp_read_table(host_definition_t *host, data_definition_t *data) {
 
   status = 0;
   while (status == 0) {
-    req = snmp_pdu_create(SNMP_MSG_GETNEXT);
+    /* If SNMP v2 and later and bulk transfers enabled, use GETBULK PDU */
+    if (host->version > 1 && host->bulk_size > 0) {
+      req = snmp_pdu_create(SNMP_MSG_GETBULK);
+      req->non_repeaters = 0;
+      req->max_repetitions = host->bulk_size;
+    } else {
+      req = snmp_pdu_create(SNMP_MSG_GETNEXT);
+    }
     if (req == NULL) {
       ERROR("snmp plugin: snmp_pdu_create failed.");
       status = -1;
@@ -1683,6 +1699,13 @@ static int csnmp_read_table(host_definition_t *host, data_definition_t *data) {
       break;
     }
 
+    if (req->command == SNMP_MSG_GETBULK) {
+      /* In bulk mode the host will send 'max_repetitions' values per
+         requested variable, so we need to split it per number of variable
+         to stay 'in budget' */
+      req->max_repetitions = floor(host->bulk_size / oid_list_todo_num);
+    }
+
     res = NULL;
     status = snmp_sess_synch_response(host->sess_handle, req, &res);
 
@@ -1754,11 +1777,18 @@ static int csnmp_read_table(host_definition_t *host, data_definition_t *data) {
       continue;
     }
 
-    for (vb = res->variables, i = 0; (vb != NULL);
-         vb = vb->next_variable, i++) {
+    size_t j;
+    for (vb = res->variables, j = 0; (vb != NULL);
+         vb = vb->next_variable, j++) {
+      i = j;
+      /* If bulk request is active convert value index of the extra value */
+      if (host->version > 1 && host->bulk_size > 0) {
+        i %= oid_list_todo_num;
+      }
       /* Calculate value index from todo list */
       while ((i < oid_list_len) && !oid_list_todo[i]) {
         i++;
+        j++;
       }
       if (i >= oid_list_len) {
         break;