simple metrics api to interface with collectd and grafana

This commit is contained in:
Jakob Ketterl 2019-08-04 18:36:03 +02:00
parent 766300bdff
commit 92321a3b4e
4 changed files with 44 additions and 0 deletions

View File

@ -9,6 +9,7 @@ from owrx.source import ClientRegistry
from owrx.connection import WebSocketMessageHandler
from owrx.version import openwebrx_version
from owrx.feature import FeatureDetector
from owrx.metrics import Metrics
import logging
@ -124,6 +125,12 @@ class ApiController(Controller):
self.send_response(data, content_type="application/json")
class MetricsController(Controller):
def handle_request(self):
data = json.dumps(Metrics.getSharedInstance().getMetrics())
self.send_response(data, content_type="application/json")
class WebSocketController(Controller):
def handle_request(self):
conn = WebSocketConnection(self.handler, WebSocketMessageHandler())

View File

@ -6,6 +6,7 @@ from owrx.controllers import (
MapController,
FeatureController,
ApiController,
MetricsController,
)
from http.server import BaseHTTPRequestHandler
import re
@ -43,6 +44,7 @@ class Router(object):
{"route": "/map", "controller": MapController},
{"route": "/features", "controller": FeatureController},
{"route": "/api/features", "controller": ApiController},
{"route": "/metrics", "controller": MetricsController},
]
def find_controller(self, path):

32
owrx/metrics.py Normal file
View File

@ -0,0 +1,32 @@
class Metrics(object):
sharedInstance = None
@staticmethod
def getSharedInstance():
if Metrics.sharedInstance is None:
Metrics.sharedInstance = Metrics()
return Metrics.sharedInstance
def __init__(self):
self.metrics = {}
def pushDecodes(self, band, mode, count = 1):
if band is None:
band = 'unknown'
else:
band = band.getName()
if mode is None:
mode = 'unknown'
if not band in self.metrics:
self.metrics[band] = {}
if not mode in self.metrics[band]:
self.metrics[band][mode] = {
"count": 0
}
self.metrics[band][mode]["count"] += count
def getMetrics(self):
return self.metrics

View File

@ -10,6 +10,7 @@ from owrx.map import Map, LocatorLocation
import re
from owrx.config import PropertyManager
from owrx.bands import Bandplan
from owrx.metrics import Metrics
import logging
@ -228,6 +229,7 @@ class WsjtParser(object):
mode = WsjtParser.modes[modeChar] if modeChar in WsjtParser.modes else "unknown"
wsjt_msg = msg[17:53].strip()
self.parseLocator(wsjt_msg, mode)
Metrics.getSharedInstance().pushDecodes(self.band, mode)
return {
"timestamp": timestamp,
"db": float(msg[0:3]),
@ -253,6 +255,7 @@ class WsjtParser(object):
# '0052 -29 2.6 0.001486 0 G02CWT IO92 23'
wsjt_msg = msg[29:].strip()
self.parseWsprMessage(wsjt_msg)
Metrics.getSharedInstance().pushDecodes(self.band, 'WSPR')
return {
"timestamp": self.parse_timestamp(msg[0:4], "%H%M"),
"db": float(msg[5:8]),