X-Git-Url: https://git.octo.it/?a=blobdiff_plain;f=src%2Fpowerdns.c;h=0b08b29870c5532b520818d3f66e9404a5477f68;hb=8fd8f76dc11064e75e44448d16d35e09e46191a2;hp=5f7a542ec9df32f1ce37ab6aa6078eabdc525b63;hpb=2079ee1517e34de372f58e7e2267ad5c71a8a41f;p=collectd.git diff --git a/src/powerdns.c b/src/powerdns.c index 5f7a542e..0b08b298 100644 --- a/src/powerdns.c +++ b/src/powerdns.c @@ -48,6 +48,12 @@ ERROR("powerdns plugin: %s failed: %s", func, \ sstrerror(errno, errbuf, sizeof(errbuf))); \ } while (0) +#define SOCK_ERROR(func, sockpath) \ + do { \ + char errbuf[1024]; \ + ERROR("powerdns plugin: Socket `%s` %s failed: %s", sockpath, func, \ + sstrerror(errno, errbuf, sizeof(errbuf))); \ + } while (0) #define SERVER_SOCKET LOCALSTATEDIR "/run/pdns.controlsocket" #define SERVER_COMMAND "SHOW * \n" @@ -314,9 +320,9 @@ static char *local_sockpath = NULL; /* */ static void submit(const char *plugin_instance, /* {{{ */ - const char *pdns_type, const char *value) { + const char *pdns_type, const char *value_str) { value_list_t vl = VALUE_LIST_INIT; - value_t values[1]; + value_t value; const char *type = NULL; const char *type_instance = NULL; @@ -330,7 +336,7 @@ static void submit(const char *plugin_instance, /* {{{ */ if (i >= lookup_table_length) { INFO("powerdns plugin: submit: Not found in lookup table: %s = %s;", - pdns_type, value); + pdns_type, value_str); return; } @@ -355,16 +361,15 @@ static void submit(const char *plugin_instance, /* {{{ */ return; } - if (0 != parse_value(value, &values[0], ds->ds[0].type)) { + if (0 != parse_value(value_str, &value, ds->ds[0].type)) { ERROR("powerdns plugin: Cannot convert `%s' " "to a number.", - value); + value_str); return; } - vl.values = values; + vl.values = &value; vl.values_len = 1; - sstrncpy(vl.host, hostname_g, sizeof(vl.host)); sstrncpy(vl.plugin, "powerdns", sizeof(vl.plugin)); sstrncpy(vl.type, type, sizeof(vl.type)); if (type_instance != NULL) @@ -385,13 +390,12 @@ static int powerdns_get_data_dgram(list_item_t *item, /* {{{ */ struct sockaddr_un sa_unix = {0}; - struct timeval stv_timeout; cdtime_t cdt_timeout; sd = socket(PF_UNIX, item->socktype, 0); if (sd < 0) { FUNC_ERROR("socket"); - return (-1); + return -1; } sa_unix.sun_family = AF_UNIX; @@ -401,9 +405,9 @@ static int powerdns_get_data_dgram(list_item_t *item, /* {{{ */ status = unlink(sa_unix.sun_path); if ((status != 0) && (errno != ENOENT)) { - FUNC_ERROR("unlink"); + SOCK_ERROR("unlink", sa_unix.sun_path); close(sd); - return (-1); + return -1; } do /* while (0) */ @@ -412,14 +416,14 @@ static int powerdns_get_data_dgram(list_item_t *item, /* {{{ */ * and otherwise the daemon cannot answer. */ status = bind(sd, (struct sockaddr *)&sa_unix, sizeof(sa_unix)); if (status != 0) { - FUNC_ERROR("bind"); + SOCK_ERROR("bind", sa_unix.sun_path); break; } /* Make the socket writeable by the daemon.. */ status = chmod(sa_unix.sun_path, 0666); if (status != 0) { - FUNC_ERROR("chmod"); + SOCK_ERROR("chmod", sa_unix.sun_path); break; } @@ -427,31 +431,30 @@ static int powerdns_get_data_dgram(list_item_t *item, /* {{{ */ if (cdt_timeout < TIME_T_TO_CDTIME_T(2)) cdt_timeout = TIME_T_TO_CDTIME_T(2); - CDTIME_T_TO_TIMEVAL(cdt_timeout, &stv_timeout); - - status = setsockopt(sd, SOL_SOCKET, SO_RCVTIMEO, &stv_timeout, - sizeof(stv_timeout)); + status = + setsockopt(sd, SOL_SOCKET, SO_RCVTIMEO, + &CDTIME_T_TO_TIMEVAL(cdt_timeout), sizeof(struct timeval)); if (status != 0) { - FUNC_ERROR("setsockopt"); + SOCK_ERROR("setsockopt", sa_unix.sun_path); break; } status = connect(sd, (struct sockaddr *)&item->sockaddr, sizeof(item->sockaddr)); if (status != 0) { - FUNC_ERROR("connect"); + SOCK_ERROR("connect", sa_unix.sun_path); break; } status = send(sd, item->command, strlen(item->command), 0); if (status < 0) { - FUNC_ERROR("send"); + SOCK_ERROR("send", sa_unix.sun_path); break; } status = recv(sd, temp, sizeof(temp), /* flags = */ 0); if (status < 0) { - FUNC_ERROR("recv"); + SOCK_ERROR("recv", sa_unix.sun_path); break; } buffer_size = status + 1; @@ -462,13 +465,13 @@ static int powerdns_get_data_dgram(list_item_t *item, /* {{{ */ unlink(sa_unix.sun_path); if (status != 0) - return (-1); + return -1; assert(buffer_size > 0); buffer = malloc(buffer_size); if (buffer == NULL) { FUNC_ERROR("malloc"); - return (-1); + return -1; } memcpy(buffer, temp, buffer_size - 1); @@ -477,7 +480,7 @@ static int powerdns_get_data_dgram(list_item_t *item, /* {{{ */ *ret_buffer = buffer; *ret_buffer_size = buffer_size; - return (0); + return 0; } /* }}} int powerdns_get_data_dgram */ static int powerdns_get_data_stream(list_item_t *item, /* {{{ */ @@ -493,7 +496,7 @@ static int powerdns_get_data_stream(list_item_t *item, /* {{{ */ sd = socket(PF_UNIX, item->socktype, 0); if (sd < 0) { FUNC_ERROR("socket"); - return (-1); + return -1; } struct timeval timeout; @@ -503,24 +506,24 @@ static int powerdns_get_data_stream(list_item_t *item, /* {{{ */ if (status != 0) { FUNC_ERROR("setsockopt"); close(sd); - return (-1); + return -1; } status = connect(sd, (struct sockaddr *)&item->sockaddr, sizeof(item->sockaddr)); if (status != 0) { - FUNC_ERROR("connect"); + SOCK_ERROR("connect", item->sockaddr.sun_path); close(sd); - return (-1); + return -1; } /* strlen + 1, because we need to send the terminating NULL byte, too. */ status = send(sd, item->command, strlen(item->command) + 1, /* flags = */ 0); if (status < 0) { - FUNC_ERROR("send"); + SOCK_ERROR("send", item->sockaddr.sun_path); close(sd); - return (-1); + return -1; } while (42) { @@ -528,7 +531,7 @@ static int powerdns_get_data_stream(list_item_t *item, /* {{{ */ status = recv(sd, temp, sizeof(temp), /* flags = */ 0); if (status < 0) { - FUNC_ERROR("recv"); + SOCK_ERROR("recv", item->sockaddr.sun_path); break; } else if (status == 0) break; @@ -555,18 +558,18 @@ static int powerdns_get_data_stream(list_item_t *item, /* {{{ */ *ret_buffer_size = buffer_size; } - return (status); + return status; } /* }}} int powerdns_get_data_stream */ static int powerdns_get_data(list_item_t *item, char **ret_buffer, size_t *ret_buffer_size) { if (item->socktype == SOCK_DGRAM) - return (powerdns_get_data_dgram(item, ret_buffer, ret_buffer_size)); + return powerdns_get_data_dgram(item, ret_buffer, ret_buffer_size); else if (item->socktype == SOCK_STREAM) - return (powerdns_get_data_stream(item, ret_buffer, ret_buffer_size)); + return powerdns_get_data_stream(item, ret_buffer, ret_buffer_size); else { ERROR("powerdns plugin: Unknown socket type: %i", (int)item->socktype); - return (-1); + return -1; } } /* int powerdns_get_data */ @@ -589,12 +592,12 @@ static int powerdns_read_server(list_item_t *item) /* {{{ */ item->command = strdup(SERVER_COMMAND); if (item->command == NULL) { ERROR("powerdns plugin: strdup failed."); - return (-1); + return -1; } status = powerdns_get_data(item, &buffer, &buffer_size); if (status != 0) - return (-1); + return -1; if (item->fields_num != 0) { fields = (const char *const *)item->fields; @@ -637,7 +640,7 @@ static int powerdns_read_server(list_item_t *item) /* {{{ */ sfree(buffer); - return (0); + return 0; } /* }}} int powerdns_read_server */ /* @@ -653,7 +656,7 @@ static int powerdns_update_recursor_command(list_item_t *li) /* {{{ */ int status; if (li == NULL) - return (0); + return 0; if (li->fields_num < 1) { sstrncpy(buffer, RECURSOR_COMMAND, sizeof(buffer)); @@ -664,7 +667,7 @@ static int powerdns_update_recursor_command(list_item_t *li) /* {{{ */ /* seperator = */ " "); if (status < 0) { ERROR("powerdns plugin: strjoin failed."); - return (-1); + return -1; } buffer[sizeof(buffer) - 1] = 0; size_t len = strlen(buffer); @@ -679,10 +682,10 @@ static int powerdns_update_recursor_command(list_item_t *li) /* {{{ */ li->command = strdup(buffer); if (li->command == NULL) { ERROR("powerdns plugin: strdup failed."); - return (-1); + return -1; } - return (0); + return 0; } /* }}} int powerdns_update_recursor_command */ static int powerdns_read_recursor(list_item_t *item) /* {{{ */ @@ -703,7 +706,7 @@ static int powerdns_read_recursor(list_item_t *item) /* {{{ */ status = powerdns_update_recursor_command(item); if (status != 0) { ERROR("powerdns plugin: powerdns_update_recursor_command failed."); - return (-1); + return -1; } DEBUG("powerdns plugin: powerdns_read_recursor: item->command = %s;", @@ -714,14 +717,14 @@ static int powerdns_read_recursor(list_item_t *item) /* {{{ */ status = powerdns_get_data(item, &buffer, &buffer_size); if (status != 0) { ERROR("powerdns plugin: powerdns_get_data failed."); - return (-1); + return -1; } keys_list = strdup(item->command); if (keys_list == NULL) { FUNC_ERROR("strdup"); sfree(buffer); - return (-1); + return -1; } key_saveptr = NULL; @@ -744,7 +747,7 @@ static int powerdns_read_recursor(list_item_t *item) /* {{{ */ sfree(buffer); sfree(keys_list); - return (0); + return 0; } /* }}} int powerdns_read_recursor */ static int powerdns_config_add_collect(list_item_t *li, /* {{{ */ @@ -754,21 +757,21 @@ static int powerdns_config_add_collect(list_item_t *li, /* {{{ */ if (ci->values_num < 1) { WARNING("powerdns plugin: The `Collect' option needs " "at least one argument."); - return (-1); + return -1; } for (int i = 0; i < ci->values_num; i++) if (ci->values[i].type != OCONFIG_TYPE_STRING) { WARNING("powerdns plugin: Only string arguments are allowed to " "the `Collect' option."); - return (-1); + return -1; } temp = realloc(li->fields, sizeof(char *) * (li->fields_num + ci->values_num)); if (temp == NULL) { WARNING("powerdns plugin: realloc failed."); - return (-1); + return -1; } li->fields = temp; @@ -784,7 +787,7 @@ static int powerdns_config_add_collect(list_item_t *li, /* {{{ */ /* Invalidate a previously computed command */ sfree(li->command); - return (0); + return 0; } /* }}} int powerdns_config_add_collect */ static int powerdns_config_add_server(oconfig_item_t *ci) /* {{{ */ @@ -797,20 +800,20 @@ static int powerdns_config_add_server(oconfig_item_t *ci) /* {{{ */ if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING)) { WARNING("powerdns plugin: `%s' needs exactly one string argument.", ci->key); - return (-1); + return -1; } item = calloc(1, sizeof(*item)); if (item == NULL) { ERROR("powerdns plugin: calloc failed."); - return (-1); + return -1; } item->instance = strdup(ci->values[0].value.string); if (item->instance == NULL) { ERROR("powerdns plugin: strdup failed."); sfree(item); - return (-1); + return -1; } /* @@ -829,7 +832,7 @@ static int powerdns_config_add_server(oconfig_item_t *ci) /* {{{ */ } else { /* We must never get here.. */ assert(0); - return (-1); + return -1; } status = 0; @@ -876,13 +879,13 @@ static int powerdns_config_add_server(oconfig_item_t *ci) /* {{{ */ if (status != 0) { sfree(socket_temp); sfree(item); - return (-1); + return -1; } DEBUG("powerdns plugin: Add server: instance = %s;", item->instance); sfree(socket_temp); - return (0); + return 0; } /* }}} int powerdns_config_add_server */ static int powerdns_config(oconfig_item_t *ci) /* {{{ */ @@ -894,7 +897,7 @@ static int powerdns_config(oconfig_item_t *ci) /* {{{ */ if (list == NULL) { ERROR("powerdns plugin: `llist_create' failed."); - return (-1); + return -1; } } @@ -912,7 +915,7 @@ static int powerdns_config(oconfig_item_t *ci) /* {{{ */ } else { char *temp = strdup(option->values[0].value.string); if (temp == NULL) - return (1); + return 1; sfree(local_sockpath); local_sockpath = temp; } @@ -921,7 +924,7 @@ static int powerdns_config(oconfig_item_t *ci) /* {{{ */ } } /* for (i = 0; i < ci->children_num; i++) */ - return (0); + return 0; } /* }}} int powerdns_config */ static int powerdns_read(void) { @@ -930,12 +933,12 @@ static int powerdns_read(void) { item->func(item); } - return (0); + return 0; } /* static int powerdns_read */ static int powerdns_shutdown(void) { if (list == NULL) - return (0); + return 0; for (llentry_t *e = llist_head(list); e != NULL; e = e->next) { list_item_t *item = (list_item_t *)e->value; @@ -949,7 +952,7 @@ static int powerdns_shutdown(void) { llist_destroy(list); list = NULL; - return (0); + return 0; } /* static int powerdns_shutdown */ void module_register(void) { @@ -957,5 +960,3 @@ void module_register(void) { plugin_register_read("powerdns", powerdns_read); plugin_register_shutdown("powerdns", powerdns_shutdown); } /* void module_register */ - -/* vim: set sw=2 sts=2 ts=8 fdm=marker : */