-
Notifications
You must be signed in to change notification settings - Fork 2
/
tools.go
57 lines (51 loc) · 1.4 KB
/
tools.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
47
48
49
50
51
52
53
54
55
56
57
/*
Package tools provides various functions for working with
the monitors and alerts in the monitoring client.
*/
package tools
import (
"encoding/gob"
"fmt"
"github.com/AvanceIT/monitor/xmltools"
"net"
"os"
)
// getMonitorName takes the alertString and grabs the monitor
// name from the front. It then returns the monitorName and
// also the alertMessage without the monitorName at the start
func getMonitorName(alertMessage string) (string, string) {
var monitorName []byte
for messageStart, thisChar := range alertMessage {
if string(thisChar) != ":" {
monitorName = append(monitorName, byte(thisChar))
} else {
alertMessage = alertMessage[messageStart+2:]
break
}
}
return string(monitorName), alertMessage
}
// RaiseAlert formats the alertString into an Gob encoded message
// and passes it to the monitoring server
func RaiseAlert(alertMessage string, alertLevel int) {
var monitorName string
thisHostName, _ := os.Hostname()
monitorName, alertMessage = getMonitorName(alertMessage)
alertData := xmltools.MonResult{
MonName: monitorName,
AlertLevel: alertLevel,
HostName: thisHostName,
Detail: alertMessage,
}
srv, err := net.Dial("tcp", "192.168.0.5:2468")
if err != nil {
fmt.Printf("Error connecting: %v\n", err)
return
}
defer srv.Close()
enc := gob.NewEncoder(srv)
err = enc.Encode(&alertData)
if err != nil {
fmt.Printf("encode error: %v\n", err)
}
}