X-Git-Url: https://git.tokkee.org/?a=blobdiff_plain;f=src%2Fsnmp.c;h=d9c3bd6591555327b53f2b42ccb08e43b1799ac7;hb=016a811dbfeb8db07c78e9e660463abf0daac824;hp=00df3779bb52907c001de892b5ffae6ce9c5a442;hpb=8579aab25eae16e266b03e9b623285e56036c922;p=collectd.git diff --git a/src/snmp.c b/src/snmp.c index 00df3779..d9c3bd65 100644 --- 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); } @@ -655,8 +655,9 @@ static int csnmp_config_add_host (oconfig_item_t *ci) if (hd->interval != 0) cb_interval.tv_sec = (time_t) hd->interval; - status = plugin_register_complex_read (cb_name, csnmp_read_host, - /* interval = */ &cb_interval, /* user_data = */ &cb_data); + status = plugin_register_complex_read (/* group = */ NULL, cb_name, + csnmp_read_host, /* interval = */ &cb_interval, + /* user_data = */ &cb_data); if (status != 0) { ERROR ("snmp plugin: Registering complex read function failed."); @@ -726,7 +727,9 @@ static value_t csnmp_value_list_to_value (struct variable_list *vl, int type, 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) @@ -738,7 +741,12 @@ static value_t csnmp_value_list_to_value (struct variable_list *vl, int type, { 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) { @@ -754,7 +762,21 @@ static value_t csnmp_value_list_to_value (struct variable_list *vl, int type, } else { - WARNING ("snmp plugin: I don't know the ASN type `%i'", (int) vl->type); + char oid_buffer[1024]; + + memset (oid_buffer, 0, sizeof (oid_buffer)); + snprint_objid (oid_buffer, sizeof (oid_buffer) - 1, + vl->name, vl->name_length); + +#ifdef ASN_NULL + if (vl->type == ASN_NULL) + INFO ("snmp plugin: OID \"%s\" is undefined (type ASN_NULL)", + oid_buffer); + else +#endif + WARNING ("snmp plugin: I don't know the ASN type \"%i\" (OID: %s)", + (int) vl->type, oid_buffer); + defined = 0; } @@ -813,14 +835,24 @@ static value_t csnmp_value_list_to_value (struct variable_list *vl, int type, } 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 " @@ -854,10 +886,12 @@ static int csnmp_check_res_left_subtree (const host_definition_t *host, 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++; } @@ -951,7 +985,7 @@ static int csnmp_strvbcopy (char *dst, /* {{{ */ for (i = 0; i < num_chars; i++) { /* Check for control characters. */ - if ((src[i] >= 0) && (src[i] < 32)) + if ((unsigned char)src[i] < 32) return (csnmp_strvbcopy_hexstring (dst, vb, dst_size)); dst[i] = src[i]; } @@ -975,12 +1009,13 @@ static int csnmp_instance_list_add (csnmp_list_instances_t **head, 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; @@ -1127,7 +1162,7 @@ static int csnmp_dispatch_table (host_definition_t *host, data_definition_t *dat char temp[DATA_MAX_NAME_LEN]; if (instance_list_ptr == NULL) - ssnprintf (temp, sizeof (temp), "%u", (uint32_t) subid); + ssnprintf (temp, sizeof (temp), "%"PRIu32, (uint32_t) subid); else sstrncpy (temp, instance_list_ptr->instance, sizeof (temp)); @@ -1166,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); @@ -1211,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) @@ -1284,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."); @@ -1297,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++) @@ -1327,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.", @@ -1349,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); @@ -1370,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);