Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Dynamic host config #52

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ require (
github.com/prometheus/client_golang v1.9.0
github.com/prometheus/common v0.15.0
gopkg.in/alecthomas/kingpin.v2 v2.2.6
gopkg.in/yaml.v2 v2.3.0
)

go 1.14
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpO
github.com/hudl/fargo v1.3.0/go.mod h1:y3CKSmjA+wD2gak7sUSXTAoopbhU08POFhmITJgmKTg=
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
github.com/influxdata/influxdb1-client v0.0.0-20191209144304-8bf82d3c094d/go.mod h1:qj24IKcXYK6Iy9ceXlo3Tc+vtHo9lIhSX5JddghvEPo=
github.com/jakubjastrabik/smokeping_prober v0.4.1 h1:fVhZ67Su6QddM+Om/cntBbLoFKNrmA6to/irms0/N20=
github.com/jmespath/go-jmespath v0.0.0-20180206201540-c2b33e8439af/go.mod h1:Nht3zPeWKUH0NzdCt2Blrr5ys8VGpn0CEB0cQHVjt7k=
github.com/jonboulle/clockwork v0.1.0/go.mod h1:Ii8DK3G1RaLaWxj9trq07+26W01tbo22gdxWY5EU2bo=
github.com/jpillora/backoff v1.0.0/go.mod h1:J/6gKK9jxlEcS3zixgDgUAsiuZ7yrSoa/FX5e0EB2j4=
Expand Down
16 changes: 16 additions & 0 deletions hosts.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
targets:
- host:
ip:
- google.sk
network: ip4
protocol: icmp
labels:
name: google
- host:
ip:
- fb.com
- youtube.com
network: ip4
protocol: icmp
labels:
name: fb tr ff
86 changes: 70 additions & 16 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,24 +19,63 @@ import (
"math"
"net/http"
_ "net/http/pprof"
"os"
"strconv"
"strings"
"time"

"github.com/go-ping/ping"

"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/prometheus/common/log"
"github.com/prometheus/common/version"
"gopkg.in/alecthomas/kingpin.v2"
"gopkg.in/yaml.v2"
)

var (
// Generated with: prometheus.ExponentialBuckets(0.00005, 2, 20)
defaultBuckets = "5e-05,0.0001,0.0002,0.0004,0.0008,0.0016,0.0032,0.0064,0.0128,0.0256,0.0512,0.1024,0.2048,0.4096,0.8192,1.6384,3.2768,6.5536,13.1072,26.2144"
conf *Conf
)

// Conf Default structure for parsign,saving, etc.
// Default config and host variables from .yaml files
type Conf struct {
Targets []struct {
Host struct {
IP []string `yaml:"ip"`
Network string `yaml:"network"`
Protocol string `yaml:"protocol"`
Labels struct {
Name string `yaml:"name"`
} `yaml:"labels"`
} `yaml:"host"`
} `yaml:"targets"`
}

// NewConfig returns a new decoded Config struct
func NewConfig(configPath string) (*Conf, error) {
// Create config structure
config := &Conf{}

// Open config file
file, err := os.Open(configPath)
if err != nil {
return nil, err
}
defer file.Close()

// Init new YAML decode
d := yaml.NewDecoder(file)

// Start YAML decoding from file
if err := d.Decode(&config); err != nil {
return nil, err
}
return config, nil
}

type hostList []string

func (h *hostList) Set(value string) error {
Expand Down Expand Up @@ -87,7 +126,8 @@ func main() {
buckets = kingpin.Flag("buckets", "A comma delimited list of buckets to use").Default(defaultBuckets).String()
interval = kingpin.Flag("ping.interval", "Ping interval duration").Short('i').Default("1s").Duration()
privileged = kingpin.Flag("privileged", "Run in privileged ICMP mode").Default("true").Bool()
hosts = HostList(kingpin.Arg("hosts", "List of hosts to ping").Required())
config = kingpin.Flag("config", "Path to config file").Default("hosts.yaml").String()
err error
)

log.AddFlags(kingpin.CommandLine)
Expand All @@ -97,6 +137,12 @@ func main() {

log.Infoln("Starting smokeping_prober", version.Info())
log.Infoln("Build context", version.BuildContext())

conf, err = NewConfig(*config)
if err != nil {
fmt.Println(err)
}

bucketlist, err := parseBuckets(*buckets)
if err != nil {
log.Errorf("failed to parse buckets: %s\n", err.Error())
Expand All @@ -105,24 +151,32 @@ func main() {
pingResponseSeconds := newPingResponseHistogram(bucketlist)
prometheus.MustRegister(pingResponseSeconds)

pingers := make([]*ping.Pinger, len(*hosts))
for i, host := range *hosts {
pinger := ping.New(host)
var f = 0
for _, a := range conf.Targets {
f += len(a.Host.IP)
}

pingers := make([]*ping.Pinger, f)

err := pinger.Resolve()
if err != nil {
log.Errorf("failed to resolve pinger: %s\n", err.Error())
return
}
for l, a := range conf.Targets {
for i, host := range a.Host.IP {
pinger := ping.New(host)

log.Infof("Resolved %s as %s", host, pinger.IPAddr())
err := pinger.Resolve()
if err != nil {
log.Errorf("failed to resolve pinger: %s\n", err.Error())
return
}

pinger.Interval = *interval
pinger.Timeout = time.Duration(math.MaxInt64)
pinger.RecordRtts = false
pinger.SetPrivileged(*privileged)
log.Infof("Resolved %s as %s", host, pinger.IPAddr())

pingers[i] = pinger
pinger.Interval = *interval
pinger.Timeout = time.Duration(math.MaxInt64)
pinger.RecordRtts = false
pinger.SetPrivileged(*privileged)

pingers[l+i] = pinger
}
}

splay := time.Duration(interval.Nanoseconds() / int64(len(pingers)))
Expand Down