diff --git a/manager/config/config.go b/manager/config/config.go index 7a9716a..e6ca8d0 100644 --- a/manager/config/config.go +++ b/manager/config/config.go @@ -3,7 +3,6 @@ package config import ( "fmt" "io" - "io/ioutil" "net/http" "os" "path/filepath" @@ -43,6 +42,7 @@ type Config struct { PluginInfos PluginInfo `yaml:"plugins_infos"` } +// VatzProtocolInfo is VATZ information. type VatzProtocolInfo struct { ProtocolIdentifier string `yaml:"protocol_identifier"` Port int `yaml:"port"` @@ -53,6 +53,7 @@ type VatzProtocolInfo struct { HomePath string `yaml:"home_path"` } +// AbsoluteHomePath is the default home path func (i VatzProtocolInfo) AbsoluteHomePath() (string, error) { if strings.HasPrefix(i.HomePath, "~") { homePath := os.Getenv("HOME") @@ -121,7 +122,6 @@ type Plugin struct { } type parser struct { - rawConfig map[string]interface{} } func (p *parser) loadConfigFile(path string) ([]byte, error) { @@ -140,8 +140,14 @@ func (p *parser) loadConfigFile(path string) ([]byte, error) { } rawYAML, err = io.ReadAll(resp.Body) + if err != nil { + return nil, err + } } else { - rawYAML, err = ioutil.ReadFile(path) + rawYAML, err = os.ReadFile(path) + if err != nil { + return nil, err + } } if err != nil { diff --git a/manager/dispatcher/discord.go b/manager/dispatcher/discord.go index 244102d..3ad3fb8 100644 --- a/manager/dispatcher/discord.go +++ b/manager/dispatcher/discord.go @@ -15,6 +15,7 @@ import ( "github.com/rs/zerolog/log" ) +// DiscordColor is type for discord message color. type DiscordColor int const ( @@ -39,7 +40,11 @@ func (d *discord) SetDispatcher(firstRunMsg bool, preStat tp.StateFlag, notifyIn pUnique := deliverMessage.Options["pUnique"].(string) if reqToNotify { - d.SendNotification(deliverMessage) + err := d.SendNotification(deliverMessage) + if err != nil { + log.Error().Str("module", "dispatcher").Msgf("Channel(Discord): Send notification error: %s", err) + return err + } } if reminderState == tp.ON { @@ -57,7 +62,10 @@ func (d *discord) SetDispatcher(firstRunMsg bool, preStat tp.StateFlag, notifyIn } for _, schedule := range d.reminderSchedule { id, _ := d.reminderCron.AddFunc(schedule, func() { - d.SendNotification(deliverMessage) + err := d.SendNotification(deliverMessage) + if err != nil { + log.Error().Str("module", "dispatcher").Msgf("Channel(Discord): Send notification error: %s", err) + } }) newEntries = append(newEntries, id) } diff --git a/manager/dispatcher/dispatcher.go b/manager/dispatcher/dispatcher.go index 1b4e1aa..e999a7d 100644 --- a/manager/dispatcher/dispatcher.go +++ b/manager/dispatcher/dispatcher.go @@ -2,7 +2,6 @@ package dispatcher import ( "errors" - "github.com/dsrvlabs/vatz/utils" "strings" "sync" "time" @@ -10,6 +9,7 @@ import ( pb "github.com/dsrvlabs/vatz-proto/plugin/v1" "github.com/dsrvlabs/vatz/manager/config" tp "github.com/dsrvlabs/vatz/manager/types" + "github.com/dsrvlabs/vatz/utils" "github.com/robfig/cron/v3" "github.com/rs/zerolog/log" ) @@ -38,9 +38,10 @@ type Dispatcher interface { SendNotification(request tp.ReqMsg) error } +// GetDispatchers gets the registered alert channel. func GetDispatchers(cfg config.NotificationInfo) []Dispatcher { if len(cfg.DispatchChannels) == 0 { - dpError := errors.New("Error: No Dispatcher has set.") + dpError := errors.New("error: No Dispatcher has set") log.Error().Str("module", "dispatcher").Msg("Please, Set at least a single channel for dispatcher, e.g.) Discord or Telegram") panic(dpError) } @@ -80,7 +81,6 @@ func GetDispatchers(cfg config.NotificationInfo) []Dispatcher { } } }) - return dispatcherSingletons } diff --git a/manager/dispatcher/mock_Dispatcher.go b/manager/dispatcher/mock_Dispatcher.go index 57adb8e..2d3a1fc 100644 --- a/manager/dispatcher/mock_Dispatcher.go +++ b/manager/dispatcher/mock_Dispatcher.go @@ -47,5 +47,5 @@ func NewMockDispatchers(t mockConstructorTestingTNewDispatcher) []MockDispatcher t.Cleanup(func() { mock.AssertExpectations(t) }) - return []MockDispatcher{MockDispatcher{}} + return []MockDispatcher{} } diff --git a/manager/dispatcher/pagerduty.go b/manager/dispatcher/pagerduty.go index f01da90..276397d 100644 --- a/manager/dispatcher/pagerduty.go +++ b/manager/dispatcher/pagerduty.go @@ -3,14 +3,16 @@ package dispatcher import ( "context" "fmt" + "sync" + "time" + pd "github.com/PagerDuty/go-pagerduty" pb "github.com/dsrvlabs/vatz-proto/plugin/v1" tp "github.com/dsrvlabs/vatz/manager/types" "github.com/rs/zerolog/log" - "sync" - "time" ) +// SUCCESS is string for delivering success. const SUCCESS = "success" type pagerdutyMSGEvent struct { @@ -28,7 +30,11 @@ type pagerduty struct { func (p *pagerduty) SetDispatcher(firstRunMsg bool, preStat tp.StateFlag, notifyInfo tp.NotifyInfo) error { reqToNotify, _, deliverMessage := messageHandler(firstRunMsg, preStat, notifyInfo) if reqToNotify { - p.SendNotification(deliverMessage) + err := p.SendNotification(deliverMessage) + if err != nil { + log.Error().Str("module", "dispatcher").Msgf("Channel(Pagerduty): Send notification error: %s", err) + return err + } } return nil } diff --git a/manager/dispatcher/telegram.go b/manager/dispatcher/telegram.go index 55ca382..dbe951a 100644 --- a/manager/dispatcher/telegram.go +++ b/manager/dispatcher/telegram.go @@ -4,7 +4,7 @@ import ( "bytes" "encoding/json" "fmt" - "io/ioutil" + "io" "net/http" "sync" @@ -31,7 +31,12 @@ func (t *telegram) SetDispatcher(firstRunMsg bool, preStat tp.StateFlag, notifyI pUnique := deliverMessage.Options["pUnique"].(string) if reqToNotify { - t.SendNotification(deliverMessage) + err := t.SendNotification(deliverMessage) + if err != nil { + log.Error().Str("module", "dispatcher").Msgf("Channel(Telegram): Send notification error: %s", err) + return err + } + } if reminderState == tp.ON { @@ -49,7 +54,10 @@ func (t *telegram) SetDispatcher(firstRunMsg bool, preStat tp.StateFlag, notifyI } for _, schedule := range t.reminderSchedule { id, _ := t.reminderCron.AddFunc(schedule, func() { - t.SendNotification(deliverMessage) + err := t.SendNotification(deliverMessage) + if err != nil { + log.Error().Str("module", "dispatcher").Msgf("Channel(Telegram): Send notification error: %s", err) + } }) newEntries = append(newEntries, id) } @@ -83,7 +91,7 @@ func (t *telegram) SendNotification(msg tp.ReqMsg) error { } } - url := fmt.Sprintf("%s/sendMessage", getUrl(t.secret)) + url := fmt.Sprintf("%s/sendMessage", getURL(t.secret)) sendingText := fmt.Sprintf(` %s%s%s (%s) @@ -102,19 +110,23 @@ Plugin Name: %s return err } defer response.Body.Close() - body, err = ioutil.ReadAll(response.Body) + body, err = io.ReadAll(response.Body) if err != nil { log.Error().Str("module", "dispatcher").Msgf("Channel(Telegram): body parsing Error: %s", err) return err } respJSON := make(map[string]interface{}) - json.Unmarshal(body, &respJSON) if !respJSON["ok"].(bool) { log.Error().Str("module", "dispatcher").Msg("Channel(Telegram): Connection failed due to Invalid telegram token.") } + err = json.Unmarshal(body, &respJSON) + if err != nil { + log.Error().Str("module", "dispatcher").Msgf("Channel(Telegram): Unmarshalling JSON Error: %s", err) + return err + } return nil } -func getUrl(token string) string { +func getURL(token string) string { return fmt.Sprintf("https://api.telegram.org/bot%s", token) } diff --git a/manager/types/dispatcher.go b/manager/types/dispatcher.go index 10b1797..f83e5c5 100644 --- a/manager/types/dispatcher.go +++ b/manager/types/dispatcher.go @@ -7,9 +7,10 @@ import ( "github.com/robfig/cron/v3" ) +// DiscordColor is color for a discord alert. type DiscordColor int -// Let's Setup this message into GRPC Type +// ReqMsg is Setup message into GRPC Type. type ReqMsg struct { FuncName string `json:"func_name"` State pluginpb.STATE `json:"state"` @@ -19,18 +20,22 @@ type ReqMsg struct { Options map[string]interface{} `json:"options"` } +// UpdateState is to uptade the state of pluginpb. func (r *ReqMsg) UpdateState(stat pluginpb.STATE) { r.State = stat } +// UpdateSeverity is to uptade the severity of pluginpb. func (r *ReqMsg) UpdateSeverity(sev pluginpb.SEVERITY) { r.Severity = sev } +// UpdateMSG is to update message. func (r *ReqMsg) UpdateMSG(message string) { r.Msg = message } +// DiscordMsg is type for sending messages to a discord. type DiscordMsg struct { Username string `json:"username,omitempty"` AvatarURL string `json:"avatar_url,omitempty"` @@ -38,6 +43,7 @@ type DiscordMsg struct { Embeds []Embed `json:"embeds"` } +// Embed is information for a detailed message. type Embed struct { Author struct { Name string `json:"name,omitempty"` @@ -62,20 +68,24 @@ type Embed struct { } `json:"footer,omitempty"` } +// StateFlag is type that indicates the status of the plugins. type StateFlag struct { State pluginpb.STATE `json:"state"` Severity pluginpb.SEVERITY `json:"severity"` } +// CronTabSt is crontab structure. type CronTabSt struct { Crontab *cron.Cron `json:"crontab"` EntityID int `json:"entity_id"` } +// Update is to update CronTabSt. func (in *CronTabSt) Update(entity int) { in.EntityID = entity } +// Field is a structure for embeds that can be omitted. type Field struct { Name string `json:"name,omitempty"` Value string `json:"value,omitempty"` @@ -93,17 +103,20 @@ type NotifyInfo struct { ExecuteMsg string `json:"execute_msg"` } -// Channel types for dispatchers +// Channel types for dispatchers. type Channel string +// the type of channel. const ( Discord Channel = "DISCORD" Telegram Channel = "TELEGRAM" PagerDuty Channel = "PAGERDUTY" ) +// Reminder is for reminnig alert. type Reminder string +// The type of Reminder. const ( ON Reminder = "ON" HANG Reminder = "HANG"