From c313134ba18b9fad4a37bbb1f34f7f73b25f9fcb Mon Sep 17 00:00:00 2001 From: sparky8512 <76499194+sparky8512@users.noreply.github.com> Date: Tue, 29 Dec 2020 19:21:00 -0800 Subject: [PATCH] Scripts for CSV, InfluxDB, MQTT get_status output Initial support for dumping status info in CSV format or to InfuxDB or MQTT servers. InfluxDB script is largely what Equinox- posted into issue #2, but with a few things renamed, and the state info being recorded as a string of the enum value name, rather than the (integer) enum value. All of these need header comments still and some need a bit more options handling, cleanup, and error handling. --- dishStatusCsv.py | 25 +++++++++++++++ dishStatusInflux.py | 75 +++++++++++++++++++++++++++++++++++++++++++++ dishStatusMqtt.py | 42 +++++++++++++++++++++++++ 3 files changed, 142 insertions(+) create mode 100644 dishStatusCsv.py create mode 100644 dishStatusInflux.py create mode 100644 dishStatusMqtt.py diff --git a/dishStatusCsv.py b/dishStatusCsv.py new file mode 100644 index 0000000..0a32ee8 --- /dev/null +++ b/dishStatusCsv.py @@ -0,0 +1,25 @@ +#!/usr/bin/python3 +import grpc + +import spacex.api.device.device_pb2 +import spacex.api.device.device_pb2_grpc + +with grpc.insecure_channel('192.168.100.1:9200') as channel: + stub = spacex.api.device.device_pb2_grpc.DeviceStub(channel) + response = stub.Handle(spacex.api.device.device_pb2.Request(get_status={})) + +status = response.dish_get_status + +# More alerts may be added in future, so rather than list them individually, +# build a bit field based on field numbers of the DishAlerts message. +alert_bits = 0 +for alert in status.alerts.ListFields(): + alert_bits |= (1 if alert[1] else 0) << (alert[0].number - 1) + +print(",".join([status.device_info.id, status.device_info.hardware_version, status.device_info.software_version, + spacex.api.device.dish_pb2.DishState.Name(status.state)]) + "," + + ",".join(str(x) for x in [status.device_state.uptime_s, status.snr, status.seconds_to_first_nonempty_slot, + status.pop_ping_drop_rate, status.downlink_throughput_bps, status.uplink_throughput_bps, + status.pop_ping_latency_ms, alert_bits, status.obstruction_stats.fraction_obstructed, + status.obstruction_stats.currently_obstructed, status.obstruction_stats.last_24h_obstructed_s]) + "," + + ",".join(str(x) for x in status.obstruction_stats.wedge_abs_fraction_obstructed)) diff --git a/dishStatusInflux.py b/dishStatusInflux.py new file mode 100644 index 0000000..7afa3f9 --- /dev/null +++ b/dishStatusInflux.py @@ -0,0 +1,75 @@ +#!/usr/bin/python3 +from influxdb import InfluxDBClient +from influxdb import SeriesHelper + +import grpc + +import spacex.api.device.device_pb2 +import spacex.api.device.device_pb2_grpc + +import time + +class DeviceStatusSeries(SeriesHelper): + class Meta: + series_name = "spacex.starlink.user_terminal.status" + fields = [ + "hardware_version", + "software_version", + "state", + "alert_motors_stuck", + "alert_thermal_throttle", + "alert_thermal_shutdown", + "alert_unexpected_location", + "snr", + "seconds_to_first_nonempty_slot", + "pop_ping_drop_rate", + "downlink_throughput_bps", + "uplink_throughput_bps", + "pop_ping_latency_ms", + "currently_obstructed", + "fraction_obstructed"] + tags = ["id"] + +influxClient = InfluxDBClient("localhost", 8086, "script-user", "password", "dishstats", ssl=False, retries=1, timeout=15) + +try: + dishChannel = grpc.insecure_channel("192.168.100.1:9200") + + pending = 0 + count = 0 + while True: + stub = spacex.api.device.device_pb2_grpc.DeviceStub(dishChannel) + response = stub.Handle(spacex.api.device.device_pb2.Request(get_status={})) + status = response.dish_get_status + DeviceStatusSeries( + id=status.device_info.id, + hardware_version=status.device_info.hardware_version, + software_version=status.device_info.software_version, + state=status.state, + alert_motors_stuck=status.alerts.motors_stuck, + alert_thermal_throttle=status.alerts.thermal_throttle, + alert_thermal_shutdown=status.alerts.thermal_shutdown, + alert_unexpected_location=status.alerts.unexpected_location, + snr=status.snr, + seconds_to_first_nonempty_slot=status.seconds_to_first_nonempty_slot, + pop_ping_drop_rate=status.pop_ping_drop_rate, + downlink_throughput_bps=status.downlink_throughput_bps, + uplink_throughput_bps=status.uplink_throughput_bps, + pop_ping_latency_ms=status.pop_ping_latency_ms, + currently_obstructed=status.obstruction_stats.currently_obstructed, + fraction_obstructed=status.obstruction_stats.fraction_obstructed) + pending = pending + 1 + print("Samples: " + str(pending)) + if count > 5: + try: + DeviceStatusSeries.commit(influxClient) + print("Wrote " + str(pending)) + pending = 0 + except Exception as e: + print("Failed to write: " + str(e)) + count = 0 + count = count + 1 + time.sleep(5) +finally: + # Flush on error/exit + DeviceStatusSeries.commit(influxClient) diff --git a/dishStatusMqtt.py b/dishStatusMqtt.py new file mode 100644 index 0000000..9b2c52c --- /dev/null +++ b/dishStatusMqtt.py @@ -0,0 +1,42 @@ +#!/usr/bin/python3 +import paho.mqtt.publish + +import grpc + +import spacex.api.device.device_pb2 +import spacex.api.device.device_pb2_grpc + +with grpc.insecure_channel('192.168.100.1:9200') as channel: + stub = spacex.api.device.device_pb2_grpc.DeviceStub(channel) + response = stub.Handle(spacex.api.device.device_pb2.Request(get_status={})) + +status = response.dish_get_status + +# More alerts may be added in future, so rather than list them individually, +# build a bit field based on field numbers of the DishAlerts message. +alert_bits = 0 +for alert in status.alerts.ListFields(): + alert_bits |= (1 if alert[1] else 0) << (alert[0].number - 1) + +topicPrefix = "starlink/dish_status/" + status.device_info.id + "/" +msgs = [(topicPrefix + "hardware_version", status.device_info.hardware_version, 0, False), + (topicPrefix + "software_version", status.device_info.software_version, 0, False), + (topicPrefix + "state", spacex.api.device.dish_pb2.DishState.Name(status.state), 0, False), + (topicPrefix + "uptime", status.device_state.uptime_s, 0, False), + (topicPrefix + "snr", status.snr, 0, False), + (topicPrefix + "seconds_to_first_nonempty_slot", status.seconds_to_first_nonempty_slot, 0, False), + (topicPrefix + "pop_ping_drop_rate", status.pop_ping_drop_rate, 0, False), + (topicPrefix + "downlink_throughput_bps", status.downlink_throughput_bps, 0, False), + (topicPrefix + "uplink_throughput_bps", status.uplink_throughput_bps, 0, False), + (topicPrefix + "pop_ping_latency_ms", status.pop_ping_latency_ms, 0, False), + (topicPrefix + "alerts", alert_bits, 0, False), + (topicPrefix + "fraction_obstructed", status.obstruction_stats.fraction_obstructed, 0, False), + (topicPrefix + "currently_obstructed", status.obstruction_stats.currently_obstructed, 0, False), + # While the field name for this one implies it covers 24 hours, the + # empirical evidence suggests it only covers 12 hours. It also resets + # on dish reboot, so may not cover that whole period. Rather than try + # to convey that complexity in the topic label, just be a bit vague: + (topicPrefix + "seconds_obstructed", status.obstruction_stats.last_24h_obstructed_s, 0, False), + (topicPrefix + "wedges_fraction_obstructed", ",".join(str(x) for x in status.obstruction_stats.wedge_abs_fraction_obstructed), 0, False)] + +paho.mqtt.publish.multiple(msgs, hostname="localhost", client_id=status.device_info.id)