-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathnsca.go
46 lines (40 loc) · 1.34 KB
/
nsca.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package main
// Here we will set only the elements related to handling the NSCA calls
import (
"bytes"
"fmt"
"os/exec"
"strings"
)
// This function will take care of sending the nsca packets
// As of today, Go does not currently support the ciphersuite of nsca-ng so I'm
// using a call to the command line as an ugly workaround.
//
// Parameters:
// - state (*uint8): the nsca status to send (0, 1, 2, 3)
// - service (*string): the name of the nagios service that will handle the
// request. The current plugins contruct the service using like:
// Product name-Test name
// - message (*string): the message describing the alert to send
//
// Returns:
// - error: an error if one has been encountered
func sendNscaMessage(state *uint8, service *string, message *string) error {
if !config.NSCA.Enabled {
return nil
}
cmd := exec.Command(config.NSCA.OsCommand, "-H", config.NSCA.Server, "-c", config.NSCA.ConfigFile)
cmd.Stdin = strings.NewReader(fmt.Sprintf("%s\t%s\t%d\t%s", config.NSCA.ClientHost, *service, *state, *message))
err := cmd.Run()
if *verbose {
// In verbose mode, we print the command output before sending back the
// error as both can be helpful for debug purposes.
var out bytes.Buffer
cmd.Stdout = &out
logInfo(fmt.Sprintf("NSCA command output: %s", out.String()))
}
if err != nil {
return err
}
return nil
}