From 5ed781ecf531b7916e51c174426e222dab717fb8 Mon Sep 17 00:00:00 2001 From: Aravinda VK Date: Thu, 5 May 2016 18:34:41 +0530 Subject: eventsapi: Gluster Eventing Feature implementation [Depends on http://review.gluster.org/14627] Design is available in `glusterfs-specs`, A change from the design is support of webhook instead of Websockets as discussed in the design http://review.gluster.org/13115 Since Websocket support depends on REST APIs, I will add Websocket support once REST APIs patch gets merged Usage: Run following command to start/stop Eventsapi server in all Peers, which will collect the notifications from any Gluster daemon and emits to configured client. gluster-eventsapi start|stop|restart|reload Status of running services can be checked using, gluster-eventsapi status Events listener is a HTTP(S) server which listens to events emited by the Gluster. Create a HTTP Server to listen on POST and register that URL using, gluster-eventsapi webhook-add [--bearer-token ] For example, if HTTP Server running in `http://192.168.122.188:9000` then add that URL using, gluster-eventsapi webhook-add http://192.168.122.188:9000 If it expects a Token then specify it using `--bearer-token` or `-t` We can also test Webhook if all peer nodes can send message or not using, gluster-eventsapi webhook-test [--bearer-token ] Configurations can be viewed/updated using, gluster-eventsapi config-get [--name] gluster-eventsapi config-set gluster-eventsapi config-reset If any one peer node was down during config-set/reset or webhook modifications, Run sync command from good node when a peer node comes back. Automatic update is not yet implemented. gluster-eventsapi sync Basic Events Client(HTTP Server) is included with the code, Start running the client with required port and start listening to the events. /usr/share/glusterfs/scripts/eventsdash.py --port 8080 Default port is 9000, if no port is specified, once it started running then configure gluster-eventsapi to send events to that client. Eventsapi Client can be outside of the Cluster, it can be run event on Windows. But only requirement is the client URL should be accessible by all peer nodes.(Or ngrok(https://ngrok.com) like tools can be used) Events implemented with this patch, - Volume Create - Volume Start - Volume Stop - Volume Delete - Peer Attach - Peer Detach It is easy to add/support more events, since it touches Gluster cmd code and to avoid merge conflicts I will add support for more events once this patch merges. BUG: 1334044 Change-Id: I316827ac9dd1443454df7deffe4f54835f7f6a08 Signed-off-by: Aravinda VK Reviewed-on: http://review.gluster.org/14248 Smoke: Gluster Build System NetBSD-regression: NetBSD Build System CentOS-regression: Gluster Build System Reviewed-by: Jeff Darcy --- libglusterfs/src/Makefile.am | 6 ++++ libglusterfs/src/events.c | 83 +++++++++++++++++++++++++++++++++++++++++++ libglusterfs/src/events.h.in | 23 ++++++++++++ libglusterfs/src/eventtypes.h | 22 ++++++++++++ libglusterfs/src/glusterfs.h | 4 +++ 5 files changed, 138 insertions(+) create mode 100644 libglusterfs/src/events.c create mode 100644 libglusterfs/src/events.h.in create mode 100644 libglusterfs/src/eventtypes.h (limited to 'libglusterfs') diff --git a/libglusterfs/src/Makefile.am b/libglusterfs/src/Makefile.am index 2ec0f34a670..1dbea92b2bb 100644 --- a/libglusterfs/src/Makefile.am +++ b/libglusterfs/src/Makefile.am @@ -71,6 +71,12 @@ libglusterfs_la_SOURCES += $(CONTRIBDIR)/uuid/clear.c \ $(CONTRIBDIR)/uuid/unpack.c endif +if BUILD_EVENTS +libglusterfs_la_SOURCES += events.c + +libglusterfs_la_HEADERS += events.h eventtypes.h +endif + libgfchangelog_HEADERS = changelog.h EXTRA_DIST = graph.l graph.y defaults-tmpl.c diff --git a/libglusterfs/src/events.c b/libglusterfs/src/events.c new file mode 100644 index 00000000000..9d781874a8a --- /dev/null +++ b/libglusterfs/src/events.c @@ -0,0 +1,83 @@ +/* + Copyright (c) 2016 Red Hat, Inc. + This file is part of GlusterFS. + + This file is licensed to you under your choice of the GNU Lesser + General Public License, version 3 or any later version (LGPLv3 or + later), or the GNU General Public License, version 2 (GPLv2), in all + cases as published by the Free Software Foundation. +*/ + +#include +#include +#include +#include +#include +#include +#include +#include +#include "syscall.h" +#include "mem-pool.h" +#include "events.h" + +int +gf_event (int event, char *fmt, ...) +{ + int sock = -1; + char eventstr[EVENTS_MSG_MAX] = ""; + struct sockaddr_un server; + va_list arguments; + char *msg = NULL; + int ret = 0; + size_t eventstr_size = 0; + + if (event < 0 || event >= EVENT_LAST) { + ret = EVENT_ERROR_INVALID_INPUTS; + goto out; + } + + sock = socket(AF_UNIX, SOCK_STREAM, 0); + if (sock < 0) { + ret = EVENT_ERROR_SOCKET; + goto out; + } + server.sun_family = AF_UNIX; + strcpy(server.sun_path, EVENT_PATH); + + if (connect(sock, + (struct sockaddr *) &server, + sizeof(struct sockaddr_un)) < 0) { + ret = EVENT_ERROR_CONNECT; + goto out; + } + + va_start (arguments, fmt); + ret = gf_vasprintf (&msg, fmt, arguments); + va_end (arguments); + if (ret < 0) { + ret = EVENT_ERROR_INVALID_INPUTS; + goto out; + } + + eventstr_size = snprintf(NULL, 0, "%u %d %s", (unsigned)time(NULL), + event, msg); + + if (eventstr_size + 1 > EVENTS_MSG_MAX) { + eventstr_size = EVENTS_MSG_MAX - 1; + } + + snprintf(eventstr, eventstr_size+1, "%u %d %s", + (unsigned)time(NULL), event, msg); + + if (sys_write(sock, eventstr, strlen(eventstr)) <= 0) { + ret = EVENT_ERROR_SEND; + goto out; + } + + ret = EVENT_SEND_OK; + + out: + sys_close(sock); + GF_FREE(msg); + return ret; +} diff --git a/libglusterfs/src/events.h.in b/libglusterfs/src/events.h.in new file mode 100644 index 00000000000..37692bef732 --- /dev/null +++ b/libglusterfs/src/events.h.in @@ -0,0 +1,23 @@ +/* + Copyright (c) 2016 Red Hat, Inc. + This file is part of GlusterFS. + + This file is licensed to you under your choice of the GNU Lesser + General Public License, version 3 or any later version (LGPLv3 or + later), or the GNU General Public License, version 2 (GPLv2), in all + cases as published by the Free Software Foundation. +*/ + +#ifndef __EVENTS_H__ +#define __EVENTS_H__ + +#include + +#include "eventtypes.h" + +#define EVENT_PATH "@localstatedir@/run/gluster/events.sock" +#define EVENTS_MSG_MAX 2048 + +extern int gf_event(int key, char *fmt, ...); + +#endif /* __EVENTS_H__ */ diff --git a/libglusterfs/src/eventtypes.h b/libglusterfs/src/eventtypes.h new file mode 100644 index 00000000000..874f8ccf114 --- /dev/null +++ b/libglusterfs/src/eventtypes.h @@ -0,0 +1,22 @@ +#ifndef __EVENTTYPES_H__ +#define __EVENTTYPES_H__ + +typedef enum { + EVENT_SEND_OK, + EVENT_ERROR_INVALID_INPUTS, + EVENT_ERROR_SOCKET, + EVENT_ERROR_CONNECT, + EVENT_ERROR_SEND, +} event_errors_t; + +typedef enum { + EVENT_PEER_ATTACH, + EVENT_PEER_DETACH, + EVENT_VOLUME_CREATE, + EVENT_VOLUME_START, + EVENT_VOLUME_STOP, + EVENT_VOLUME_DELETE, + EVENT_LAST +} eventtypes_t; + +#endif /* __EVENTTYPES_H__ */ diff --git a/libglusterfs/src/glusterfs.h b/libglusterfs/src/glusterfs.h index 8d387bafb3b..d11cbdcb8ee 100644 --- a/libglusterfs/src/glusterfs.h +++ b/libglusterfs/src/glusterfs.h @@ -37,6 +37,10 @@ #include "lkowner.h" #include "compat-uuid.h" +#if (USE_EVENTS) +#include "events.h" +#endif + #define GF_YES 1 #define GF_NO 0 -- cgit