-
Notifications
You must be signed in to change notification settings - Fork 18
/
main.go
134 lines (108 loc) · 3.89 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
package main
import (
"fmt"
"time"
"github.com/cosmos/cosmos-sdk/simapp"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/spf13/cobra"
)
type Params struct {
AvgBlockTime float64
SignedBlocksWindow int64
MissedBlocksToJail int64
}
func Execute(configPath string) {
appConfig, err := LoadConfig(configPath)
if err != nil {
GetDefaultLogger().Fatal().Err(err).Msg("Could not load config")
}
appConfig.Validate() // will exit if not valid
appConfig.SetBechPrefixes() // will exit if not valid
SetSdkConfigPrefixes(appConfig)
log := GetLogger(appConfig.LogConfig)
if len(appConfig.IncludeValidators) == 0 && len(appConfig.ExcludeValidators) == 0 {
log.Info().Msg("Monitoring all validators")
} else if len(appConfig.IncludeValidators) != 0 {
log.Info().
Strs("validators", appConfig.IncludeValidators).
Msg("Monitoring specific validators")
} else {
log.Info().
Strs("validators", appConfig.ExcludeValidators).
Msg("Monitoring all validators except specific")
}
encCfg := simapp.MakeTestEncodingConfig()
interfaceRegistry := encCfg.InterfaceRegistry
rpc := NewTendermintRPC(appConfig.NodeConfig, log)
grpc := NewTendermintGRPC(appConfig.NodeConfig, interfaceRegistry, appConfig.QueryEachSigningInfo, log)
slashingParams := grpc.GetSlashingParams()
params := Params{
AvgBlockTime: rpc.GetAvgBlockTime(),
SignedBlocksWindow: slashingParams.SignedBlocksWindow,
MissedBlocksToJail: slashingParams.MissedBlocksToJail,
}
log.Info().
Int64("missedBlocksToJail", params.MissedBlocksToJail).
Float64("avgBlockTime", params.AvgBlockTime).
Msg("Chain params calculated")
appConfig.SetDefaultMissedBlocksGroups(params)
if err := appConfig.MissedBlocksGroups.Validate(params.SignedBlocksWindow); err != nil {
log.Fatal().Err(err).Msg("MissedBlockGroups config is invalid")
}
log.Info().
Str("config", fmt.Sprintf("%+v", appConfig)).
Msg("Started with following parameters")
reporters := []Reporter{
NewTelegramReporter(appConfig.ChainInfoConfig, appConfig.TelegramConfig, appConfig, ¶ms, grpc, log),
NewSlackReporter(appConfig.ChainInfoConfig, appConfig.SlackConfig, ¶ms, log),
}
for _, reporter := range reporters {
reporter.Init()
if reporter.Enabled() {
log.Info().Str("name", reporter.Name()).Msg("Init reporter")
}
}
reportGenerator := NewReportGenerator(params, grpc, appConfig, log, interfaceRegistry)
for {
report := reportGenerator.GenerateReport()
if report == nil || len(report.Entries) == 0 {
log.Info().Msg("Report is empty, not sending.")
time.Sleep(time.Duration(appConfig.Interval) * time.Second)
continue
}
for _, reporter := range reporters {
if !reporter.Enabled() {
log.Debug().Str("name", reporter.Name()).Msg("Reporter is disabled.")
continue
}
log.Info().Str("name", reporter.Name()).Msg("Sending a report to reporter...")
if err := reporter.SendReport(*report); err != nil {
log.Error().Err(err).Str("name", reporter.Name()).Msg("Could not send message")
}
}
time.Sleep(time.Duration(appConfig.Interval) * time.Second)
}
}
func SetSdkConfigPrefixes(appConfig *AppConfig) {
config := sdk.GetConfig()
config.SetBech32PrefixForValidator(appConfig.ValidatorPrefix, appConfig.ValidatorPubkeyPrefix)
config.SetBech32PrefixForConsensusNode(appConfig.ConsensusNodePrefix, appConfig.ConsensusNodePubkeyPrefix)
config.Seal()
}
func main() {
var ConfigPath string
rootCmd := &cobra.Command{
Use: "tendermint-exporter",
Long: "Scrape the data on Tendermint node.",
Run: func(cmd *cobra.Command, args []string) {
Execute(ConfigPath)
},
}
rootCmd.PersistentFlags().StringVar(&ConfigPath, "config", "", "Config file path")
if err := rootCmd.MarkPersistentFlagRequired("config"); err != nil {
GetDefaultLogger().Fatal().Err(err).Msg("Could not set flags")
}
if err := rootCmd.Execute(); err != nil {
GetDefaultLogger().Fatal().Err(err).Msg("Could not start application")
}
}