2021-01-09 14:03:37 -06:00
|
|
|
#!/usr/bin/python3
|
|
|
|
######################################################################
|
|
|
|
#
|
|
|
|
# Write Starlink user terminal packet loss statistics to an InfluxDB
|
|
|
|
# database.
|
|
|
|
#
|
|
|
|
# This script examines the most recent samples from the history data,
|
|
|
|
# computes several different metrics related to packet loss, and
|
|
|
|
# writes those to the specified InfluxDB database.
|
|
|
|
#
|
|
|
|
######################################################################
|
|
|
|
|
|
|
|
import datetime
|
2021-01-10 23:36:44 -06:00
|
|
|
import os
|
2021-01-09 14:03:37 -06:00
|
|
|
import sys
|
|
|
|
import getopt
|
|
|
|
|
2021-01-10 23:36:44 -06:00
|
|
|
import warnings
|
2021-01-09 14:03:37 -06:00
|
|
|
from influxdb import InfluxDBClient
|
|
|
|
|
|
|
|
import starlink_grpc
|
|
|
|
|
|
|
|
arg_error = False
|
|
|
|
|
|
|
|
try:
|
2021-01-10 23:36:44 -06:00
|
|
|
opts, args = getopt.getopt(sys.argv[1:], "ahn:p:rs:vC:D:IP:R:SU:")
|
2021-01-09 14:03:37 -06:00
|
|
|
except getopt.GetoptError as err:
|
|
|
|
print(str(err))
|
|
|
|
arg_error = True
|
|
|
|
|
|
|
|
# Default to 1 hour worth of data samples.
|
|
|
|
samples_default = 3600
|
|
|
|
samples = samples_default
|
|
|
|
print_usage = False
|
|
|
|
verbose = False
|
|
|
|
run_lengths = False
|
|
|
|
host_default = "localhost"
|
2021-01-11 15:03:19 -06:00
|
|
|
database_default = "starlinkstats"
|
2021-01-09 14:03:37 -06:00
|
|
|
icargs = {"host": host_default, "timeout": 5, "database": database_default}
|
|
|
|
rp = None
|
|
|
|
|
2021-01-10 23:36:44 -06:00
|
|
|
# For each of these check they are both set and not empty string
|
|
|
|
influxdb_host = os.environ.get("INFLUXDB_HOST")
|
|
|
|
if influxdb_host:
|
|
|
|
icargs["host"] = influxdb_host
|
|
|
|
influxdb_port = os.environ.get("INFLUXDB_PORT")
|
|
|
|
if influxdb_port:
|
|
|
|
icargs["port"] = int(influxdb_port)
|
|
|
|
influxdb_user = os.environ.get("INFLUXDB_USER")
|
|
|
|
if influxdb_user:
|
|
|
|
icargs["username"] = influxdb_user
|
|
|
|
influxdb_pwd = os.environ.get("INFLUXDB_PWD")
|
|
|
|
if influxdb_pwd:
|
|
|
|
icargs["password"] = influxdb_pwd
|
|
|
|
influxdb_db = os.environ.get("INFLUXDB_DB")
|
|
|
|
if influxdb_db:
|
|
|
|
icargs["database"] = influxdb_db
|
|
|
|
influxdb_rp = os.environ.get("INFLUXDB_RP")
|
|
|
|
if influxdb_rp:
|
|
|
|
rp = influxdb_rp
|
|
|
|
influxdb_ssl = os.environ.get("INFLUXDB_SSL")
|
|
|
|
if influxdb_ssl:
|
|
|
|
icargs["ssl"] = True
|
|
|
|
if influxdb_ssl.lower() == "secure":
|
|
|
|
icargs["verify_ssl"] = True
|
|
|
|
elif influxdb_ssl.lower() == "insecure":
|
|
|
|
icargs["verify_ssl"] = False
|
|
|
|
else:
|
|
|
|
icargs["verify_ssl"] = influxdb_ssl
|
|
|
|
|
2021-01-09 14:03:37 -06:00
|
|
|
if not arg_error:
|
|
|
|
if len(args) > 0:
|
|
|
|
arg_error = True
|
|
|
|
else:
|
|
|
|
for opt, arg in opts:
|
|
|
|
if opt == "-a":
|
|
|
|
samples = -1
|
|
|
|
elif opt == "-h":
|
|
|
|
print_usage = True
|
|
|
|
elif opt == "-n":
|
|
|
|
icargs["host"] = arg
|
|
|
|
elif opt == "-p":
|
|
|
|
icargs["port"] = int(arg)
|
|
|
|
elif opt == "-r":
|
|
|
|
run_lengths = True
|
|
|
|
elif opt == "-s":
|
|
|
|
samples = int(arg)
|
|
|
|
elif opt == "-v":
|
|
|
|
verbose = True
|
2021-01-10 23:36:44 -06:00
|
|
|
elif opt == "-C":
|
|
|
|
icargs["ssl"] = True
|
|
|
|
icargs["verify_ssl"] = arg
|
2021-01-09 14:03:37 -06:00
|
|
|
elif opt == "-D":
|
|
|
|
icargs["database"] = arg
|
2021-01-10 23:36:44 -06:00
|
|
|
elif opt == "-I":
|
|
|
|
icargs["ssl"] = True
|
|
|
|
icargs["verify_ssl"] = False
|
2021-01-09 14:03:37 -06:00
|
|
|
elif opt == "-P":
|
|
|
|
icargs["password"] = arg
|
|
|
|
elif opt == "-R":
|
|
|
|
rp = arg
|
2021-01-10 23:36:44 -06:00
|
|
|
elif opt == "-S":
|
|
|
|
icargs["ssl"] = True
|
|
|
|
icargs["verify_ssl"] = True
|
2021-01-09 14:03:37 -06:00
|
|
|
elif opt == "-U":
|
|
|
|
icargs["username"] = arg
|
|
|
|
|
|
|
|
if "password" in icargs and "username" not in icargs:
|
|
|
|
print("Password authentication requires username to be set")
|
|
|
|
arg_error = True
|
|
|
|
|
|
|
|
if print_usage or arg_error:
|
|
|
|
print("Usage: " + sys.argv[0] + " [options...]")
|
|
|
|
print("Options:")
|
|
|
|
print(" -a: Parse all valid samples")
|
|
|
|
print(" -h: Be helpful")
|
|
|
|
print(" -n <name>: Hostname of InfluxDB server, default: " + host_default)
|
|
|
|
print(" -p <num>: Port number to use on InfluxDB server")
|
|
|
|
print(" -r: Include ping drop run length stats")
|
|
|
|
print(" -s <num>: Number of data samples to parse, default: " + str(samples_default))
|
|
|
|
print(" -v: Be verbose")
|
2021-01-10 23:36:44 -06:00
|
|
|
print(" -C <filename>: Enable SSL/TLS using specified CA cert to verify server")
|
2021-01-09 14:03:37 -06:00
|
|
|
print(" -D <name>: Database name to use, default: " + database_default)
|
2021-01-10 23:36:44 -06:00
|
|
|
print(" -I: Enable SSL/TLS but disable certificate verification (INSECURE!)")
|
2021-01-09 14:03:37 -06:00
|
|
|
print(" -P <word>: Set password for authentication")
|
|
|
|
print(" -R <name>: Retention policy name to use")
|
2021-01-10 23:36:44 -06:00
|
|
|
print(" -S: Enable SSL/TLS using default CA cert")
|
2021-01-09 14:03:37 -06:00
|
|
|
print(" -U <name>: Set username for authentication")
|
|
|
|
sys.exit(1 if arg_error else 0)
|
|
|
|
|
|
|
|
dish_id = starlink_grpc.get_id()
|
|
|
|
|
|
|
|
if dish_id is None:
|
|
|
|
if verbose:
|
|
|
|
print("Unable to connect to Starlink user terminal")
|
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
timestamp = datetime.datetime.utcnow()
|
|
|
|
|
|
|
|
g_stats, pd_stats, rl_stats = starlink_grpc.history_ping_stats(samples, verbose)
|
|
|
|
|
|
|
|
if g_stats is None:
|
|
|
|
# verbose output already happened, so just bail.
|
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
all_stats = g_stats.copy()
|
|
|
|
all_stats.update(pd_stats)
|
|
|
|
if run_lengths:
|
|
|
|
for k, v in rl_stats.items():
|
|
|
|
if k.startswith("run_"):
|
|
|
|
for i, subv in enumerate(v, start=1):
|
|
|
|
all_stats[k + "_" + str(i)] = subv
|
|
|
|
else:
|
|
|
|
all_stats[k] = v
|
|
|
|
|
|
|
|
points = [{
|
|
|
|
"measurement": "spacex.starlink.user_terminal.ping_stats",
|
|
|
|
"tags": {"id": dish_id},
|
|
|
|
"time": timestamp,
|
|
|
|
"fields": all_stats,
|
|
|
|
}]
|
|
|
|
|
2021-01-10 23:36:44 -06:00
|
|
|
if "verify_ssl" in icargs and not icargs["verify_ssl"]:
|
|
|
|
# user has explicitly said be insecure, so don't warn about it
|
|
|
|
warnings.filterwarnings("ignore", message="Unverified HTTPS request")
|
|
|
|
|
2021-01-09 14:03:37 -06:00
|
|
|
influx_client = InfluxDBClient(**icargs)
|
|
|
|
try:
|
|
|
|
influx_client.write_points(points, retention_policy=rp)
|
2021-01-10 23:36:44 -06:00
|
|
|
rc = 0
|
|
|
|
except Exception as e:
|
|
|
|
print("Failed writing to InfluxDB database: " + str(e))
|
|
|
|
rc = 1
|
2021-01-09 14:03:37 -06:00
|
|
|
finally:
|
|
|
|
influx_client.close()
|
2021-01-10 23:36:44 -06:00
|
|
|
sys.exit(rc)
|