mqtt-launcher.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. #!/usr/bin/env python
  2. # Copyright (c) 2014 Jan-Piet Mens <jpmens()gmail.com>
  3. # All rights reserved.
  4. #
  5. # Redistribution and use in source and binary forms, with or without
  6. # modification, are permitted provided that the following conditions are met:
  7. #
  8. # 1. Redistributions of source code must retain the above copyright notice,
  9. # this list of conditions and the following disclaimer.
  10. # 2. Redistributions in binary form must reproduce the above copyright
  11. # notice, this list of conditions and the following disclaimer in the
  12. # documentation and/or other materials provided with the distribution.
  13. # 3. Neither the name of mosquitto nor the names of its
  14. # contributors may be used to endorse or promote products derived from
  15. # this software without specific prior written permission.
  16. #
  17. # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  18. # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  19. # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  20. # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  21. # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  22. # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  23. # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  24. # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  25. # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  26. # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  27. # POSSIBILITY OF SUCH DAMAGE.
  28. __author__ = 'Jan-Piet Mens <jpmens()gmail.com>'
  29. __copyright__ = 'Copyright 2014 Jan-Piet Mens'
  30. import os
  31. import sys
  32. import subprocess
  33. import logging
  34. import mosquitto
  35. import socket
  36. import string
  37. qos=2
  38. CONFIG=os.getenv('MQTTLAUNCHERCONFIG', 'launcher.conf')
  39. class Config(object):
  40. def __init__(self, filename=CONFIG):
  41. self.config = {}
  42. execfile(filename, self.config)
  43. def get(self, key, default=None):
  44. return self.config.get(key, default)
  45. cf = Config()
  46. LOGFILE = cf.get('logfile', 'logfile')
  47. LOGFORMAT = '%(asctime)-15s %(message)s'
  48. DEBUG=True
  49. if DEBUG:
  50. logging.basicConfig(filename=LOGFILE, level=logging.DEBUG, format=LOGFORMAT)
  51. else:
  52. logging.basicConfig(filename=LOGFILE, level=logging.INFO, format=LOGFORMAT)
  53. logging.info("Starting")
  54. logging.debug("DEBUG MODE")
  55. def runprog(topic, param=None):
  56. publish = "%s/report" % topic
  57. if param is not None and all(c in string.printable for c in param) == False:
  58. logging.debug("Param for topic %s is not printable; skipping" % (topic))
  59. return
  60. if not topic in topiclist:
  61. logging.info("Topic %s isn't configured" % topic)
  62. return
  63. if param is not None and param in topiclist[topic]:
  64. cmd = topiclist[topic].get(param)
  65. else:
  66. if None in topiclist[topic]: ### and topiclist[topic][None] is not None:
  67. cmd = []
  68. for p in topiclist[topic][None]:
  69. if p == '@!@':
  70. p = param
  71. cmd.append(p)
  72. else:
  73. logging.info("No matching param (%s) for %s" % (param, topic))
  74. return
  75. logging.debug("Running t=%s: %s" % (topic, cmd))
  76. try:
  77. res = subprocess.check_output(cmd, stdin=None, stderr=subprocess.STDOUT, shell=False, universal_newlines=True, cwd='/tmp')
  78. except Exception, e:
  79. res = "*****> %s" % str(e)
  80. payload = res.rstrip('\n')
  81. (res, mid) = mqttc.publish(publish, payload, qos=qos, retain=False)
  82. def on_message(mosq, userdata, msg):
  83. logging.debug(msg.topic+" "+str(msg.qos)+" "+str(msg.payload))
  84. runprog(msg.topic, str(msg.payload))
  85. def on_disconnect(mosq, userdata, rc):
  86. logging.debug("OOOOPS! launcher disconnects")
  87. time.sleep(10)
  88. if __name__ == '__main__':
  89. userdata = {
  90. }
  91. topiclist = cf.get('topiclist')
  92. if topiclist is None:
  93. logging.info("No topic list. Aborting")
  94. sys.exit(2)
  95. clientid = cf.get('mqtt_clientid', 'mqtt-launcher-%s' % os.getpid())
  96. mqttc = mosquitto.Mosquitto(clientid, userdata=userdata, clean_session=False)
  97. mqttc.on_message = on_message
  98. mqttc.on_disconnect = on_disconnect
  99. mqttc.will_set('clients/mqtt-launcher', payload="Adios!", qos=0, retain=False)
  100. # Delays will be: 3, 6, 12, 24, 30, 30, ...
  101. mqttc.reconnect_delay_set(delay=3, delay_max=30, exponential_backoff=True)
  102. mqttc.username_pw_set(cf.get('username'), cf.get('password'))
  103. mqttc.connect(cf.get('mqtt_broker', 'localhost'), int(cf.get('mqtt_port', '1883')), 60)
  104. for topic in topiclist:
  105. mqttc.subscribe(topic, qos)
  106. while True:
  107. try:
  108. mqttc.loop_forever()
  109. except socket.error:
  110. time.sleep(5)
  111. except KeyboardInterrupt:
  112. sys.exit(0)