/
main.go
126 lines (102 loc) · 3.06 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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
// A simple package to export basic information from the nats monitoring
package main
import (
"encoding/json"
"flag"
"fmt"
"log"
"net/http"
"time"
"github.com/prometheus/client_golang/prometheus"
)
var (
addr = flag.String("web.listen-address", ":9104", "Address on which to expose metrics and web interface.")
natsaddr = flag.String("nats.address", ":8043", "Address on which gnatsd is giving metrics")
consumeTime = flag.Int("consume-time", 10, "Time between data scrapes from gnatsd monitoring")
bytesCounter = prometheus.NewCounterVec(prometheus.CounterOpts{
Namespace: "natsio",
Name: "bytes_total",
Help: "Total bytes in and out",
},
[]string{"direction"},
)
messageCounter = prometheus.NewCounterVec(prometheus.CounterOpts{
Namespace: "natsio",
Name: "msg_total",
Help: "Total messages in and out",
},
[]string{"direction"},
)
connections = prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: "natsio",
Name: "connections",
Help: "Current active connections to gnatsd deamon",
})
slowConsumers = prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: "natsio",
Name: "slow_consumers",
Help: "Amount of slow consumers",
})
)
func main() {
flag.Parse()
if *consumeTime < 1 {
log.Fatal("consume time should be >0")
}
resp, err := http.Get(fmt.Sprintf("http://%s/varz", *natsaddr))
if err != nil {
log.Fatal(err)
}
if resp.StatusCode != http.StatusOK {
log.Fatalf("expected statuscode 200 got %v", resp.StatusCode)
}
resp.Body.Close()
prometheus.MustRegister(bytesCounter)
prometheus.MustRegister(messageCounter)
prometheus.MustRegister(connections)
prometheus.MustRegister(slowConsumers)
go consume()
http.Handle("/metrics", prometheus.Handler())
if err := http.ListenAndServe(*addr, nil); err != nil {
log.Fatal(err)
}
}
func consume() {
for {
time.Sleep(time.Second * time.Duration(*consumeTime))
client := http.Client{
Timeout: time.Second * time.Duration(*consumeTime),
}
resp, err := client.Get(fmt.Sprintf("http://%s/varz", *natsaddr))
if err != nil {
log.Print(err)
continue
}
if resp.StatusCode != http.StatusOK {
log.Printf("expected statuscode 200 got %v", resp.StatusCode)
continue
}
var natsinfo struct {
BytesIn float64 `json:"in_bytes"`
BytesOut float64 `json:"out_bytes"`
MessagesIn float64 `json:"in_msgs"`
MessagesOut float64 `json:"out_msgs"`
SlowConsumers float64 `json:"slow_consumers"`
Connections float64 `json:"connections"`
MaxConnections float64 `json:"max_connections"`
}
err = json.NewDecoder(resp.Body).Decode(&natsinfo)
if err != nil {
log.Printf("could not decode nats-info got %v", err)
resp.Body.Close()
continue
}
resp.Body.Close()
bytesCounter.WithLabelValues("in").Add(natsinfo.BytesIn)
bytesCounter.WithLabelValues("out").Add(natsinfo.BytesOut)
messageCounter.WithLabelValues("in").Add(natsinfo.MessagesIn)
messageCounter.WithLabelValues("out").Add(natsinfo.MessagesOut)
connections.Set(natsinfo.Connections)
slowConsumers.Set(natsinfo.SlowConsumers)
}
}