Blob Blame History Raw
From bc6fac6847c3fe2db021e6a0bbe8d55dc24487af Mon Sep 17 00:00:00 2001
From: Aravinda VK <avishwan@redhat.com>
Date: Mon, 1 Aug 2016 17:16:50 +0530
Subject: [PATCH 34/86] eventsapi: Python library for gf_event

Python library to emit Events from Geo-replication or any other Python apps
running in storage nodes.

Add $LIBEXEC/glusterfs/events to Python path to use gf_event

gf_event(event_type, **kwargs)

For example,

    sys.path.insert(1, "LIBEXECDIR/glusterfs")
    from events.gf_event import gf_event
    from events import eventtypes

    gf_event(eventtypes.GEOREP_FAULTY,
             volname="gv1",
             slaveuser="root",
             slavehost="node1",
             slavevol="gv2")

Errors will be logged in $LOGDIR/glusterfs/events.log

> Reviewed-on: http://review.gluster.org/15063
> Smoke: Gluster Build System <jenkins@build.gluster.org>
> NetBSD-regression: NetBSD Build System <jenkins@build.gluster.org>
> CentOS-regression: Gluster Build System <jenkins@build.gluster.org>
> Reviewed-by: Samikshan Bairagya <samikshan@gmail.com>
> Reviewed-by: Prashanth Pai <ppai@redhat.com>

BUG: 1351589
Change-Id: I2af2bd77f9961975e4387006b9e99e4543e12b57
Signed-off-by: Aravinda VK <avishwan@redhat.com>
Reviewed-on: https://code.engineering.redhat.com/gerrit/84740
Reviewed-by: Milind Changire <mchangir@redhat.com>
Reviewed-by: Atin Mukherjee <amukherj@redhat.com>
---
 events/eventskeygen.py |    6 +++++-
 events/src/Makefile.am |    5 +++--
 events/src/gf_event.py |   48 ++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 56 insertions(+), 3 deletions(-)
 create mode 100644 events/src/gf_event.py

diff --git a/events/eventskeygen.py b/events/eventskeygen.py
index bccb91a..7357d8e 100644
--- a/events/eventskeygen.py
+++ b/events/eventskeygen.py
@@ -78,4 +78,8 @@ if gen_header_type == "PY_HEADER":
         f.write("all_events = [\n")
         for ev in keys:
             f.write('    "{0}",\n'.format(ev))
-        f.write("]\n")
+
+        f.write("]\n\n")
+
+        for idx, ev in enumerate(keys):
+            f.write("{0} = {1}\n".format(ev.replace("EVENT_", ""), idx))
diff --git a/events/src/Makefile.am b/events/src/Makefile.am
index 8b2150e..423bc88 100644
--- a/events/src/Makefile.am
+++ b/events/src/Makefile.am
@@ -1,6 +1,6 @@
 noinst_PYTHON = $(top_srcdir)/events/eventskeygen.py
 EXTRA_DIST = glustereventsd.py __init__.py  eventsapiconf.py.in \
-	handlers.py utils.py peer_eventsapi.py eventsconfig.json
+	handlers.py utils.py peer_eventsapi.py eventsconfig.json gf_event.py
 
 BUILT_SOURCES = eventtypes.py
 CLEANFILES = eventtypes.py
@@ -10,7 +10,8 @@ eventspeerscriptdir = $(libexecdir)/glusterfs
 eventsconfdir = $(sysconfdir)/glusterfs
 eventsconf_DATA = eventsconfig.json
 
-events_PYTHON = __init__.py eventsapiconf.py eventtypes.py handlers.py utils.py
+events_PYTHON = __init__.py eventsapiconf.py eventtypes.py handlers.py \
+	utils.py gf_event.py
 events_SCRIPTS = glustereventsd.py
 eventspeerscript_SCRIPTS = peer_eventsapi.py
 
diff --git a/events/src/gf_event.py b/events/src/gf_event.py
new file mode 100644
index 0000000..0924a65
--- /dev/null
+++ b/events/src/gf_event.py
@@ -0,0 +1,48 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+#
+#  Copyright (c) 2016 Red Hat, Inc. <http://www.redhat.com>
+#  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.
+#
+
+import socket
+import time
+
+from eventsapiconf import SERVER_ADDRESS
+from eventtypes import all_events
+
+from utils import logger, setup_logger
+
+# Run this when this lib loads
+setup_logger()
+
+
+def gf_event(event_type, **kwargs):
+    if not isinstance(event_type, int) or event_type >= len(all_events):
+        logger.error("Invalid Event Type: {0}".format(event_type))
+        return
+
+    try:
+        client = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
+        client.connect(SERVER_ADDRESS)
+    except socket.error as e:
+        logger.error("Unable to connect to events.sock: {0}".format(e))
+        return
+
+    # Convert key value args into KEY1=VALUE1;KEY2=VALUE2;..
+    msg = ""
+    for k, v in kwargs.items():
+        msg += "{0}={1};".format(k, v)
+
+    # <TIMESTAMP> <EVENT_TYPE> <MSG>
+    msg = "{0} {1} {2}".format(int(time.time()), event_type, msg.strip(";"))
+
+    try:
+        client.sendall(msg)
+    except socket.error as e:
+        logger.error("Unable to Send message: {0}".format(e))
-- 
1.7.1