Skip to content

Commit

Permalink
Merge pull request #21 from FortnoxAB/feature/remove-host
Browse files Browse the repository at this point in the history
Ability to remove host with API call
  • Loading branch information
jonaz authored Aug 23, 2024
2 parents b220a3f + 742acd9 commit 57b2dcc
Show file tree
Hide file tree
Showing 6 changed files with 216 additions and 140 deletions.
30 changes: 23 additions & 7 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,16 +1,32 @@
module github.com/fortnoxab/prometheus-net-discovery

go 1.15
go 1.22

require (
github.com/BurntSushi/toml v0.3.1 // indirect
github.com/fortnoxab/fnxlogrus v0.0.0-20220823093317-6a580c56b8fd
github.com/koding/multiconfig v0.0.0-20171124222453-69c27309b2d7
github.com/prometheus/client_golang v1.20.1
github.com/sirupsen/logrus v1.9.3
github.com/stretchr/testify v1.9.0
)

require (
github.com/BurntSushi/toml v1.4.0 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/cespare/xxhash/v2 v2.3.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/fatih/camelcase v1.0.0 // indirect
github.com/fatih/structs v1.1.0 // indirect
github.com/fortnoxab/fnxlogrus v0.0.0-20181213065106-cbb7cf837eba
github.com/koding/multiconfig v0.0.0-20171124222453-69c27309b2d7
github.com/klauspost/compress v1.17.9 // indirect
github.com/kr/text v0.2.0 // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/sirupsen/logrus v1.7.0
github.com/stretchr/testify v1.2.2
golang.org/x/sys v0.1.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/prometheus/client_model v0.6.1 // indirect
github.com/prometheus/common v0.55.0 // indirect
github.com/prometheus/procfs v0.15.1 // indirect
golang.org/x/sys v0.24.0 // indirect
google.golang.org/protobuf v1.34.2 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
60 changes: 48 additions & 12 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,27 +1,63 @@
github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/BurntSushi/toml v1.4.0 h1:kuoIxZQy2WRRk1pttg9asf+WVv6tWQuBNVmK8+nqPr0=
github.com/BurntSushi/toml v1.4.0/go.mod h1:ukJfTF/6rtPPRCnwkur4qwRxa8vTRFBF0uk2lLoLwho=
github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=
github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw=
github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UFvs=
github.com/cespare/xxhash/v2 v2.3.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/fatih/camelcase v1.0.0 h1:hxNvNX/xYBp0ovncs8WyWZrOrpBNub/JfaMvbURyft8=
github.com/fatih/camelcase v1.0.0/go.mod h1:yN2Sb0lFhZJUdVvtELVWefmrXpuZESvPmqwoZc+/fpc=
github.com/fatih/structs v1.1.0 h1:Q7juDM0QtcnhCpeyLGQKyg4TOIghuNXrkL32pHAUMxo=
github.com/fatih/structs v1.1.0/go.mod h1:9NiDSp5zOcgEDl+j00MP/WkGVPOlPRLejGD8Ga6PJ7M=
github.com/fortnoxab/fnxlogrus v0.0.0-20181213065106-cbb7cf837eba h1:v8Zke9co1hdogIVKYUykqbsS+vRQT2a70FiOOR1zPfU=
github.com/fortnoxab/fnxlogrus v0.0.0-20181213065106-cbb7cf837eba/go.mod h1:NVouiena6I9bpmehL53/Z3h9c1nVbz01cLdtBaMjA/c=
github.com/fortnoxab/fnxlogrus v0.0.0-20220823093317-6a580c56b8fd h1:vjKVmkcuAhOGIJnbCGfm1MvKvJgE7jpIZTHS6pqdV+s=
github.com/fortnoxab/fnxlogrus v0.0.0-20220823093317-6a580c56b8fd/go.mod h1:R6mab3Zy+NZ3qi4krgbsMSPFVh2AFVSBG2xnus0fl0Q=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/klauspost/compress v1.17.9 h1:6KIumPrER1LHsvBVuDa0r5xaG0Es51mhhB9BQB2qeMA=
github.com/klauspost/compress v1.17.9/go.mod h1:Di0epgTjJY877eYKx5yC51cX2A2Vl2ibi7bDH9ttBbw=
github.com/koding/multiconfig v0.0.0-20171124222453-69c27309b2d7 h1:SWlt7BoQNASbhTUD0Oy5yysI2seJ7vWuGUp///OM4TM=
github.com/koding/multiconfig v0.0.0-20171124222453-69c27309b2d7/go.mod h1:Y2SaZf2Rzd0pXkLVhLlCiAXFCLSXAIbTKDivVgff/AM=
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/kylelemons/godebug v1.1.0 h1:RPNrshWIDI6G2gRW9EHilWtl7Z6Sb1BR0xunSBf0SNc=
github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw=
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 h1:C3w9PqII01/Oq1c1nUAm88MOHcQC9l5mIlSMApZMrHA=
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822/go.mod h1:+n7T8mK8HuQTcFwEeznm/DIxMOiR9yIdICNftLE1DvQ=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.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/sirupsen/logrus v1.7.0 h1:ShrD1U9pZB12TX0cVy0DtePoCH97K8EtX+mg7ZARUtM=
github.com/sirupsen/logrus v1.7.0/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0=
github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.1.0 h1:kunALQeHf1/185U1i0GOB/fy1IPRDDpuoOOqRReG57U=
golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
github.com/prometheus/client_golang v1.20.1 h1:IMJXHOD6eARkQpxo8KkhgEVFlBNm+nkrFUyGlIu7Na8=
github.com/prometheus/client_golang v1.20.1/go.mod h1:PIEt8X02hGcP8JWbeHyeZ53Y/jReSnHgO035n//V5WE=
github.com/prometheus/client_model v0.6.1 h1:ZKSh/rekM+n3CeS952MLRAdFwIKqeY8b62p8ais2e9E=
github.com/prometheus/client_model v0.6.1/go.mod h1:OrxVMOVHjw3lKMa8+x6HeMGkHMQyHDk9E3jmP2AmGiY=
github.com/prometheus/common v0.55.0 h1:KEi6DK7lXW/m7Ig5i47x0vRzuBsHuvJdi5ee6Y3G1dc=
github.com/prometheus/common v0.55.0/go.mod h1:2SECS4xJG1kd8XF9IcM1gMX6510RAEL65zxzNImwdc8=
github.com/prometheus/procfs v0.15.1 h1:YagwOFzUgYfKKHX6Dr+sHT7km/hxC76UB0learggepc=
github.com/prometheus/procfs v0.15.1/go.mod h1:fB45yRUv8NstnjriLhBQLuOUt+WW4BsoGhij/e3PBqk=
github.com/rogpeppe/go-internal v1.10.0 h1:TMyTOH3F/DB16zRVcYyreMH6GnZZrwQVAoYjRBZyWFQ=
github.com/rogpeppe/go-internal v1.10.0/go.mod h1:UQnix2H7Ngw/k4C5ijL5+65zddjncjaFoBhdsK/akog=
github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ=
github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.24.0 h1:Twjiwq9dn6R1fQcyiK+wQyHWfaz/BJB+YIpzU/Cv3Xg=
golang.org/x/sys v0.24.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
google.golang.org/protobuf v1.34.2 h1:6xV6lTsCfpGD21XK49h7MhtcApnLqkfYgPcdHftf6hg=
google.golang.org/protobuf v1.34.2/go.mod h1:qYOHts0dSfpeUzUFpOMr/WGzszTmLH+DiWniOlNbLDw=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
13 changes: 11 additions & 2 deletions helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"bufio"
"bytes"
"context"
"crypto/tls"
"encoding/json"
"fmt"
"net"
Expand All @@ -30,9 +29,19 @@ func getFirst(s []string) string {
return ""
}

var dialer = &net.Dialer{
Timeout: 3 * time.Second,
KeepAlive: 30 * time.Second,
}
var client = &http.Client{
Transport: &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true}, // #nosec only cockroach at the moment
// TLSClientConfig: &tls.Config{InsecureSkipVerify: true}, // #nosec only cockroach at the moment
DialContext: dialer.DialContext,
ForceAttemptHTTP2: true,
MaxIdleConns: 100,
IdleConnTimeout: 10 * time.Second,
TLSHandshakeTimeout: 10 * time.Second,
ExpectContinueTimeout: 1 * time.Second,
},
}

Expand Down
136 changes: 19 additions & 117 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,107 +23,26 @@ import (

// ExporterConfig configures ports to scan to what filename to save it to.
// if path is set we will try to make a HTTP get and find # TYPE in the first 10 rows of the response to make sure we know its prometheus metrics.
// TODO make this runtime configurable.
type ExporterConfig []struct {
port string
filename string
path string
}

const ExporterExporterPort = "9999"

var exporterConfig = ExporterConfig{
{ // special exporter_exporter we scan this first to know if we can skip the other ports.
port: ExporterExporterPort,
// We only support exporter_exporter at the moment
{
port: "",
filename: "",
},
/*
disabled those since we only use exporter_exporter on 9999 for now.
TODO move this to a config
{
port: "8081",
filename: "php",
path: "http://%s/metrics",
},
{
port: "9100",
filename: "node",
},
{
port: "9108",
filename: "elasticsearch",
},
{
port: "9114",
filename: "elasticsearch",
},
{
port: "9216",
filename: "mongodb",
},
{
port: "9091",
filename: "minio",
path: "http://%s/minio/prometheus/metrics",
},
{
port: "9101",
filename: "haproxy",
},
{
port: "9104",
filename: "mysql",
},
{
port: "9113",
filename: "nginx",
},
{
port: "9121",
filename: "redis",
},
{
port: "9150",
filename: "memcached",
},
{
port: "9154",
filename: "postfix",
},
{
port: "9182",
filename: "wmi",
},
{
port: "9187",
filename: "postgres",
},
{
port: "9188",
filename: "pgbouncer",
},
{
port: "9189",
filename: "barman",
},
{
port: "9253",
filename: "php-fpm",
},
{
port: "9308",
filename: "kafka",
},
{
port: "9496",
filename: "389ds",
},
*/
}

var mutex sync.Mutex

func main() {
config := &Config{}
multiconfig.MustLoad(&config)
exporterConfig[0].port = config.ExpoterExporterPort

fnxlogrus.Init(config.Log, logrus.StandardLogger())

Expand All @@ -138,14 +57,11 @@ func main() {
logrus.Infof("Running with interval %s", interval)

ticker := time.NewTicker(interval)
interrupt := make(chan os.Signal, 1)
signal.Notify(interrupt, os.Interrupt, syscall.SIGQUIT, syscall.SIGTERM, syscall.SIGINT)

ctx, cancel := context.WithCancel(context.Background())
ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGQUIT, syscall.SIGTERM)
defer cancel()

go func() {
<-interrupt
cancel()
startWs(config, ctx)
ticker.Stop()
log.Println("Shutting down")
}()
Expand Down Expand Up @@ -226,6 +142,8 @@ func runDiscovery(parentCtx context.Context, config *Config, networks []string)
}

func saveConfigs(ctx context.Context, config *Config, exporters Exporters) {
mutex.Lock()
defer mutex.Unlock()
for name, addresses := range exporters {
if ctx.Err() != nil {
return
Expand Down Expand Up @@ -260,16 +178,11 @@ func discoverNetwork(network string, queue chan func(context.Context), exporter
port := data.port
logrus.Debugf("scanning port: %s:%s", ip, port)
var exporters []string
if port == ExporterExporterPort {
exporters, err = checkExporterExporter(ctx, ip, port)
if err != nil {
if !errors.Is(err, context.DeadlineExceeded) && !IsTimeout(err) {
logrus.Errorf("error fetching from exporter_exporter: %s", err)
}
logrus.Debugf("%s:%s was not open", ip, port)
continue
exporters, err = checkExporterExporter(ctx, ip, port)
if err != nil {
if !errors.Is(err, context.DeadlineExceeded) && !IsTimeout(err) {
logrus.Debugf("error fetching from exporter_exporter: %s", err)
}
} else if !alive(ctx, ip, port, data.path) {
logrus.Debugf("%s:%s was not open", ip, port)
continue
}
Expand Down Expand Up @@ -297,18 +210,7 @@ func discoverNetwork(network string, queue chan func(context.Context), exporter
}
exporter <- &a
}
return // we found exporter_exporter dont scan other ports so return here
}

a := Address{
IP: strings.TrimSpace(ip),
Hostname: strings.TrimSpace(hostname),
Subnet: strings.TrimSpace(network),
Exporter: data.filename,
Port: port,
}

exporter <- &a
}
}
}
Expand All @@ -335,9 +237,9 @@ func getOldGroups(path string) ([]Group, error) {
func writeFileSDConfig(config *Config, exporterName string, addresses []Address) error {
path := filepath.Join(config.FileSdPath, exporterName+".json")

if _, err := os.Stat(config.FileSdPath); os.IsNotExist(err) {
os.MkdirAll(config.FileSdPath, 0755)
}
if _, err := os.Stat(config.FileSdPath); os.IsNotExist(err) {
os.MkdirAll(config.FileSdPath, 0755)
}

groups := []Group{}

Expand All @@ -349,7 +251,7 @@ func writeFileSDConfig(config *Config, exporterName string, addresses []Address)
"host": v.Hostname,
},
}
if v.Port == ExporterExporterPort {
if v.Port == config.ExpoterExporterPort {
group.Labels["__metrics_path__"] = "/proxy"
group.Labels["__param_module"] = exporterName
}
Expand Down
6 changes: 4 additions & 2 deletions models.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ type Config struct {
// Interval is how often to scan. Default 60m
Interval string `default:"60m"`
// FileSdPath specifies where to put your generated files. Example /etc/prometheus/file_sd/
FileSdPath string
Log fnxlogrus.Config
FileSdPath string
Log fnxlogrus.Config
Port string `default:"8080"`
ExpoterExporterPort string `default:"9999"`
}

// Exporters is a list of addresses grouped by exporter name.
Expand Down
Loading

0 comments on commit 57b2dcc

Please sign in to comment.