From b802b9cadb2c5279e4284a1020dc3409b0beb554 Mon Sep 17 00:00:00 2001 From: Thomas Klaehn Date: Wed, 15 Feb 2017 17:36:03 +0100 Subject: [PATCH] mqtt_logger: administrative stuff * add systemctl service * add installation procedure Signed-off-by: Thomas Klaehn --- .pydevproject | 2 -- Readme.md | 32 +++++++++++++++++++++++++++ mqtt_logger.service | 10 +++++++++ mqtt_logger/__init__.py | 43 +++++++++++++++++++++++++++++++++++++ pylint_wrapper.py | 32 --------------------------- pylintrc | 2 -- setup.py | 36 +++++++++++++++++++++++++++++++ source/main/__init__.py | 14 ------------ source/setup.py | 18 ---------------- tests/__init__.py | 5 ----- tests/unittests/__init__.py | 5 ----- 11 files changed, 121 insertions(+), 78 deletions(-) create mode 100644 mqtt_logger.service create mode 100644 mqtt_logger/__init__.py delete mode 100644 pylint_wrapper.py delete mode 100644 pylintrc create mode 100644 setup.py delete mode 100644 source/main/__init__.py delete mode 100644 source/setup.py delete mode 100644 tests/__init__.py delete mode 100644 tests/unittests/__init__.py diff --git a/.pydevproject b/.pydevproject index cb13045..037bd25 100644 --- a/.pydevproject +++ b/.pydevproject @@ -2,8 +2,6 @@ /${PROJECT_DIR_NAME} -/${PROJECT_DIR_NAME}/source -/${PROJECT_DIR_NAME}/tests python 2.7 Default diff --git a/Readme.md b/Readme.md index ee0b3c6..4b053d8 100644 --- a/Readme.md +++ b/Readme.md @@ -1 +1,33 @@ # mqtt data logger +## Prerequisites + +Depends on `communication_protocols.py` (https://files.blackfinn.de/files/python/communication_protocols.py). + +## Installation + + * extract tar file: + +```shell +$ tar xvf mqtt_logger-0.1.0.tar.xz +``` + + * run install script (as super user): + +```shell +$ python setup.py install +``` + + * enable `systemd` service (as superuser): + +```shell +$ systemctl enable mqtt_logger.service +``` + + * start `systemd` service (as superuser): + +```shell +$ systemctl start mqtt_logger.service +``` + +## Service +Log file are stored at `/var/log/mqtt/...` \ No newline at end of file diff --git a/mqtt_logger.service b/mqtt_logger.service new file mode 100644 index 0000000..201534b --- /dev/null +++ b/mqtt_logger.service @@ -0,0 +1,10 @@ +[Unit] +Description=Mqtt logger service +After=multi-user.target + +[Service] +Type=idle +ExecStart=/usr/bin/python /usr/local/lib/python2.7/dist-packages/mqtt_logger/__init__.py + +[Install] +WantedBy=multi-user.target \ No newline at end of file diff --git a/mqtt_logger/__init__.py b/mqtt_logger/__init__.py new file mode 100644 index 0000000..8fc5047 --- /dev/null +++ b/mqtt_logger/__init__.py @@ -0,0 +1,43 @@ +''' +Created on Feb 15, 2017 + +@author: tkl +''' +import logging +import os +import re +import sys +import time +import mqtt + +BASE_LOG_DIR = '/var/log/mqtt/' +OWN_LOG_FILE = '/var/log/mqtt_logger.log' +LOG_FORMAT = '%(asctime)s %(levelname)s %(message)s' + +def main(): + ''' + Entry point for mqtt logger. + ''' + logging.basicConfig(format=LOG_FORMAT, filename=OWN_LOG_FILE) + conn = mqtt.Mqtt(hostname='gitlab', subscribe=['#']) + if conn.connect() is False: + logging.error('couldn\'t connect...') + return 1 + else: + try: + while True: + msg = conn.receive() + logfile = BASE_LOG_DIR + msg.topic + '.log' + logdir = re.sub('/[^/]*$', '', logfile) + if not os.path.isdir(logdir): + os.makedirs(logdir) + log = open(logfile, 'a') + log.write(msg.payload + '\n') + log.close() + except KeyboardInterrupt: + logging.exception('Ececution interrupted by Keyboard Interrupt') + conn.disconnect() + return 0 + +if __name__ == '__main__': + sys.exit(main()) diff --git a/pylint_wrapper.py b/pylint_wrapper.py deleted file mode 100644 index 9489183..0000000 --- a/pylint_wrapper.py +++ /dev/null @@ -1,32 +0,0 @@ -''' -Created on Feb 11, 2017 -The pylint wrapper is needed for ci because pylint will return != 0 also in -warning case. - -@author: tkl -''' -import os -import sys -import getopt - -def main(argv): - ''' Entry Point for the pylint wrapper. ''' - options, _ = getopt.getopt(argv, "s:", ["source="]) - source_list = [] - for opt, args in options: - if opt in ("-s", "--source"): - source_list.append(args) - - source_str = "" - for source in source_list: - source_str += source + " " - - os.system("pylint " + source_str) -# pylint for sonar cube -# os.system("pylint " + source_str + \ -# " -r n --msg-template=\"{path}:{line}: [{msg_id}({symbol}), " + \ -# "{obj}] {msg}\" > sonar.report") - return 0 - -if __name__ == "__main__": - sys.exit(main(sys.argv[1:])) diff --git a/pylintrc b/pylintrc deleted file mode 100644 index 1b59ef2..0000000 --- a/pylintrc +++ /dev/null @@ -1,2 +0,0 @@ -[TYPECHECK] -ignored-modules = numpy \ No newline at end of file diff --git a/setup.py b/setup.py new file mode 100644 index 0000000..2cb4be2 --- /dev/null +++ b/setup.py @@ -0,0 +1,36 @@ +#!/usr/bin/env python +''' +Created on Feb 15, 2017 + +Type 'python setup.py sdist' to create the distribution, +type 'python setup.py install' to install the distribution. + +@author: tkl +''' +from distutils.core import setup +import shutil +import os +import stat +import sys + +DAEMON_START_SCRIPT_SRC = 'mqtt_logger.service' +DAEMON_START_SCRIPT_DST = '/lib/systemd/system/mqtt_logger.service' +PKG_NAME = 'mqtt_logger' +PKG_VERSION = '0.1.0' +PKG_AUTHOR = 'tkl' +PKG_AUTHOR_EMAIL = 'tkl@blackfinn.de' +PKG_URL = 'files.blackfinn.de/python/' + PKG_NAME + '.py/' + PKG_NAME + '-' + \ + PKG_VERSION + '.tar.gz' +PKG_PACKAGES = [PKG_NAME] +PKG_SCRIPTS = [DAEMON_START_SCRIPT_SRC] + +if sys.argv[1] == 'install': + shutil.copyfile(DAEMON_START_SCRIPT_SRC, DAEMON_START_SCRIPT_DST) + os.chmod(DAEMON_START_SCRIPT_DST, stat.S_IRUSR | stat.S_IWUSR | \ + stat.S_IRGRP | stat.S_IROTH) + setup(name=PKG_NAME, version=PKG_VERSION, author=PKG_AUTHOR, + author_email=PKG_AUTHOR_EMAIL, url=PKG_URL, packages=PKG_PACKAGES) +elif sys.argv[1] == 'sdist': + setup(name=PKG_NAME, version=PKG_VERSION, author=PKG_AUTHOR, + author_email=PKG_AUTHOR_EMAIL, url=PKG_URL, packages=PKG_PACKAGES, + scripts=PKG_SCRIPTS) diff --git a/source/main/__init__.py b/source/main/__init__.py deleted file mode 100644 index 4c973d6..0000000 --- a/source/main/__init__.py +++ /dev/null @@ -1,14 +0,0 @@ -''' -Created on Feb 15, 2017 - -@author: tkl -''' -import sys - -def main(): - ''' Entry point for the mqtt data logger. - ''' - pass - -if __name__ == '__main__': - sys.exit(main()) diff --git a/source/setup.py b/source/setup.py deleted file mode 100644 index 4731727..0000000 --- a/source/setup.py +++ /dev/null @@ -1,18 +0,0 @@ -''' -Created on Feb 15, 2017 - -Type 'python setup.py sdist' to create the distribution, -type 'python setup.py install' to install the distribution. - -@author: tkl -''' -from distutils.core import setup - -setup( - name='mqtt_logger', - version='0.1', - author='tkl', - author_email='tkl@blackfinn.de', - url='files.blackfinn.de/python/mqtt_logger-0.1.tar.gz', - packages=['main'], - ) diff --git a/tests/__init__.py b/tests/__init__.py deleted file mode 100644 index 5a3505b..0000000 --- a/tests/__init__.py +++ /dev/null @@ -1,5 +0,0 @@ -''' -Created on Feb 15, 2017 - -@author: tkl -''' diff --git a/tests/unittests/__init__.py b/tests/unittests/__init__.py deleted file mode 100644 index 5a3505b..0000000 --- a/tests/unittests/__init__.py +++ /dev/null @@ -1,5 +0,0 @@ -''' -Created on Feb 15, 2017 - -@author: tkl -'''