initial commit
This commit is contained in:
		
						commit
						eebe1afd5d
					
				
							
								
								
									
										13
									
								
								Pipfile
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										13
									
								
								Pipfile
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,13 @@
 | 
			
		||||
[[source]]
 | 
			
		||||
name = "pypi"
 | 
			
		||||
url = "https://pypi.org/simple"
 | 
			
		||||
verify_ssl = true
 | 
			
		||||
 | 
			
		||||
[dev-packages]
 | 
			
		||||
 | 
			
		||||
[packages]
 | 
			
		||||
paho-mqtt = "*"
 | 
			
		||||
prometheus-client = "*"
 | 
			
		||||
 | 
			
		||||
[requires]
 | 
			
		||||
python_version = "3.6"
 | 
			
		||||
							
								
								
									
										36
									
								
								Pipfile.lock
									
									
									
										generated
									
									
									
										Normal file
									
								
							
							
						
						
									
										36
									
								
								Pipfile.lock
									
									
									
										generated
									
									
									
										Normal file
									
								
							@ -0,0 +1,36 @@
 | 
			
		||||
{
 | 
			
		||||
    "_meta": {
 | 
			
		||||
        "hash": {
 | 
			
		||||
            "sha256": "580f0831d7226ccb878b639f429faf10c3dde1880ddc99549be76d3a5cfc6ad7"
 | 
			
		||||
        },
 | 
			
		||||
        "pipfile-spec": 6,
 | 
			
		||||
        "requires": {
 | 
			
		||||
            "python_version": "3.6"
 | 
			
		||||
        },
 | 
			
		||||
        "sources": [
 | 
			
		||||
            {
 | 
			
		||||
                "name": "pypi",
 | 
			
		||||
                "url": "https://pypi.org/simple",
 | 
			
		||||
                "verify_ssl": true
 | 
			
		||||
            }
 | 
			
		||||
        ]
 | 
			
		||||
    },
 | 
			
		||||
    "default": {
 | 
			
		||||
        "paho-mqtt": {
 | 
			
		||||
            "hashes": [
 | 
			
		||||
                "sha256:e440a052b46d222e184be3be38676378722072fcd4dfd2c8f509fb861a7b0b79"
 | 
			
		||||
            ],
 | 
			
		||||
            "index": "pypi",
 | 
			
		||||
            "version": "==1.4.0"
 | 
			
		||||
        },
 | 
			
		||||
        "prometheus-client": {
 | 
			
		||||
            "hashes": [
 | 
			
		||||
                "sha256:71cd24a2b3eb335cb800c7159f423df1bd4dcd5171b234be15e3f31ec9f622da",
 | 
			
		||||
                "sha256:82d609eb7bf5843d4d2a94fed9dfd352ca9f4865ed12ff99569c2249858a8e51"
 | 
			
		||||
            ],
 | 
			
		||||
            "index": "pypi",
 | 
			
		||||
            "version": "==0.7.1"
 | 
			
		||||
        }
 | 
			
		||||
    },
 | 
			
		||||
    "develop": {}
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										60
									
								
								exporter.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										60
									
								
								exporter.py
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,60 @@
 | 
			
		||||
from prometheus_client import start_http_server, Summary, Gauge
 | 
			
		||||
import paho.mqtt.publish as publish
 | 
			
		||||
import paho.mqtt.client as mqtt
 | 
			
		||||
import random
 | 
			
		||||
import time
 | 
			
		||||
from node_api import NodeAPI
 | 
			
		||||
 | 
			
		||||
client_id = "exporter"
 | 
			
		||||
topics = [
 | 
			
		||||
        ("$SYS/broker/clients/connected", 0),
 | 
			
		||||
        ("$CONNECTED/#", 0)
 | 
			
		||||
        ]
 | 
			
		||||
 | 
			
		||||
node_api = NodeAPI()
 | 
			
		||||
 | 
			
		||||
MQTT_CONNECTED = Gauge('mqtt_node_connected', 'MQTT node connection status', ['node', 'description'])
 | 
			
		||||
MQTT_BROKER_CLIENTS = Gauge('mqtt_broker_clients', 'MQTT broker clients connected')
 | 
			
		||||
 | 
			
		||||
def on_connect(client, userdata, flags, rc):
 | 
			
		||||
    print("Connected with result code " + str(rc))
 | 
			
		||||
 | 
			
		||||
    client.subscribe(topics)
 | 
			
		||||
    if rc==0:
 | 
			
		||||
        client.connected_flag=True
 | 
			
		||||
        client.publish("$CONNECTED/"+client_id, 1, retain=True)
 | 
			
		||||
    else:
 | 
			
		||||
        client.bad_connection_flag=True
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
#def on_disconnect(client, userdata, rc):
 | 
			
		||||
#    client.publish(connection_topic,0,retain=True)
 | 
			
		||||
 | 
			
		||||
def on_message(client, userdata, msg):
 | 
			
		||||
    topic = msg.topic.split('/')
 | 
			
		||||
 | 
			
		||||
    if topic[0] == "$CONNECTED":
 | 
			
		||||
        MQTT_CONNECTED.labels(topic[1], node_api.get(topic[1])).set(int(msg.payload))
 | 
			
		||||
 | 
			
		||||
    if msg.topic == "$SYS/broker/clients/connected":
 | 
			
		||||
        MQTT_BROKER_CLIENTS.set(int(msg.payload))
 | 
			
		||||
 | 
			
		||||
    print(msg.topic, int(msg.payload))
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
client = mqtt.Client(client_id)
 | 
			
		||||
 | 
			
		||||
client.on_connect = on_connect
 | 
			
		||||
#client.on_disconnect = on_disconnect
 | 
			
		||||
client.on_message = on_message
 | 
			
		||||
client.will_set("$CONNECTED/"+client_id, 0, qos=0, retain=True)
 | 
			
		||||
client.connect('localhost')
 | 
			
		||||
client.loop_start()
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
if __name__ == '__main__':
 | 
			
		||||
    # Start up the server to expose the metrics.
 | 
			
		||||
    start_http_server(8000)
 | 
			
		||||
 | 
			
		||||
    while True:
 | 
			
		||||
        time.sleep(1)
 | 
			
		||||
							
								
								
									
										23
									
								
								node_api.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										23
									
								
								node_api.py
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,23 @@
 | 
			
		||||
import urllib.request, urllib.parse, json
 | 
			
		||||
 | 
			
		||||
nodes = {}
 | 
			
		||||
 | 
			
		||||
class NodeAPI:
 | 
			
		||||
    def get(self, node):
 | 
			
		||||
        if node in nodes:
 | 
			
		||||
            return nodes[node]
 | 
			
		||||
 | 
			
		||||
        try:
 | 
			
		||||
            with urllib.request.urlopen("http://logistics.dev.uctrl.net/api/nodes/"+node) as url:
 | 
			
		||||
                data = json.loads(url.read().decode())
 | 
			
		||||
 | 
			
		||||
            response = {
 | 
			
		||||
                    'title': data['data']['description'] if data['data']['description'] is not None else data['data']['project']['title'],
 | 
			
		||||
                    'location': data['data']['location']
 | 
			
		||||
                    }
 | 
			
		||||
            nodes[node] = response['title']
 | 
			
		||||
 | 
			
		||||
            return nodes[node]
 | 
			
		||||
 | 
			
		||||
        except urllib.error.HTTPError:
 | 
			
		||||
            return node.capitalize()
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user