3604df
From bc6fac6847c3fe2db021e6a0bbe8d55dc24487af Mon Sep 17 00:00:00 2001
3604df
From: Aravinda VK <avishwan@redhat.com>
3604df
Date: Mon, 1 Aug 2016 17:16:50 +0530
3604df
Subject: [PATCH 34/86] eventsapi: Python library for gf_event
3604df
3604df
Python library to emit Events from Geo-replication or any other Python apps
3604df
running in storage nodes.
3604df
3604df
Add $LIBEXEC/glusterfs/events to Python path to use gf_event
3604df
3604df
gf_event(event_type, **kwargs)
3604df
3604df
For example,
3604df
3604df
    sys.path.insert(1, "LIBEXECDIR/glusterfs")
3604df
    from events.gf_event import gf_event
3604df
    from events import eventtypes
3604df
3604df
    gf_event(eventtypes.GEOREP_FAULTY,
3604df
             volname="gv1",
3604df
             slaveuser="root",
3604df
             slavehost="node1",
3604df
             slavevol="gv2")
3604df
3604df
Errors will be logged in $LOGDIR/glusterfs/events.log
3604df
3604df
> Reviewed-on: http://review.gluster.org/15063
3604df
> Smoke: Gluster Build System <jenkins@build.gluster.org>
3604df
> NetBSD-regression: NetBSD Build System <jenkins@build.gluster.org>
3604df
> CentOS-regression: Gluster Build System <jenkins@build.gluster.org>
3604df
> Reviewed-by: Samikshan Bairagya <samikshan@gmail.com>
3604df
> Reviewed-by: Prashanth Pai <ppai@redhat.com>
3604df
3604df
BUG: 1351589
3604df
Change-Id: I2af2bd77f9961975e4387006b9e99e4543e12b57
3604df
Signed-off-by: Aravinda VK <avishwan@redhat.com>
3604df
Reviewed-on: https://code.engineering.redhat.com/gerrit/84740
3604df
Reviewed-by: Milind Changire <mchangir@redhat.com>
3604df
Reviewed-by: Atin Mukherjee <amukherj@redhat.com>
3604df
---
3604df
 events/eventskeygen.py |    6 +++++-
3604df
 events/src/Makefile.am |    5 +++--
3604df
 events/src/gf_event.py |   48 ++++++++++++++++++++++++++++++++++++++++++++++++
3604df
 3 files changed, 56 insertions(+), 3 deletions(-)
3604df
 create mode 100644 events/src/gf_event.py
3604df
3604df
diff --git a/events/eventskeygen.py b/events/eventskeygen.py
3604df
index bccb91a..7357d8e 100644
3604df
--- a/events/eventskeygen.py
3604df
+++ b/events/eventskeygen.py
3604df
@@ -78,4 +78,8 @@ if gen_header_type == "PY_HEADER":
3604df
         f.write("all_events = [\n")
3604df
         for ev in keys:
3604df
             f.write('    "{0}",\n'.format(ev))
3604df
-        f.write("]\n")
3604df
+
3604df
+        f.write("]\n\n")
3604df
+
3604df
+        for idx, ev in enumerate(keys):
3604df
+            f.write("{0} = {1}\n".format(ev.replace("EVENT_", ""), idx))
3604df
diff --git a/events/src/Makefile.am b/events/src/Makefile.am
3604df
index 8b2150e..423bc88 100644
3604df
--- a/events/src/Makefile.am
3604df
+++ b/events/src/Makefile.am
3604df
@@ -1,6 +1,6 @@
3604df
 noinst_PYTHON = $(top_srcdir)/events/eventskeygen.py
3604df
 EXTRA_DIST = glustereventsd.py __init__.py  eventsapiconf.py.in \
3604df
-	handlers.py utils.py peer_eventsapi.py eventsconfig.json
3604df
+	handlers.py utils.py peer_eventsapi.py eventsconfig.json gf_event.py
3604df
 
3604df
 BUILT_SOURCES = eventtypes.py
3604df
 CLEANFILES = eventtypes.py
3604df
@@ -10,7 +10,8 @@ eventspeerscriptdir = $(libexecdir)/glusterfs
3604df
 eventsconfdir = $(sysconfdir)/glusterfs
3604df
 eventsconf_DATA = eventsconfig.json
3604df
 
3604df
-events_PYTHON = __init__.py eventsapiconf.py eventtypes.py handlers.py utils.py
3604df
+events_PYTHON = __init__.py eventsapiconf.py eventtypes.py handlers.py \
3604df
+	utils.py gf_event.py
3604df
 events_SCRIPTS = glustereventsd.py
3604df
 eventspeerscript_SCRIPTS = peer_eventsapi.py
3604df
 
3604df
diff --git a/events/src/gf_event.py b/events/src/gf_event.py
3604df
new file mode 100644
3604df
index 0000000..0924a65
3604df
--- /dev/null
3604df
+++ b/events/src/gf_event.py
3604df
@@ -0,0 +1,48 @@
3604df
+#!/usr/bin/env python
3604df
+# -*- coding: utf-8 -*-
3604df
+#
3604df
+#  Copyright (c) 2016 Red Hat, Inc. <http://www.redhat.com>
3604df
+#  This file is part of GlusterFS.
3604df
+#
3604df
+#  This file is licensed to you under your choice of the GNU Lesser
3604df
+#  General Public License, version 3 or any later version (LGPLv3 or
3604df
+#  later), or the GNU General Public License, version 2 (GPLv2), in all
3604df
+#  cases as published by the Free Software Foundation.
3604df
+#
3604df
+
3604df
+import socket
3604df
+import time
3604df
+
3604df
+from eventsapiconf import SERVER_ADDRESS
3604df
+from eventtypes import all_events
3604df
+
3604df
+from utils import logger, setup_logger
3604df
+
3604df
+# Run this when this lib loads
3604df
+setup_logger()
3604df
+
3604df
+
3604df
+def gf_event(event_type, **kwargs):
3604df
+    if not isinstance(event_type, int) or event_type >= len(all_events):
3604df
+        logger.error("Invalid Event Type: {0}".format(event_type))
3604df
+        return
3604df
+
3604df
+    try:
3604df
+        client = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
3604df
+        client.connect(SERVER_ADDRESS)
3604df
+    except socket.error as e:
3604df
+        logger.error("Unable to connect to events.sock: {0}".format(e))
3604df
+        return
3604df
+
3604df
+    # Convert key value args into KEY1=VALUE1;KEY2=VALUE2;..
3604df
+    msg = ""
3604df
+    for k, v in kwargs.items():
3604df
+        msg += "{0}={1};".format(k, v)
3604df
+
3604df
+    # <TIMESTAMP> <EVENT_TYPE> <MSG>
3604df
+    msg = "{0} {1} {2}".format(int(time.time()), event_type, msg.strip(";"))
3604df
+
3604df
+    try:
3604df
+        client.sendall(msg)
3604df
+    except socket.error as e:
3604df
+        logger.error("Unable to Send message: {0}".format(e))
3604df
-- 
3604df
1.7.1
3604df