├── README.md ├── mqtt-spawn.py └── screencast-oneshot.gif /README.md: -------------------------------------------------------------------------------- 1 | mqtt-spawn 2 | ========== 3 | 4 | Spawn a process per MQTT message, provide message on stdin 5 | 6 | Usage: 7 | 8 | ./mqtt-spawn.py .. 9 | 10 | Example: 11 | 12 | ./mqtt-spawn.py 172.16.68.10 'dotty -' oneshot/dot 13 | 14 | will plot every graphviz message received on `oneshot/dot`. 15 | 16 | (My actual usage is `./mqtt-spawn.py 172.16.68.10 'sponge ../test.dot' oneshot/dot` with GraphViz.app for OS X opened on `test.dot`. `cat > ../test.dot` also works but is not as clean). 17 | 18 | Example message from actual usage: 19 | 20 | mosquitto_pub -s -t oneshot/dot << EOF 21 | digraph oneshot { 22 | subgraph cluster_ { DS_ -> DNSKEY_ [ label = "19036/2" ]; label = "zone: "; } 23 | DNSKEY_ -> DNSKEY_; 24 | DNSKEY_ -> DS_org; 25 | DNSKEY_ -> DS_org; 26 | subgraph cluster_org { DS_org -> DNSKEY_org [ label = "21366/1" ]; label = "zone: org"; } 27 | subgraph cluster_org { DS_org -> DNSKEY_org [ label = "21366/2" ]; label = "zone: org"; } 28 | DNSKEY_org -> DNSKEY_org; 29 | DNSKEY_org -> DNSKEY_org; 30 | DNSKEY_org -> DS_powerdnssec_org; 31 | subgraph cluster_powerdnssec_org { DS_powerdnssec_org -> DNSKEY_powerdnssec_org [ label = "2224/1" ]; label = "zone: powerdnssec.org"; } 32 | DNSKEY_powerdnssec_org -> DNSKEY_powerdnssec_org; 33 | } 34 | EOF 35 | 36 | Mandatory screencast: 37 | 38 | ![screencast-oneshot](screencast-oneshot.gif) 39 | -------------------------------------------------------------------------------- /mqtt-spawn.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | import mosquitto, sys 3 | import subprocess 4 | import signal 5 | 6 | client = mosquitto.Mosquitto() 7 | client.connect(sys.argv[1]) 8 | 9 | def on_connect(mosq, userdata, rc): 10 | for topic in sys.argv[3:]: 11 | client.subscribe(topic, 0) 12 | 13 | def on_message(mosq, obj, msg): 14 | print("Message received on topic "+msg.topic+" with QoS "+str(msg.qos)+" and payload "+msg.payload) 15 | p = subprocess.Popen(sys.argv[2], shell=True, stdin=subprocess.PIPE) 16 | p.stdin.write(msg.payload) 17 | p.stdin.close() 18 | 19 | # don't accumulate zombies, we don't care anyway 20 | signal.signal(signal.SIGCHLD, signal.SIG_IGN) 21 | 22 | client.on_connect = on_connect 23 | client.on_message = on_message 24 | 25 | while True: 26 | client.loop() -------------------------------------------------------------------------------- /screencast-oneshot.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PowerDNS/mqtt-spawn/5c56c843cb0f0086e2edc1dcb4918ab350212eb0/screencast-oneshot.gif --------------------------------------------------------------------------------