mqtt_logger: administrative stuff

* add systemctl service
 * add installation procedure

Signed-off-by: Thomas Klaehn <tkl@blackfinn.de>
This commit is contained in:
Thomas Klaehn 2017-02-15 17:36:03 +01:00
parent e4655c25ae
commit b802b9cadb
11 changed files with 121 additions and 78 deletions

View File

@ -2,8 +2,6 @@
<?eclipse-pydev version="1.0"?><pydev_project>
<pydev_pathproperty name="org.python.pydev.PROJECT_SOURCE_PATH">
<path>/${PROJECT_DIR_NAME}</path>
<path>/${PROJECT_DIR_NAME}/source</path>
<path>/${PROJECT_DIR_NAME}/tests</path>
</pydev_pathproperty>
<pydev_property name="org.python.pydev.PYTHON_PROJECT_VERSION">python 2.7</pydev_property>
<pydev_property name="org.python.pydev.PYTHON_PROJECT_INTERPRETER">Default</pydev_property>

View File

@ -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/...`

10
mqtt_logger.service Normal file
View File

@ -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

43
mqtt_logger/__init__.py Normal file
View File

@ -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())

View File

@ -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:]))

View File

@ -1,2 +0,0 @@
[TYPECHECK]
ignored-modules = numpy

36
setup.py Normal file
View File

@ -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)

View File

@ -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())

View File

@ -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'],
)

View File

@ -1,5 +0,0 @@
'''
Created on Feb 15, 2017
@author: tkl
'''

View File

@ -1,5 +0,0 @@
'''
Created on Feb 15, 2017
@author: tkl
'''