Skip to content

Commit

Permalink
6.x changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Jason Greenberg authored and Jason Greenberg committed Feb 20, 2018
1 parent 56b8267 commit 7c24ed9
Show file tree
Hide file tree
Showing 14 changed files with 302 additions and 987 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -3,4 +3,5 @@
.DS_Store
/nagioscheckbeat
/nagioscheckbeat.test
/vendor
*.pyc
29 changes: 22 additions & 7 deletions Makefile
@@ -1,18 +1,29 @@
BEATNAME=nagioscheckbeat
BEAT_DIR=github.com/PhaedrusTheGreek/
BEAT_NAME=nagioscheckbeat
BEAT_PATH=github.com/PhaedrusTheGreek/nagioscheckbeat
BEAT_GOPATH=$(firstword $(subst :, ,${GOPATH}))
BEAT_URL=https://${BEAT_PATH}
SYSTEM_TESTS=false
TEST_ENVIRONMENT=false
ES_BEATS?=${CURDIR}/vendor/github.com/elastic/beats
GOPACKAGES=$(shell glide novendor)
ES_BEATS?=./vendor/github.com/elastic/beats
GOPACKAGES=$(shell govendor list -no-status +local)
PREFIX?=.
NOTICE_FILE=NOTICE
GOBUILD_FLAGS=-i -ldflags "-X $(BEAT_PATH)/vendor/github.com/elastic/beats/libbeat/version.buildTime=$(NOW) -X $(BEAT_PATH)/vendor/github.com/elastic/beats/libbeat/version.commit=$(COMMIT_ID)"

# Path to the libbeat Makefile
-include $(ES_BEATS)/libbeat/scripts/Makefile

# Initial beat setup
.PHONY: setup
setup:
make update
setup: copy-vendor
$(MAKE) update

# Copy beats into vendor directory
.PHONY: copy-vendor
copy-vendor:
mkdir -p vendor/github.com/elastic/
cp -R ${BEAT_GOPATH}/src/github.com/elastic/beats vendor/github.com/elastic/
rm -rf vendor/github.com/elastic/beats/.git

.PHONY: git-init
git-init:
Expand All @@ -25,10 +36,14 @@ git-init:
git commit -m "Add git settings"
git add .
git reset -- .travis.yml
git commit -m "Add nagioscheckbeat"
git commit -m "Add examplebeat"
git add .travis.yml
git commit -m "Add Travis CI"

# This is called by the beats packer before building starts
.PHONY: before-build
before-build:

# Collects all dependencies and then calls update
.PHONY: collect
collect:
15 changes: 10 additions & 5 deletions beater/nagioscheckbeat.go
Expand Up @@ -6,7 +6,6 @@ import (
"github.com/elastic/beats/libbeat/beat"
"github.com/elastic/beats/libbeat/common"
"github.com/elastic/beats/libbeat/logp"
"github.com/elastic/beats/libbeat/publisher"

"github.com/PhaedrusTheGreek/nagioscheckbeat/check"
"github.com/PhaedrusTheGreek/nagioscheckbeat/config"
Expand All @@ -15,7 +14,7 @@ import (
type Nagioscheckbeat struct {
done chan struct{}
config config.Config
client publisher.Client
client beat.Client
}

// Creates beater
Expand All @@ -32,17 +31,23 @@ func New(b *beat.Beat, cfg *common.Config) (beat.Beater, error) {
return bt, nil
}

// Beat.Publisher is a called pipeline and is an instance of beat.Pipeline,

func (bt *Nagioscheckbeat) Run(b *beat.Beat) error {
logp.Info("nagioscheckbeat is running! Hit CTRL-C to stop it.")

bt.client = b.Publisher.Connect()
var err error
bt.client, err = b.Publisher.Connect()
if err != nil {
return err
}

for _, checkConfig := range bt.config.Checks {

checkInstance := check.NagiosCheck{}
checkInstance.Setup(&checkConfig)
go checkInstance.Run(func(events []common.MapStr) {
bt.client.PublishEvents(events)
go checkInstance.Run(func(events []beat.Event) {
bt.client.PublishAll(events)
})

}
Expand Down
3 changes: 0 additions & 3 deletions blog/put-template.sh

This file was deleted.

51 changes: 28 additions & 23 deletions check/check.go
Expand Up @@ -4,6 +4,7 @@ import (
"errors"
"github.com/PhaedrusTheGreek/nagioscheckbeat/config"
"github.com/PhaedrusTheGreek/nagioscheckbeat/nagiosperf"
"github.com/elastic/beats/libbeat/beat"
"github.com/elastic/beats/libbeat/common"
"github.com/elastic/beats/libbeat/logp"
"github.com/mgutz/str"
Expand Down Expand Up @@ -56,7 +57,7 @@ func (nagiosCheck *NagiosCheck) Setup(config *config.NagiosCheckConfig) error {
return nil
}

func (nagiosCheck *NagiosCheck) Run(publish func([]common.MapStr)) {
func (nagiosCheck *NagiosCheck) Run(publish func([]beat.Event)) {

if !nagiosCheck.enabled {
logp.Info("Not starting module %s as not enabled.", nagiosCheck.name)
Expand All @@ -78,17 +79,19 @@ func (nagiosCheck *NagiosCheck) Run(publish func([]common.MapStr)) {

}

func (nagiosCheck *NagiosCheck) Check() (events []common.MapStr, err error) {
func (nagiosCheck *NagiosCheck) Check() (events []beat.Event, err error) {

startTime := time.Now()
startMs := startTime.UnixNano() / int64(time.Millisecond)

check_event := common.MapStr{
"@timestamp": common.Time(startTime),
"type": "nagioscheck",
"name": nagiosCheck.name,
"cmd": nagiosCheck.cmd,
"args": nagiosCheck.args,
check_event := beat.Event{
Timestamp: startTime,
Fields: common.MapStr{
"type": "nagioscheck",
"name": nagiosCheck.name,
"cmd": nagiosCheck.cmd,
"args": nagiosCheck.args,
},
}

logp.Debug("nagioscheck", "Running Command: %q", nagiosCheck.cmd)
Expand All @@ -114,10 +117,10 @@ func (nagiosCheck *NagiosCheck) Check() (events []common.MapStr, err error) {
logp.Debug("nagioscheck", "Command Returned: %q, exit code %d", output, waitStatus.ExitStatus())

parts := strings.Split(string(output), "|")
check_event["message"] = parts[0]
check_event["status"] = nagiosperf.NiceStatus(waitStatus.ExitStatus())
check_event["status_code"] = waitStatus.ExitStatus()
check_event["took_ms"] = time.Now().UnixNano()/int64(time.Millisecond) - startMs
check_event.Fields["message"] = parts[0]
check_event.Fields["status"] = nagiosperf.NiceStatus(waitStatus.ExitStatus())
check_event.Fields["status_code"] = waitStatus.ExitStatus()
check_event.Fields["took_ms"] = time.Now().UnixNano()/int64(time.Millisecond) - startMs

// publish the check result, even if there is no perf data
events = append(events, check_event)
Expand All @@ -135,17 +138,19 @@ func (nagiosCheck *NagiosCheck) Check() (events []common.MapStr, err error) {

for _, perf := range perfs {

metric_event := common.MapStr{
"@timestamp": common.Time(startTime),
"type": "nagiosmetric",
"name": nagiosCheck.name,
"label": perf.Label,
"uom": perf.Uom,
"value": perf.Value,
"min": perf.Min,
"max": perf.Max,
"warning": perf.Warning,
"critical": perf.Critical,
metric_event := beat.Event{
Timestamp: startTime,
Fields: common.MapStr{
"type": "nagiosmetric",
"name": nagiosCheck.name,
"label": perf.Label,
"uom": perf.Uom,
"value": perf.Value,
"min": perf.Min,
"max": perf.Max,
"warning": perf.Warning,
"critical": perf.Critical,
},
}

events = append(events, metric_event)
Expand Down
10 changes: 5 additions & 5 deletions check/check_test.go
Expand Up @@ -6,16 +6,16 @@ package check

import (
"github.com/PhaedrusTheGreek/nagioscheckbeat/config"
"github.com/elastic/beats/libbeat/common"
"github.com/elastic/beats/libbeat/logp"
"github.com/elastic/beats/libbeat/beat"
// "github.com/elastic/beats/libbeat/logp"
"strings"
"testing"
"time"
)

func TestCheck(t *testing.T) {

logp.LogInit(logp.LOG_DEBUG, "", false, true, []string{"*"})
// logp.LogInit(logp.LOG_DEBUG, "", false, true, []string{"*"})

/*
1st Check, makes sure command runs and returns OK
Expand All @@ -37,8 +37,8 @@ func TestCheck(t *testing.T) {

// [{"@timestamp":"2016-01-10T05:40:19.738Z","args":"0 hello","cmd":"/usr/local/sbin/check_dummy","message":"OK: hello\n","status":"OK","took_ms":6,"type":"nagioscheck"}]

var event common.MapStr = got[0]
var message = event["message"].(string)
var event beat.Event = got[0]
var message = event.Fields["message"].(string)
if strings.Compare(message, "OK: hello\n") != 0 {
t.Errorf("Expected 'OK: hello\\n', and got %q", message)
}
Expand Down

0 comments on commit 7c24ed9

Please sign in to comment.