forked from sebkl/splunk-golang
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmessages.go
64 lines (51 loc) · 1.68 KB
/
messages.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
58
59
60
61
62
63
64
package splunk
import (
"fmt"
"time"
"net/url"
"encoding/json"
)
type MessageSeverity string
type Message struct {
Name string `json:"name"`
Content MessageContent `json:"content"`
}
type MessageContent struct {
Message string `json:"message"`
Severity MessageSeverity `json:"severity"`
created int64 `json:"timeCreated_epochSecs"`
}
func (mc *MessageContent) Content() (time.Time) {
return time.Unix(mc.created, 0)
}
type Messages struct {
Origin string `json:"origin"`
Messages []Message `json:"entry"`
}
const (
Info MessageSeverity = "info"
Warn MessageSeverity = "warn"
Error MessageSeverity = "error"
)
// SendMessage sends an informational message to Splunk
func (conn SplunkConnection) SendMessage(message *Message) (string, error) {
data := make(url.Values)
data.Add("name", message.Name)
data.Add("value", message.Content.Message)
data.Add("severity", string(message.Content.Severity))
response, err := conn.httpPost(fmt.Sprintf("%s/services/messages", conn.BaseURL), &data)
return response, err
}
func (conn SplunkConnection) GetMessage(name string) ([]Message, error) {
data := make(url.Values)
data.Add("name", name)
data.Add("output_mode", "json")
response, err := conn.httpGet(fmt.Sprintf("%s/services/messages/%s", conn.BaseURL, name), &data)
if err != nil {
return []Message{}, err
}
bytes := []byte(response)
var messages Messages
unmarshall_error := json.Unmarshal(bytes, &messages)
return messages.Messages, unmarshall_error
}