forked from nodebreaker0-0/hlmon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
369 lines (317 loc) · 10.7 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
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
package main
import (
"bytes"
"context"
"encoding/json"
"fmt"
"log"
"net/http"
"os"
"os/exec"
"sort"
"strconv"
"time"
"github.com/BurntSushi/toml"
"github.com/PagerDuty/go-pagerduty"
)
// Config struct defines configuration values from the TOML file.
type Config struct {
SlackWebhookURL string `toml:"slack_webhook_url"`
PagerDutyRoutingKey string `toml:"pagerduty_routing_key"`
BasePath string `toml:"base_path"`
ValidatorAddress string `toml:"validator_address"`
CheckInterval int `toml:"check_interval"`
AlertThresholdSuccess float64 `toml:"alert_threshold_success"`
AlertThresholdAck float64 `toml:"alert_threshold_ack"`
LogUpdateInterval int `toml:"log_update_interval"`
}
// ValidatorData stores the health and heartbeat status of the validator.
type ValidatorData struct {
HomeValidator string `json:"home_validator"`
CurrentJailedValidators []string `json:"current_jailed_validators"`
ValidatorsMissingHeartbeat []string `json:"validators_missing_heartbeat"`
HeartbeatStatuses map[string]HeartbeatStatus `json:"heartbeat_statuses"`
}
// HeartbeatStatus holds details of the heartbeat status.
type HeartbeatStatus struct {
SinceLastSuccess float64 `json:"since_last_success"`
LastAckDuration *float64 `json:"last_ack_duration"`
}
// LogArrayEntry holds a single log entry for monitoring.
type LogArrayEntry struct {
Timestamp string `json:"timestamp"`
Validator ValidatorData `json:"validator_data"`
}
// sendSlackAlert sends a message to a Slack channel using a webhook.
func sendSlackAlert(webhookURL, message string) {
payload := map[string]string{"text": message}
payloadBytes, err := json.Marshal(payload)
if err != nil {
log.Printf("Failed to marshal Slack payload: %v", err)
return
}
req, err := http.NewRequest("POST", webhookURL, bytes.NewBuffer(payloadBytes))
if err != nil {
log.Printf("Failed to create Slack request: %v", err)
return
}
req.Header.Set("Content-Type", "application/json")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
log.Printf("Failed to send Slack alert: %v", err)
return
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
log.Printf("Slack alert returned non-200 status: %d", resp.StatusCode)
}
}
// sendPagerDutyAlert triggers a PagerDuty incident using the provided routing key.
func sendPagerDutyAlert(routingKey, description string) {
event := pagerduty.V2Event{
RoutingKey: routingKey,
Action: "trigger",
Payload: &pagerduty.V2Payload{
Summary: description,
Source: "validator-monitoring-script",
Severity: "critical",
Component: "Validator Monitoring",
},
}
_, err := pagerduty.ManageEventWithContext(context.Background(), event)
if err != nil {
log.Printf("PagerDuty API Error: %s\n", err)
}
}
// UnmarshalJSON custom unmarshaller for ValidatorData to handle nested JSON array.
func (vd *ValidatorData) UnmarshalJSON(data []byte) error {
// Create a temporary struct for the standard fields
type Alias ValidatorData
aux := &struct {
HeartbeatStatuses [][]interface{} `json:"heartbeat_statuses"`
*Alias
}{
Alias: (*Alias)(vd),
}
// Unmarshal the JSON into the auxiliary structure
if err := json.Unmarshal(data, &aux); err != nil {
return err
}
// Convert HeartbeatStatuses from array to map
vd.HeartbeatStatuses = make(map[string]HeartbeatStatus)
for _, entry := range aux.HeartbeatStatuses {
if len(entry) != 2 {
continue
}
key, ok := entry[0].(string)
if !ok {
continue
}
valueBytes, err := json.Marshal(entry[1])
if err != nil {
continue
}
var heartbeatStatus HeartbeatStatus
if err := json.Unmarshal(valueBytes, &heartbeatStatus); err != nil {
continue
}
vd.HeartbeatStatuses[key] = heartbeatStatus
}
return nil
}
// main function reads configuration, processes the latest log file, and monitors.
func main() {
var config Config
if _, err := toml.DecodeFile("config.toml", &config); err != nil {
log.Fatalf("Error loading configuration: %s\n", err)
}
var lastLogTimestamp time.Time
for {
latestLogFile, err := findLatestLogFile(config.BasePath)
if err != nil {
log.Fatalf("Failed to find latest log file: %v", err)
}
println(latestLogFile)
file, err := os.Open(latestLogFile)
if err != nil {
log.Printf("Error opening log file: %s\n", err)
time.Sleep(30 * time.Second)
continue
}
decoder := json.NewDecoder(file)
var lastRawEntry json.RawMessage
for {
var rawEntry json.RawMessage
if err := decoder.Decode(&rawEntry); err != nil {
if err.Error() == "EOF" {
break
}
log.Printf("Error decoding JSON line: %s\n", err)
continue
}
lastRawEntry = rawEntry
}
if lastRawEntry != nil {
// Attempt to unmarshal as an array containing a timestamp and data
var logArray []interface{}
if err := json.Unmarshal(lastRawEntry, &logArray); err == nil && len(logArray) == 2 {
// Get only the last element
timestamp, ok := logArray[0].(string)
if !ok {
log.Printf("Error: Expected timestamp as first element, got: %v", logArray[0])
continue
}
validatorDataBytes, err := json.Marshal(logArray[1])
if err != nil {
log.Printf("Error marshaling validator data: %s", err)
continue
}
var validatorData ValidatorData
if err := json.Unmarshal(validatorDataBytes, &validatorData); err != nil {
log.Printf("Error decoding validator data: %s", err)
continue
}
// Create the log entry with the last element
logEntry := LogArrayEntry{
Timestamp: timestamp,
Validator: validatorData,
}
// Process the last log entry only
processLogEntry(logEntry, config)
if contains(logEntry.Validator.CurrentJailedValidators, config.ValidatorAddress) {
alertMessage := fmt.Sprintf("Automatic jail state due to an update or chain halt. Attempt to unjail, which usually takes an hour or more. recoverying..")
log.Println("Validator is jailed, executing unjail script.")
sendSlackAlert(config.SlackWebhookURL, alertMessage)
executeUnjailScript(config.BasePath)
}
// Update last log timestamp
parsedTimestamp, err := time.Parse("2006-01-02T15:04:05.999999999", timestamp)
if err != nil {
log.Printf("Error parsing timestamp: %s", err)
continue
}
lastLogTimestamp = parsedTimestamp
} else {
log.Printf("Error: Could not unmarshal JSON line as expected array")
}
}
file.Close()
// Check if the log file has not been updated for more than 5 minutes
if !lastLogTimestamp.IsZero() && time.Since(lastLogTimestamp) > time.Duration(config.LogUpdateInterval)*time.Second {
alertMessage := fmt.Sprintf("Alert for HyperLiq validator: no updates for 5 minutes, the hl-visor (or consensus) should be considered dead.")
sendSlackAlert(config.SlackWebhookURL, alertMessage)
sendPagerDutyAlert(config.PagerDutyRoutingKey, alertMessage)
log.Println(alertMessage)
}
time.Sleep(time.Duration(config.CheckInterval) * time.Second)
}
}
// contains checks if a string is in a slice of strings
func contains(slice []string, item string) bool {
for _, s := range slice {
if s == item {
return true
}
}
return false
}
// executeUnjailScript runs the unjail script located at /data/unjail.sh
func executeUnjailScript(basePath string) {
cmd := exec.Command("/bin/sh", fmt.Sprintf("%s/%s", basePath, "unjail.sh"))
output, err := cmd.CombinedOutput()
if err != nil {
log.Printf("Failed to execute unjail script: %v", err)
}
log.Printf("Unjail script output: %s", output)
}
func formatLastAckDuration(d *float64) string {
if d == nil {
return "N/A" // 기본값 또는 설명 메시지
}
return fmt.Sprintf("%.6f", *d) // 소수점 6자리까지 출력
}
// processLogEntry checks if thresholds are exceeded and triggers alerts accordingly.
func processLogEntry(logEntry LogArrayEntry, config Config) {
log.Printf("Timestamp: %s\n", logEntry.Timestamp)
if status, found := logEntry.Validator.HeartbeatStatuses[config.ValidatorAddress]; found {
lastAckDurationStr := formatLastAckDuration(status.LastAckDuration)
if status.SinceLastSuccess > config.AlertThresholdSuccess ||
(status.LastAckDuration != nil && *status.LastAckDuration > config.AlertThresholdAck) ||
status.LastAckDuration == nil {
alertMessage := fmt.Sprintf(
"Alert for HyperLiq validator %s:\nsince_last_success = %v, last_ack_duration = %s (Value Exceeded)",
config.ValidatorAddress, status.SinceLastSuccess, lastAckDurationStr,
)
sendSlackAlert(config.SlackWebhookURL, alertMessage)
sendPagerDutyAlert(config.PagerDutyRoutingKey, alertMessage)
log.Println(alertMessage)
}
} else {
alertMessage := fmt.Sprintf("Validator %s not found in heartbeat statuses.", config.ValidatorAddress)
sendSlackAlert(config.SlackWebhookURL, alertMessage)
sendPagerDutyAlert(config.PagerDutyRoutingKey, alertMessage)
log.Printf("Validator %s not found in heartbeat statuses.", config.ValidatorAddress)
}
}
// findLatestLogFile finds the latest log file to be processed.
func findLatestLogFile(basePath string) (string, error) {
latestDateDir, err := findLatestDir(basePath)
if err != nil {
return "", fmt.Errorf("failed to find latest date directory: %w", err)
}
latestLogFile, err := findLatestFile(latestDateDir)
if err != nil {
return "", fmt.Errorf("failed to find latest log file: %w", err)
}
return latestLogFile, nil
}
// findLatestDir returns the latest directory based on lexicographical order.
func findLatestDir(basePath string) (string, error) {
entries, err := os.ReadDir(basePath)
if err != nil {
return "", err
}
var dirs []string
for _, entry := range entries {
if entry.IsDir() {
dirs = append(dirs, entry.Name())
}
}
if len(dirs) == 0 {
return "", fmt.Errorf("no directories found in %s", basePath)
}
// Sort directories in lexicographical order to ensure latest date is last
sort.Slice(dirs, func(i, j int) bool {
return dirs[i] < dirs[j]
})
latestDir := dirs[len(dirs)-1]
return fmt.Sprintf("%s/%s", basePath, latestDir), nil
}
// findLatestFile returns the latest log file within a directory.
func findLatestFile(dirPath string) (string, error) {
entries, err := os.ReadDir(dirPath)
if err != nil {
return "", err
}
var files []string
for _, entry := range entries {
if !entry.IsDir() {
files = append(files, entry.Name())
}
}
if len(files) == 0 {
return "", fmt.Errorf("no files found in %s", dirPath)
}
// Sort files to ensure correct order
sort.Slice(files, func(i, j int) bool {
iInt, errI := strconv.Atoi(files[i])
jInt, errJ := strconv.Atoi(files[j])
if errI == nil && errJ == nil {
return iInt < jInt
}
return files[i] < files[j]
})
latestFile := files[len(files)-1]
return fmt.Sprintf("%s/%s", dirPath, latestFile), nil
}