-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
95 lines (78 loc) · 2.11 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package main
import (
"encoding/json"
"fmt"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/spf13/cobra"
"io/ioutil"
"log"
"net/http"
"time"
)
var (
up = prometheus.NewGauge(prometheus.GaugeOpts{
Name: "total_up_nodes",
Help: "Current number of up node.",
})
down = prometheus.NewGauge(prometheus.GaugeOpts{
Name: "total_down_nodes",
Help: "Current number of down node.",
})
inactive = prometheus.NewGauge(prometheus.GaugeOpts{
Name: "total_inactive_nodes",
Help: "Current number of inactive node.",
})
url = "https://securenodes2.eu.zensystem.io/api/grid/nodes"
)
func init() {
// Metrics have to be registered to be exposed:
prometheus.MustRegister(up)
prometheus.MustRegister(down)
prometheus.MustRegister(inactive)
}
func main() {
//CLI setup using cobra
var duration int
var rootCmd = &cobra.Command{
Use: "app",
}
rootCmd.PersistentFlags().IntVarP(&duration, "duration", "d", 5, "Duration of interval between calls.")
rootCmd.Execute()
//loop in interval to get node status
ticker := time.NewTicker(time.Second * time.Duration(duration))
go func() {
for range ticker.C {
response, err := http.Get(url)
if err != nil {
fmt.Println(err)
return
}
textBytes, err := ioutil.ReadAll(response.Body)
if err != nil {
fmt.Println(err)
return
}
ns := nodeStatus{}
if err := json.Unmarshal(textBytes, &ns); err != nil {
panic(err)
}
up.Set(ns.Userdata.Up)
down.Set(ns.Userdata.Down)
inactive.Set(ns.Userdata.Inactive)
fmt.Printf("UP: %.f\tDOWN: %.f\tINACTIVE: %.f\n", ns.Userdata.Up, ns.Userdata.Down, ns.Userdata.Inactive)
fmt.Println("Nodes status updated.")
}
}()
// The Handler function provides a default handler to expose metrics
// via an HTTP server. "/metrics" is the usual endpoint for that.
http.Handle("/metrics", promhttp.Handler())
log.Fatal(http.ListenAndServe(":8080", nil))
}
type nodeStatus struct {
Userdata struct {
Up float64 `json:"up"`
Down float64 `json:"down"`
Inactive float64 `json:"inactive"`
} `json:"userdata"`
}