start http only after successful scrape
This commit is contained in:
parent
c194de1861
commit
1507dcc158
1 changed files with 16 additions and 14 deletions
30
main.go
30
main.go
|
@ -88,6 +88,16 @@ func readRegs(client modbus.Client, start, qty uint16) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func scrape(client modbus.Client) {
|
||||||
|
start := time.Now()
|
||||||
|
scrapeStart.SetToCurrentTime()
|
||||||
|
readRegs(client, 5000, 50)
|
||||||
|
readRegs(client, 5100, 50)
|
||||||
|
scrapeEnd.SetToCurrentTime()
|
||||||
|
scrapeDuration.Set(time.Since(start).Seconds())
|
||||||
|
scrapeCounter.Inc()
|
||||||
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
scrapeIntervalGauge.Set(float64(*scrapeInterval))
|
scrapeIntervalGauge.Set(float64(*scrapeInterval))
|
||||||
|
@ -117,10 +127,9 @@ func main() {
|
||||||
io.Copy(conn, sgc)
|
io.Copy(conn, sgc)
|
||||||
}()
|
}()
|
||||||
|
|
||||||
// HTTP stuff
|
// HTTP setup
|
||||||
http.Handle("/metrics", promhttp.Handler())
|
http.Handle("/metrics", promhttp.Handler())
|
||||||
http.HandleFunc("/", statusHandler)
|
http.HandleFunc("/", statusHandler)
|
||||||
go http.ListenAndServe(*httpAddr, nil)
|
|
||||||
|
|
||||||
// Modbus scrape loop
|
// Modbus scrape loop
|
||||||
handler := modbus.NewTCPClientHandler(ln.Addr().String())
|
handler := modbus.NewTCPClientHandler(ln.Addr().String())
|
||||||
|
@ -128,17 +137,10 @@ func main() {
|
||||||
handler.Connect()
|
handler.Connect()
|
||||||
defer handler.Close()
|
defer handler.Close()
|
||||||
client := modbus.NewClient(handler)
|
client := modbus.NewClient(handler)
|
||||||
|
scrape(client)
|
||||||
ticker := time.Tick(*scrapeInterval)
|
// Start http interface only after first successful scrape
|
||||||
for {
|
go http.ListenAndServe(*httpAddr, nil)
|
||||||
start := time.Now()
|
for range time.Tick(*scrapeInterval) {
|
||||||
scrapeStart.SetToCurrentTime()
|
scrape(client)
|
||||||
readRegs(client, 5000, 50)
|
|
||||||
readRegs(client, 5100, 50)
|
|
||||||
scrapeEnd.SetToCurrentTime()
|
|
||||||
scrapeDuration.Set(time.Since(start).Seconds())
|
|
||||||
scrapeCounter.Inc()
|
|
||||||
|
|
||||||
<-ticker
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue