Skip to content

Commit

Permalink
Downslice the interface necessary to fire Events
Browse files Browse the repository at this point in the history
We don't need the entire Checkable interface here.
  • Loading branch information
mperham committed Oct 9, 2014
1 parent c95880a commit 060b4bc
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 13 deletions.
12 changes: 6 additions & 6 deletions actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,13 @@ func init() {
An Action is something which is triggered when a rule is broken. This is typically
either a Notification or to Restart the service.
*/
type ActionBuilder func(Checkable, *AlertRoute) (Action, error)
type ActionBuilder func(Eventable, *AlertRoute) (Action, error)

/*
A Notifier is a route to send an alert somewhere else. The global
conf sets up the necessary params for the notification to work.
*/
type NotifierBuilder func(Checkable, map[string]string) (Action, error)
type NotifierBuilder func(Eventable, map[string]string) (Action, error)

var (
Actions = map[string]ActionBuilder{
Expand All @@ -57,7 +57,7 @@ var (
}
)

func buildAlerter(check Checkable, route *AlertRoute) (Action, error) {
func buildAlerter(check Eventable, route *AlertRoute) (Action, error) {
funk := Notifier[route.Channel]
if funk == nil {
// TODO Include valid channels
Expand All @@ -66,7 +66,7 @@ func buildAlerter(check Checkable, route *AlertRoute) (Action, error) {
return funk(check, route.Config)
}

func buildRestarter(check Checkable, _ *AlertRoute) (Action, error) {
func buildRestarter(check Eventable, _ *AlertRoute) (Action, error) {
switch check.(type) {
case *Service:
return &Restarter{check.(*Service)}, nil
Expand All @@ -83,7 +83,7 @@ func (r Restarter) Trigger(event *Event) error {
return r.Service.Restart()
}

func buildEmailNotifier(check Checkable, config map[string]string) (Action, error) {
func buildEmailNotifier(check Eventable, config map[string]string) (Action, error) {
en := &EmailNotifier{}
err := en.setup(config)
if err != nil {
Expand All @@ -92,7 +92,7 @@ func buildEmailNotifier(check Checkable, config map[string]string) (Action, erro
return en, nil
}

func buildGmailNotifier(check Checkable, params map[string]string) (Action, error) {
func buildGmailNotifier(check Eventable, params map[string]string) (Action, error) {
params["smtp_server"] = "smtp.gmail.com"
return buildEmailNotifier(check, params)
}
Expand Down
12 changes: 8 additions & 4 deletions events.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,16 @@ func (s EventType) String() string {

type Event struct {
Type EventType
Checkable
Eventable
*Rule
}

func (e *Event) Thing() Eventable {
return e.Eventable
}

func (e *Event) Service() *Service {
return e.Checkable.(*Service)
return e.Eventable.(*Service)
}

func (e *Event) Hostname() string {
Expand All @@ -49,12 +53,12 @@ func (e *Event) Hostname() string {
}

func (e *Event) Target() string {
switch x := e.Checkable.(type) {
switch x := e.Eventable.(type) {
case *Service:
return fmt.Sprintf("%s[%s]", e.Hostname(), x.Name())
case *Host:
return fmt.Sprintf("%s", x.Name())
default:
return fmt.Sprintf("Unknown: %s", e.Checkable)
return fmt.Sprintf("Unknown: %s", e.Eventable)
}
}
2 changes: 1 addition & 1 deletion inq_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ func convertRule(global *ConfigFile, check Checkable, inqrule ast.Rule) (*Rule,
op, inqrule.Threshold.Raw, float64(inqrule.Threshold.Parsed), 0, inqrule.CycleCount, 0, Ok, actions}, nil
}

func convertAction(global *ConfigFile, check Checkable, action ast.Action) (Action, error) {
func convertAction(global *ConfigFile, check Eventable, action ast.Action) (Action, error) {
switch action.Name() {
case "alert":
route := global.AlertRoutes[""]
Expand Down
2 changes: 1 addition & 1 deletion templates/email/ProcessDoesNotExist.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ To: {{.Config.To}} <{{.Config.To}}>
From: {{.Config.From}} <{{.Config.From}}>
Subject: {{.Target}} does not exist.

I can't locate a process for the {{.Checkable.Name}} service on {{.Hostname}}.
I can't locate a process for the {{.Thing.Name}} service on {{.Hostname}}.

Your Pal,
Inspeqtor
2 changes: 1 addition & 1 deletion templates/email/ProcessExists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ To: {{.Config.To}} <{{.Config.To}}>
From: {{.Config.From}} <{{.Config.From}}>
Subject: {{.Target}} is now running.

The {{.Checkable.Name}} service is now running with PID {{.Service.Process.Pid}}
The {{.Thing.Name}} service is now running with PID {{.Service.Process.Pid}}

Your Pal,
Inspeqtor
5 changes: 5 additions & 0 deletions types.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,11 @@ func (h *Host) Collect(silenced bool, completeCallback func(Checkable)) {
}
}

type Eventable interface {
Name() string
Parameter(string) string
}

type Checkable interface {
Name() string
Parameter(string) string
Expand Down

0 comments on commit 060b4bc

Please sign in to comment.