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

Add source interface selection

Signed-off-by: Knut Ahlers <knut@ahlers.me>
This commit is contained in:
Knut Ahlers 2021-03-06 17:42:38 +01:00
parent fe7f6ac428
commit ad2b39d4b9
Signed by: luzifer
GPG key ID: 0066F03ED215AD7D
2 changed files with 31 additions and 7 deletions

View file

@ -18,6 +18,7 @@ var (
InfluxHost string `flag:"influx-host" default:"http://localhost:8086" description:"Host with protocol of the InfluxDB"` 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"` InfluxPass string `flag:"influx-pass" default:"" description:"Password for the InfluxDB user"`
InfluxUser string `flag:"influx-user" default:"" description:"Username for the InfluxDB connection"` 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"` 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)"` 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)"` OneShot bool `flag:"oneshot,1" default:"false" description:"Execute one measurement and exit (for cron execution)"`
@ -183,7 +184,7 @@ func writeTSV(t *testResult) error {
func execTest() (*testResult, error) { func execTest() (*testResult, error) {
t := newTestResult() t := newTestResult()
sc := newSparkClient(cfg.Server, cfg.Port) sc := newSparkClient(cfg.Server, cfg.Port, cfg.Interface)
if err := sc.ExecutePingTest(t); err != nil { if err := sc.ExecutePingTest(t); err != nil {
return nil, errors.Wrap(err, "Ping-test failed") return nil, errors.Wrap(err, "Ping-test failed")
} }

View file

@ -61,19 +61,42 @@ func (t testResult) String() string {
} }
type sparkClient struct { type sparkClient struct {
remote string bindInterfaceName string
conn net.Conn remote string
reader *bufio.Reader conn net.Conn
reader *bufio.Reader
} }
func newSparkClient(hostname string, port int) *sparkClient { func newSparkClient(hostname string, port int, bindInterfaceName string) *sparkClient {
return &sparkClient{ return &sparkClient{
remote: fmt.Sprintf("%s:%d", hostname, port), bindInterfaceName: bindInterfaceName,
remote: fmt.Sprintf("%s:%d", hostname, port),
} }
} }
func (s *sparkClient) dial() error { func (s *sparkClient) dial() error {
c, err := net.Dial("tcp", s.remote) d := net.Dialer{}
if s.bindInterfaceName != "" {
iface, err := net.InterfaceByName(s.bindInterfaceName)
if err != nil {
return errors.Wrap(err, "select interface")
}
addrs, err := iface.Addrs()
if err != nil {
return errors.Wrap(err, "get interface IPs")
}
if len(addrs) == 0 {
return errors.New("no addresses found on interface")
}
d.LocalAddr = &net.TCPAddr{IP: addrs[0].(*net.IPNet).IP}
log.WithField("ip", d.LocalAddr).Warn("Set local address")
}
c, err := d.Dial("tcp", s.remote)
if err != nil { if err != nil {
return errors.Wrap(err, "Unable to dial") return errors.Wrap(err, "Unable to dial")
} }