X-Git-Url: https://git.octo.it/?a=blobdiff_plain;f=src%2Fsnmp.c;h=89cbc46016db0610e5e2e6f71f234b2eedcaa495;hb=db35efb33e81d0a013e09a8a6ffa362ad5962f7c;hp=d9a932a393ba65e33be5c3970046326d5df6c0e8;hpb=8eb05d21637cd1eb3b6c4c4d3ed519cc2fd3ebf6;p=collectd.git diff --git a/src/snmp.c b/src/snmp.c index d9a932a3..89cbc460 100644 --- a/src/snmp.c +++ b/src/snmp.c @@ -310,7 +310,7 @@ static int csnmp_config_add_data_values (data_definition_t *dd, oconfig_item_t * sfree (dd->values); dd->values_len = 0; - dd->values = (oid_t *) malloc (sizeof (oid_t) * ci->values_num); + dd->values = malloc (sizeof (*dd->values) * ci->values_num); if (dd->values == NULL) return (-1); dd->values_len = (size_t) ci->values_num; @@ -384,10 +384,9 @@ static int csnmp_config_add_data (oconfig_item_t *ci) int status = 0; int i; - dd = (data_definition_t *) malloc (sizeof (data_definition_t)); + dd = calloc (1, sizeof (*dd)); if (dd == NULL) return (-1); - memset (dd, '\0', sizeof (data_definition_t)); status = cf_util_get_string(ci, &dd->name); if (status != 0) @@ -521,7 +520,7 @@ static int csnmp_config_add_host_collect (host_definition_t *host, } data_list_len = host->data_list_len + ci->values_num; - data_list = (data_definition_t **) realloc (host->data_list, + data_list = realloc (host->data_list, sizeof (data_definition_t *) * data_list_len); if (data_list == NULL) return (-1); @@ -646,16 +645,18 @@ static int csnmp_config_add_host (oconfig_item_t *ci) char cb_name[DATA_MAX_NAME_LEN]; user_data_t cb_data; - hd = (host_definition_t *) malloc (sizeof (host_definition_t)); + hd = calloc (1, sizeof (*hd)); if (hd == NULL) return (-1); - memset (hd, '\0', sizeof (host_definition_t)); hd->version = 2; C_COMPLAIN_INIT (&hd->complaint); status = cf_util_get_string(ci, &hd->name); if (status != 0) + { + sfree (hd); return status; + } hd->sess_handle = NULL; hd->interval = 0; @@ -924,8 +925,7 @@ 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; - if ((vl->type == ASN_INTEGER) - || (vl->type == ASN_GAUGE)) + if (vl->type == ASN_INTEGER) prefer_signed = 1; DEBUG ("snmp plugin: Parsed int32 value is %"PRIu64".", tmp_unsigned); @@ -1104,6 +1104,14 @@ static int csnmp_strvbcopy (char *dst, /* {{{ */ src = (char *) vb->val.string; else if (vb->type == ASN_BIT_STR) src = (char *) vb->val.bitstring; + else if (vb->type == ASN_IPADDRESS) + { + return ssnprintf (dst, dst_size, "%"PRIu8".%"PRIu8".%"PRIu8".%"PRIu8"", + (uint8_t) vb->val.string[0], + (uint8_t) vb->val.string[1], + (uint8_t) vb->val.string[2], + (uint8_t) vb->val.string[3]); + } else { dst[0] = 0; @@ -1152,13 +1160,12 @@ static int csnmp_instance_list_add (csnmp_list_instances_t **head, csnmp_oid_init (&vb_name, vb->name, vb->name_length); - il = malloc (sizeof (*il)); + il = calloc (1, sizeof (*il)); if (il == NULL) { - ERROR ("snmp plugin: malloc failed."); + ERROR ("snmp plugin: calloc failed."); return (-1); } - memset (il, 0, sizeof (*il)); il->next = NULL; status = csnmp_oid_suffix (&il->suffix, &vb_name, &dd->instance.oid); @@ -1169,7 +1176,7 @@ static int csnmp_instance_list_add (csnmp_list_instances_t **head, } /* Get instance name */ - if ((vb->type == ASN_OCTET_STR) || (vb->type == ASN_BIT_STR)) + if ((vb->type == ASN_OCTET_STR) || (vb->type == ASN_BIT_STR) || (vb->type == ASN_IPADDRESS)) { char *ptr; @@ -1388,7 +1395,7 @@ static int csnmp_dispatch_table (host_definition_t *host, data_definition_t *dat static int csnmp_read_table (host_definition_t *host, data_definition_t *data) { struct snmp_pdu *req; - struct snmp_pdu *res; + struct snmp_pdu *res = NULL; struct variable_list *vb; const data_set_t *ds; @@ -1597,14 +1604,13 @@ static int csnmp_read_table (host_definition_t *host, data_definition_t *data) continue; } - vt = malloc (sizeof (*vt)); + vt = calloc (1, sizeof (*vt)); if (vt == NULL) { - ERROR ("snmp plugin: malloc failed."); + ERROR ("snmp plugin: calloc failed."); status = -1; break; } - memset (vt, 0, sizeof (*vt)); vt->value = csnmp_value_list_to_value (vb, ds->ds[i].type, data->scale, data->shift, host->name, data->name); @@ -1667,7 +1673,7 @@ static int csnmp_read_table (host_definition_t *host, data_definition_t *data) static int csnmp_read_value (host_definition_t *host, data_definition_t *data) { struct snmp_pdu *req; - struct snmp_pdu *res; + struct snmp_pdu *res = NULL; struct variable_list *vb; const data_set_t *ds; @@ -1681,7 +1687,7 @@ static int csnmp_read_value (host_definition_t *host, data_definition_t *data) if (host->sess_handle == NULL) { - DEBUG ("snmp plugin: csnmp_read_table: host->sess_handle == NULL"); + DEBUG ("snmp plugin: csnmp_read_value: host->sess_handle == NULL"); return (-1); } @@ -1700,7 +1706,7 @@ static int csnmp_read_value (host_definition_t *host, data_definition_t *data) } vl.values_len = ds->ds_num; - vl.values = (value_t *) malloc (sizeof (value_t) * vl.values_len); + vl.values = malloc (sizeof (*vl.values) * vl.values_len); if (vl.values == NULL) return (-1); for (i = 0; i < vl.values_len; i++) @@ -1729,7 +1735,6 @@ static int csnmp_read_value (host_definition_t *host, data_definition_t *data) for (i = 0; i < data->values_len; i++) snmp_add_null_var (req, data->values[i].oid, data->values[i].oid_len); - res = NULL; status = snmp_sess_synch_response (host->sess_handle, req, &res); if ((status != STAT_SUCCESS) || (res == NULL)) @@ -1742,9 +1747,9 @@ static int csnmp_read_value (host_definition_t *host, data_definition_t *data) if (res != NULL) snmp_free_pdu (res); - res = NULL; sfree (errstr); + sfree (vl.values); csnmp_host_close_session (host); return (-1); @@ -1767,9 +1772,7 @@ static int csnmp_read_value (host_definition_t *host, data_definition_t *data) data->scale, data->shift, host->name, data->name); } /* for (res->variables) */ - if (res != NULL) - snmp_free_pdu (res); - res = NULL; + snmp_free_pdu (res); DEBUG ("snmp plugin: -> plugin_dispatch_values (&vl);"); plugin_dispatch_values (&vl); @@ -1781,8 +1784,6 @@ static int csnmp_read_value (host_definition_t *host, data_definition_t *data) static int csnmp_read_host (user_data_t *ud) { host_definition_t *host; - cdtime_t time_start; - cdtime_t time_end; int status; int success; int i; @@ -1792,8 +1793,6 @@ static int csnmp_read_host (user_data_t *ud) if (host->interval == 0) host->interval = plugin_get_interval (); - time_start = cdtime (); - if (host->sess_handle == NULL) csnmp_host_open_session (host); @@ -1814,16 +1813,6 @@ static int csnmp_read_host (user_data_t *ud) success++; } - time_end = cdtime (); - if ((time_end - time_start) > host->interval) - { - WARNING ("snmp plugin: Host `%s' should be queried every %.3f " - "seconds, but reading all values takes %.3f seconds.", - host->name, - CDTIME_T_TO_DOUBLE (host->interval), - CDTIME_T_TO_DOUBLE (time_end - time_start)); - } - if (success == 0) return (-1);