1
0
Fork 0
mirror of https://github.com/Luzifer/continuous-spark.git synced 2024-12-20 17:51:22 +00:00
continuous-spark/main.go

210 lines
5.6 KiB
Go
Raw Normal View History

package main
import (
"fmt"
"os"
"time"
2024-12-12 12:12:29 +00:00
"github.com/sirupsen/logrus"
"github.com/Luzifer/rconfig"
)
2024-12-12 12:12:29 +00:00
const tsvPermission = 0o600
var (
cfg struct {
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"`
Interface string `flag:"interface" default:"" description:"Bind to interface for testing a specific interface throughput"`
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)"`
OneShot bool `flag:"oneshot,1" default:"false" description:"Execute one measurement and exit (for cron execution)"`
Port int `flag:"port" default:"7121" description:"Port the sparkyfish server is running on"`
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)"`
VersionAndExit bool `flag:"version" default:"false" description:"Print version information and exit"`
}
metrics *metricsSender
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)
}
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)
}
2024-12-12 12:12:29 +00:00
logrus.SetLevel(l)
2024-12-12 12:12:29 +00:00
return nil
}
func main() {
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)
}
if cfg.InfluxDB != "" {
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")
}
go func() {
for err := range metrics.Errors() {
2024-12-12 12:12:29 +00:00
logrus.WithError(err).Error("transmitting metrics")
}
}()
}
if err := updateStats(execTest()); err != nil {
2024-12-12 12:12:29 +00:00
logrus.WithError(err).Error("updating stats")
}
if cfg.OneShot {
// Return before loop for oneshot execution
if metrics != nil {
if err := metrics.ForceTransmit(); err != nil {
2024-12-12 12:12:29 +00:00
logrus.WithError(err).Error("storing metrics")
}
}
return
}
for range time.Tick(cfg.Interval) {
if err := updateStats(execTest()); err != nil {
2024-12-12 12:12:29 +00:00
logrus.WithError(err).Error("updating stats")
}
}
}
func updateStats(t *testResult, err error) error {
if err != nil {
2024-12-12 12:12:29 +00:00
return err
}
hostname, err := os.Hostname()
if err != nil {
2024-12-12 12:12:29 +00:00
return fmt.Errorf("getting hostname: %w", err)
}
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)
}
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)
}
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)
}
}
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)
}
}
return nil
}
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)
}
}
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)
}
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)")
}
}()
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",
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)
}
2024-12-12 12:12:29 +00:00
return nil
}
func execTest() (*testResult, error) {
t := newTestResult()
sc := newSparkClient(cfg.Server, cfg.Port, cfg.Interface)
if err := sc.ExecutePingTest(t); err != nil {
2024-12-12 12:12:29 +00:00
return nil, fmt.Errorf("executing ping-test: %w", err)
}
if err := sc.ExecuteThroughputTest(t); err != nil {
2024-12-12 12:12:29 +00:00
return nil, fmt.Errorf("executing throughput-test: %w", err)
}
2024-12-12 12:12:29 +00:00
logrus.Debugf("%s", t)
return t, nil
}