|
|
@ -21,7 +21,8 @@ type Config struct {
|
|
|
|
Targets []struct {
|
|
|
|
Targets []struct {
|
|
|
|
// Name of the target. Used to identify the target from Prometheus.
|
|
|
|
// Name of the target. Used to identify the target from Prometheus.
|
|
|
|
Name string `yaml:"name"`
|
|
|
|
Name string `yaml:"name"`
|
|
|
|
// URL of the target. The target should be accessible from the machine running the exporter. The URL should contain the protocol (http:// or https://) and the port if it's not the default one.
|
|
|
|
// URL of the target. The target should be accessible from the machine running the exporter.
|
|
|
|
|
|
|
|
// The URL should contain the protocol (http:// or https://) and the port if it's not the default one.
|
|
|
|
Url string `yaml:"url"`
|
|
|
|
Url string `yaml:"url"`
|
|
|
|
// Interval to ping the target. Default is 5 seconds
|
|
|
|
// Interval to ping the target. Default is 5 seconds
|
|
|
|
Interval int `yaml:"interval,omitempty"`
|
|
|
|
Interval int `yaml:"interval,omitempty"`
|
|
|
@ -51,11 +52,13 @@ func NewConfig(configPath string) (*Config, error) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func Ping(url string) (int, time.Duration, error) {
|
|
|
|
func Ping(url string) (int, time.Duration, error) {
|
|
|
|
req, err := http.NewRequest("HEAD", url, nil)
|
|
|
|
req, err := http.NewRequest(http.MethodHead, url, nil)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
return 0, 0, err
|
|
|
|
return 0, 0, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var start, connect, dns, tlsHandshake time.Time
|
|
|
|
var start, connect, dns, tlsHandshake time.Time
|
|
|
|
|
|
|
|
|
|
|
|
trace := &httptrace.ClientTrace{
|
|
|
|
trace := &httptrace.ClientTrace{
|
|
|
|
DNSStart: func(dsi httptrace.DNSStartInfo) { dns = time.Now() },
|
|
|
|
DNSStart: func(dsi httptrace.DNSStartInfo) { dns = time.Now() },
|
|
|
|
DNSDone: func(ddi httptrace.DNSDoneInfo) {
|
|
|
|
DNSDone: func(ddi httptrace.DNSDoneInfo) {
|
|
|
@ -76,18 +79,21 @@ func Ping(url string) (int, time.Duration, error) {
|
|
|
|
fmt.Printf("Time from start to first byte: %v\n", time.Since(start))
|
|
|
|
fmt.Printf("Time from start to first byte: %v\n", time.Since(start))
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
req = req.WithContext(httptrace.WithClientTrace(req.Context(), trace))
|
|
|
|
req = req.WithContext(httptrace.WithClientTrace(req.Context(), trace))
|
|
|
|
start = time.Now()
|
|
|
|
start = time.Now()
|
|
|
|
|
|
|
|
|
|
|
|
resp, err := http.DefaultTransport.RoundTrip(req)
|
|
|
|
resp, err := http.DefaultTransport.RoundTrip(req)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
return 0, 0, err
|
|
|
|
return 0, 0, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
resp.Body.Close()
|
|
|
|
|
|
|
|
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
|
|
|
|
return resp.StatusCode, time.Since(start), nil
|
|
|
|
return resp.StatusCode, time.Since(start), nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
func main() {
|
|
|
|
|
|
|
|
|
|
|
|
// Load config
|
|
|
|
// Load config
|
|
|
|
// TODO: Use a flag to specify the config file
|
|
|
|
// TODO: Use a flag to specify the config file
|
|
|
|
config, err := NewConfig("config/config.yaml")
|
|
|
|
config, err := NewConfig("config/config.yaml")
|
|
|
@ -100,7 +106,7 @@ func main() {
|
|
|
|
// create a scheduler
|
|
|
|
// create a scheduler
|
|
|
|
s, err := gocron.NewScheduler()
|
|
|
|
s, err := gocron.NewScheduler()
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
// handle error
|
|
|
|
log.Fatalf("Error creating the scheduler: %s",err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Prometheus metrics
|
|
|
|
// Prometheus metrics
|
|
|
@ -112,7 +118,7 @@ func main() {
|
|
|
|
|
|
|
|
|
|
|
|
// Intercept the signal to stop the program
|
|
|
|
// Intercept the signal to stop the program
|
|
|
|
go func() {
|
|
|
|
go func() {
|
|
|
|
sigchan := make(chan os.Signal)
|
|
|
|
sigchan := make(chan os.Signal, 1)
|
|
|
|
signal.Notify(sigchan, os.Interrupt)
|
|
|
|
signal.Notify(sigchan, os.Interrupt)
|
|
|
|
<-sigchan
|
|
|
|
<-sigchan
|
|
|
|
log.Println("Program killed !")
|
|
|
|
log.Println("Program killed !")
|
|
|
@ -122,7 +128,7 @@ func main() {
|
|
|
|
// when you're done, shut it down
|
|
|
|
// when you're done, shut it down
|
|
|
|
err = s.Shutdown()
|
|
|
|
err = s.Shutdown()
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
// handle error
|
|
|
|
log.Fatalf("Error shutting down scheduler: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
os.Exit(0)
|
|
|
|
os.Exit(0)
|
|
|
@ -134,8 +140,11 @@ func main() {
|
|
|
|
gocron.DurationJob(
|
|
|
|
gocron.DurationJob(
|
|
|
|
func(interval int) time.Duration {
|
|
|
|
func(interval int) time.Duration {
|
|
|
|
if interval == 0 {
|
|
|
|
if interval == 0 {
|
|
|
|
return time.Duration(5) * time.Second
|
|
|
|
const defaultInterval = 5
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return time.Duration(defaultInterval) * time.Second
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return time.Duration(interval) * time.Second
|
|
|
|
return time.Duration(interval) * time.Second
|
|
|
|
}(target.Interval),
|
|
|
|
}(target.Interval),
|
|
|
|
),
|
|
|
|
),
|
|
|
@ -144,8 +153,10 @@ func main() {
|
|
|
|
status, elapsedTime, err := Ping(url)
|
|
|
|
status, elapsedTime, err := Ping(url)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
fmt.Println(err)
|
|
|
|
|
|
|
|
|
|
|
|
return
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
fmt.Printf("%s - Status: %d in %v\n", target.Name, status, elapsedTime.Seconds())
|
|
|
|
fmt.Printf("%s - Status: %d in %v\n", target.Name, status, elapsedTime.Seconds())
|
|
|
|
// push to Prometheus
|
|
|
|
// push to Prometheus
|
|
|
|
responseTimeMonitor.With(prometheus.Labels{"target_name": target.Name}).Set(elapsedTime.Seconds())
|
|
|
|
responseTimeMonitor.With(prometheus.Labels{"target_name": target.Name}).Set(elapsedTime.Seconds())
|
|
|
@ -154,7 +165,7 @@ func main() {
|
|
|
|
),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
// handle error
|
|
|
|
log.Fatalf("Error creating job: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// each job has a unique id
|
|
|
|
// each job has a unique id
|
|
|
|
fmt.Printf("Job %s started with ID: %s\n", target.Name, j.ID().String())
|
|
|
|
fmt.Printf("Job %s started with ID: %s\n", target.Name, j.ID().String())
|
|
|
@ -163,12 +174,9 @@ func main() {
|
|
|
|
// start the scheduler
|
|
|
|
// start the scheduler
|
|
|
|
s.Start()
|
|
|
|
s.Start()
|
|
|
|
|
|
|
|
|
|
|
|
// // block until you are ready to shut down
|
|
|
|
|
|
|
|
// select {
|
|
|
|
|
|
|
|
// // case <-time.After(time.Minute):
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Serve Prometheus metrics
|
|
|
|
// Serve Prometheus metrics
|
|
|
|
http.Handle("/metrics", promhttp.Handler())
|
|
|
|
http.Handle("/metrics", promhttp.Handler())
|
|
|
|
http.ListenAndServe(":2112", nil)
|
|
|
|
if err := http.ListenAndServe(":2112", nil); err != nil {
|
|
|
|
|
|
|
|
log.Fatalf("Error starting server: %s", err)
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|