From ec94c55c232e5c298787920cd00f2e680bdea535 Mon Sep 17 00:00:00 2001 From: Dominik Menke Date: Thu, 18 Mar 2021 18:38:20 +0100 Subject: [PATCH 1/2] cleanup: remove Gopkg configuration This project uses Go modules. --- Gopkg.lock | 186 ----------------------------------------------------- Gopkg.toml | 34 ---------- 2 files changed, 220 deletions(-) delete mode 100644 Gopkg.lock delete mode 100644 Gopkg.toml diff --git a/Gopkg.lock b/Gopkg.lock deleted file mode 100644 index 86fa97a..0000000 --- a/Gopkg.lock +++ /dev/null @@ -1,186 +0,0 @@ -# This file is autogenerated, do not edit; changes may be undone by the next 'dep ensure'. - - -[[projects]] - branch = "master" - digest = "1:a74730e052a45a3fab1d310fdef2ec17ae3d6af16228421e238320846f2aaec8" - name = "github.com/alecthomas/template" - packages = [ - ".", - "parse", - ] - pruneopts = "" - revision = "a0175ee3bccc567396460bf5acd36800cb10c49c" - -[[projects]] - branch = "master" - digest = "1:8483994d21404c8a1d489f6be756e25bfccd3b45d65821f25695577791a08e68" - name = "github.com/alecthomas/units" - packages = ["."] - pruneopts = "" - revision = "2efee857e7cfd4f3d0138cc3cbb1b4966962b93a" - -[[projects]] - branch = "master" - digest = "1:c0bec5f9b98d0bc872ff5e834fac186b807b656683bd29cb82fb207a1513fabb" - name = "github.com/beorn7/perks" - packages = ["quantile"] - pruneopts = "" - revision = "3a771d992973f24aa725d07868b467d1ddfceafb" - -[[projects]] - digest = "1:cdb525fba7b35d9f023bfe55796b9038e349ce600c40279bf9c3c02cf7415b47" - name = "github.com/czerwonk/ping_exporter" - packages = ["config"] - pruneopts = "" - revision = "eed8f09ace8bb067f689ea69adb2ab680b184bbf" - version = "0.44" - -[[projects]] - branch = "master" - digest = "1:b65e8acfacb6222c6d6d27e5ad0c40dcc1338846040bec6c241684c825cbf1da" - name = "github.com/digineo/go-ping" - packages = [ - ".", - "monitor", - ] - pruneopts = "" - revision = "8e2c4a96fb5c8726040fb451f46ec1cb4f137d57" - -[[projects]] - digest = "1:3dd078fda7500c341bc26cfbc6c6a34614f295a2457149fc1045cab767cbcf18" - name = "github.com/golang/protobuf" - packages = ["proto"] - pruneopts = "" - revision = "aa810b61a9c79d51363740d207bb46cf8e620ed5" - version = "v1.2.0" - -[[projects]] - digest = "1:63722a4b1e1717be7b98fc686e0b30d5e7f734b9e93d7dee86293b6deab7ea28" - name = "github.com/matttproud/golang_protobuf_extensions" - packages = ["pbutil"] - pruneopts = "" - revision = "c12348ce28de40eed0136aa2b644d0ee0650e56c" - version = "v1.0.1" - -[[projects]] - digest = "1:8b2082f564fe20dbb43a621ee0d57ae2777656ab14111d100d3d92d1b5b958b9" - name = "github.com/prometheus/client_golang" - packages = [ - "prometheus", - "prometheus/internal", - "prometheus/promhttp", - ] - pruneopts = "" - revision = "abad2d1bd44235a26707c172eab6bca5bf2dbad3" - version = "v0.9.1" - -[[projects]] - branch = "master" - digest = "1:185cf55b1f44a1bf243558901c3f06efa5c64ba62cfdcbb1bf7bbe8c3fb68561" - name = "github.com/prometheus/client_model" - packages = ["go"] - pruneopts = "" - revision = "5c3871d89910bfb32f5fcab2aa4b9ec68e65a99f" - -[[projects]] - branch = "master" - digest = "1:f477ef7b65d94fb17574fc6548cef0c99a69c1634ea3b6da248b63a61ebe0498" - name = "github.com/prometheus/common" - packages = [ - "expfmt", - "internal/bitbucket.org/ww/goautoneg", - "log", - "model", - ] - pruneopts = "" - revision = "c7de2306084e37d54b8be01f3541a8464345e9a5" - -[[projects]] - branch = "master" - digest = "1:5a57ea878c9a40657ebe7916eca6bea7c76808f5acb68fd42ea5e204dd35f6f7" - name = "github.com/prometheus/procfs" - packages = [ - ".", - "internal/util", - "nfs", - "xfs", - ] - pruneopts = "" - revision = "418d78d0b9a7b7de3a6bbc8a23def624cc977bb2" - -[[projects]] - digest = "1:3fcbf733a8d810a21265a7f2fe08a3353db2407da052b233f8b204b5afc03d9b" - name = "github.com/sirupsen/logrus" - packages = ["."] - pruneopts = "" - revision = "3e01752db0189b9157070a0e1668a620f9a85da2" - version = "v1.0.6" - -[[projects]] - branch = "master" - digest = "1:61a86f0be8b466d6e3fbdabb155aaa4006137cb5e3fd3b949329d103fa0ceb0f" - name = "golang.org/x/crypto" - packages = ["ssh/terminal"] - pruneopts = "" - revision = "0e37d006457bf46f9e6692014ba72ef82c33022c" - -[[projects]] - branch = "master" - digest = "1:507e8b2dd680599d447d627e102f161b58f4f5ef17eea0e81c181adad2d28a1f" - name = "golang.org/x/net" - packages = [ - "bpf", - "icmp", - "internal/iana", - "internal/socket", - "ipv4", - "ipv6", - ] - pruneopts = "" - revision = "2f5d2388922f370f4355f327fcf4cfe9f5583908" - -[[projects]] - branch = "master" - digest = "1:599d10864dbd891db49513bbf6cbe9229710c932cf3991074d5fb547a3f15c50" - name = "golang.org/x/sys" - packages = [ - "unix", - "windows", - "windows/registry", - "windows/svc/eventlog", - ] - pruneopts = "" - revision = "d47a0f3392421c5624713c9a19fe781f651f8a50" - -[[projects]] - digest = "1:15d017551627c8bb091bde628215b2861bed128855343fdd570c62d08871f6e1" - name = "gopkg.in/alecthomas/kingpin.v2" - packages = ["."] - pruneopts = "" - revision = "947dcec5ba9c011838740e680966fd7087a71d0d" - version = "v2.2.6" - -[[projects]] - digest = "1:f0620375dd1f6251d9973b5f2596228cc8042e887cd7f827e4220bc1ce8c30e2" - name = "gopkg.in/yaml.v2" - packages = ["."] - pruneopts = "" - revision = "5420a8b6744d3b0345ab293f6fcba19c978f1183" - version = "v2.2.1" - -[solve-meta] - analyzer-name = "dep" - analyzer-version = 1 - input-imports = [ - "github.com/czerwonk/ping_exporter/config", - "github.com/digineo/go-ping", - "github.com/digineo/go-ping/monitor", - "github.com/prometheus/client_golang/prometheus", - "github.com/prometheus/client_golang/prometheus/promhttp", - "github.com/prometheus/common/log", - "gopkg.in/alecthomas/kingpin.v2", - "gopkg.in/yaml.v2", - ] - solver-name = "gps-cdcl" - solver-version = 1 diff --git a/Gopkg.toml b/Gopkg.toml deleted file mode 100644 index 3d3d504..0000000 --- a/Gopkg.toml +++ /dev/null @@ -1,34 +0,0 @@ - -# Gopkg.toml example -# -# Refer to https://github.com/golang/dep/blob/master/docs/Gopkg.toml.md -# for detailed Gopkg.toml documentation. -# -# required = ["github.com/user/thing/cmd/thing"] -# ignored = ["github.com/user/project/pkgX", "bitbucket.org/user/project/pkgA/pkgY"] -# -# [[constraint]] -# name = "github.com/user/project" -# version = "1.0.0" -# -# [[constraint]] -# name = "github.com/user/project2" -# branch = "dev" -# source = "github.com/myfork/project2" -# -# [[override]] -# name = "github.com/x/y" -# version = "2.4.0" - - -[[constraint]] - branch = "master" - name = "github.com/digineo/go-ping" - -[[constraint]] - name = "github.com/prometheus/client_golang" - version = "0.9.1" - -[[constraint]] - branch = "master" - name = "github.com/prometheus/common" From 5a0d23c66e665af4d3ff4ab0ee071e109dfbb8f1 Mon Sep 17 00:00:00 2001 From: Dominik Menke Date: Fri, 19 Mar 2021 18:51:02 +0100 Subject: [PATCH 2/2] cleanup: satisfy linter --- config/config.go | 11 +++++++---- config/config_test.go | 3 ++- main.go | 17 +++++++++++------ target.go | 10 +++++++--- 4 files changed, 27 insertions(+), 14 deletions(-) diff --git a/config/config.go b/config/config.go index 9d4eb4a..e0959a4 100644 --- a/config/config.go +++ b/config/config.go @@ -1,13 +1,14 @@ package config import ( + "fmt" "io" "time" yaml "gopkg.in/yaml.v2" ) -// Config represents configuration for the exporter +// Config represents configuration for the exporter. type Config struct { Targets []string `yaml:"targets"` @@ -34,9 +35,10 @@ func (d *duration) UnmarshalYAML(unmashal func(interface{}) error) error { } dur, err := time.ParseDuration(s) if err != nil { - return err + return fmt.Errorf("failed to decode duration: %w", err) } *d = duration(dur) + return nil } @@ -50,12 +52,13 @@ func (d *duration) Set(dur time.Duration) { *d = duration(dur) } -// FromYAML reads YAML from reader and unmarshals it to Config +// FromYAML reads YAML from reader and unmarshals it to Config. func FromYAML(r io.Reader) (*Config, error) { c := &Config{} err := yaml.NewDecoder(r).Decode(c) if err != nil { - return nil, err + return nil, fmt.Errorf("failed to decode YAML: %w", err) } + return c, nil } diff --git a/config/config_test.go b/config/config_test.go index 53c5fa5..1825cf8 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -8,6 +8,8 @@ import ( ) func TestParseConfig(t *testing.T) { + t.Parallel() + f, err := os.Open("testdata/config_test.yml") if err != nil { t.Error("failed to open file", err) @@ -52,5 +54,4 @@ func TestParseConfig(t *testing.T) { if expected := 120; c.Ping.Size != uint16(expected) { t.Errorf("expected ping.payload-size to be %d, got %d", expected, c.Ping.Size) } - } diff --git a/main.go b/main.go index d690bdd..e7007f3 100644 --- a/main.go +++ b/main.go @@ -84,7 +84,7 @@ func main() { kingpin.FatalUsage("ping.history-size must be greater than 0") } - if cfg.Ping.Size < 0 || cfg.Ping.Size > 65500 { + if cfg.Ping.Size > 65500 { kingpin.FatalUsage("ping.size must be between 0 and 65500") } @@ -112,18 +112,18 @@ func startMonitor(cfg *config.Config) (*mon.Monitor, error) { resolver := setupResolver(cfg) var bind4, bind6 string if ln, err := net.Listen("tcp4", "127.0.0.1:0"); err == nil { - //ipv4 enabled + // ipv4 enabled ln.Close() bind4 = "0.0.0.0" } if ln, err := net.Listen("tcp6", "[::1]:0"); err == nil { - //ipv6 enabled + // ipv6 enabled ln.Close() bind6 = "::" } pinger, err := ping.New(bind4, bind6) if err != nil { - return nil, err + return nil, fmt.Errorf("cannot start monitoring: %w", err) } if pinger.PayloadSize() != cfg.Ping.Size { @@ -188,7 +188,8 @@ func startServer(monitor *mon.Monitor) { reg.MustRegister(&pingCollector{monitor: monitor}) h := promhttp.HandlerFor(reg, promhttp.HandlerOpts{ ErrorLog: log.NewErrorLogger(), - ErrorHandling: promhttp.ContinueOnError}) + ErrorHandling: promhttp.ContinueOnError, + }) http.Handle(*metricsPath, h) log.Infof("Listening for %s on %s", *metricsPath, *listenAddress) @@ -199,12 +200,13 @@ func loadConfig() (*config.Config, error) { if *configFile == "" { cfg := config.Config{} addFlagToConfig(&cfg) + return &cfg, nil } f, err := os.Open(*configFile) if err != nil { - return nil, err + return nil, fmt.Errorf("cannot load config file: %w", err) } defer f.Close() @@ -212,6 +214,7 @@ func loadConfig() (*config.Config, error) { if err != nil { addFlagToConfig(cfg) } + return cfg, err } @@ -225,8 +228,10 @@ func setupResolver(cfg *config.Config) *net.Resolver { } dialer := func(ctx context.Context, network, address string) (net.Conn, error) { d := net.Dialer{} + return d.DialContext(ctx, "udp", cfg.DNS.Nameserver) } + return &net.Resolver{PreferGo: true, Dial: dialer} } diff --git a/target.go b/target.go index 28e09e1..7c2ae10 100644 --- a/target.go +++ b/target.go @@ -25,7 +25,7 @@ func (t *target) addOrUpdateMonitor(monitor *mon.Monitor) error { addrs, err := t.resolver.LookupIPAddr(context.Background(), t.host) if err != nil { - return fmt.Errorf("error resolving target: %v", err) + return fmt.Errorf("error resolving target: %w", err) } for _, addr := range addrs { @@ -45,12 +45,13 @@ func (t *target) addIfNew(addr net.IPAddr, monitor *mon.Monitor) error { if isIPAddrInSlice(addr, t.addresses) { return nil } + return t.add(addr, monitor) } -func (t *target) cleanUp(new []net.IPAddr, monitor *mon.Monitor) { +func (t *target) cleanUp(addr []net.IPAddr, monitor *mon.Monitor) { for _, o := range t.addresses { - if !isIPAddrInSlice(o, new) { + if !isIPAddrInSlice(o, addr) { name := t.nameForIP(o) log.Infof("removing target for host %s (%v)", t.host, o) monitor.RemoveTarget(name) @@ -61,6 +62,7 @@ func (t *target) cleanUp(new []net.IPAddr, monitor *mon.Monitor) { func (t *target) add(addr net.IPAddr, monitor *mon.Monitor) error { name := t.nameForIP(addr) log.Infof("adding target for host %s (%v)", t.host, addr) + return monitor.AddTargetDelayed(name, addr, t.delay) } @@ -69,6 +71,7 @@ func (t *target) nameForIP(addr net.IPAddr) string { if addr.IP.To4() == nil { v = 6 } + return fmt.Sprintf("%s %s %d", t.host, addr.IP, v) } @@ -78,5 +81,6 @@ func isIPAddrInSlice(ipa net.IPAddr, slice []net.IPAddr) bool { return true } } + return false }