Skip to content

Commit

Permalink
Add more logging during monitoring
Browse files Browse the repository at this point in the history
  • Loading branch information
itzg committed Dec 27, 2019
1 parent 17cf3eb commit 140548f
Show file tree
Hide file tree
Showing 5 changed files with 119 additions and 7 deletions.
13 changes: 10 additions & 3 deletions gather_telegraf.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"github.com/google/subcommands"
"github.com/itzg/go-flagsfiller"
"go.uber.org/zap"
"log"
"os"
"strconv"
Expand All @@ -17,6 +18,7 @@ type gatherTelegrafCmd struct {
Interval time.Duration `default:"1m" usage:"gathers and sends metrics at this interval"`
Servers []string `usage:"one or more [host:port] addresses of servers to monitor"`
TelegrafAddress string `default:"localhost:8094" usage:"[host:port] of telegraf accepting Influx line protocol"`
logger *zap.Logger
}

func (c *gatherTelegrafCmd) Name() string {
Expand Down Expand Up @@ -51,7 +53,12 @@ func (c *gatherTelegrafCmd) Execute(ctx context.Context, f *flag.FlagSet, args .
return subcommands.ExitUsageError
}

fmt.Printf("monitoring %v at interval %s and reporting to %s\n", c.Servers, c.Interval, c.TelegrafAddress)
c.logger = args[0].(*zap.Logger).Named("gather")

c.logger.Info("starting monitoring",
zap.Strings("servers", c.Servers),
zap.Duration("interval", c.Interval),
zap.String("telegrafAddress", c.TelegrafAddress))

ticker := time.NewTicker(c.Interval)

Expand Down Expand Up @@ -80,10 +87,10 @@ func (c *gatherTelegrafCmd) createGatherers() []*TelegrafGatherer {
if err != nil {
log.Printf("WARN: unable to process %s: %s\n", addr, err)
} else {
gatherers = append(gatherers, NewTelegrafGatherer(parts[0], port, c.TelegrafAddress))
gatherers = append(gatherers, NewTelegrafGatherer(parts[0], port, c.TelegrafAddress, c.logger))
}
} else {
gatherers = append(gatherers, NewTelegrafGatherer(parts[0], DefaultPort, c.TelegrafAddress))
gatherers = append(gatherers, NewTelegrafGatherer(parts[0], DefaultPort, c.TelegrafAddress, c.logger))
}
}

Expand Down
4 changes: 3 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,7 @@ require (
github.com/Raqbit/mc-pinger v0.0.0-20190414124050-6c3fd84faaa1
github.com/google/subcommands v1.0.1
github.com/influxdata/line-protocol v0.0.0-20190509173118-5712a8124a9a
github.com/itzg/go-flagsfiller v1.4.0
github.com/itzg/go-flagsfiller v1.4.1
github.com/itzg/zapconfigs v0.1.0
go.uber.org/zap v1.13.0
)
45 changes: 45 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/Raqbit/mc-pinger v0.0.0-20190414124050-6c3fd84faaa1 h1:s2UZugpc4wHQMBYoUI6wi2HE9ZR3TI8pmJrZpNhVFvU=
github.com/Raqbit/mc-pinger v0.0.0-20190414124050-6c3fd84faaa1/go.mod h1:r2rVvqOwaYCU3rYUNeSmCiJbcX2wJkDisXH7rZsjjuM=
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/google/renameio v0.1.0/go.mod h1:KWCgfxg9yswjAJkECMjeO8J8rahYeXnNhOm40UhjYkI=
github.com/google/subcommands v1.0.1 h1:/eqq+otEXm5vhfBrbREPCSVQbvofip6kIz+mX5TUH7k=
github.com/google/subcommands v1.0.1/go.mod h1:ZjhPrFU+Olkh9WazFPsl27BQ4UPiG37m3yTrtFlrHVk=
github.com/iancoleman/strcase v0.0.0-20191112232945-16388991a334 h1:VHgatEHNcBFEB7inlalqfNqw65aNkM1lGX2yt3NmbS8=
Expand All @@ -10,12 +14,53 @@ github.com/influxdata/line-protocol v0.0.0-20190509173118-5712a8124a9a h1:p2OJKX
github.com/influxdata/line-protocol v0.0.0-20190509173118-5712a8124a9a/go.mod h1:4kt73NQhadE3daL3WhR5EJ/J2ocX0PZzwxQ0gXJ7oFE=
github.com/itzg/go-flagsfiller v1.4.0 h1:iFi1Xd2yTOP9J+FMJ9QFxM9KdtMMeGMnhgDSWCE4gwk=
github.com/itzg/go-flagsfiller v1.4.0/go.mod h1:mfQgTahSs4OHn8PYev2Wwi1LJXUiYiGuZVCpBLxzbYs=
github.com/itzg/go-flagsfiller v1.4.1 h1:h/t5g+WkvsOR449bz1ngU8UGosKNm4Sr3iMNNgOqHfo=
github.com/itzg/go-flagsfiller v1.4.1/go.mod h1:mfQgTahSs4OHn8PYev2Wwi1LJXUiYiGuZVCpBLxzbYs=
github.com/itzg/zapconfigs v0.1.0 h1:Gokocm8VaTNnZjvIiVA5NEhzZ1v7lEyXY/AbeBmq6YQ=
github.com/itzg/zapconfigs v0.1.0/go.mod h1:y4dArgRUOFbGRkUNJ8XSSw98FGn03wtkvMPy+OSA5Rc=
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
go.uber.org/atomic v1.5.0 h1:OI5t8sDa1Or+q8AeE+yKeB/SDYioSHAgcVljj9JIETY=
go.uber.org/atomic v1.5.0/go.mod h1:sABNBOSYdrvTF6hTgEIbc7YasKWGhgEQZyfxyTvoXHQ=
go.uber.org/multierr v1.3.0 h1:sFPn2GLc3poCkfrpIXGhBD2X0CMIo4Q/zSULXrj/+uc=
go.uber.org/multierr v1.3.0/go.mod h1:VgVr7evmIr6uPjLBxg28wmKNXyqE9akIJ5XnfpiKl+4=
go.uber.org/tools v0.0.0-20190618225709-2cfd321de3ee h1:0mgffUl7nfd+FpvXMVz4IDEaUSmT1ysygQC7qYo7sG4=
go.uber.org/tools v0.0.0-20190618225709-2cfd321de3ee/go.mod h1:vJERXedbb3MVM5f9Ejo0C68/HhF8uaILCdgjnY+goOA=
go.uber.org/zap v1.13.0 h1:nR6NoDBgAf67s68NhaXbsojM+2gxp3S1hWkHDl27pVU=
go.uber.org/zap v1.13.0/go.mod h1:zwrFLgMcdUuIBviXEYEH1YKNaOBnKXsx2IPda5bBwHM=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20190510104115-cbcb75029529/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/lint v0.0.0-20190930215403-16217165b5de h1:5hukYrvBGR8/eNkX5mdUezrA6JiaEZDtJb9Ei+1LlBs=
golang.org/x/lint v0.0.0-20190930215403-16217165b5de/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc=
golang.org/x/mod v0.0.0-20190513183733-4bf6d317e70e/go.mod h1:mXi4GBBbnImb6dmsKGUJ2LatrhH/nqhxcFungHvyanc=
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
golang.org/x/tools v0.0.0-20190621195816-6e04913cbbac/go.mod h1:/rFqwRUd4F7ZHNgwSSTFct+R/Kf4OFW1sUzUTQQTgfc=
golang.org/x/tools v0.0.0-20191029041327-9cc4af7d6b2c/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.0.0-20191029190741-b9c20aec41a5 h1:hKsoRgsbwY1NafxrwTs+k64bikrLBkAgPir1TNCj3Zs=
golang.org/x/tools v0.0.0-20191029190741-b9c20aec41a5/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI=
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
honnef.co/go/tools v0.0.1-2019.2.3 h1:3JgtbtFHMiCmsznwGVTUWbgGov+pVqnlf1dEJTNAXeM=
honnef.co/go/tools v0.0.1-2019.2.3/go.mod h1:a3bituU0lyd329TUQxRnasdCoJDkEUEAqEt0JzvZhAg=
55 changes: 53 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,67 @@ package main
import (
"context"
"flag"
"fmt"
"github.com/google/subcommands"
"github.com/itzg/go-flagsfiller"
"github.com/itzg/zapconfigs"
"go.uber.org/zap"
"log"
"os"
)

var (
version = ""
commit = ""
date = ""
)

func main() {
subcommands.Register(subcommands.HelpCommand(), "")
subcommands.Register(subcommands.FlagsCommand(), "")
subcommands.Register(&versionCmd{}, "")
subcommands.Register(&statusCmd{}, "")
subcommands.Register(&gatherTelegrafCmd{}, "monitoring")

flag.Parse()
var config GlobalConfig
err := flagsfiller.Parse(&config, flagsfiller.WithEnv(""))
if err != nil {
log.Fatal(err)
}

var logger *zap.Logger
if config.Debug {
logger = zapconfigs.NewDebugLogger()
} else {
logger = zapconfigs.NewDefaultLogger()
}
defer logger.Sync()

os.Exit(int(subcommands.Execute(context.Background(), logger)))
}

type GlobalConfig struct {
Debug bool `usage:"enable debug logging"`
}

type versionCmd struct{}

func (c *versionCmd) Name() string {
return "version"
}

func (c *versionCmd) Synopsis() string {
return "Show version and exit"
}

func (c *versionCmd) Usage() string {
return ""
}

func (c *versionCmd) SetFlags(*flag.FlagSet) {
}

os.Exit(int(subcommands.Execute(context.Background())))
func (c *versionCmd) Execute(ctx context.Context, f *flag.FlagSet, args ...interface{}) subcommands.ExitStatus {
fmt.Printf("%s commit=%s date=%s\n", version, commit, date)
return subcommands.ExitSuccess
}
9 changes: 8 additions & 1 deletion telegraf.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
mcpinger "github.com/Raqbit/mc-pinger"
protocol "github.com/influxdata/line-protocol"
"go.uber.org/zap"
"log"
"net"
"strconv"
Expand All @@ -15,18 +16,21 @@ type TelegrafGatherer struct {
port string
telegrafEndpoint string
pinger mcpinger.Pinger
logger *zap.Logger
}

func NewTelegrafGatherer(host string, port int, telegrafEndpoint string) *TelegrafGatherer {
func NewTelegrafGatherer(host string, port int, telegrafEndpoint string, logger *zap.Logger) *TelegrafGatherer {
return &TelegrafGatherer{
host: host,
port: strconv.FormatInt(int64(port), 10),
pinger: mcpinger.New(host, uint16(port)),
telegrafEndpoint: telegrafEndpoint,
logger: logger,
}
}

func (g *TelegrafGatherer) Gather() {
g.logger.Debug("gathering", zap.String("host", g.host), zap.String("port", g.port))
startTime := time.Now()
info, err := g.pinger.Ping()
elapsed := time.Now().Sub(startTime)
Expand Down Expand Up @@ -96,6 +100,9 @@ func (g *TelegrafGatherer) sendFailedMetrics(err error, elapsed time.Duration) e
}

func (g *TelegrafGatherer) sendLine(lineBytes []byte) error {
g.logger.Debug("sending metrics",
zap.String("endpoint", g.telegrafEndpoint),
zap.ByteString("line", lineBytes))
conn, err := net.Dial("tcp", g.telegrafEndpoint)
if err != nil {
return err
Expand Down

0 comments on commit 140548f

Please sign in to comment.