X-Git-Url: https://git.octo.it/?p=collectd.git;a=blobdiff_plain;f=src%2Fonewire.c;h=5c5152db6dce1bf1b2501f673961091eb9df13c7;hp=2901b78491fe700c9da551b2d17dee2ea28db0fc;hb=1159cb5d383c55a80a0db100b8f7aadcf44740a5;hpb=2079ee1517e34de372f58e7e2267ad5c71a8a41f diff --git a/src/onewire.c b/src/onewire.c index 2901b784..5c5152db 100644 --- a/src/onewire.c +++ b/src/onewire.c @@ -123,7 +123,7 @@ static int timeval_subtract(struct timeval *result, struct timeval *t2, result->tv_sec = diff / 1000000; result->tv_usec = diff % 1000000; - return (diff < 0); + return diff < 0; } #endif /* COLLECT_DEBUG */ @@ -169,7 +169,7 @@ static int direct_list_insert(const char *config) { if (regcomp(®ex_direct, regexp_to_match, REG_EXTENDED)) { ERROR("onewire plugin: Cannot compile regex"); direct_list_element_free(element); - return (1); + return 1; } regex_direct_initialized = 1; DEBUG("onewire plugin: Compiled regex!!"); @@ -242,7 +242,7 @@ static int cow_load_config(const char *key, const char *value) { if (ignorelist_add(sensor_list, value)) { ERROR("onewire plugin: Cannot add value to ignorelist."); - return (1); + return 1; } } else { DEBUG("onewire plugin: %s is a direct access", value); @@ -257,7 +257,7 @@ static int cow_load_config(const char *key, const char *value) { temp = strdup(value); if (temp == NULL) { ERROR("onewire plugin: strdup failed."); - return (1); + return 1; } sfree(device_g); device_g = temp; @@ -269,15 +269,14 @@ static int cow_load_config(const char *key, const char *value) { else ERROR("onewire plugin: Invalid `Interval' setting: %s", value); } else { - return (-1); + return -1; } - return (0); + return 0; } static int cow_read_values(const char *path, const char *name, const ow_family_features_t *family_info) { - value_t values[1]; value_list_t vl = VALUE_LIST_INIT; int success = 0; @@ -287,10 +286,6 @@ static int cow_read_values(const char *path, const char *name, return 0; } - vl.values = values; - vl.values_len = 1; - - sstrncpy(vl.host, hostname_g, sizeof(vl.host)); sstrncpy(vl.plugin, "onewire", sizeof(vl.plugin)); sstrncpy(vl.plugin_instance, name, sizeof(vl.plugin_instance)); @@ -298,6 +293,7 @@ static int cow_read_values(const char *path, const char *name, char *buffer; size_t buffer_size; int status; + char errbuf[1024]; char file[4096]; char *endptr; @@ -311,14 +307,15 @@ static int cow_read_values(const char *path, const char *name, DEBUG("Start reading onewire device %s", file); status = OW_get(file, &buffer, &buffer_size); if (status < 0) { - ERROR("onewire plugin: OW_get (%s/%s) failed. status = %#x;", path, - family_info->features[i].filename, status); - return (-1); + ERROR("onewire plugin: OW_get (%s/%s) failed. error = %s;", path, + family_info->features[i].filename, + sstrerror(errno, errbuf, sizeof(errbuf))); + return -1; } DEBUG("Read onewire device %s as %s", file, buffer); endptr = NULL; - values[0].gauge = strtod(buffer, &endptr); + gauge_t g = strtod(buffer, &endptr); if (endptr == NULL) { ERROR("onewire plugin: Buffer is not a number: %s", buffer); continue; @@ -328,13 +325,16 @@ static int cow_read_values(const char *path, const char *name, sstrncpy(vl.type_instance, family_info->features[i].type_instance, sizeof(vl.type_instance)); + vl.values = &(value_t){.gauge = g}; + vl.values_len = 1; + plugin_dispatch_values(&vl); success++; free(buffer); } /* for (i = 0; i < features_num; i++) */ - return ((success > 0) ? 0 : -1); + return (success > 0) ? 0 : -1; } /* int cow_read_values */ /* Forward declaration so the recursion below works */ @@ -350,21 +350,22 @@ static int cow_read_ds2409(const char *path) { char subpath[4096]; int status; - status = ssnprintf(subpath, sizeof(subpath), "%s/main", path); + status = snprintf(subpath, sizeof(subpath), "%s/main", path); if ((status > 0) && (status < (int)sizeof(subpath))) cow_read_bus(subpath); - status = ssnprintf(subpath, sizeof(subpath), "%s/aux", path); + status = snprintf(subpath, sizeof(subpath), "%s/aux", path); if ((status > 0) && (status < (int)sizeof(subpath))) cow_read_bus(subpath); - return (0); + return 0; } /* int cow_read_ds2409 */ static int cow_read_bus(const char *path) { char *buffer; size_t buffer_size; int status; + char errbuf[1024]; char *buffer_ptr; char *dummy; @@ -373,8 +374,9 @@ static int cow_read_bus(const char *path) { status = OW_get(path, &buffer, &buffer_size); if (status < 0) { - ERROR("onewire plugin: OW_get (%s) failed. status = %#x;", path, status); - return (-1); + ERROR("onewire plugin: OW_get (%s) failed. error = %s;", path, + sstrerror(errno, errbuf, sizeof(errbuf))); + return -1; } DEBUG("onewire plugin: OW_get (%s) returned: %s", path, buffer); @@ -386,9 +388,9 @@ static int cow_read_bus(const char *path) { dummy = NULL; if (strcmp("/", path) == 0) - status = ssnprintf(subpath, sizeof(subpath), "/%s", buffer_ptr); + status = snprintf(subpath, sizeof(subpath), "/%s", buffer_ptr); else - status = ssnprintf(subpath, sizeof(subpath), "%s/%s", path, buffer_ptr); + status = snprintf(subpath, sizeof(subpath), "%s/%s", path, buffer_ptr); if ((status <= 0) || (status >= (int)sizeof(subpath))) continue; @@ -413,41 +415,37 @@ static int cow_read_bus(const char *path) { } /* while (strtok_r) */ free(buffer); - return (0); + return 0; } /* int cow_read_bus */ /* =================================================================================== */ static int cow_simple_read(void) { - value_t values[1]; value_list_t vl = VALUE_LIST_INIT; char *buffer; size_t buffer_size; int status; + char errbuf[1024]; char *endptr; direct_access_element_t *traverse; /* traverse list and check entries */ for (traverse = direct_list; traverse != NULL; traverse = traverse->next) { - vl.values = values; - vl.values_len = 1; - - sstrncpy(vl.host, hostname_g, sizeof(vl.host)); sstrncpy(vl.plugin, "onewire", sizeof(vl.plugin)); sstrncpy(vl.plugin_instance, traverse->address, sizeof(vl.plugin_instance)); status = OW_get(traverse->path, &buffer, &buffer_size); if (status < 0) { - ERROR("onewire plugin: OW_get (%s) failed. status = %#x;", traverse->path, - status); - return (-1); + ERROR("onewire plugin: OW_get (%s) failed. status = %s;", traverse->path, + sstrerror(errno, errbuf, sizeof(errbuf))); + return -1; } DEBUG("onewire plugin: Read onewire device %s as %s", traverse->path, buffer); endptr = NULL; - values[0].gauge = strtod(buffer, &endptr); + gauge_t g = strtod(buffer, &endptr); if (endptr == NULL) { ERROR("onewire plugin: Buffer is not a number: %s", buffer); continue; @@ -456,6 +454,9 @@ static int cow_simple_read(void) { sstrncpy(vl.type, traverse->file, sizeof(vl.type)); sstrncpy(vl.type_instance, "", sizeof("")); + vl.values = &(value_t){.gauge = g}; + vl.values_len = 1; + plugin_dispatch_values(&vl); free(buffer); } /* for (traverse) */ @@ -501,29 +502,31 @@ static int cow_shutdown(void) { regfree(®ex_direct); } - return (0); + return 0; } /* int cow_shutdown */ static int cow_init(void) { int status; + char errbuf[1024]; if (device_g == NULL) { ERROR("onewire plugin: cow_init: No device configured."); - return (-1); + return -1; } DEBUG("onewire plugin: about to init device <%s>.", device_g); status = (int)OW_init(device_g); if (status != 0) { - ERROR("onewire plugin: OW_init(%s) failed: %i.", device_g, status); - return (1); + ERROR("onewire plugin: OW_init(%s) failed: %s.", device_g, + sstrerror(errno, errbuf, sizeof(errbuf))); + return 1; } plugin_register_complex_read(/* group = */ NULL, "onewire", cow_read, ow_interval, /* user data = */ NULL); plugin_register_shutdown("onewire", cow_shutdown); - return (0); + return 0; } /* int cow_init */ void module_register(void) { @@ -531,5 +534,3 @@ void module_register(void) { plugin_register_config("onewire", cow_load_config, config_keys, config_keys_num); } - -/* vim: set sw=2 sts=2 ts=8 et fdm=marker cindent : */