X-Git-Url: https://git.octo.it/?a=blobdiff_plain;f=src%2Fovs_events.c;h=65042343215adb4d31d723a7d36cd4a5a56cd86c;hb=307c875e5a78a2729fbbe1a588d232e9a129d75a;hp=20af0c26893678c0f0f77425e09b36eb048f8218;hpb=3e891e50eedcb53ed42b140b942947856ca9c8a0;p=collectd.git diff --git a/src/ovs_events.c b/src/ovs_events.c index 20af0c26..65042343 100644 --- a/src/ovs_events.c +++ b/src/ovs_events.c @@ -86,16 +86,20 @@ typedef struct ovs_events_ctx_s ovs_events_ctx_t; */ static ovs_events_ctx_t ovs_events_ctx = { .mutex = PTHREAD_MUTEX_INITIALIZER, - .config = {.ovs_db_node = "localhost", /* use default OVS DB node */ + .config = {.send_notification = 1, /* send notification by default */ + .ovs_db_node = "localhost", /* use default OVS DB node */ .ovs_db_serv = "6640"} /* use default OVS DB service */ }; +/* Forward declaration */ +static int ovs_events_plugin_read(user_data_t *u); + /* This function is used only by "OVS_EVENTS_CTX_LOCK" define (see above). * It always returns 1 when context is locked. */ static int ovs_events_ctx_lock() { pthread_mutex_lock(&ovs_events_ctx.mutex); - return (1); + return 1; } /* This function is used only by "OVS_EVENTS_CTX_LOCK" define (see above). @@ -103,7 +107,7 @@ static int ovs_events_ctx_lock() { */ static int ovs_events_ctx_unlock() { pthread_mutex_unlock(&ovs_events_ctx.mutex); - return (0); + return 0; } /* Check if given interface name exists in configuration file. It @@ -112,15 +116,15 @@ 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) if (strcmp(ifname, iface->name) == 0) - return (1); + return 1; - return (0); + return 0; } /* Get OVS DB select parameter request based on rfc7047, @@ -200,13 +204,13 @@ static int ovs_events_config_get_interfaces(const oconfig_item_t *ci) { if (ci->values[j].type != OCONFIG_TYPE_STRING) { ERROR(OVS_EVENTS_PLUGIN ": given interface name is not a string [idx=%d]", j); - return (-1); + 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); + return -1; } else { /* store interface name */ sstrncpy(new_iface->name, ci->values[j].value.string, @@ -217,33 +221,34 @@ static int ovs_events_config_get_interfaces(const oconfig_item_t *ci) { new_iface->name); } } - return (0); + 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) { + _Bool dispatch_values = 0; 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_free(); - return (-1); + 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); + return -1; } } else if (strcasecmp("Port", child->key) == 0) { char *service = NULL; if (cf_util_get_service(child, &service) != 0) { ovs_events_config_free(); - return (-1); + return -1; } strncpy(ovs_events_ctx.config.ovs_db_serv, service, sizeof(ovs_events_ctx.config.ovs_db_serv)); @@ -253,20 +258,36 @@ static int ovs_events_plugin_config(oconfig_item_t *ci) { child, ovs_events_ctx.config.ovs_db_unix, sizeof(ovs_events_ctx.config.ovs_db_unix)) != 0) { ovs_events_config_free(); - return (-1); + return -1; } } else if (strcasecmp("Interfaces", child->key) == 0) { if (ovs_events_config_get_interfaces(child) != 0) { ovs_events_config_free(); - return (-1); + return -1; + } + } else if (strcasecmp("DispatchValues", child->key) == 0) { + if (cf_util_get_boolean(child, &dispatch_values) != 0) { + ovs_events_config_free(); + return -1; } } else { ERROR(OVS_EVENTS_PLUGIN ": option '%s' is not allowed here", child->key); ovs_events_config_free(); - return (-1); + return -1; } } - return (0); + /* Check and warn about invalid configuration */ + if (!ovs_events_ctx.config.send_notification && !dispatch_values) { + WARNING(OVS_EVENTS_PLUGIN ": send notification and dispatch values " + "options are disabled. No information will be dispatched by the " + "plugin. Please check your configuration"); + } + /* Dispatch link status values if configured */ + if (dispatch_values) + return plugin_register_complex_read(NULL, OVS_EVENTS_PLUGIN, + ovs_events_plugin_read, 0, NULL); + + return 0; } /* Dispatch OVS interface link status event to collectd */ @@ -375,7 +396,7 @@ static int ovs_events_get_iface_info(yajl_val jobject, /* check YAJL type */ if (!YAJL_IS_OBJECT(jobject)) - return (-1); + return -1; /* zero the interface info structure */ memset(ifinfo, 0, sizeof(*ifinfo)); @@ -383,7 +404,7 @@ static int ovs_events_get_iface_info(yajl_val jobject, /* 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) - return (-1); + return -1; /* get iface-id from external_ids field */ jvalue = ovs_utils_get_map_value(jexternal_ids, "iface-id"); @@ -401,16 +422,16 @@ static int ovs_events_get_iface_info(yajl_val jobject, jvalue = ovs_utils_get_value_by_key(jobject, "_uuid"); if (jvalue == NULL || !YAJL_IS_ARRAY(jvalue) || YAJL_GET_ARRAY(jvalue)->len != 2) - return (-1); + return -1; juuid = YAJL_GET_ARRAY(jvalue)->values[1]; if (juuid == NULL || !YAJL_IS_STRING(juuid)) - return (-1); + return -1; sstrncpy(ifinfo->uuid, YAJL_GET_STRING(juuid), sizeof(ifinfo->uuid)); /* get interface name */ jvalue = ovs_utils_get_value_by_key(jobject, "name"); if (jvalue == NULL || !YAJL_IS_STRING(jvalue)) - return (-1); + return -1; sstrncpy(ifinfo->name, YAJL_GET_STRING(jvalue), sizeof(ifinfo->name)); /* get OVS DB interface link status */ @@ -422,7 +443,7 @@ static int ovs_events_get_iface_info(yajl_val jobject, else if (strcmp(state, "down") == 0) ifinfo->link_status = DOWN; } - return (0); + return 0; } /* Process OVS DB update table event. It handles link status update event(s) @@ -465,7 +486,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]; @@ -512,14 +533,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++) { + 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 @@ -572,9 +593,9 @@ static int ovs_events_plugin_read(__attribute__((unused)) user_data_t *u) { ovs_events_ctx.ovs_db_select_params, ovs_events_poll_result_cb) < 0) { ERROR(OVS_EVENTS_PLUGIN ": get interface info failed"); - return (-1); + return -1; } - return (0); + return 0; } /* Initialize OVS plugin */ @@ -607,13 +628,13 @@ static int ovs_events_plugin_init(void) { OVS_EVENTS_CTX_LOCK { ovs_events_ctx.ovs_db = ovs_db; } DEBUG(OVS_EVENTS_PLUGIN ": plugin has been initialized"); - return (0); + return 0; ovs_events_failure: ERROR(OVS_EVENTS_PLUGIN ": plugin initialize failed"); /* release allocated memory */ ovs_events_config_free(); - return (-1); + return -1; } /* Shutdown OVS plugin */ @@ -626,14 +647,12 @@ static int ovs_events_plugin_shutdown(void) { ovs_events_config_free(); DEBUG(OVS_EVENTS_PLUGIN ": plugin has been destroyed"); - return (0); + return 0; } /* Register OVS plugin callbacks */ void module_register(void) { plugin_register_complex_config(OVS_EVENTS_PLUGIN, ovs_events_plugin_config); plugin_register_init(OVS_EVENTS_PLUGIN, ovs_events_plugin_init); - plugin_register_complex_read(NULL, OVS_EVENTS_PLUGIN, ovs_events_plugin_read, - 0, NULL); plugin_register_shutdown(OVS_EVENTS_PLUGIN, ovs_events_plugin_shutdown); }