Skip to content

Commit

Permalink
nsca: Replace special characters from Message to prevent Shinken erro…
Browse files Browse the repository at this point in the history
…rs when parsing
  • Loading branch information
rbeuque74 committed Aug 23, 2018
1 parent ce30e6a commit 216a9fe
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 15 deletions.
5 changes: 4 additions & 1 deletion consumers/nsca/nsca.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package nsca

import (
"fmt"
"strings"
"time"

"github.com/rbeuque74/jagozzi/config"
Expand All @@ -10,6 +11,8 @@ import (
log "github.com/sirupsen/logrus"
)

var replacer = strings.NewReplacer(",", "", "\"", "")

// Consumer is the representation of a NSCA consumer
type Consumer struct {
cfg config.ConsumerConfiguration
Expand Down Expand Up @@ -78,7 +81,7 @@ func (consumer Consumer) handle() {
State: int16(result.Status),
Host: result.Hostname,
Service: result.Checker.ServiceName(),
Message: result.Message,
Message: replacer.Replace(result.Message),
Status: consumer.error,
}
log.Debugf("consumer: send message %+v", *msg)
Expand Down
43 changes: 37 additions & 6 deletions consumers/nsca/nsca_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/rbeuque74/jagozzi/consumers"
"github.com/rbeuque74/jagozzi/plugins"
"github.com/rbeuque74/nsca"
nsrv "github.com/tubemogul/nscatools"
)

type fakeChecker struct {
Expand Down Expand Up @@ -40,7 +41,7 @@ func (fc fakeChecker) Run(ctx context.Context) plugins.Result {

func TestConsumerSendMessage(t *testing.T) {
// creating NSCA server
srvCh := make(chan Message, 10)
srvCh := make(chan nsrv.DataPacket, 10)
go NewNscaServerChannel(srvCh)

// sleeping a bit to let NSCA server start
Expand All @@ -53,20 +54,39 @@ func TestConsumerSendMessage(t *testing.T) {

consumer := New(*cfg)

var messages []string

var message = "example message"
res := plugins.Result{
Status: plugins.STATE_CRITICAL,
Message: "example message",
Message: message,
Checker: fakeChecker{
t: t,
},
}
messages = append(messages, message)

consumer.MessageChannel() <- consumers.ResultWithHostname{
Result: res,
Hostname: "hostname-example-1",
}

messageReceived := false
message = "message with unallowed characters, \"multiple ,characters\""
res = plugins.Result{
Status: plugins.STATE_CRITICAL,
Message: message,
Checker: fakeChecker{
t: t,
},
}
messages = append(messages, "message with unallowed characters multiple characters")

consumer.MessageChannel() <- consumers.ResultWithHostname{
Result: res,
Hostname: "hostname-example-1",
}

messageReceived := 0
for {
select {
case err := <-consumer.ErrorChannel():
Expand All @@ -75,11 +95,22 @@ func TestConsumerSendMessage(t *testing.T) {
} else {
t.Log("nsca send OK")
}
messageReceived = true
close(consumer.ExitChannel())
messageReceived += 1

msg := <-srvCh
if msg.PluginOutput != messages[0] {
t.Fatalf("message received incorrect: %s", msg.PluginOutput)
return
}

if messageReceived == 2 {
close(consumer.ExitChannel())
} else {
messages = messages[1:]
}
case <-time.After(time.Second):
t.Log("timed out")
if !messageReceived {
if messageReceived != 2 {
t.Fatal("timeout and message not received")
}
return
Expand Down
10 changes: 2 additions & 8 deletions consumers/nsca/nsca_utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,13 @@ const (
EncryptKey = "toto"
)

type Message nsrv.DataPacket

func NewNscaServerChannel(ch chan<- Message) {
func NewNscaServerChannel(ch chan<- nsrv.DataPacket) {
cfg := nsrv.NewConfig("localhost", 5667, nsrv.EncryptXOR, EncryptKey, func(p *nsrv.DataPacket) error {
if p == nil {
return errors.New("packet is nil")
}
ch <- typeCastMessage(*p)
ch <- *p
return nil
})
nsrv.StartServer(cfg, true)
}

func typeCastMessage(msg interface{}) Message {
return msg.(Message)
}

0 comments on commit 216a9fe

Please sign in to comment.