forked from aerostitch/nscatools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
35 lines (30 loc) · 1021 Bytes
/
client.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
package nscatools
import (
"fmt"
"net"
"time"
)
// SendStatus connects to the nsca server and sends the provided data in the
// right format. If you want to send a host status (as opposed to a service
// status), just let the "service" parameter empty
func SendStatus(conf *Config, clientHost string, service string, status int16, message string) error {
nscaServer := fmt.Sprint(conf.Host, ":", conf.Port)
conn, err := net.DialTimeout("tcp", nscaServer, 10*time.Second)
if err != nil {
return fmt.Errorf("unable to connect to the provided server: %s", err)
}
defer conn.Close()
ipkt, err := NewInitPacket()
if err = ipkt.Read(conn); err != nil {
return fmt.Errorf("unable to read the initialization vector: %s", err)
}
dp := NewDataPacket(conf.EncryptionMethod, []byte(conf.Password), ipkt)
dp.HostName = clientHost
dp.Service = service
dp.State = status
dp.PluginOutput = message
if err = dp.Write(conn); err != nil {
return fmt.Errorf("unable to Send the packet: %s", err)
}
return nil
}