X-Git-Url: https://git.octo.it/?a=blobdiff_plain;f=src%2Fovs_events.c;h=c77bde4a436eb3a079321f78541c33a3ce3f3bab;hb=f1a8429a57361b11d7c4faca5f78dd12831f9377;hp=7521b280d2ec1769e538b7bc6c86dcd9e1bac0a6;hpb=4c5d22285e8a149a82909061f320faa22531326e;p=collectd.git diff --git a/src/ovs_events.c b/src/ovs_events.c index 7521b280..c77bde4a 100644 --- a/src/ovs_events.c +++ b/src/ovs_events.c @@ -38,11 +38,6 @@ #define OVS_EVENTS_PLUGIN "ovs_events" #define OVS_EVENTS_CTX_LOCK \ for (int __i = ovs_events_ctx_lock(); __i != 0; __i = ovs_events_ctx_unlock()) -#define OVS_EVENTS_CONFIG_ERROR(option) \ - do { \ - ERROR(OVS_EVENTS_PLUGIN ": read '%s' config option failed", option); \ - goto failure; \ - } while (0) /* Link status type */ enum ovs_events_link_status_e { DOWN, UP }; @@ -71,6 +66,7 @@ struct ovs_events_config_s { _Bool send_notification; /* sent notification to collectd? */ char ovs_db_node[OVS_DB_ADDR_NODE_SIZE]; /* OVS DB node */ char ovs_db_serv[OVS_DB_ADDR_SERVICE_SIZE]; /* OVS DB service */ + char ovs_db_unix[OVS_DB_ADDR_UNIX_SIZE]; /* OVS DB unix socket path */ ovs_events_iface_list_t *ifaces; /* interface info */ }; typedef struct ovs_events_config_s ovs_events_config_t; @@ -90,13 +86,9 @@ typedef struct ovs_events_ctx_s ovs_events_ctx_t; */ static ovs_events_ctx_t ovs_events_ctx = { .mutex = PTHREAD_MUTEX_INITIALIZER, - .config = {.send_notification = 0, /* do not send notification */ - .ovs_db_node = "localhost", /* use default OVS DB node */ - .ovs_db_serv = "6640", /* use default OVS DB service */ - .ifaces = NULL}, - .ovs_db_select_params = NULL, - .is_db_available = 0, - .ovs_db = NULL}; + .config = {.ovs_db_node = "localhost", /* use default OVS DB node */ + .ovs_db_serv = "6640"} /* use default OVS DB service */ +}; /* This function is used only by "OVS_EVENTS_CTX_LOCK" define (see above). * It always returns 1 when context is locked. @@ -120,69 +112,72 @@ static int ovs_events_ctx_unlock() { */ static int ovs_events_config_iface_exists(const char *ifname) { if (ovs_events_ctx.config.ifaces == NULL) - return -1; + return (-1); /* check if given interface exists */ for (ovs_events_iface_list_t *iface = ovs_events_ctx.config.ifaces; iface; iface = iface->next) - return (strcmp(ifname, iface->name) == 0); + if (strcmp(ifname, iface->name) == 0) + return (1); - return 0; + return (0); } /* Get OVS DB select parameter request based on rfc7047, * "Transact" & "Select" section */ static char *ovs_events_get_select_params() { - int ret = 0; size_t buff_size = 0; - size_t offset = 0; - char *buff = NULL; - char *new_buff = NULL; - const char params_fmt[] = "[\"Open_vSwitch\"%s]"; - const char option_fmt[] = ",{\"op\":\"select\",\"table\":\"Interface\"," - "\"where\":[[\"name\",\"==\",\"%s\"]]," - "\"columns\":[\"link_state\",\"external_ids\"," - "\"name\",\"_uuid\"]}"; - const char default_opt[] = ",{\"op\":\"select\",\"table\":\"Interface\"," - "\"where\":[],\"columns\":[\"link_state\"," - "\"external_ids\",\"name\",\"_uuid\"]}"; + size_t buff_off = 0; + char *opt_buff = NULL; + static const char params_fmt[] = "[\"Open_vSwitch\"%s]"; + static const char option_fmt[] = + ",{\"op\":\"select\",\"table\":\"Interface\"," + "\"where\":[[\"name\",\"==\",\"%s\"]]," + "\"columns\":[\"link_state\",\"external_ids\"," + "\"name\",\"_uuid\"]}"; + static const char default_opt[] = + ",{\"op\":\"select\",\"table\":\"Interface\"," + "\"where\":[],\"columns\":[\"link_state\"," + "\"external_ids\",\"name\",\"_uuid\"]}"; /* setup OVS DB interface condition */ for (ovs_events_iface_list_t *iface = ovs_events_ctx.config.ifaces; iface; - iface = iface->next, offset += ret) { + iface = iface->next) { /* allocate new buffer (format size + ifname len is good enough) */ - buff_size += (sizeof(option_fmt) + strlen(iface->name)); - new_buff = realloc(buff, buff_size); - if (new_buff == NULL) - goto failure; - buff = new_buff; - ret = ssnprintf(buff + offset, buff_size, option_fmt, iface->name); - if (ret < 0) - goto failure; + buff_size += sizeof(option_fmt) + strlen(iface->name); + char *new_buff = realloc(opt_buff, buff_size); + if (new_buff == NULL) { + sfree(opt_buff); + return NULL; + } + opt_buff = new_buff; + int ret = ssnprintf(opt_buff + buff_off, buff_size - buff_off, option_fmt, + iface->name); + if (ret < 0) { + sfree(opt_buff); + return NULL; + } + buff_off += ret; } /* if no interfaces are configured, use default params */ - if (buff == NULL) { - buff = strdup(default_opt); - offset = strlen(default_opt); - } + if (opt_buff == NULL) + if ((opt_buff = strdup(default_opt)) == NULL) + return NULL; /* allocate memory for OVS DB select params */ - buff_size = offset + sizeof(params_fmt); - new_buff = malloc(buff_size); - if (new_buff == NULL) - goto failure; + size_t params_size = sizeof(params_fmt) + strlen(opt_buff); + char *params_buff = calloc(1, params_size); + if (params_buff == NULL) { + sfree(opt_buff); + return NULL; + } /* create OVS DB select params */ - if (ssnprintf(new_buff, buff_size, params_fmt, buff) < 0) - goto failure; + if (ssnprintf(params_buff, params_size, params_fmt, opt_buff) < 0) + sfree(params_buff); - sfree(buff); - return new_buff; - -failure: - sfree(new_buff); - sfree(buff); - return NULL; + sfree(opt_buff); + return params_buff; } /* Release memory allocated for configuration data */ @@ -196,78 +191,86 @@ static void ovs_events_config_free() { } } +/* Parse/process "Interfaces" configuration option. Returns 0 if success + * otherwise -1 (error) + */ +static int ovs_events_config_get_interfaces(const oconfig_item_t *ci) { + for (int j = 0; j < ci->values_num; j++) { + /* check interface name type */ + if (ci->values[j].type != OCONFIG_TYPE_STRING) { + ERROR(OVS_EVENTS_PLUGIN + ": given interface name is not a string [idx=%d]", j); + return (-1); + } + /* allocate memory for configured interface */ + ovs_events_iface_list_t *new_iface = calloc(1, sizeof(*new_iface)); + if (new_iface == NULL) { + ERROR(OVS_EVENTS_PLUGIN ": calloc () copy interface name fail"); + return (-1); + } else { + /* store interface name */ + sstrncpy(new_iface->name, ci->values[j].value.string, + sizeof(new_iface->name)); + new_iface->next = ovs_events_ctx.config.ifaces; + ovs_events_ctx.config.ifaces = new_iface; + DEBUG(OVS_EVENTS_PLUGIN ": found monitored interface \"%s\"", + new_iface->name); + } + } + return (0); +} + /* Parse plugin configuration file and store the config * in allocated memory. Returns negative value in case of error. */ static int ovs_events_plugin_config(oconfig_item_t *ci) { - ovs_events_iface_list_t *new_iface; - for (int i = 0; i < ci->children_num; i++) { oconfig_item_t *child = ci->children + i; if (strcasecmp("SendNotification", child->key) == 0) { - if (cf_util_get_boolean(child, &ovs_events_ctx.config.send_notification) < - 0) - OVS_EVENTS_CONFIG_ERROR(child->key); - } else if (strcasecmp("OvsDbAddress", child->key) == 0) { - if (child->values_num < 1) { - ERROR(OVS_EVENTS_PLUGIN ": invalid OVS DB address specified"); - goto failure; + if (cf_util_get_boolean(child, + &ovs_events_ctx.config.send_notification) != 0) { + ovs_events_config_free(); + return (-1); + } + } else if (strcasecmp("Address", child->key) == 0) { + if (cf_util_get_string_buffer( + child, ovs_events_ctx.config.ovs_db_node, + sizeof(ovs_events_ctx.config.ovs_db_node)) != 0) { + ovs_events_config_free(); + return (-1); } - /* check node type and get the value */ - if (child->values[0].type != OCONFIG_TYPE_STRING) { - ERROR(OVS_EVENTS_PLUGIN ": OVS DB node is not a string"); - goto failure; + } else if (strcasecmp("Port", child->key) == 0) { + char *service = NULL; + if (cf_util_get_service(child, &service) != 0) { + ovs_events_config_free(); + return (-1); } - sstrncpy(ovs_events_ctx.config.ovs_db_node, child->values[0].value.string, - sizeof(ovs_events_ctx.config.ovs_db_node)); - /* get OVS DB address service name (optional) */ - if (child->values_num > 1) { - if (child->values[1].type != OCONFIG_TYPE_STRING) { - ERROR(OVS_EVENTS_PLUGIN ": OVS DB service is not a string"); - goto failure; - } - sstrncpy(ovs_events_ctx.config.ovs_db_serv, - child->values[1].value.string, - sizeof(ovs_events_ctx.config.ovs_db_serv)); + strncpy(ovs_events_ctx.config.ovs_db_serv, service, + sizeof(ovs_events_ctx.config.ovs_db_serv)); + sfree(service); + } else if (strcasecmp("Socket", child->key) == 0) { + if (cf_util_get_string_buffer( + child, ovs_events_ctx.config.ovs_db_unix, + sizeof(ovs_events_ctx.config.ovs_db_unix)) != 0) { + ovs_events_config_free(); + return (-1); } } else if (strcasecmp("Interfaces", child->key) == 0) { - for (int j = 0; j < child->values_num; j++) { - /* check value type */ - if (child->values[j].type != OCONFIG_TYPE_STRING) { - ERROR(OVS_EVENTS_PLUGIN - ": given interface name is not a string [idx=%d]", - j); - goto failure; - } - /* allocate memory for configured interface */ - if ((new_iface = malloc(sizeof(*new_iface))) == NULL) { - ERROR(OVS_EVENTS_PLUGIN ": malloc () copy interface name fail"); - goto failure; - } else { - /* store interface name */ - sstrncpy(new_iface->name, child->values[j].value.string, - sizeof(new_iface->name)); - new_iface->next = ovs_events_ctx.config.ifaces; - ovs_events_ctx.config.ifaces = new_iface; - DEBUG(OVS_EVENTS_PLUGIN ": found monitored interface \"%s\"", - new_iface->name); - } + if (ovs_events_config_get_interfaces(child) != 0) { + ovs_events_config_free(); + return (-1); } } else { ERROR(OVS_EVENTS_PLUGIN ": option '%s' is not allowed here", child->key); - goto failure; + ovs_events_config_free(); + return (-1); } } return (0); - -failure: - ovs_events_config_free(); - return (-1); } /* Dispatch OVS interface link status event to collectd */ -static void -ovs_events_dispatch_notification(const ovs_events_iface_info_t *ifinfo) { +static void ovs_events_dispatch_notification(const ovs_events_iface_info_t *ifinfo) { const char *msg_link_status = NULL; notification_t n = { NOTIF_FAILURE, cdtime(), "", "", OVS_EVENTS_PLUGIN, "", "", "", NULL}; @@ -293,19 +296,21 @@ ovs_events_dispatch_notification(const ovs_events_iface_info_t *ifinfo) { return; } - if (strlen(ifinfo->ext_vm_uuid) > 0) + if (strlen(ifinfo->ext_vm_uuid) > 0) { if (plugin_notification_meta_add_string(&n, "vm-uuid", ifinfo->ext_vm_uuid) < 0) { ERROR(OVS_EVENTS_PLUGIN ": add interface vm-uuid meta data failed"); return; } + } - if (strlen(ifinfo->ext_iface_id) > 0) + if (strlen(ifinfo->ext_iface_id) > 0) { if (plugin_notification_meta_add_string(&n, "iface-id", ifinfo->ext_iface_id) < 0) { ERROR(OVS_EVENTS_PLUGIN ": add interface iface-id meta data failed"); return; } + } /* fill the notification data */ ssnprintf(n.message, sizeof(n.message), @@ -319,8 +324,7 @@ ovs_events_dispatch_notification(const ovs_events_iface_info_t *ifinfo) { } /* Dispatch OVS interface link status value to collectd */ -static void -ovs_events_link_status_submit(const ovs_events_iface_info_t *ifinfo) { +static void ovs_events_link_status_submit(const ovs_events_iface_info_t *ifinfo) { value_list_t vl = VALUE_LIST_INIT; meta_data_t *meta = NULL; @@ -373,6 +377,9 @@ static int ovs_events_get_iface_info(yajl_val jobject, if (!YAJL_IS_OBJECT(jobject)) return (-1); + /* zero the interface info structure */ + memset(ifinfo, 0, sizeof(*ifinfo)); + /* try to find external_ids, name and link_state fields */ jexternal_ids = ovs_utils_get_value_by_key(jobject, "external_ids"); if (jexternal_ids == NULL || ifinfo == NULL) @@ -458,7 +465,7 @@ static void ovs_events_table_update_cb(yajl_val jupdates) { return; } /* go through all row updates */ - for (int row_index = 0; row_index < YAJL_GET_OBJECT(jupdate)->len; + for (size_t row_index = 0; row_index < YAJL_GET_OBJECT(jupdate)->len; ++row_index) { jrow_update = YAJL_GET_OBJECT(jupdate)->values[row_index]; @@ -474,15 +481,18 @@ static void ovs_events_table_update_cb(yajl_val jupdates) { " :unexpected interface information data received"); return; } - if (ovs_events_config_iface_exists(ifinfo.name) != 0) + if (ovs_events_config_iface_exists(ifinfo.name) != 0) { + DEBUG("name=%s, uuid=%s, ext_iface_id=%s, ext_vm_uuid=%s", ifinfo.name, + ifinfo.uuid, ifinfo.ext_iface_id, ifinfo.ext_vm_uuid); /* dispatch notification */ ovs_events_dispatch_notification(&ifinfo); + } } } -/* OVD DB reply callback. It parses reply, receives +/* OVS DB reply callback. It parses reply, receives * interface information and dispatches the info to - * collecd + * collectd */ static void ovs_events_poll_result_cb(yajl_val jresult, yajl_val jerror) { yajl_val *jvalues = NULL; @@ -502,15 +512,14 @@ static void ovs_events_poll_result_cb(yajl_val jresult, yajl_val jerror) { /* go through all rows and get interface info */ jvalues = YAJL_GET_ARRAY(jresult)->values; - for (int i = 0; i < YAJL_GET_ARRAY(jresult)->len; i++) { + for (size_t i = 0; i < YAJL_GET_ARRAY(jresult)->len; i++) { jvalue = ovs_utils_get_value_by_key(jvalues[i], "rows"); if (jvalue == NULL || !YAJL_IS_ARRAY(jvalue)) { ERROR(OVS_EVENTS_PLUGIN "invalid data (array of rows is expected)"); return; } /* get interfaces info */ - for (int j = 0; j < YAJL_GET_ARRAY(jvalue)->len; j++) { - memset(&ifinfo, 0, sizeof(ifinfo)); + for (size_t j = 0; j < YAJL_GET_ARRAY(jvalue)->len; j++) { if (ovs_events_get_iface_info(YAJL_GET_ARRAY(jvalue)->values[j], &ifinfo) < 0) { ERROR(OVS_EVENTS_PLUGIN @@ -528,22 +537,21 @@ static void ovs_events_poll_result_cb(yajl_val jresult, yajl_val jerror) { * to receive link status event(s). */ static void ovs_events_conn_initialize(ovs_db_t *pdb) { - int ret = 0; const char tb_name[] = "Interface"; const char *columns[] = {"_uuid", "external_ids", "name", "link_state", NULL}; /* register update link status event if needed */ if (ovs_events_ctx.config.send_notification) { - ret = ovs_db_table_cb_register(pdb, tb_name, columns, - ovs_events_table_update_cb, NULL, - OVS_DB_TABLE_CB_FLAG_MODIFY); + int ret = ovs_db_table_cb_register(pdb, tb_name, columns, + ovs_events_table_update_cb, NULL, + OVS_DB_TABLE_CB_FLAG_MODIFY); if (ret < 0) { ERROR(OVS_EVENTS_PLUGIN ": register OVS DB update callback failed"); return; } } OVS_EVENTS_CTX_LOCK { ovs_events_ctx.is_db_available = 1; } - DEBUG(OVS_EVENTS_PLUGIN ": OVS DB has been initialized"); + DEBUG(OVS_EVENTS_PLUGIN ": OVS DB connection has been initialized"); } /* OVS DB terminate connection notification callback */ @@ -575,8 +583,9 @@ static int ovs_events_plugin_init(void) { ovs_db_callback_t cb = {.post_conn_init = ovs_events_conn_initialize, .post_conn_terminate = ovs_events_conn_terminate}; - DEBUG(OVS_EVENTS_PLUGIN ": OVS DB node = %s, service=%s", - ovs_events_ctx.config.ovs_db_node, ovs_events_ctx.config.ovs_db_serv); + DEBUG(OVS_EVENTS_PLUGIN ": OVS DB address=%s, service=%s, unix=%s", + ovs_events_ctx.config.ovs_db_node, ovs_events_ctx.config.ovs_db_serv, + ovs_events_ctx.config.ovs_db_unix); /* generate OVS DB select condition based on list on configured interfaces */ ovs_events_ctx.ovs_db_select_params = ovs_events_get_select_params(); @@ -587,7 +596,8 @@ static int ovs_events_plugin_init(void) { /* initialize OVS DB */ ovs_db = ovs_db_init(ovs_events_ctx.config.ovs_db_node, - ovs_events_ctx.config.ovs_db_serv, &cb); + ovs_events_ctx.config.ovs_db_serv, + ovs_events_ctx.config.ovs_db_unix, &cb); if (ovs_db == NULL) { ERROR(OVS_EVENTS_PLUGIN ": fail to connect to OVS DB server"); goto ovs_events_failure;