2017-07-26 09:29:10 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"time"
|
|
|
|
|
2024-12-12 12:12:29 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
2017-07-26 11:01:15 +00:00
|
|
|
|
2019-01-27 14:25:13 +00:00
|
|
|
"github.com/Luzifer/rconfig"
|
2017-07-26 09:29:10 +00:00
|
|
|
)
|
|
|
|
|
2024-12-12 12:12:29 +00:00
|
|
|
const tsvPermission = 0o600
|
|
|
|
|
2017-07-26 09:29:10 +00:00
|
|
|
var (
|
|
|
|
cfg struct {
|
2019-01-27 14:25:13 +00:00
|
|
|
InfluxDB string `flag:"influx-db" default:"" description:"Name of the database to write to (if unset, InfluxDB feature is disabled)"`
|
|
|
|
InfluxHost string `flag:"influx-host" default:"http://localhost:8086" description:"Host with protocol of the InfluxDB"`
|
|
|
|
InfluxPass string `flag:"influx-pass" default:"" description:"Password for the InfluxDB user"`
|
|
|
|
InfluxUser string `flag:"influx-user" default:"" description:"Username for the InfluxDB connection"`
|
2021-03-06 16:42:38 +00:00
|
|
|
Interface string `flag:"interface" default:"" description:"Bind to interface for testing a specific interface throughput"`
|
2017-07-26 09:29:10 +00:00
|
|
|
Interval time.Duration `flag:"interval" default:"15m" description:"Interval to execute test in"`
|
|
|
|
LogLevel string `flag:"log-level" default:"info" description:"Set log level (debug, info, warning, error)"`
|
2019-01-27 14:25:13 +00:00
|
|
|
OneShot bool `flag:"oneshot,1" default:"false" description:"Execute one measurement and exit (for cron execution)"`
|
2017-07-26 09:29:10 +00:00
|
|
|
Port int `flag:"port" default:"7121" description:"Port the sparkyfish server is running on"`
|
2019-01-27 14:25:13 +00:00
|
|
|
Server string `flag:"server" default:"" description:"Hostname / IP of the sparkyfish server" validate:"nonzero"`
|
|
|
|
TSVFile string `flag:"tsv-file" default:"measures.tsv" description:"File to write the results to (set to empty string to disable)"`
|
2017-07-26 09:29:10 +00:00
|
|
|
VersionAndExit bool `flag:"version" default:"false" description:"Print version information and exit"`
|
|
|
|
}
|
|
|
|
|
2019-01-27 14:25:13 +00:00
|
|
|
metrics *metricsSender
|
|
|
|
|
2017-07-26 09:29:10 +00:00
|
|
|
version = "dev"
|
|
|
|
)
|
|
|
|
|
2024-12-12 12:12:29 +00:00
|
|
|
func initApp() (err error) {
|
|
|
|
if err = rconfig.ParseAndValidate(&cfg); err != nil {
|
|
|
|
return fmt.Errorf("parsing CLI params: %w", err)
|
2017-07-26 09:29:10 +00:00
|
|
|
}
|
|
|
|
|
2024-12-12 12:12:29 +00:00
|
|
|
l, err := logrus.ParseLevel(cfg.LogLevel)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("parsing log-level: %w", err)
|
2017-07-26 09:29:10 +00:00
|
|
|
}
|
2024-12-12 12:12:29 +00:00
|
|
|
logrus.SetLevel(l)
|
2017-07-26 09:29:10 +00:00
|
|
|
|
2024-12-12 12:12:29 +00:00
|
|
|
return nil
|
2017-07-26 09:29:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
2019-01-27 14:25:13 +00:00
|
|
|
var err error
|
2024-12-12 12:12:29 +00:00
|
|
|
if err = initApp(); err != nil {
|
|
|
|
logrus.WithError(err).Fatal("initializing app")
|
|
|
|
}
|
|
|
|
|
|
|
|
if cfg.VersionAndExit {
|
|
|
|
fmt.Printf("continuous-spark %s\n", version) //nolint:forbidigo
|
|
|
|
os.Exit(0)
|
|
|
|
}
|
2019-01-27 14:25:13 +00:00
|
|
|
|
|
|
|
if cfg.InfluxDB != "" {
|
2019-01-27 14:48:43 +00:00
|
|
|
if metrics, err = newMetricsSender(cfg.InfluxHost, cfg.InfluxUser, cfg.InfluxPass, cfg.InfluxDB); err != nil {
|
2024-12-12 12:12:29 +00:00
|
|
|
logrus.WithError(err).Fatalf("initializing InfluxDB sender")
|
2019-01-27 14:25:13 +00:00
|
|
|
}
|
2021-01-10 14:56:58 +00:00
|
|
|
|
|
|
|
go func() {
|
|
|
|
for err := range metrics.Errors() {
|
2024-12-12 12:12:29 +00:00
|
|
|
logrus.WithError(err).Error("transmitting metrics")
|
2021-01-10 14:56:58 +00:00
|
|
|
}
|
|
|
|
}()
|
2019-01-27 14:25:13 +00:00
|
|
|
}
|
|
|
|
|
2017-07-26 11:01:15 +00:00
|
|
|
if err := updateStats(execTest()); err != nil {
|
2024-12-12 12:12:29 +00:00
|
|
|
logrus.WithError(err).Error("updating stats")
|
2019-01-27 14:25:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if cfg.OneShot {
|
|
|
|
// Return before loop for oneshot execution
|
2020-11-30 19:49:05 +00:00
|
|
|
if metrics != nil {
|
|
|
|
if err := metrics.ForceTransmit(); err != nil {
|
2024-12-12 12:12:29 +00:00
|
|
|
logrus.WithError(err).Error("storing metrics")
|
2020-11-30 19:49:05 +00:00
|
|
|
}
|
2019-01-27 14:25:13 +00:00
|
|
|
}
|
|
|
|
return
|
2017-07-26 11:01:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for range time.Tick(cfg.Interval) {
|
2017-07-26 09:29:10 +00:00
|
|
|
if err := updateStats(execTest()); err != nil {
|
2024-12-12 12:12:29 +00:00
|
|
|
logrus.WithError(err).Error("updating stats")
|
2017-07-26 09:29:10 +00:00
|
|
|
}
|
2017-07-26 11:01:15 +00:00
|
|
|
}
|
2017-07-26 09:29:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func updateStats(t *testResult, err error) error {
|
|
|
|
if err != nil {
|
2024-12-12 12:12:29 +00:00
|
|
|
return err
|
2017-07-26 09:29:10 +00:00
|
|
|
}
|
|
|
|
|
2019-01-27 14:25:13 +00:00
|
|
|
hostname, err := os.Hostname()
|
|
|
|
if err != nil {
|
2024-12-12 12:12:29 +00:00
|
|
|
return fmt.Errorf("getting hostname: %w", err)
|
2019-01-27 14:25:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if metrics != nil {
|
|
|
|
if err := metrics.RecordPoint(
|
|
|
|
"sparkyfish_ping",
|
|
|
|
map[string]string{
|
|
|
|
"hostname": hostname,
|
|
|
|
"server": cfg.Server,
|
|
|
|
},
|
|
|
|
map[string]interface{}{
|
|
|
|
"avg": t.Ping.Avg,
|
|
|
|
"min": t.Ping.Min,
|
|
|
|
"max": t.Ping.Max,
|
|
|
|
"dev": t.Ping.Dev,
|
|
|
|
},
|
|
|
|
); err != nil {
|
2024-12-12 12:12:29 +00:00
|
|
|
return fmt.Errorf("recording ping-metric: %w", err)
|
2019-01-27 14:25:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := metrics.RecordPoint(
|
|
|
|
"sparkyfish_transfer",
|
|
|
|
map[string]string{
|
|
|
|
"direction": "down",
|
|
|
|
"hostname": hostname,
|
|
|
|
"server": cfg.Server,
|
|
|
|
},
|
|
|
|
map[string]interface{}{
|
|
|
|
"avg": t.Receive.Avg,
|
|
|
|
"min": t.Receive.Min,
|
|
|
|
"max": t.Receive.Max,
|
|
|
|
},
|
|
|
|
); err != nil {
|
2024-12-12 12:12:29 +00:00
|
|
|
return fmt.Errorf("recording down-metric: %w", err)
|
2019-01-27 14:25:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := metrics.RecordPoint(
|
|
|
|
"sparkyfish_transfer",
|
|
|
|
map[string]string{
|
|
|
|
"direction": "up",
|
|
|
|
"hostname": hostname,
|
|
|
|
"server": cfg.Server,
|
|
|
|
},
|
|
|
|
map[string]interface{}{
|
|
|
|
"avg": t.Send.Avg,
|
|
|
|
"min": t.Send.Min,
|
|
|
|
"max": t.Send.Max,
|
|
|
|
},
|
|
|
|
); err != nil {
|
2024-12-12 12:12:29 +00:00
|
|
|
return fmt.Errorf("recording up-metric: %w", err)
|
2019-01-27 14:25:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if cfg.TSVFile != "" {
|
|
|
|
if err := writeTSV(t); err != nil {
|
2024-12-12 12:12:29 +00:00
|
|
|
return fmt.Errorf("writing TSV file: %w", err)
|
2019-01-27 14:25:13 +00:00
|
|
|
}
|
|
|
|
}
|
2017-07-26 09:29:10 +00:00
|
|
|
|
2019-01-27 14:25:13 +00:00
|
|
|
return nil
|
2017-07-28 05:13:51 +00:00
|
|
|
}
|
|
|
|
|
2024-12-12 12:12:29 +00:00
|
|
|
func writeTSV(t *testResult) (err error) {
|
|
|
|
if _, err = os.Stat(cfg.TSVFile); err != nil && os.IsNotExist(err) {
|
|
|
|
if err = os.WriteFile(cfg.TSVFile, []byte("Date\tPing Min (ms)\tPing Avg (ms)\tPing Max (ms)\tPing StdDev (ms)\tRX Avg (bps)\tTX Avg (bps)\n"), tsvPermission); err != nil {
|
|
|
|
return fmt.Errorf("writing TSV headers: %w", err)
|
2017-07-28 05:13:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
f, err := os.OpenFile(cfg.TSVFile, os.O_APPEND|os.O_WRONLY, os.ModeAppend)
|
|
|
|
if err != nil {
|
2024-12-12 12:12:29 +00:00
|
|
|
return fmt.Errorf("opening TSV file: %w", err)
|
2017-07-28 05:13:51 +00:00
|
|
|
}
|
2024-12-12 12:12:29 +00:00
|
|
|
defer func() {
|
|
|
|
if err := f.Close(); err != nil {
|
|
|
|
logrus.WithError(err).Error("closing TSV file (leaked fd)")
|
|
|
|
}
|
|
|
|
}()
|
2017-07-28 05:13:51 +00:00
|
|
|
|
2024-12-12 12:12:29 +00:00
|
|
|
if _, err = fmt.Fprintf(f, "%s\t%.2f\t%.2f\t%.2f\t%.2f\t%.0f\t%.0f\n",
|
2017-07-28 05:13:51 +00:00
|
|
|
time.Now().Format(time.RFC3339),
|
|
|
|
t.Ping.Min,
|
|
|
|
t.Ping.Avg,
|
|
|
|
t.Ping.Max,
|
|
|
|
t.Ping.Dev,
|
|
|
|
t.Receive.Avg,
|
|
|
|
t.Send.Avg,
|
2024-12-12 12:12:29 +00:00
|
|
|
); err != nil {
|
|
|
|
return fmt.Errorf("writing measurement: %w", err)
|
|
|
|
}
|
2017-07-28 05:13:51 +00:00
|
|
|
|
2024-12-12 12:12:29 +00:00
|
|
|
return nil
|
2017-07-26 09:29:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func execTest() (*testResult, error) {
|
|
|
|
t := newTestResult()
|
|
|
|
|
2021-03-06 16:42:38 +00:00
|
|
|
sc := newSparkClient(cfg.Server, cfg.Port, cfg.Interface)
|
2017-07-26 09:29:10 +00:00
|
|
|
if err := sc.ExecutePingTest(t); err != nil {
|
2024-12-12 12:12:29 +00:00
|
|
|
return nil, fmt.Errorf("executing ping-test: %w", err)
|
2017-07-26 09:29:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := sc.ExecuteThroughputTest(t); err != nil {
|
2024-12-12 12:12:29 +00:00
|
|
|
return nil, fmt.Errorf("executing throughput-test: %w", err)
|
2017-07-26 09:29:10 +00:00
|
|
|
}
|
|
|
|
|
2024-12-12 12:12:29 +00:00
|
|
|
logrus.Debugf("%s", t)
|
2017-07-26 09:29:10 +00:00
|
|
|
return t, nil
|
|
|
|
}
|