X-Git-Url: https://git.octo.it/?p=collectd.git;a=blobdiff_plain;f=src%2Fapache.c;h=5c67a3885fa9b0fccfbe76bc57df9854ef91693d;hp=e2f75c180ae219dfeb81d7abfdfa5fda0b54db25;hb=06a86a60a7dabc685bdbd81ce3d36ea5f7e2c2d4;hpb=b165cd13cd30a30ac830df1f6f6fbd52474a6f64 diff --git a/src/apache.c b/src/apache.c index e2f75c18..5c67a388 100644 --- a/src/apache.c +++ b/src/apache.c @@ -40,8 +40,8 @@ struct apache_s { char *url; char *user; char *pass; - _Bool verify_peer; - _Bool verify_host; + bool verify_peer; + bool verify_host; char *cacert; char *ssl_ciphers; char *server; /* user specific server type */ @@ -82,23 +82,19 @@ static void apache_free(void *arg) { static size_t apache_curl_callback(void *buf, size_t size, size_t nmemb, void *user_data) { - size_t len = size * nmemb; - apache_t *st; - - st = user_data; + apache_t *st = user_data; if (st == NULL) { ERROR("apache plugin: apache_curl_callback: " "user_data pointer is NULL."); return 0; } + size_t len = size * nmemb; if (len == 0) return len; if ((st->apache_buffer_fill + len) >= st->apache_buffer_size) { - char *temp; - - temp = realloc(st->apache_buffer, st->apache_buffer_fill + len + 1); + char *temp = realloc(st->apache_buffer, st->apache_buffer_fill + len + 1); if (temp == NULL) { ERROR("apache plugin: realloc failed."); return 0; @@ -116,16 +112,14 @@ static size_t apache_curl_callback(void *buf, size_t size, size_t nmemb, static size_t apache_header_callback(void *buf, size_t size, size_t nmemb, void *user_data) { - size_t len = size * nmemb; - apache_t *st; - - st = user_data; + apache_t *st = user_data; if (st == NULL) { ERROR("apache plugin: apache_header_callback: " "user_data pointer is NULL."); return 0; } + size_t len = size * nmemb; if (len == 0) return len; @@ -158,10 +152,7 @@ static size_t apache_header_callback(void *buf, size_t size, size_t nmemb, * */ static int config_add(oconfig_item_t *ci) { - apache_t *st; - int status; - - st = calloc(1, sizeof(*st)); + apache_t *st = calloc(1, sizeof(*st)); if (st == NULL) { ERROR("apache plugin: calloc failed."); return -1; @@ -169,7 +160,7 @@ static int config_add(oconfig_item_t *ci) { st->timeout = -1; - status = cf_util_get_string(ci, &st->name); + int status = cf_util_get_string(ci, &st->name); if (status != 0) { sfree(st); return status; @@ -216,33 +207,28 @@ static int config_add(oconfig_item_t *ci) { status = -1; } - if (status == 0) { - 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_t){ - .data = st, .free_func = apache_free, - }); - } - if (status != 0) { apache_free(st); 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) { - int status = 0; - for (int i = 0; i < ci->children_num; i++) { oconfig_item_t *child = ci->children + i; @@ -256,7 +242,7 @@ static int config(oconfig_item_t *ci) { child->key); } /* for (ci->children) */ - return status; + return 0; } /* int config */ /* initialize curl for each host */ @@ -311,10 +297,8 @@ static int init_host(apache_t *st) /* {{{ */ (st->pass == NULL) ? "" : st->pass); #else static char credentials[1024]; - int status; - - status = ssnprintf(credentials, sizeof(credentials), "%s:%s", st->user, - (st->pass == NULL) ? "" : st->pass); + int 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 " @@ -328,7 +312,6 @@ static int init_host(apache_t *st) /* {{{ */ #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); @@ -483,33 +466,21 @@ static void submit_scoreboard(char *buf, apache_t *st) { static int apache_read_host(user_data_t *user_data) /* {{{ */ { - char *ptr; - char *saveptr; - char *line; - - char *fields[4]; - int fields_num; - - apache_t *st; - - st = user_data->data; - - int status; - - char *content_type; - static const char *text_plain = "text/plain"; + apache_t *st = user_data->data; assert(st->url != NULL); /* (Assured by `config_add') */ if (st->curl == NULL) { - status = init_host(st); - if (status != 0) + if (init_host(st) != 0) 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; @@ -522,7 +493,10 @@ static int apache_read_host(user_data_t *user_data) /* {{{ */ st->server_type = APACHE; } - status = curl_easy_getinfo(st->curl, CURLINFO_CONTENT_TYPE, &content_type); + char *content_type; + static const char *text_plain = "text/plain"; + int status = + curl_easy_getinfo(st->curl, CURLINFO_CONTENT_TYPE, &content_type); if ((status == CURLE_OK) && (content_type != NULL) && (strncasecmp(content_type, text_plain, strlen(text_plain)) != 0)) { WARNING("apache plugin: `Content-Type' response header is not `%s' " @@ -531,11 +505,14 @@ static int apache_read_host(user_data_t *user_data) /* {{{ */ text_plain, content_type); } - ptr = st->apache_buffer; - saveptr = NULL; + char *ptr = st->apache_buffer; + char *saveptr = NULL; + char *line; while ((line = strtok_r(ptr, "\n\r", &saveptr)) != NULL) { ptr = NULL; - fields_num = strsplit(line, fields, STATIC_ARRAY_SIZE(fields)); + char *fields[4]; + + int fields_num = strsplit(line, fields, STATIC_ARRAY_SIZE(fields)); if (fields_num == 3) { if ((strcmp(fields[0], "Total") == 0) &&