diff --git a/src/snmp.c b/src/snmp.c
index 1c2828c1fb2469494e84aafebb6a77d01fadcde9..d9c3bd6591555327b53f2b42ccb08e43b1799ac7 100644 (file)
--- a/src/snmp.c
+++ b/src/snmp.c
@@ -302,7 +302,7 @@ static int csnmp_config_add_data_shift (data_definition_t *dd, oconfig_item_t *c
if ((ci->values_num != 1)
|| (ci->values[0].type != OCONFIG_TYPE_NUMBER))
{
- WARNING ("snmp plugin: The `Scale' config option needs exactly one number argument.");
+ WARNING ("snmp plugin: The `Shift' config option needs exactly one number argument.");
return (-1);
}
value_t ret;
uint64_t tmp_unsigned = 0;
int64_t tmp_signed = 0;
- int defined = 1;
+ _Bool defined = 1;
+ /* Set to true when the original SNMP type appears to have been signed. */
+ _Bool prefer_signed = 0;
if ((vl->type == ASN_INTEGER)
|| (vl->type == ASN_UINTEGER)
{
tmp_unsigned = (uint32_t) *vl->val.integer;
tmp_signed = (int32_t) *vl->val.integer;
- DEBUG ("snmp plugin: Parsed int32 value is %"PRIi64".", tmp_signed);
+
+ if ((vl->type == ASN_INTEGER)
+ || (vl->type == ASN_GAUGE))
+ prefer_signed = 1;
+
+ DEBUG ("snmp plugin: Parsed int32 value is %"PRIu64".", tmp_unsigned);
}
else if (vl->type == ASN_COUNTER64)
{
}
else if (type == DS_TYPE_GAUGE)
{
- ret.gauge = NAN;
- if (defined != 0)
+ if (!defined)
+ ret.gauge = NAN;
+ else if (prefer_signed)
ret.gauge = (scale * tmp_signed) + shift;
+ else
+ ret.gauge = (scale * tmp_unsigned) + shift;
}
else if (type == DS_TYPE_DERIVE)
- ret.derive = (derive_t) tmp_signed;
+ {
+ if (prefer_signed)
+ ret.derive = (derive_t) tmp_signed;
+ else
+ ret.derive = (derive_t) tmp_unsigned;
+ }
else if (type == DS_TYPE_ABSOLUTE)
+ {
ret.absolute = (absolute_t) tmp_unsigned;
+ }
else
{
ERROR ("snmp plugin: csnmp_value_list_to_value: Unknown data source "
vb = vb->next_variable, i++)
{
num_checked++;
- if (snmp_oid_ncompare (data->values[i].oid,
- data->values[i].oid_len,
- vb->name, vb->name_length,
- data->values[i].oid_len) != 0)
+
+ if ((vb->type == SNMP_ENDOFMIBVIEW)
+ || (snmp_oid_ncompare (data->values[i].oid,
+ data->values[i].oid_len,
+ vb->name, vb->name_length,
+ data->values[i].oid_len) != 0))
num_left_subtree++;
}
if (vb == NULL)
return (-1);
- il = (csnmp_list_instances_t *) malloc (sizeof (csnmp_list_instances_t));
+ il = malloc (sizeof (*il));
if (il == NULL)
{
ERROR ("snmp plugin: malloc failed.");
return (-1);
}
+ /* XXX copy entire OID */
il->subid = vb->name[vb->name_length - 1];
il->next = NULL;
@@ -1181,13 +1201,13 @@ static int csnmp_read_table (host_definition_t *host, data_definition_t *data)
int status;
int i;
- /* `value_table' and `value_table_ptr' implement a linked list for each
- * value. `instance_list' and `instance_list_ptr' implement a linked list of
+ /* `value_list_head' and `value_list_tail' implement a linked list for each
+ * value. `instance_list_head' and `instance_list_tail' implement a linked list of
* instance names. This is used to jump gaps in the table. */
- csnmp_list_instances_t *instance_list;
- csnmp_list_instances_t *instance_list_ptr;
- csnmp_table_values_t **value_table;
- csnmp_table_values_t **value_table_ptr;
+ csnmp_list_instances_t *instance_list_head;
+ csnmp_list_instances_t *instance_list_tail;
+ csnmp_table_values_t **value_list_head;
+ csnmp_table_values_t **value_list_tail;
DEBUG ("snmp plugin: csnmp_read_table (host = %s, data = %s)",
host->name, data->name);
@@ -1226,20 +1246,22 @@ static int csnmp_read_table (host_definition_t *host, data_definition_t *data)
else
oid_list_len--;
- /* Allocate the `value_table' */
- value_table = (csnmp_table_values_t **) malloc (sizeof (csnmp_table_values_t *)
- * 2 * data->values_len);
- if (value_table == NULL)
+ /* We're going to construct n linked lists, one for each "value".
+ * value_list_head will contain pointers to the heads of these linked lists,
+ * value_list_tail will contain pointers to the tail of the lists. */
+ value_list_head = calloc (data->values_len, sizeof (*value_list_head));
+ value_list_tail = calloc (data->values_len, sizeof (*value_list_tail));
+ if ((value_list_head == NULL) || (value_list_tail == NULL))
{
- ERROR ("snmp plugin: csnmp_read_table: malloc failed.");
+ ERROR ("snmp plugin: csnmp_read_table: calloc failed.");
sfree (oid_list);
+ sfree (value_list_head);
+ sfree (value_list_tail);
return (-1);
}
- memset (value_table, '\0', sizeof (csnmp_table_values_t *) * 2 * data->values_len);
- value_table_ptr = value_table + data->values_len;
- instance_list = NULL;
- instance_list_ptr = NULL;
+ instance_list_head = NULL;
+ instance_list_tail = NULL;
status = 0;
while (status == 0)
@@ -1299,12 +1321,13 @@ static int csnmp_read_table (host_definition_t *host, data_definition_t *data)
break;
}
- /* if an instance-OID is configured.. */
+ /* Copy the OID of the value used as instance to oid_list, if an instance
+ * is configured. */
if (data->instance.oid.oid_len > 0)
{
/* Allocate a new `csnmp_list_instances_t', insert the instance name and
* add it to the list */
- if (csnmp_instance_list_add (&instance_list, &instance_list_ptr,
+ if (csnmp_instance_list_add (&instance_list_head, &instance_list_tail,
res) != 0)
{
ERROR ("snmp plugin: csnmp_instance_list_add failed.");
@@ -1312,19 +1335,24 @@ static int csnmp_read_table (host_definition_t *host, data_definition_t *data)
break;
}
- /* Set vb on the last variable */
+ /* The instance OID is added to the list of OIDs to GET from the
+ * snmp agent last, so set vb on the last variable returned and copy
+ * that OID. */
for (vb = res->variables;
(vb != NULL) && (vb->next_variable != NULL);
vb = vb->next_variable)
/* do nothing */;
assert (vb != NULL);
- /* Copy OID to oid_list[data->values_len] */
+ /* Copy the OID of the instance value to oid_list[data->values_len] */
memcpy (oid_list[data->values_len].oid, vb->name,
sizeof (oid) * vb->name_length);
oid_list[data->values_len].oid_len = vb->name_length;
}
+ /* Iterate over all the (non-instance) values returned by the agent. The
+ * (i < value_len) check will make sure we're not handling the instance OID
+ * twice. */
for (vb = res->variables, i = 0;
(vb != NULL) && (i < data->values_len);
vb = vb->next_variable, i++)
@@ -1342,8 +1370,8 @@ static int csnmp_read_table (host_definition_t *host, data_definition_t *data)
continue;
}
- if ((value_table_ptr[i] != NULL)
- && (vb->name[vb->name_length - 1] <= value_table_ptr[i]->subid))
+ if ((value_list_tail[i] != NULL)
+ && (vb->name[vb->name_length - 1] <= value_list_tail[i]->subid))
{
DEBUG ("snmp plugin: host = %s; data = %s; i = %i; "
"SUBID is not increasing.",
@@ -1364,11 +1392,11 @@ static int csnmp_read_table (host_definition_t *host, data_definition_t *data)
data->scale, data->shift);
vt->next = NULL;
- if (value_table_ptr[i] == NULL)
- value_table[i] = vt;
+ if (value_list_tail[i] == NULL)
+ value_list_head[i] = vt;
else
- value_table_ptr[i]->next = vt;
- value_table_ptr[i] = vt;
+ value_list_tail[i]->next = vt;
+ value_list_tail[i] = vt;
/* Copy OID to oid_list[i + 1] */
memcpy (oid_list[i].oid, vb->name, sizeof (oid) * vb->name_length);
@@ -1385,28 +1413,28 @@ static int csnmp_read_table (host_definition_t *host, data_definition_t *data)
res = NULL;
if (status == 0)
- csnmp_dispatch_table (host, data, instance_list, value_table);
+ csnmp_dispatch_table (host, data, instance_list_head, value_list_head);
/* Free all allocated variables here */
- while (instance_list != NULL)
+ while (instance_list_head != NULL)
{
- instance_list_ptr = instance_list->next;
- sfree (instance_list);
- instance_list = instance_list_ptr;
+ csnmp_list_instances_t *next = instance_list_head->next;
+ sfree (instance_list_head);
+ instance_list_head = next;
}
for (i = 0; i < data->values_len; i++)
{
- csnmp_table_values_t *tmp;
- while (value_table[i] != NULL)
+ while (value_list_head[i] != NULL)
{
- tmp = value_table[i]->next;
- sfree (value_table[i]);
- value_table[i] = tmp;
+ csnmp_table_values_t *next = value_list_head[i]->next;
+ sfree (value_list_head[i]);
+ value_list_head[i] = next;
}
}
- sfree (value_table);
+ sfree (value_list_head);
+ sfree (value_list_tail);
sfree (oid_list);
return (0);