2 * collectd - src/ovs_events.c
4 * Copyright(c) 2016 Intel Corporation. All rights reserved.
6 * Permission is hereby granted, free of charge, to any person obtaining a copy of
7 * this software and associated documentation files (the "Software"), to deal in
8 * the Software without restriction, including without limitation the rights to
9 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
10 * of the Software, and to permit persons to whom the Software is furnished to do
11 * so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in all
14 * copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25 * Volodymyr Mytnyk <volodymyrx.mytnyk@intel.com>
28 #include "common.h" /* auxiliary functions */
30 #include "utils_ovs.h" /* OVS helpers */
32 #define OVS_EVENTS_IFACE_NAME_SIZE 128
33 #define OVS_EVENTS_IFACE_UUID_SIZE 64
34 #define OVS_EVENTS_EXT_IFACE_ID_SIZE 64
35 #define OVS_EVENTS_EXT_VM_UUID_SIZE 64
36 #define OVS_EVENTS_OVS_DB_URL_SIZE 64
37 #define OVS_EVENTS_PLUGIN "ovs_events"
38 #define OVS_EVENTS_CTX_LOCK \
39 for (int __i = ovs_events_ctx_lock(); __i != 0; __i = ovs_events_ctx_unlock())
40 #define OVS_EVENTS_CONFIG_ERROR(option) \
42 ERROR(OVS_EVENTS_PLUGIN ": read '%s' config option failed", option); \
46 /* Link status type */
47 enum ovs_events_link_status_e { DOWN, UP };
48 typedef enum ovs_events_link_status_e ovs_events_link_status_t;
51 struct ovs_events_iface_info_s {
52 char name[OVS_EVENTS_IFACE_NAME_SIZE]; /* interface name */
53 char uuid[OVS_EVENTS_IFACE_UUID_SIZE]; /* interface UUID */
54 char ext_iface_id[OVS_EVENTS_EXT_IFACE_ID_SIZE]; /* external interface id */
55 char ext_vm_uuid[OVS_EVENTS_EXT_VM_UUID_SIZE]; /* external VM UUID */
56 ovs_events_link_status_t link_status; /* interface link status */
57 struct ovs_events_iface_info_s *next; /* next interface info */
59 typedef struct ovs_events_iface_info_s ovs_events_iface_info_t;
62 struct ovs_events_iface_list_s {
63 char name[OVS_EVENTS_IFACE_NAME_SIZE]; /* interface name */
64 struct ovs_events_iface_list_s *next; /* next interface info */
66 typedef struct ovs_events_iface_list_s ovs_events_iface_list_t;
68 /* OVS events configuration data */
69 struct ovs_events_config_s {
70 _Bool send_notification; /* sent notification to collectd? */
71 char ovs_db_server_url[OVS_EVENTS_OVS_DB_URL_SIZE]; /* OVS DB server URL */
72 ovs_events_iface_list_t *ifaces; /* interface info */
74 typedef struct ovs_events_config_s ovs_events_config_t;
76 /* OVS events context type */
77 struct ovs_events_ctx_s {
78 pthread_mutex_t mutex; /* mutex to lock the context */
79 ovs_db_t *ovs_db; /* pointer to OVS DB instance */
80 ovs_events_config_t config; /* plugin config */
81 char *ovs_db_select_params; /* OVS DB select parameter request */
82 _Bool is_db_available; /* specify whether OVS DB is available */
84 typedef struct ovs_events_ctx_s ovs_events_ctx_t;
89 static ovs_events_ctx_t ovs_events_ctx = {
90 .mutex = PTHREAD_MUTEX_INITIALIZER,
91 .config = {.send_notification = 0, /* do not send notification */
93 "tcp:127.0.0.1:6640", /* use default OVS DB URL */
95 .ovs_db_select_params = NULL,
99 /* This function is used only by "OVS_EVENTS_CTX_LOCK" define (see above).
100 * It always returns 1 when context is locked.
102 static inline int ovs_events_ctx_lock() {
103 pthread_mutex_lock(&ovs_events_ctx.mutex);
107 /* This function is used only by "OVS_EVENTS_CTX_LOCK" define (see above).
108 * It always returns 0 when context is unlocked.
110 static inline int ovs_events_ctx_unlock() {
111 pthread_mutex_unlock(&ovs_events_ctx.mutex);
115 /* Check if given interface name exists in configuration file. It
116 * returns 1 if exists otherwise 0. If no interfaces are configured,
119 static int ovs_events_config_iface_exists(const char *ifname) {
120 if (ovs_events_ctx.config.ifaces == NULL)
123 /* check if given interface exists */
124 for (ovs_events_iface_list_t *iface = ovs_events_ctx.config.ifaces; iface;
126 return (strcmp(ifname, iface->name) == 0);
131 /* Get OVS DB select parameter request based on rfc7047,
132 * "Transact" & "Select" section
134 static inline char *ovs_events_get_select_params() {
136 size_t buff_size = 0;
139 char *new_buff = NULL;
140 const char params_fmt[] = "[\"Open_vSwitch\"%s]";
141 const char option_fmt[] = ",{\"op\":\"select\",\"table\":\"Interface\","
142 "\"where\":[[\"name\",\"==\",\"%s\"]],"
143 "\"columns\":[\"link_state\",\"external_ids\","
144 "\"name\",\"_uuid\"]}";
145 const char default_opt[] = ",{\"op\":\"select\",\"table\":\"Interface\","
146 "\"where\":[],\"columns\":[\"link_state\","
147 "\"external_ids\",\"name\",\"_uuid\"]}";
148 /* setup OVS DB interface condition */
149 for (ovs_events_iface_list_t *iface = ovs_events_ctx.config.ifaces; iface;
150 iface = iface->next, offset += ret) {
151 /* allocate new buffer (format size + ifname len is good enough) */
152 buff_size += (sizeof(option_fmt) + strlen(iface->name));
153 new_buff = realloc(buff, buff_size);
154 if (new_buff == NULL)
157 ret = ssnprintf(buff + offset, buff_size, option_fmt, iface->name);
161 /* if no interfaces are configured, use default params */
163 buff = strdup(default_opt);
164 offset = strlen(default_opt);
167 /* allocate memory for OVS DB select params */
168 buff_size = offset + sizeof(params_fmt);
169 new_buff = malloc(buff_size);
170 if (new_buff == NULL)
173 /* create OVS DB select params */
174 if (ssnprintf(new_buff, buff_size, params_fmt, buff) < 0)
186 /* Release memory allocated for configuration data */
187 static void ovs_events_config_free() {
188 ovs_events_iface_list_t *del_iface = NULL;
189 sfree(ovs_events_ctx.ovs_db_select_params);
190 while (ovs_events_ctx.config.ifaces) {
191 del_iface = ovs_events_ctx.config.ifaces;
192 ovs_events_ctx.config.ifaces = ovs_events_ctx.config.ifaces->next;
197 /* Parse plugin configuration file and store the config
198 * in allocated memory. Returns negative value in case of error.
200 static int ovs_events_plugin_config(oconfig_item_t *ci) {
201 ovs_events_iface_list_t *new_iface;
203 for (int i = 0; i < ci->children_num; i++) {
204 oconfig_item_t *child = ci->children + i;
205 if (strcasecmp("SendNotification", child->key) == 0) {
206 if (cf_util_get_boolean(child, &ovs_events_ctx.config.send_notification) <
208 OVS_EVENTS_CONFIG_ERROR(child->key);
209 } else if (strcasecmp("OvsDbServerUrl", child->key) == 0) {
210 if (cf_util_get_string_buffer(
211 child, ovs_events_ctx.config.ovs_db_server_url,
212 sizeof(ovs_events_ctx.config.ovs_db_server_url)) < 0)
213 OVS_EVENTS_CONFIG_ERROR(child->key);
214 } else if (strcasecmp("Interfaces", child->key) == 0) {
215 for (int j = 0; j < child->values_num; j++) {
216 /* check value type */
217 if (child->values[j].type != OCONFIG_TYPE_STRING) {
218 ERROR(OVS_EVENTS_PLUGIN
219 ": given interface name is not a string [idx=%d]",
223 /* allocate memory for configured interface */
224 if ((new_iface = malloc(sizeof(*new_iface))) == NULL) {
225 ERROR(OVS_EVENTS_PLUGIN ": malloc () copy interface name fail");
228 /* store interface name */
229 sstrncpy(new_iface->name, child->values[j].value.string,
230 sizeof(new_iface->name));
231 new_iface->next = ovs_events_ctx.config.ifaces;
232 ovs_events_ctx.config.ifaces = new_iface;
233 DEBUG(OVS_EVENTS_PLUGIN ": found monitored interface \"%s\"",
238 ERROR(OVS_EVENTS_PLUGIN ": option '%s' is not allowed here", child->key);
245 ovs_events_config_free();
249 /* Dispatch OVS interface link status event to collectd */
251 ovs_events_dispatch_notification(const ovs_events_iface_info_t *ifinfo) {
252 const char *msg_link_status = NULL;
254 NOTIF_FAILURE, cdtime(), "", "", OVS_EVENTS_PLUGIN, "", "", "", NULL};
256 /* convert link status to message string */
257 switch (ifinfo->link_status) {
259 msg_link_status = "UP";
260 n.severity = NOTIF_OKAY;
263 msg_link_status = "DOWN";
264 n.severity = NOTIF_WARNING;
267 ERROR(OVS_EVENTS_PLUGIN ": unknown interface link status");
271 /* add interface metadata to the notification */
272 if (plugin_notification_meta_add_string(&n, "uuid", ifinfo->uuid) < 0) {
273 ERROR(OVS_EVENTS_PLUGIN ": add interface uuid meta data failed");
277 if (strlen(ifinfo->ext_vm_uuid) > 0)
278 if (plugin_notification_meta_add_string(&n, "vm-uuid",
279 ifinfo->ext_vm_uuid) < 0) {
280 ERROR(OVS_EVENTS_PLUGIN ": add interface vm-uuid meta data failed");
284 if (strlen(ifinfo->ext_iface_id) > 0)
285 if (plugin_notification_meta_add_string(&n, "iface-id",
286 ifinfo->ext_iface_id) < 0) {
287 ERROR(OVS_EVENTS_PLUGIN ": add interface iface-id meta data failed");
291 /* fill the notification data */
292 ssnprintf(n.message, sizeof(n.message),
293 "link state of \"%s\" interface has been changed to \"%s\"",
294 ifinfo->name, msg_link_status);
295 sstrncpy(n.host, hostname_g, sizeof(n.host));
296 sstrncpy(n.plugin_instance, ifinfo->name, sizeof(n.plugin_instance));
297 sstrncpy(n.type, "gauge", sizeof(n.type));
298 sstrncpy(n.type_instance, "link_status", sizeof(n.type_instance));
299 plugin_dispatch_notification(&n);
302 /* Dispatch OVS interface link status value to collectd */
304 ovs_events_link_status_submit(const ovs_events_iface_info_t *ifinfo) {
305 value_list_t vl = VALUE_LIST_INIT;
306 meta_data_t *meta = NULL;
308 /* add interface metadata to the submit value */
309 if ((meta = meta_data_create()) != NULL) {
310 if (meta_data_add_string(meta, "uuid", ifinfo->uuid) < 0)
311 ERROR(OVS_EVENTS_PLUGIN ": add interface uuid meta data failed");
313 if (strlen(ifinfo->ext_vm_uuid) > 0)
314 if (meta_data_add_string(meta, "vm-uuid", ifinfo->ext_vm_uuid) < 0)
315 ERROR(OVS_EVENTS_PLUGIN ": add interface vm-uuid meta data failed");
317 if (strlen(ifinfo->ext_iface_id) > 0)
318 if (meta_data_add_string(meta, "iface-id", ifinfo->ext_iface_id) < 0)
319 ERROR(OVS_EVENTS_PLUGIN ": add interface iface-id meta data failed");
322 ERROR(OVS_EVENTS_PLUGIN ": create metadata failed");
325 vl.values = &(value_t){.gauge = (gauge_t)ifinfo->link_status};
327 sstrncpy(vl.plugin, OVS_EVENTS_PLUGIN, sizeof(vl.plugin));
328 sstrncpy(vl.plugin_instance, ifinfo->name, sizeof(vl.plugin_instance));
329 sstrncpy(vl.type, "gauge", sizeof(vl.type));
330 sstrncpy(vl.type_instance, "link_status", sizeof(vl.type_instance));
331 plugin_dispatch_values(&vl);
332 meta_data_destroy(meta);
335 /* Dispatch OVS DB terminate connection event to collectd */
336 static void ovs_events_dispatch_terminate_notification(const char *msg) {
338 NOTIF_FAILURE, cdtime(), "", "", OVS_EVENTS_PLUGIN, "", "", "", NULL};
339 sstrncpy(n.message, msg, sizeof(n.message));
340 sstrncpy(n.host, hostname_g, sizeof(n.host));
341 plugin_dispatch_notification(&n);
344 /* Get OVS DB interface information and stores it into
345 * ovs_events_iface_info_t structure */
346 static int ovs_events_get_iface_info(yajl_val jobject,
347 ovs_events_iface_info_t *ifinfo) {
348 yajl_val jexternal_ids = NULL;
349 yajl_val jvalue = NULL;
350 yajl_val juuid = NULL;
351 const char *state = NULL;
353 /* check YAJL type */
354 if (!YAJL_IS_OBJECT(jobject))
357 /* try to find external_ids, name and link_state fields */
358 jexternal_ids = ovs_utils_get_value_by_key(jobject, "external_ids");
359 if (jexternal_ids == NULL || ifinfo == NULL)
362 /* get iface-id from external_ids field */
363 jvalue = ovs_utils_get_map_value(jexternal_ids, "iface-id");
364 if (jvalue != NULL && YAJL_IS_STRING(jvalue))
365 sstrncpy(ifinfo->ext_iface_id, YAJL_GET_STRING(jvalue),
366 sizeof(ifinfo->ext_iface_id));
368 /* get vm-uuid from external_ids field */
369 jvalue = ovs_utils_get_map_value(jexternal_ids, "vm-uuid");
370 if (jvalue != NULL && YAJL_IS_STRING(jvalue))
371 sstrncpy(ifinfo->ext_vm_uuid, YAJL_GET_STRING(jvalue),
372 sizeof(ifinfo->ext_vm_uuid));
374 /* get interface uuid */
375 jvalue = ovs_utils_get_value_by_key(jobject, "_uuid");
376 if (jvalue == NULL || !YAJL_IS_ARRAY(jvalue) ||
377 YAJL_GET_ARRAY(jvalue)->len != 2)
379 juuid = YAJL_GET_ARRAY(jvalue)->values[1];
380 if (juuid == NULL || !YAJL_IS_STRING(juuid))
382 sstrncpy(ifinfo->uuid, YAJL_GET_STRING(juuid), sizeof(ifinfo->uuid));
384 /* get interface name */
385 jvalue = ovs_utils_get_value_by_key(jobject, "name");
386 if (jvalue == NULL || !YAJL_IS_STRING(jvalue))
388 sstrncpy(ifinfo->name, YAJL_GET_STRING(jvalue), sizeof(ifinfo->name));
390 /* get OVS DB interface link status */
391 jvalue = ovs_utils_get_value_by_key(jobject, "link_state");
392 if (jvalue != NULL && ((state = YAJL_GET_STRING(jvalue)) != NULL)) {
393 /* convert OVS table link state to link status */
394 if (strcmp(state, "up") == 0)
395 ifinfo->link_status = UP;
396 else if (strcmp(state, "down") == 0)
397 ifinfo->link_status = DOWN;
402 /* Process OVS DB update table event. It handles link status update event(s)
403 * and dispatches the value(s) to collectd if interface name matches one of
404 * interfaces specified in configuration file.
406 static void ovs_events_table_update_cb(yajl_val jupdates) {
407 yajl_val jnew_val = NULL;
408 yajl_val jupdate = NULL;
409 yajl_val jrow_update = NULL;
410 ovs_events_iface_info_t ifinfo;
412 /* JSON "Interface" table update example:
413 * ---------------------------------
416 * "9adf1db2-29ca-4140-ab22-ae347a4484de":
425 * "link_state":"down"
431 if (!YAJL_IS_OBJECT(jupdates) || !(YAJL_GET_OBJECT(jupdates)->len > 0)) {
432 ERROR(OVS_EVENTS_PLUGIN ": unexpected OVS DB update event received");
435 /* verify if this is a table event */
436 jupdate = YAJL_GET_OBJECT(jupdates)->values[0];
437 if (!YAJL_IS_OBJECT(jupdate)) {
438 ERROR(OVS_EVENTS_PLUGIN ": unexpected table update event received");
441 /* go through all row updates */
442 for (int row_index = 0; row_index < YAJL_GET_OBJECT(jupdate)->len;
444 jrow_update = YAJL_GET_OBJECT(jupdate)->values[row_index];
446 /* check row update */
447 jnew_val = ovs_utils_get_value_by_key(jrow_update, "new");
448 if (jnew_val == NULL) {
449 ERROR(OVS_EVENTS_PLUGIN ": unexpected row update received");
452 /* get OVS DB interface information */
453 if (ovs_events_get_iface_info(jnew_val, &ifinfo) < 0) {
454 ERROR(OVS_EVENTS_PLUGIN
455 " :unexpected interface information data received");
458 if (ovs_events_config_iface_exists(ifinfo.name) != 0)
459 /* dispatch notification */
460 ovs_events_dispatch_notification(&ifinfo);
464 /* OVD DB reply callback. It parses reply, receives
465 * interface information and dispatches the info to
468 static void ovs_events_poll_result_cb(yajl_val jresult, yajl_val jerror) {
469 yajl_val *jvalues = NULL;
470 yajl_val jvalue = NULL;
471 ovs_events_iface_info_t ifinfo;
473 if (!YAJL_IS_NULL(jerror)) {
474 ERROR(OVS_EVENTS_PLUGIN "error received by OVS DB server");
478 /* result should be an array */
479 if (!YAJL_IS_ARRAY(jresult)) {
480 ERROR(OVS_EVENTS_PLUGIN "invalid data (array is expected)");
484 /* go through all rows and get interface info */
485 jvalues = YAJL_GET_ARRAY(jresult)->values;
486 for (int i = 0; i < YAJL_GET_ARRAY(jresult)->len; i++) {
487 jvalue = ovs_utils_get_value_by_key(jvalues[i], "rows");
488 if (jvalue == NULL || !YAJL_IS_ARRAY(jvalue)) {
489 ERROR(OVS_EVENTS_PLUGIN "invalid data (array of rows is expected)");
492 /* get interfaces info */
493 for (int j = 0; j < YAJL_GET_ARRAY(jvalue)->len; j++) {
494 memset(&ifinfo, 0, sizeof(ifinfo));
495 if (ovs_events_get_iface_info(YAJL_GET_ARRAY(jvalue)->values[j],
497 ERROR(OVS_EVENTS_PLUGIN
498 "unexpected interface information data received");
501 DEBUG("name=%s, uuid=%s, ext_iface_id=%s, ext_vm_uuid=%s", ifinfo.name,
502 ifinfo.uuid, ifinfo.ext_iface_id, ifinfo.ext_vm_uuid);
503 ovs_events_link_status_submit(&ifinfo);
508 /* Setup OVS DB table callback. It subscribes to OVS DB 'Interface' table
509 * to receive link status event(s).
511 static void ovs_events_conn_initialize(ovs_db_t *pdb) {
513 const char tb_name[] = "Interface";
514 const char *columns[] = {"_uuid", "external_ids", "name", "link_state", NULL};
516 /* register update link status event if needed */
517 if (ovs_events_ctx.config.send_notification) {
518 ret = ovs_db_table_cb_register(pdb, tb_name, columns,
519 ovs_events_table_update_cb, NULL,
520 OVS_DB_TABLE_CB_FLAG_MODIFY);
522 ERROR(OVS_EVENTS_PLUGIN ": register OVS DB update callback failed");
526 OVS_EVENTS_CTX_LOCK { ovs_events_ctx.is_db_available = 1; }
527 DEBUG(OVS_EVENTS_PLUGIN ": OVS DB has been initialized");
530 /* OVS DB terminate connection notification callback */
531 static void ovs_events_conn_terminate() {
532 const char msg[] = "OVS DB connection has been lost";
533 if (ovs_events_ctx.config.send_notification)
534 ovs_events_dispatch_terminate_notification(msg);
535 WARNING(OVS_EVENTS_PLUGIN ": %s", msg);
536 OVS_EVENTS_CTX_LOCK { ovs_events_ctx.is_db_available = 0; }
539 /* Read OVS DB interface link status callback */
540 static int ovs_events_plugin_read(__attribute__((unused)) user_data_t *u) {
541 _Bool is_connected = 0;
542 OVS_EVENTS_CTX_LOCK { is_connected = ovs_events_ctx.is_db_available; }
544 if (ovs_db_send_request(ovs_events_ctx.ovs_db, "transact",
545 ovs_events_ctx.ovs_db_select_params,
546 ovs_events_poll_result_cb) < 0) {
547 ERROR(OVS_EVENTS_PLUGIN ": get interface info failed");
553 /* Initialize OVS plugin */
554 static int ovs_events_plugin_init(void) {
555 ovs_db_t *ovs_db = NULL;
556 ovs_db_callback_t cb = {.post_conn_init = ovs_events_conn_initialize,
557 .post_conn_terminate = ovs_events_conn_terminate};
559 DEBUG(OVS_EVENTS_PLUGIN ": OVS DB url = %s",
560 ovs_events_ctx.config.ovs_db_server_url);
562 /* generate OVS DB select condition based on list on configured interfaces */
563 ovs_events_ctx.ovs_db_select_params = ovs_events_get_select_params();
564 if (ovs_events_ctx.ovs_db_select_params == NULL) {
565 ERROR(OVS_EVENTS_PLUGIN ": fail to get OVS DB select condition");
566 goto ovs_events_failure;
569 /* initialize OVS DB */
570 ovs_db = ovs_db_init(ovs_events_ctx.config.ovs_db_server_url, &cb);
571 if (ovs_db == NULL) {
572 ERROR(OVS_EVENTS_PLUGIN ": fail to connect to OVS DB server");
573 goto ovs_events_failure;
576 /* store OVS DB handler */
577 OVS_EVENTS_CTX_LOCK { ovs_events_ctx.ovs_db = ovs_db; }
579 DEBUG(OVS_EVENTS_PLUGIN ": plugin has been initialized");
583 ERROR(OVS_EVENTS_PLUGIN ": plugin initialize failed");
584 /* release allocated memory */
585 ovs_events_config_free();
589 /* Shutdown OVS plugin */
590 static int ovs_events_plugin_shutdown(void) {
592 if (ovs_db_destroy(ovs_events_ctx.ovs_db))
593 ERROR(OVS_EVENTS_PLUGIN ": OVSDB object destroy failed");
595 /* release memory allocated for config */
596 ovs_events_config_free();
598 DEBUG(OVS_EVENTS_PLUGIN ": plugin has been destroyed");
602 /* Register OVS plugin callbacks */
603 void module_register(void) {
604 plugin_register_complex_config(OVS_EVENTS_PLUGIN, ovs_events_plugin_config);
605 plugin_register_init(OVS_EVENTS_PLUGIN, ovs_events_plugin_init);
606 plugin_register_complex_read(NULL, OVS_EVENTS_PLUGIN, ovs_events_plugin_read,
608 plugin_register_shutdown(OVS_EVENTS_PLUGIN, ovs_events_plugin_shutdown);