-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Lot of work on alerts and other stuff
- Loading branch information
Zef Hemel
committed
Sep 7, 2017
1 parent
d5b49e6
commit e23c71f
Showing
14 changed files
with
468 additions
and
152 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"time" | ||
|
||
"github.com/zefhemel/ax/pkg/alert" | ||
"github.com/zefhemel/ax/pkg/alert/slack" | ||
"github.com/zefhemel/ax/pkg/backend/common" | ||
"github.com/zefhemel/ax/pkg/cache" | ||
"github.com/zefhemel/ax/pkg/config" | ||
) | ||
|
||
var ( | ||
alertFlags = addQueryFlags(addAlertCommand) | ||
alertFlagName string | ||
) | ||
|
||
func init() { | ||
addAlertCommand.Flag("name", "Name for alert").Required().StringVar(&alertFlagName) | ||
} | ||
|
||
func setupDest() alert.Alerter { | ||
return nil | ||
} | ||
|
||
func addAlertMain(rc config.RuntimeConfig, client common.Client) { | ||
alertConfig := config.AlertConfig{ | ||
Env: rc.ActiveEnv, | ||
Name: alertFlagName, | ||
Selector: *alertFlags, | ||
} | ||
|
||
fmt.Printf("Config: %+v\n", alertConfig) | ||
conf := config.LoadConfig() | ||
conf.Alerts = append(conf.Alerts, alertConfig) | ||
config.SaveConfig(conf) | ||
} | ||
|
||
func watchAlerts(rc config.RuntimeConfig, alertConfig config.AlertConfig) { | ||
var alerter alert.Alerter | ||
switch alertConfig.Service["backend"] { | ||
case "slack": | ||
alerter = slack.New(alertConfig.Name, alertConfig.Service) | ||
default: | ||
panic("No such backend") | ||
} | ||
query := querySelectorsToQuery(&alertConfig.Selector) | ||
query.Follow = true | ||
query.MaxResults = 5 | ||
seenIdCache := cache.New(fmt.Sprintf("%s/alert-%s-seen.json", rc.DataDir, alertConfig.Name)) | ||
client := determineClient(rc.Config.Environments[alertConfig.Env]) | ||
if client == nil { | ||
fmt.Println("Cannot obtain a client for", alertConfig) | ||
return | ||
} | ||
fmt.Println("Now waiting for alerts for", alertConfig.Name) | ||
for message := range client.Query(query) { | ||
if seenIdCache.Contains(message.UniqueID()) { | ||
//fmt.Println("Skipping one") | ||
continue | ||
} | ||
expire := time.Now().Add(time.Hour * 24 * 30) | ||
seenIdCache.Set(message.UniqueID(), true, &expire) | ||
fmt.Printf("[%s] Sending alert to %s: %+v\n", alertConfig.Name, alertConfig.Service["backend"], message.Map()) | ||
err := alerter.SendAlert(message) | ||
if err != nil { | ||
fmt.Println("Couldn't send alert", err) | ||
} | ||
err = seenIdCache.Flush() | ||
if err != nil { | ||
fmt.Println("Couldn't flush cache", err) | ||
} | ||
} | ||
} | ||
|
||
func alertMain(rc config.RuntimeConfig) { | ||
for _, alert := range rc.Config.Alerts { | ||
go watchAlerts(rc, alert) | ||
} | ||
for { | ||
time.Sleep(time.Minute) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package main | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
func TestSlack(t *testing.T) { | ||
//alertSlack(os.Getenv("SLACK_TOKEN"), "#zeftest", "Sup from Go") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
xoxp-139242487670-137856077392-237793436246-2e631b7bf7134e6cc442ad496c4d189f |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package alert | ||
|
||
import "github.com/zefhemel/ax/pkg/backend/common" | ||
|
||
type Alerter interface { | ||
SendAlert(lm common.LogMessage) error | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package slack | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"io/ioutil" | ||
"net/http" | ||
"net/url" | ||
"strings" | ||
|
||
yaml "gopkg.in/yaml.v2" | ||
|
||
"github.com/zefhemel/ax/pkg/alert" | ||
"github.com/zefhemel/ax/pkg/backend/common" | ||
) | ||
|
||
type SlackAlerter struct { | ||
name string | ||
token string | ||
channel string | ||
username string | ||
} | ||
|
||
func New(name string, config map[string]string) *SlackAlerter { | ||
return &SlackAlerter{ | ||
name: name, | ||
token: config["token"], | ||
channel: config["channel"], | ||
username: config["username"], | ||
} | ||
} | ||
|
||
func (alerter *SlackAlerter) SendAlert(lm common.LogMessage) error { | ||
form := url.Values{} | ||
form.Add("token", alerter.token) | ||
form.Add("icon_emoji", ":bell:") | ||
form.Add("channel", alerter.channel) | ||
buf, _ := yaml.Marshal(lm.Attributes) | ||
form.Add("text", fmt.Sprintf("*[%s]* %s", lm.Timestamp.Format(common.TimeFormat), buf)) | ||
form.Add("username", alerter.username) | ||
|
||
url := "https://slack.com/api/chat.postMessage" | ||
req, err := http.NewRequest("POST", url, strings.NewReader(form.Encode())) | ||
if err != nil { | ||
return err | ||
} | ||
req.Header.Set("Content-Type", "application/x-www-form-urlencoded") | ||
res, err := http.DefaultClient.Do(req) | ||
if err != nil { | ||
return err | ||
} | ||
if res.StatusCode != http.StatusOK { | ||
all, _ := ioutil.ReadAll(res.Body) | ||
return errors.New(string(all)) | ||
} | ||
return nil | ||
} | ||
|
||
var _ alert.Alerter = &SlackAlerter{} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.