Skip to content
This repository has been archived by the owner on Feb 22, 2024. It is now read-only.

Commit

Permalink
Merge pull request #2 from strangelove-ventures/andrew/config_file_pa…
Browse files Browse the repository at this point in the history
…th_flag

Add flag for config file path
  • Loading branch information
agouin committed Feb 27, 2022
2 parents bf0323d + 2dd1a30 commit 7d40c61
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 9 deletions.
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,15 @@ You can optionally provide the `sentries` array to also monitor the sentries via
See [here](https://support.discord.com/hc/en-us/articles/228383668-Intro-to-Webhooks) for how to create a webhook for a discord channel.

Begin monitoring with:

```bash
half-life monitor
```

By default, `half-life monitor` will look for `config.yaml` in the current working directory. To specify a different config file path, use the `--file`/`-f` flag:

```bash
halflife monitor
half-life monitor -f ~/config.yaml
```

When a validator is first added to `config.yaml` and halflife is started, a status message will be created in the discord channel and the ID of that message will be added to `config.yaml`. Pin this message so that the channel's pinned messages can act as a dashboard to see the realtime status of the validators.
Expand Down
2 changes: 1 addition & 1 deletion cmd/NotificationService.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ type NotificationService interface {
SendValidatorAlertNotification(config *HalfLifeConfig, vm *ValidatorMonitor, stats ValidatorStats, alertNotification *ValidatorAlertNotification)

// update (or create) realtime status for validator
UpdateValidatorRealtimeStatus(config *HalfLifeConfig, vm *ValidatorMonitor, stats ValidatorStats, writeConfigMutex *sync.Mutex)
UpdateValidatorRealtimeStatus(configFile string, config *HalfLifeConfig, vm *ValidatorMonitor, stats ValidatorStats, writeConfigMutex *sync.Mutex)
}
4 changes: 2 additions & 2 deletions cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ type ValidatorMonitor struct {
Sentries *[]Sentry `yaml:"sentries"`
}

func saveConfig(config *HalfLifeConfig, writeConfigMutex *sync.Mutex) {
func saveConfig(configFile string, config *HalfLifeConfig, writeConfigMutex *sync.Mutex) {
writeConfigMutex.Lock()
defer writeConfigMutex.Unlock()

Expand All @@ -128,7 +128,7 @@ func saveConfig(config *HalfLifeConfig, writeConfigMutex *sync.Mutex) {
fmt.Printf("Error during config yaml marshal %v\n", err)
}

err = os.WriteFile(configFilePath, yamlBytes, 0644)
err = os.WriteFile(configFile, yamlBytes, 0644)
if err != nil {
fmt.Printf("Error saving config yaml %v\n", err)
}
Expand Down
3 changes: 2 additions & 1 deletion cmd/discord.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ func getCurrentStatsEmbed(stats ValidatorStats, vm *ValidatorMonitor) discord.Em

// implements NotificationService interface
func (service *DiscordNotificationService) UpdateValidatorRealtimeStatus(
configFile string,
config *HalfLifeConfig,
vm *ValidatorMonitor,
stats ValidatorStats,
Expand Down Expand Up @@ -138,7 +139,7 @@ func (service *DiscordNotificationService) UpdateValidatorRealtimeStatus(
messageID := string(message.ID)
vm.DiscordStatusMessageID = &messageID
fmt.Printf("Saved message ID: %s\n", messageID)
saveConfig(config, writeConfigMutex)
saveConfig(configFile, config, writeConfigMutex)
}
}

Expand Down
8 changes: 5 additions & 3 deletions cmd/monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ var monitorCmd = &cobra.Command{
Short: "Daemon to monitor validators",
Long: "Monitors validators and pushes alerts to Discord using the configuration in config.yaml",
Run: func(cmd *cobra.Command, args []string) {
dat, err := os.ReadFile(configFilePath)
configFile, _ := cmd.Flags().GetString("file")
dat, err := os.ReadFile(configFile)
if err != nil {
log.Fatalf("Error reading config.yaml: %v", err)
}
Expand Down Expand Up @@ -48,14 +49,15 @@ var monitorCmd = &cobra.Command{
alertState := make(map[string]*ValidatorAlertState)
for i, vm := range config.Validators {
if i == len(config.Validators)-1 {
runMonitor(notificationService, &alertState, &config, vm, &writeConfigMutex)
runMonitor(notificationService, &alertState, configFile, &config, vm, &writeConfigMutex)
} else {
go runMonitor(notificationService, &alertState, &config, vm, &writeConfigMutex)
go runMonitor(notificationService, &alertState, configFile, &config, vm, &writeConfigMutex)
}
}
},
}

func init() {
rootCmd.AddCommand(monitorCmd)
monitorCmd.Flags().StringP("file", "f", configFilePath, "File path to config yaml")
}
3 changes: 2 additions & 1 deletion cmd/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ func monitorSentries(
func runMonitor(
notificationService NotificationService,
alertState *map[string]*ValidatorAlertState,
configFile string,
config *HalfLifeConfig,
vm *ValidatorMonitor,
writeConfigMutex *sync.Mutex,
Expand Down Expand Up @@ -260,7 +261,7 @@ func runMonitor(
notificationService.SendValidatorAlertNotification(config, vm, stats, notification)
}

notificationService.UpdateValidatorRealtimeStatus(config, vm, stats, writeConfigMutex)
notificationService.UpdateValidatorRealtimeStatus(configFile, config, vm, stats, writeConfigMutex)

time.Sleep(30 * time.Second)
}
Expand Down

0 comments on commit 7d40c61

Please sign in to comment.