Skip to content

Commit

Permalink
Alerts active after refactor
Browse files Browse the repository at this point in the history
- added testalert command to test authentication credentials

- Added build for makefile

- Changed default initconfig settings
  • Loading branch information
jeffwillette committed Sep 19, 2017
1 parent 13f3b94 commit 765c727
Show file tree
Hide file tree
Showing 12 changed files with 285 additions and 162 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.docker-alertd.yaml
3 changes: 2 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ services:
- docker

script:
- go test
- go test ./cmd
- go test
- go install
9 changes: 9 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
DIR=~/docker-alertd-dist

build:
@rm -r $(DIR)
@mkdir -p $(DIR)/osx $(DIR)/linux $(DIR)/windows
@go build -o ~/dist/osx/docker-alertd
@GOOS=linux go build -o ~/dist/linux/docker-alertd
@GOOS=windows go build -o ~/dist/windows/docker-alertd.exe

6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -151,3 +151,9 @@ Refer to the [launchd plist example file](https://github.com/deltaskelta/docker-
### With Sys V Init (various Linux systems without systemd)

Refer to this [Sys V Init tutorial](https://www.cyberciti.biz/tips/linux-write-sys-v-init-script-to-start-stop-service.html)

### Testing Alert Authentication

Docker-Alertd comes with a `testalert` command which will search for a nonexistant
container name and send an alert to the appropriate places, use this for testing that
smtp or other authentication settings are correct
16 changes: 8 additions & 8 deletions cmd/alertd_container_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,9 @@ func Teardown(t *testing.T, cnt []TestContainer) {
}
}

func CheckHasErr(e []error, s string) bool {
for _, msg := range e {
if ErrIsErr(msg, s) {
func CheckHasErr(e []error, s error) bool {
for _, err := range e {
if ErrContainsErr(err, s) {
return true
}
}
Expand Down Expand Up @@ -210,7 +210,7 @@ func TestCheckExists(t *testing.T) {
}

if test.ExpectedAlert != nil {
gotErr := CheckHasErr(a.Messages, test.ExpectedAlert.Error())
gotErr := CheckHasErr(a.Messages, test.ExpectedAlert)
if !gotErr {
t.Errorf("expected error message: %s not found in error messages", test.ExpectedAlert.Error())
t.Error(a.Messages)
Expand Down Expand Up @@ -330,7 +330,7 @@ func TestCheckRunning(t *testing.T) {
}

if test.ExpectedAlert != nil {
gotErr := CheckHasErr(a.Messages, test.ExpectedAlert.Error())
gotErr := CheckHasErr(a.Messages, test.ExpectedAlert)
if !gotErr {
t.Errorf("expected error message: %s not found in error messages", test.ExpectedAlert.Error())
t.Error(a.Messages)
Expand Down Expand Up @@ -453,7 +453,7 @@ func TestCheckCPUUsage(t *testing.T) {
}

if test.ExpectedAlert != nil {
gotErr := CheckHasErr(a.Messages, test.ExpectedAlert.Error())
gotErr := CheckHasErr(a.Messages, test.ExpectedAlert)
if !gotErr {
t.Errorf("expected error message: %s not found in error messages", test.ExpectedAlert.Error())
t.Error(a.Messages)
Expand Down Expand Up @@ -576,7 +576,7 @@ func TestCheckMemory(t *testing.T) {
}

if test.ExpectedAlert != nil {
gotErr := CheckHasErr(a.Messages, test.ExpectedAlert.Error())
gotErr := CheckHasErr(a.Messages, test.ExpectedAlert)
if !gotErr {
t.Errorf("expected error message: %s not found in error messages", test.ExpectedAlert.Error())
t.Error(a.Messages)
Expand Down Expand Up @@ -707,7 +707,7 @@ func TestCheckPID(t *testing.T) {
}

if test.ExpectedAlert != nil {
gotErr := CheckHasErr(a.Messages, test.ExpectedAlert.Error())
gotErr := CheckHasErr(a.Messages, test.ExpectedAlert)
if !gotErr {
t.Errorf("expected error message: %s not found in error messages", test.ExpectedAlert.Error())
t.Error(a.Messages)
Expand Down
3 changes: 1 addition & 2 deletions cmd/initconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,7 @@ containers:
# If email settings are present and active, then email alerts will be sent when an alert
# is triggered.
emailSettings:
active: true
smtp: smtp.someserver.com
smtp: smtp.nonexistantserver.com
password: s00p3rS33cret
port: 587
from: auto@freshpowpow.com
Expand Down
8 changes: 2 additions & 6 deletions cmd/monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,18 +118,14 @@ func Monitor(c *Conf, a *Alert) {
for {
a.Clear()
CheckContainers(cnt, cli, a)
if a.ShouldSend() {
a.Alert()
}
a.Evaluate()
time.Sleep(time.Duration(c.Duration) * time.Millisecond)
}
default:
for i := int64(0); i < c.Iterations; i++ {
a.Clear()
CheckContainers(cnt, cli, a)
if a.ShouldSend() {
a.Alert()
}
a.Evaluate()
time.Sleep(time.Duration(c.Duration) * time.Millisecond)
}
}
Expand Down
63 changes: 0 additions & 63 deletions cmd/monitor_test.go

This file was deleted.

2 changes: 1 addition & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ the docker api.`,
// has an action associated with it:
Run: func(cmd *cobra.Command, args []string) {

log.Println(Config)
//log.Println(Config)
err := Config.Validate()
if err != nil {
log.Println(err)
Expand Down
61 changes: 61 additions & 0 deletions cmd/testalert.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
// Copyright © 2017 NAME HERE <EMAIL ADDRESS>
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package cmd

import (
"log"
"os"

"github.com/spf13/cobra"
)

// testalertCmd represents the testalert command
var testalertCmd = &cobra.Command{
Use: "testalert",
Short: "send a test alert to test auth credentials",
Long: `Use the authentication credentials in the config file to send a test alert`,
Run: func(cmd *cobra.Command, args []string) {
Config.Containers = []Container{
Container{
Name: "fd42c70222be1d96224ffeb28416d4b61ffa431c0aa97818cf5ef67e9317a7d8",
},
}

Config.EmailSettings.Subject = "TEST: DOCKER-ALERTD"

err := Config.Validate()
if err != nil {
log.Println(err)
os.Exit(1)
}

Start(&Config)
},
}

func init() {
RootCmd.AddCommand(testalertCmd)

// Here you will define your flags and configuration settings.

// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// testalertCmd.PersistentFlags().String("foo", "", "A help for foo")

// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// testalertCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")

}

0 comments on commit 765c727

Please sign in to comment.