2020-12-29 21:21:00 -06:00
|
|
|
#!/usr/bin/python3
|
2020-12-29 22:45:30 -06:00
|
|
|
######################################################################
|
|
|
|
#
|
|
|
|
# Write get_status info to an InfluxDB database.
|
|
|
|
#
|
|
|
|
# This script will periodically poll current status and write it to
|
|
|
|
# the specified InfluxDB database in a loop.
|
|
|
|
#
|
|
|
|
######################################################################
|
2021-01-06 12:12:56 -06:00
|
|
|
import time
|
|
|
|
|
2020-12-29 21:21:00 -06:00
|
|
|
from influxdb import InfluxDBClient
|
|
|
|
from influxdb import SeriesHelper
|
|
|
|
|
|
|
|
import grpc
|
|
|
|
|
|
|
|
import spacex.api.device.device_pb2
|
|
|
|
import spacex.api.device.device_pb2_grpc
|
|
|
|
|
2021-01-06 12:12:56 -06:00
|
|
|
verbose = True
|
|
|
|
sleep_time = 30
|
2020-12-30 12:17:02 -06:00
|
|
|
|
2020-12-29 21:21:00 -06:00
|
|
|
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"]
|
|
|
|
|
2021-01-06 12:12:56 -06:00
|
|
|
influx_client = InfluxDBClient(host="localhost", port=8086, username="script-user", password="password", database="dishstats", ssl=False, retries=1, timeout=15)
|
2020-12-29 21:21:00 -06:00
|
|
|
|
|
|
|
try:
|
2021-01-06 12:12:56 -06:00
|
|
|
dish_channel = None
|
|
|
|
last_id = None
|
|
|
|
last_failed = False
|
2020-12-29 21:21:00 -06:00
|
|
|
|
|
|
|
pending = 0
|
|
|
|
count = 0
|
|
|
|
while True:
|
2020-12-30 12:17:02 -06:00
|
|
|
try:
|
2021-01-06 12:12:56 -06:00
|
|
|
if dish_channel is None:
|
|
|
|
dish_channel = grpc.insecure_channel("192.168.100.1:9200")
|
|
|
|
stub = spacex.api.device.device_pb2_grpc.DeviceStub(dish_channel)
|
2020-12-30 12:17:02 -06:00
|
|
|
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=spacex.api.device.dish_pb2.DishState.Name(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)
|
2021-01-06 12:12:56 -06:00
|
|
|
last_id = status.device_info.id
|
|
|
|
last_failed = False
|
|
|
|
except grpc.RpcError:
|
|
|
|
if dish_channel is not None:
|
|
|
|
dish_channel.close()
|
|
|
|
dish_channel = None
|
|
|
|
if last_failed:
|
|
|
|
if last_id is not None:
|
|
|
|
DeviceStatusSeries(id=last_id, state="DISH_UNREACHABLE")
|
2020-12-30 12:17:02 -06:00
|
|
|
else:
|
|
|
|
# Retry once, because the connection may have been lost while
|
|
|
|
# we were sleeping
|
2021-01-06 12:12:56 -06:00
|
|
|
last_failed = True
|
2020-12-30 12:17:02 -06:00
|
|
|
continue
|
2020-12-29 21:21:00 -06:00
|
|
|
pending = pending + 1
|
2021-01-06 12:12:56 -06:00
|
|
|
if verbose:
|
2020-12-30 12:17:02 -06:00
|
|
|
print("Samples: " + str(pending))
|
|
|
|
count = count + 1
|
2020-12-29 21:21:00 -06:00
|
|
|
if count > 5:
|
|
|
|
try:
|
2021-01-06 12:12:56 -06:00
|
|
|
DeviceStatusSeries.commit(influx_client)
|
|
|
|
if verbose:
|
2020-12-30 12:17:02 -06:00
|
|
|
print("Wrote " + str(pending))
|
2020-12-29 21:21:00 -06:00
|
|
|
pending = 0
|
|
|
|
except Exception as e:
|
|
|
|
print("Failed to write: " + str(e))
|
|
|
|
count = 0
|
2021-01-06 12:12:56 -06:00
|
|
|
if sleep_time > 0:
|
|
|
|
time.sleep(sleep_time)
|
2020-12-30 12:17:02 -06:00
|
|
|
else:
|
|
|
|
break
|
2020-12-29 21:21:00 -06:00
|
|
|
finally:
|
|
|
|
# Flush on error/exit
|
2020-12-30 12:17:02 -06:00
|
|
|
try:
|
2021-01-06 12:12:56 -06:00
|
|
|
DeviceStatusSeries.commit(influx_client)
|
|
|
|
if verbose:
|
2020-12-30 12:17:02 -06:00
|
|
|
print("Wrote " + str(pending))
|
|
|
|
except Exception as e:
|
|
|
|
print("Failed to write: " + str(e))
|
2021-01-06 12:12:56 -06:00
|
|
|
influx_client.close()
|
|
|
|
if dish_channel is not None:
|
|
|
|
dish_channel.close()
|