Skip to content
This repository has been archived by the owner on Aug 31, 2020. It is now read-only.

Commit

Permalink
implemented a basic discovery loop
Browse files Browse the repository at this point in the history
  • Loading branch information
nustiueudinastea committed Mar 24, 2017
1 parent cf6d1d9 commit 6c459a5
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions swarm.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"io/ioutil"
"net"
"time"

"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/filters"
Expand All @@ -16,6 +17,7 @@ import (
)

var prometheusService string
var discoveryInterval int

// allocateIP returns the 3rd last IP in the network range.
func allocateIP(netCIDR *net.IPNet) string {
Expand Down Expand Up @@ -109,8 +111,7 @@ type scrapeTarget struct {
Labels map[string]string
}

func discoverSwarm(cmd *cobra.Command, args []string) {
findPrometheusContainer(prometheusService)
func discoverSwarm() {
cli, err := client.NewEnvClient()
if err != nil {
panic(err)
Expand Down Expand Up @@ -159,15 +160,23 @@ func discoverSwarm(cmd *cobra.Command, args []string) {
writeSDConfig(scrapeTargets)
}

func discoveryProcess(cmd *cobra.Command, args []string) {
for {
discoverSwarm()
time.Sleep(time.Duration(discoveryInterval) * time.Second)
}
}

func main() {

var cmdDiscover = &cobra.Command{
Use: "discover",
Short: "Starts Swarm service discovery",
Run: discoverSwarm,
Run: discoveryProcess,
}

cmdDiscover.Flags().StringVarP(&prometheusService, "prometheus", "p", "prometheus", "Name of the Prometheus service")
cmdDiscover.Flags().IntVarP(&discoveryInterval, "interval", "i", 30, "The interval, in seconds, at which the discovery process is kicked off")

var rootCmd = &cobra.Command{Use: "promswarm"}
rootCmd.AddCommand(cmdDiscover)
Expand Down

0 comments on commit 6c459a5

Please sign in to comment.