[Python-modules-commits] [python-protobix] 01/01: Import python-protobix_0.1.1.orig.tar.gz

Jean Baptiste Favre jbfavre-guest at moszumanska.debian.org
Sat Jul 9 12:16:46 UTC 2016


This is an automated email from the git hooks/post-receive script.

jbfavre-guest pushed a commit to branch upstream
in repository python-protobix.

commit 9abfa8ad7580f46678505743d81b416d280bd8da
Author: Jean Baptiste Favre <debian at jbfavre.org>
Date:   Sat Jul 9 13:54:41 2016 +0200

    Import python-protobix_0.1.1.orig.tar.gz
---
 .gitignore                   |   6 +--
 .travis.yml                  |   9 ++++
 README.md                    |   3 ++
 conftest.py                  |  18 +++++++
 protobix/datacontainer.py    |   2 +
 protobix/senderprotocol.py   |  67 +++++++++++++------------
 requirements.txt             |   5 ++
 setup.py                     |  15 +++---
 tests/ZabbixServerTrapper.py | 113 ++++++++++++++++++++++---------------------
 9 files changed, 142 insertions(+), 96 deletions(-)

diff --git a/.gitignore b/.gitignore
index 300e628..eff9bc7 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,6 +1,6 @@
 .coverage
-protobix/*.pyc
-protobix/__pycache__/
-tests/__pycache__/
+*.pyc
+/__pycache__/
 MANIFEST
 dist/
+*.egg-info
diff --git a/.travis.yml b/.travis.yml
new file mode 100644
index 0000000..b7f42ff
--- /dev/null
+++ b/.travis.yml
@@ -0,0 +1,9 @@
+language: python
+python:
+  - "2.7"
+  - "3.4"
+  - "3.5"
+# command to install dependencies
+install: pip install -r requirements.txt
+# command to run tests
+script: python setup.py test
diff --git a/README.md b/README.md
index 7dcbea0..eafd269 100644
--- a/README.md
+++ b/README.md
@@ -1,5 +1,8 @@
 # python-protobix
 
+dev Branch: [![Build Status](https://travis-ci.org/jbfavre/python-protobix.svg?branch=dev)](https://travis-ci.org/jbfavre/python-protobix)
+upstream Branch: [![Build Status](https://travis-ci.org/jbfavre/python-protobix.svg?branch=upstream)](https://travis-ci.org/jbfavre/python-protobix)
+
 Very simple python module implementing Zabbix Sender protocol.  
 It allows one to build list of items and send them as trapper.
 It currently supports `items` as well as [`Low Level Discovery`](https://www.zabbix.com/documentation/2.4/manual/discovery/low_level_discovery).
diff --git a/conftest.py b/conftest.py
new file mode 100644
index 0000000..3a79b77
--- /dev/null
+++ b/conftest.py
@@ -0,0 +1,18 @@
+import pytest
+import os
+import subprocess
+import time
+
+ at pytest.fixture(scope="session", autouse=True)
+def start_zbx_server (request):
+    zbx_cmd = [
+        'python',
+        'tests/ZabbixServerTrapper.py'
+    ]
+    zbx_proc = subprocess.Popen(zbx_cmd,
+        stdout=open(os.devnull),
+        stderr=open(os.devnull),
+        shell=False
+    )
+    request.addfinalizer(zbx_proc.kill)
+    time.sleep(0.5)
diff --git a/protobix/datacontainer.py b/protobix/datacontainer.py
index 277e663..24c645b 100644
--- a/protobix/datacontainer.py
+++ b/protobix/datacontainer.py
@@ -56,6 +56,8 @@ class DataContainer(SenderProtocol):
                        log_output = None,
                        dryrun     = False,
                        logger     = None):
+        super(DataContainer,self).__init__()
+
 
         # Loads config from zabbix_agentd file
         # If no file, it uses the default _config as configuration
diff --git a/protobix/senderprotocol.py b/protobix/senderprotocol.py
index 2c9ad40..1d1d60b 100644
--- a/protobix/senderprotocol.py
+++ b/protobix/senderprotocol.py
@@ -61,16 +61,18 @@ ZBX_DBG_SEND_ITEM   = "[%s %s %s]"
 ZBX_SEND_ITEM   = "[%d items]"
 
 class SenderProtocol(object):
-
-    _config = {
-        'server': '127.0.0.1',
-        'port': 10051,
-        'log_output': '/tmp/zabbix_agentd.log',
-        'log_level': 3,
-        'timeout': 3,
-        'dryrun': False,
-        'data_type': None }
-
+    def __init__(self):
+        self._config = {
+            'server': '127.0.0.1',
+            'port': 10051,
+            'log_output': '/tmp/zabbix_agentd.log',
+            'log_level': 3,
+            'timeout': 3,
+            'dryrun': False,
+            'data_type': None }
+        self._items_list = []
+        self.data = None
+    
     @property
     def zbx_host(self):
         return self._config['server']
@@ -103,7 +105,7 @@ class SenderProtocol(object):
 
     @property
     def clock(self):
-        return time.time()
+        return int(time.time())
 
     def _send_to_zabbix(self, item):
         # Return 0 if dryrun mode enabled
@@ -112,18 +114,19 @@ class SenderProtocol(object):
         # Format data to be sent
         if type(item) is dict:
             item = [ item ]
-        data = json.dumps({ "data": item,
-                            "request": self.REQUEST,
-                            "clock": self.clock })
+        self.data = json.dumps({ "data": item,
+                                 "request": self.REQUEST,
+                                 "clock": self.clock })
         # Set socket options & open connection
         socket.setdefaulttimeout(self._config['timeout'])
         try:
-            zbx_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
-            zbx_sock.connect((self._config['server'], int(self._config['port'])))
+            self.zbx_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
+            self.zbx_sock.connect((self._config['server'], int(self._config['port'])))
         except Exception as e:
             # Maybe we could consider storing missed sent data for later retry
+            self.data = None
             self._items_list = []
-            zbx_sock.close()
+            self.zbx_sock.close()
             if self._logger:
                 self._logger.error(
                     "Unable to connect to server %s on port %d" % \
@@ -131,12 +134,12 @@ class SenderProtocol(object):
                 )
             raise
         # Build Zabbix Sender payload
-        data_length = len(data)
+        data_length = len(self.data)
         data_header = struct.pack('<Q', data_length)
-        packet = b(ZBX_HDR) + data_header + b(data)
+        packet = b(ZBX_HDR) + data_header + b(self.data)
         # Send payload to Zabbix Server and check response header
         try:
-            zbx_sock.sendall(packet)
+            self.zbx_sock.sendall(packet)
         except:
             if self._logger:
                 self._logger.error('Error while sending data to Zabbix server')
@@ -144,11 +147,8 @@ class SenderProtocol(object):
 
         try:
             # Check the 5 first bytes from answer to ensure it's well formatted
-            zbx_srv_resp_hdr = zbx_sock.recv(5)
+            zbx_srv_resp_hdr = self.zbx_sock.recv(5)
             assert(zbx_srv_resp_hdr == b(ZBX_HDR))
-        except IOError as e:
-            # Handles a socket.error exception here
-            raise
         except Exception as e:
             if self._logger:
                 self._logger.error(
@@ -156,24 +156,26 @@ class SenderProtocol(object):
                 )
             raise
         # Get the 8 next bytes and unpack to get response's payload length
-        zbx_srv_resp_data_hdr = zbx_sock.recv(8)
+        zbx_srv_resp_data_hdr = self.zbx_sock.recv(8)
         zbx_srv_resp_body_len = struct.unpack('<Q', zbx_srv_resp_data_hdr)[0]
         # Get response payload from Zabbix Server
-        zbx_srv_resp_body = zbx_sock.recv(zbx_srv_resp_body_len)
-        zbx_sock.close()
+        zbx_srv_resp_body = self.zbx_sock.recv(zbx_srv_resp_body_len)
+        self.data = None
+        self._items_list = []
+        self.zbx_sock.close()
         if sys.version_info[0] == 3:
             zbx_srv_resp_body = zbx_srv_resp_body.decode()
         return json.loads(zbx_srv_resp_body)
 
     def send(self, container = None):
-        if container != None:
+        if container != None and self.logger:
             # Using container argument is deprecated
             self.logger.warning(
                 'Deprecated call of send() function with container argument'
             )
         zbx_answer = 0
         self._result = []
-        if self._config['log_level'] == 4:
+        if self._config['log_level'] >= 4:
             # Per item sent if debug mode enabled
             for item in self._items_list:
                 output =  ZBX_DBG_SEND_ITEM % (
@@ -210,14 +212,17 @@ class SenderProtocol(object):
                     )
                 )
             self._result.append(result)
+        self.data = None
         self._items_list = []
+        if not self._config['dryrun']:
+            self.zbx_sock.close()
         self._config['data_type'] = None
 
     def _handle_response(self, zbx_answer, output):
         if zbx_answer and self.logger:
             self.logger.debug("Got [%s] as response from Zabbix server" % zbx_answer)
         nb_item = len(self._items_list)
-        if self._config['log_level'] == 4:
+        if self._config['log_level'] >= 4:
             nb_item = 1
         if zbx_answer:
             if zbx_answer.get('response') == 'success':
@@ -255,7 +260,7 @@ class SenderProtocol(object):
     @property
     @deprecated
     def debug(self):
-        if self._config['log_level'] == 4:
+        if self._config['log_level'] >= 4:
             return True
         else:
             return False
diff --git a/requirements.txt b/requirements.txt
new file mode 100644
index 0000000..e75c2c0
--- /dev/null
+++ b/requirements.txt
@@ -0,0 +1,5 @@
+configobj
+simplejson
+traceback2
+pytest
+pytest-cov
diff --git a/setup.py b/setup.py
index 36e8dca..36ee764 100755
--- a/setup.py
+++ b/setup.py
@@ -23,25 +23,24 @@ class PyTest(TestCommand):
 setup(
     name = 'protobix',
     packages = ['protobix'],
-    version = '0.0.9',
+    version = '0.1.1',
     install_requires = [
         'configobj',
-        'logging',
-        'functools',
         'simplejson',
         'traceback2'
     ],
 
     description = 'Implementation of Zabbix Sender protocol',
     long_description = ( 'This module implements Zabbix Sender Protocol.\n'
-                         'It allows to build list of items and send items and send '
+                         'It allows to build list of items and send '
                          'them as trapper.\n'
-                         'It currently supports items as well as Low Level Discovery.' ),    
+                         'It currently supports items update as well as '
+                         'Low Level Discovery.' ),    
     author = 'Jean Baptiste Favre',
     author_email = 'jean-baptiste.favre at blablacar.com',
-    license = 'GPL',
-    url='http://github.com/jbfavre/python-protobix/',
-    download_url = 'http://github.com/jbfavre/python-protobix/tarball/0.0.9',
+    license = 'GPL-3+',
+    url='https://github.com/jbfavre/python-protobix/',
+    download_url = 'https://github.com/jbfavre/python-protobix/archive/0.1.1.tar.gz',
     keywords = ['monitoring','zabbix','trappers'],
     classifiers = [],
     cmdclass={'test': PyTest}
diff --git a/tests/ZabbixServerTrapper.py b/tests/ZabbixServerTrapper.py
index afa8e8c..d41ade3 100644
--- a/tests/ZabbixServerTrapper.py
+++ b/tests/ZabbixServerTrapper.py
@@ -13,6 +13,7 @@ except ImportError: import json
 
 try: from thread import * # python 2
 except ImportError: from _thread import * # python 3
+import threading
 
 if sys.version_info < (3,): # python 2
     def b(x):
@@ -54,10 +55,14 @@ REGISTERED_LLD_ITEMS = {
 }
 
 class ZabbixServer(object):
-    class Trapper(object):
-        HOST = '127.0.0.1'
-        PORT = 10051
+    HOST = '127.0.0.1'
+    PORT = 10051
+
+    class TrapperServer():
         ZBX_HDR = "ZBXD\1"
+        def __init__(self, clientsock, logger):
+            self.clientsock = clientsock
+            self.logger = logger
 
         def _parse_payload(self, payload):
             processed = 0
@@ -92,7 +97,7 @@ class ZabbixServer(object):
                        key not in REGISTERED_ITEMS[host] or \
                        value != REGISTERED_ITEMS[host][key]:
                         self.logger.error(
-                            '        host or key not registered' + 
+                            '        host or key not registered' +
                             ' or incorrect value'
                         )
                         failed += 1
@@ -108,17 +113,17 @@ class ZabbixServer(object):
             try:
                 # Check the 5 first bytes from answer
                 # to ensure it's well formatted
-                clt_hdr = self._conn.recv(5)
+                clt_hdr = self.clientsock.recv(5)
                 output = clt_hdr
                 assert(clt_hdr == b(self.ZBX_HDR))
                 self.logger.debug('Got ZBX header')
                 # Get the 8 next bytes and unpack
                 # to get response's payload length
-                payload_hdr = self._conn.recv(8)
+                payload_hdr = self.clientsock.recv(8)
                 output += payload_hdr
                 payload_len = struct.unpack('<Q', payload_hdr)[0]
                 # Get response payload from Zabbix Server
-                payload_body = self._conn.recv(payload_len).decode('ASCII')
+                payload_body = self.clientsock.recv(payload_len).decode('ASCII')
             except:
                 self.logger.debug("Did not get ZBX header. Ok, let's try anyway")
                 # We don't have Zabbix Header which seems to be OK
@@ -128,7 +133,7 @@ class ZabbixServer(object):
             if not payload_body:
                 while True:
                     # Get data from connection
-                    output += self._conn.recv(1024)
+                    output += self.clientsock.recv(1024)
                     if len(output)<1024:
                         # If last received piece of data is shorter
                         # than buffer size, we're done
@@ -144,7 +149,7 @@ class ZabbixServer(object):
             packet = b(self.ZBX_HDR) + data_header + b(data)
             # Send payload to Zabbix Server and check response header
             try:
-                self._conn.send(packet)
+                self.clientsock.sendall(packet)
             except:
                 self.logger.error('Error while sending answer to client')
                 raise Exception('Error while sending answer to client')
@@ -155,13 +160,13 @@ class ZabbixServer(object):
                 self.logger.info('Receiving request')
                 payload = self._get_request()
             except:
-                self._conn.close()
+                self.clientsock.close()
                 self.logger.error('Error while receiveing data')
                 raise Exception('Error while receiveing data')
 
             # We got payload, let's play with it
             result = "success"
-            info = "processed: %d; failed: %d; total: %d; seconds spent: %f"              
+            info = "processed: %d; failed: %d; total: %d; seconds spent: %f"
             try:
                 processed, failed, total = self._parse_payload(payload)
             except Exception as e:
@@ -188,48 +193,48 @@ class ZabbixServer(object):
             # And send back the answer to the client
             self._answer_request(result)
             # End of play, let's close connection
-            self._conn.close()
-
-        def _setup_logging(self):
-            common_log_format = '[%(name)s:%(levelname)s] %(message)s'
-            # Enable default console logging
-            consoleHandler = logging.StreamHandler()
-            consoleFormatter = logging.Formatter(
-                fmt = common_log_format,
-                datefmt = '%Y%m%d:%H%M%S'
+            self.clientsock.close()
+
+    def _setup_logging(self):
+        self.logger = logging.getLogger(self.__class__.__name__)
+        common_log_format = '[%(name)s:%(levelname)s] %(message)s'
+        # Enable default console logging
+        consoleHandler = logging.StreamHandler()
+        consoleFormatter = logging.Formatter(
+            fmt = common_log_format,
+            datefmt = '%Y%m%d:%H%M%S'
+        )
+        consoleHandler.setFormatter(consoleFormatter)
+        self.logger.addHandler(consoleHandler)
+        self.logger.setLevel(logging.DEBUG)
+
+    def run(self):
+        self._setup_logging()
+        self.logger.debug('Initialized ZabbixTrapper server')
+        self.srv_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
+        # Avoid bind: adress already in use error
+        # Reuse addr+port socket except if a process listens on it
+        self.srv_sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
+        # Bind socket to local host and port
+        try:
+            self.logger.debug('Trying to bind ' + self.HOST + ':' + str(self.PORT))
+            self.srv_sock.bind((self.HOST, self.PORT))
+        except socket.error as msg:
+            self.logger.error(
+                'Bind failed. Error Code : ' + str(msg[0]) + ' Message ' + msg[1]
             )
-            consoleHandler.setFormatter(consoleFormatter)
-            self.logger.addHandler(consoleHandler)
-            self.logger.setLevel(logging.DEBUG)
-
-        def run(self):
-            self.logger = logging.getLogger(self.__class__.__name__)
-            self._setup_logging()
-            self.logger.debug('Initialized ZabbixTrapper server')
-            self.srv_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
-            # Avoid bind: adress already in use error
-            # Reuse addr+port socket except if a process listens on it
-            self.srv_sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
-            #Bind socket to local host and port
-            try:
-                self.logger.debug('Trying to bind ' + self.HOST + ':' + str(self.PORT))
-                self.srv_sock.bind((self.HOST, self.PORT))
-            except socket.error as msg:
-                self.logger.error(
-                    'Bind failed. Error Code : ' + str(msg[0]) + ' Message ' + msg[1]
-                )
-                sys.exit()
-            self.logger.debug('Starts listening on ' + self.HOST + ':' + str(self.PORT))
-            # Start listening on socket
-            self.srv_sock.listen(20)
-            # Now keep talking with the client
-            while 1:
-                #wait to accept a connection - blocking call
-                self._conn, addr = self.srv_sock.accept()
-                self.logger.debug(
-                    'Connected with ' + addr[0] + ':' + str(addr[1])
-                )
-                start_new_thread(self._clientthread ,())
-            self.srv_sock.close()
+            sys.exit()
+        self.logger.debug('Starts listening on ' + self.HOST + ':' + str(self.PORT))
+        # Start listening on socket
+        self.srv_sock.listen(5)
+        # Now keep talking with the client
+        while 1:
+            # wait to accept a connection - blocking call
+            self._conn, addr = self.srv_sock.accept()
+            self.logger.debug(
+                'Connected with ' + addr[0] + ':' + str(addr[1])
+            )
+            start_new_thread(self.TrapperServer(self._conn, self.logger)._clientthread ,())
+        self.srv_sock.close()
 
-ZabbixServer.Trapper().run()
\ No newline at end of file
+ZabbixServer().run()

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/python-modules/packages/python-protobix.git



More information about the Python-modules-commits mailing list