-
Notifications
You must be signed in to change notification settings - Fork 0
/
stargazer_exporter.go
186 lines (153 loc) · 4.36 KB
/
stargazer_exporter.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package main
import (
"bytes"
"encoding/json"
"fmt"
"log"
"net/http"
"strconv"
"strings"
"time"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/thilinapiy/stargazer_exporter/stargazer"
"gopkg.in/alecthomas/kingpin.v2"
)
var (
_ prometheus.Collector = &collector{}
sendClient = &http.Client{Timeout: 10 * time.Second}
oldStats []stargazer.MissesBlock
blockAddress = kingpin.Flag("block-address", "Hash address of the block that needs to monitor").Required().String()
bindPort = kingpin.Flag("bind-port", "Port which listens for prometheus to scrape").Default(":9119").String()
chatID = kingpin.Flag("chat-id", "Telegram chat group id").Required().String()
token = kingpin.Flag("bot-token", "Telegram bot secret token").Required().String()
)
type collector struct {
MissedBlocksTotal *prometheus.Desc
NewMissesBlocks *prometheus.Desc
stats func() ([]stargazer.MissesBlock, error)
}
func newCollector(stats func() ([]stargazer.MissesBlock, error)) prometheus.Collector {
return &collector{
MissedBlocksTotal: prometheus.NewDesc(
"stargazer_missed_blocks_total",
"Description of Stargazer",
[]string{"FirstBlock", "LastBlock"},
nil,
),
NewMissesBlocks: prometheus.NewDesc(
"new_missed_blocks",
"If there are new missed blocks this will return true, compaired to previous scrape",
[]string{"FirstBlock", "LastBlock"},
nil,
),
stats: stats,
}
}
func (c *collector) Describe(ch chan<- *prometheus.Desc) {
ds := []*prometheus.Desc{
c.MissedBlocksTotal,
c.NewMissesBlocks,
}
for _, d := range ds {
ch <- d
}
}
func (c *collector) Collect(ch chan<- prometheus.Metric) {
stats, err := c.stats()
if err != nil {
ch <- prometheus.NewInvalidMetric(c.MissedBlocksTotal, err)
return
}
for _, s := range stats {
FirstBlock := s.StartHeight
LastBlock := s.EndHeight
count, _ := strconv.Atoi(s.Count)
ch <- prometheus.MustNewConstMetric(
c.MissedBlocksTotal,
prometheus.CounterValue,
float64(count),
FirstBlock,
LastBlock,
)
}
if oldStats != nil {
for _, s := range getChanges(oldStats, stats) {
FirstBlock := s.StartHeight
LastBlock := s.EndHeight
count, _ := strconv.Atoi(s.Count)
ch <- prometheus.MustNewConstMetric(
c.NewMissesBlocks,
prometheus.CounterValue,
float64(count),
FirstBlock,
LastBlock,
)
}
}
oldStats = stats
}
func getChanges(oldStats, newStats []stargazer.MissesBlock) []stargazer.MissesBlock {
k := 0
for ; oldStats[0].StartHeight != newStats[k].StartHeight; k++ {
}
if k != 0 {
sendMessage(newStats[:k], false)
}
return newStats[:k]
}
func sendMessage(msg []stargazer.MissesBlock, alert bool) bool {
url := "https://api.telegram.org/bot" + *token + "/sendMessage"
message := ""
critical := 0
msgBuff := "The Chainflow Validator missed\n"
for _, block := range msg {
msgBuff += fmt.Sprintf("%s blocks between block numbers %s-%s\n", block.Count, block.StartHeight, block.EndHeight)
intCount, _ := strconv.Atoi(block.Count)
critical += intCount
}
msgBuff += fmt.Sprintf("at %s", time.Now().UTC())
if critical > 24 {
message = " ⚠️ " + " Critical Alert!\n"
}
message += msgBuff
for _, chat := range strings.Split(*chatID, ",") {
body := map[string]interface{}{
"chat_id": chat,
"text": message,
"disable_notification": alert,
}
bytesRepresentation, err := json.Marshal(body)
if err != nil {
log.Fatalln(err)
}
req, err := http.NewRequest("POST", url, bytes.NewBuffer(bytesRepresentation))
req.Header.Set("Content-Type", "application/json")
resp, er := sendClient.Do(req)
if er != nil {
log.Fatal("Error in request send")
return false
}
if err != nil {
log.Fatal("Error in request create")
return false
}
fmt.Printf("Send message: %s, to chat_id: %s.\n", message, chat)
defer resp.Body.Close()
}
return true
}
func main() {
kingpin.Parse()
stats := func() ([]stargazer.MissesBlock, error) {
return stargazer.GetMissedGroups(*blockAddress)
}
c := newCollector(stats)
prometheus.MustRegister(c)
mux := http.NewServeMux()
mux.Handle("/metrics", promhttp.Handler())
log.Printf("starting exporter on %q", *bindPort)
if err := http.ListenAndServe(*bindPort, mux); err != nil {
log.Fatalf("cannot start exporter: %s", err)
}
}