Quellcode durchsuchen

Add init script & supervisor for running in daemon mode

badele vor 9 Jahren
Ursprung
Commit
c3c177e317
4 geänderte Dateien mit 73 neuen und 5 gelöschten Zeilen
  1. 11 3
      README.md
  2. 57 0
      init_server.sh
  3. 1 1
      mqtt2graphite.py
  4. 4 1
      setup.py

+ 11 - 3
README.md

@@ -16,11 +16,19 @@ numeric, these are then sent off to Carbon (see example below)
 * A running Carbon/Graphite server with UDP-enabled reception
 * Access to an MQTT broker. (I use [Mosquitto](http://mosquitto.org/))
 
+## Installation and Configuration
+
+* Configure the mqtt2graphite init script
+
+```bash
+pip install git+https://github.com/jpmens/mqtt2graphite.git
+./init_server.sh mqtt.hostname
+```
+
 ## Running
 
-* Set the environment variable `DEBUG` and `MQTT_HOST` to the name/IP of your MQTT broker. (`localhost` is default.)
-* Edit the `map` file
-* Run `./mqtt2graphite.py [map file]` if the `map file` is not provided a file called `map` in the current working directory is used
+* Edit the `mqtt.hostname.conf` file
+* Run or add this cmd `supervisord -c /etc/supervisord.conf` in your /etc/rc.local
 
 ## Handling numeric payloads
 

+ 57 - 0
init_server.sh

@@ -0,0 +1,57 @@
+#!/bin/bash
+
+################################################
+# Get Variable
+################################################
+
+if [ "$2" != "" ]; then
+    MQTT_HOST=$1
+    CARBON_SERVER=$2
+else
+    echo "Usage: $0 mqtt.hostname carbon.hostname '[Debug monde (True/False)]"
+    exit
+fi
+
+if [ "$3" != "" ]; then
+    DEBUG=$3
+else
+    DEBUG=False
+fi
+
+PYTHONCMD=$(which python)
+MQTT2GRAPHITECMD=$(readlink -f ./mqtt2graphite.py)
+
+################################################
+# Check if the supervisor is already initialized
+################################################
+
+if [  ! -d /etc/supervisord.d ]; then
+    mkdir /etc/supervisord.d
+fi
+
+if [  ! -f /etc/supervisord.conf ]; then
+	echo_supervisord_conf | grep -v '^;' > /etc/supervisord.conf
+
+	echo "[include]" >> /etc/supervisord.conf
+	echo "files = /etc/supervisord.d/*.conf" >> /etc/supervisord.conf
+fi
+
+# Configure the mqtt2graphite supervisor stater script
+echo "
+[program:mqtt2graphite]
+command=$PYTHONCMD $MQTT2GRAPHITECMD $MQTT_HOST.conf
+environment=MQTT_HOST='$1',CARBON_SERVER='$CARBON_SERVER',DEBUG=$DEBUG
+autostart=true
+autorestart=true
+redirect_stderr=true
+stdout_logfile=/tmp/mqtt2graphite.log
+startsecs=5" > /etc/supervisord.d/mqtt2graphite.conf
+
+# Copy the default map to the MQTT_HOST.conf
+
+if [  ! -f $MQTT_HOST.conf ]; then
+    cp map $MQTT_HOST.conf
+fi
+
+echo "execute or add this cmd 'supervisord -c /etc/supervisord.conf' in your /etc/rc.local"
+

+ 1 - 1
mqtt2graphite.py

@@ -13,7 +13,7 @@ import json
 import signal
 
 MQTT_HOST = os.environ.get('MQTT_HOST', 'localhost')
-CARBON_SERVER = '127.0.0.1'
+CARBON_SERVER = os.environ.get('CARBON_SERVER', '127.0.0.1')
 CARBON_PORT = 2003
 
 LOGFORMAT = '%(asctime)-15s %(message)s'

+ 4 - 1
setup.py

@@ -5,7 +5,10 @@ setup(
     description="This program subscribes to any number of MQTT topics, extracts a value from the messages' payload and sends that off to Graphite via Carbon over a UDP socket.",
 
     py_modules=['mqtt2graphite'],
-    install_requires=['paho-mqtt'],
+    install_requires=[
+        'paho-mqtt',
+        'supervisor',
+        ],
     entry_points={
         'console_scripts': [
             "mqtt2graphite = mqtt2graphite:main",