X-Git-Url: https://git.octo.it/?a=blobdiff_plain;f=src%2Fapache.c;h=07b2b57d0700cc0272e828d94bf273fcf05086cc;hb=0d9f395599348e735e6f461e1c328293bef0d060;hp=35e02ab5f9bfb21fa46c6482b6e1db9bb4c87e91;hpb=2079ee1517e34de372f58e7e2267ad5c71a8a41f;p=collectd.git diff --git a/src/apache.c b/src/apache.c index 35e02ab5..07b2b57d 100644 --- a/src/apache.c +++ b/src/apache.c @@ -89,11 +89,11 @@ static size_t apache_curl_callback(void *buf, size_t size, size_t nmemb, if (st == NULL) { ERROR("apache plugin: apache_curl_callback: " "user_data pointer is NULL."); - return (0); + return 0; } if (len == 0) - return (len); + return len; if ((st->apache_buffer_fill + len) >= st->apache_buffer_size) { char *temp; @@ -101,7 +101,7 @@ static size_t apache_curl_callback(void *buf, size_t size, size_t nmemb, temp = realloc(st->apache_buffer, st->apache_buffer_fill + len + 1); if (temp == NULL) { ERROR("apache plugin: realloc failed."); - return (0); + return 0; } st->apache_buffer = temp; st->apache_buffer_size = st->apache_buffer_fill + len + 1; @@ -111,7 +111,7 @@ static size_t apache_curl_callback(void *buf, size_t size, size_t nmemb, st->apache_buffer_fill += len; st->apache_buffer[st->apache_buffer_fill] = 0; - return (len); + return len; } /* int apache_curl_callback */ static size_t apache_header_callback(void *buf, size_t size, size_t nmemb, @@ -123,15 +123,15 @@ static size_t apache_header_callback(void *buf, size_t size, size_t nmemb, if (st == NULL) { ERROR("apache plugin: apache_header_callback: " "user_data pointer is NULL."); - return (0); + return 0; } if (len == 0) - return (len); + return len; /* look for the Server header */ if (strncasecmp(buf, "Server: ", strlen("Server: ")) != 0) - return (len); + return len; if (strstr(buf, "Apache") != NULL) st->server_type = APACHE; @@ -146,7 +146,7 @@ static size_t apache_header_callback(void *buf, size_t size, size_t nmemb, NOTICE("apache plugin: Unknown server software: %s", hdr); } - return (len); + return len; } /* apache_header_callback */ /* Configuration handling functiions @@ -164,7 +164,7 @@ static int config_add(oconfig_item_t *ci) { st = calloc(1, sizeof(*st)); if (st == NULL) { ERROR("apache plugin: calloc failed."); - return (-1); + return -1; } st->timeout = -1; @@ -172,7 +172,7 @@ static int config_add(oconfig_item_t *ci) { status = cf_util_get_string(ci, &st->name); if (status != 0) { sfree(st); - return (status); + return status; } assert(st->name != NULL); @@ -216,28 +216,25 @@ static int config_add(oconfig_item_t *ci) { status = -1; } - if (status == 0) { - user_data_t ud = {.data = st, .free_func = apache_free}; - - char callback_name[3 * DATA_MAX_NAME_LEN]; - - ssnprintf(callback_name, sizeof(callback_name), "apache/%s/%s", - (st->host != NULL) ? st->host : hostname_g, - (st->name != NULL) ? st->name : "default"); - - status = plugin_register_complex_read(/* group = */ NULL, - /* name = */ callback_name, - /* callback = */ apache_read_host, - /* interval = */ 0, - /* user_data = */ &ud); - } - if (status != 0) { apache_free(st); - return (-1); + return -1; } - return (0); + char callback_name[3 * DATA_MAX_NAME_LEN]; + + snprintf(callback_name, sizeof(callback_name), "apache/%s/%s", + (st->host != NULL) ? st->host : hostname_g, + (st->name != NULL) ? st->name : "default"); + + return plugin_register_complex_read( + /* group = */ NULL, + /* name = */ callback_name, + /* callback = */ apache_read_host, + /* interval = */ 0, + &(user_data_t){ + .data = st, .free_func = apache_free, + }); } /* int config_add */ static int config(oconfig_item_t *ci) { @@ -256,7 +253,7 @@ static int config(oconfig_item_t *ci) { child->key); } /* for (ci->children) */ - return (status); + return status; } /* int config */ /* initialize curl for each host */ @@ -272,7 +269,7 @@ static int init_host(apache_t *st) /* {{{ */ if ((st->curl = curl_easy_init()) == NULL) { ERROR("apache plugin: init_host: `curl_easy_init' failed."); - return (-1); + return -1; } curl_easy_setopt(st->curl, CURLOPT_NOSIGNAL, 1L); @@ -313,22 +310,21 @@ static int init_host(apache_t *st) /* {{{ */ static char credentials[1024]; int status; - status = ssnprintf(credentials, sizeof(credentials), "%s:%s", st->user, - (st->pass == NULL) ? "" : st->pass); + status = snprintf(credentials, sizeof(credentials), "%s:%s", st->user, + (st->pass == NULL) ? "" : st->pass); if ((status < 0) || ((size_t)status >= sizeof(credentials))) { ERROR("apache plugin: init_host: Returning an error " "because the credentials have been " "truncated."); curl_easy_cleanup(st->curl); st->curl = NULL; - return (-1); + return -1; } curl_easy_setopt(st->curl, CURLOPT_USERPWD, credentials); #endif } - curl_easy_setopt(st->curl, CURLOPT_URL, st->url); curl_easy_setopt(st->curl, CURLOPT_FOLLOWLOCATION, 1L); curl_easy_setopt(st->curl, CURLOPT_MAXREDIRS, 50L); @@ -347,7 +343,7 @@ static int init_host(apache_t *st) /* {{{ */ (long)CDTIME_T_TO_MS(plugin_get_interval())); #endif - return (0); + return 0; } /* }}} int init_host */ static void submit_value(const char *type, const char *type_instance, @@ -357,8 +353,8 @@ static void submit_value(const char *type, const char *type_instance, vl.values = &value; vl.values_len = 1; - sstrncpy(vl.host, (st->host != NULL) ? st->host : hostname_g, - sizeof(vl.host)); + if (st->host != NULL) + sstrncpy(vl.host, st->host, sizeof(vl.host)); sstrncpy(vl.plugin, "apache", sizeof(vl.plugin)); if (st->name != NULL) @@ -372,17 +368,13 @@ static void submit_value(const char *type, const char *type_instance, } /* void submit_value */ static void submit_derive(const char *type, const char *type_instance, - derive_t c, apache_t *st) { - value_t v; - v.derive = c; - submit_value(type, type_instance, v, st); + derive_t d, apache_t *st) { + submit_value(type, type_instance, (value_t){.derive = d}, st); } /* void submit_derive */ static void submit_gauge(const char *type, const char *type_instance, gauge_t g, apache_t *st) { - value_t v; - v.gauge = g; - submit_value(type, type_instance, v, st); + submit_value(type, type_instance, (value_t){.gauge = g}, st); } /* void submit_gauge */ static void submit_scoreboard(char *buf, apache_t *st) { @@ -509,14 +501,17 @@ static int apache_read_host(user_data_t *user_data) /* {{{ */ if (st->curl == NULL) { status = init_host(st); if (status != 0) - return (-1); + return -1; } assert(st->curl != NULL); st->apache_buffer_fill = 0; + + curl_easy_setopt(st->curl, CURLOPT_URL, st->url); + if (curl_easy_perform(st->curl) != CURLE_OK) { ERROR("apache: curl_easy_perform failed: %s", st->apache_curl_error); - return (-1); + return -1; } /* fallback - server_type to apache if not set at this time */ @@ -562,7 +557,7 @@ static int apache_read_host(user_data_t *user_data) /* {{{ */ st->apache_buffer_fill = 0; - return (0); + return 0; } /* }}} int apache_read_host */ static int apache_init(void) /* {{{ */ @@ -570,12 +565,10 @@ static int apache_init(void) /* {{{ */ /* Call this while collectd is still single-threaded to avoid * initialization issues in libgcrypt. */ curl_global_init(CURL_GLOBAL_SSL); - return (0); + return 0; } /* }}} int apache_init */ void module_register(void) { plugin_register_complex_config("apache", config); plugin_register_init("apache", apache_init); } /* void module_register */ - -/* vim: set sw=8 noet fdm=marker : */