diff options
Diffstat (limited to 'events/src/gf_event.py')
-rw-r--r-- | events/src/gf_event.py | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/events/src/gf_event.py b/events/src/gf_event.py new file mode 100644 index 00000000000..0924a65dddb --- /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)) |