forked from zabbix-tools/go-zabbix
-
Notifications
You must be signed in to change notification settings - Fork 2
/
event_json.go
72 lines (59 loc) · 1.81 KB
/
event_json.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
65
66
67
68
69
70
71
72
package zabbix
import (
"fmt"
"strconv"
"time"
)
// jEvent is a private map for the Zabbix API Event object.
// See: https://www.zabbix.com/documentation/2.2/manual/api/reference/event/object
type jEvent struct {
EventID string `json:"eventid"`
Acknowledged string `json:"acknowledged"`
Clock string `json:"clock"`
Nanoseconds string `json:"ns"`
ObjectType string `json:"object"`
ObjectID string `json:"objectid"`
Source string `json:"source"`
Value string `json:"value"`
ValueChanged string `json:"value_changed"`
Hosts jHosts `json:"hosts"`
}
// Event returns a native Go Event struct mapped from the given JSON Event data.
func (c *jEvent) Event() (*Event, error) {
event := &Event{}
event.EventID = c.EventID
event.Acknowledged = (c.Acknowledged == "1")
// parse timestamp
sec, err := strconv.ParseInt(c.Clock, 10, 64)
if err != nil {
return nil, fmt.Errorf("Error parsing Event timestamp: %v", err)
}
nsec, err := strconv.ParseInt(c.Nanoseconds, 10, 64)
if err != nil {
return nil, fmt.Errorf("Error parsing Event timestamp nanoseconds: %v", err)
}
event.Timestamp = time.Unix(sec, nsec)
event.ObjectType, err = strconv.Atoi(c.ObjectType)
if err != nil {
return nil, fmt.Errorf("Error parsing Event Object Type: %v", err)
}
event.ObjectID, err = strconv.Atoi(c.ObjectID)
if err != nil {
return nil, fmt.Errorf("Error parsing Event Object ID: %v", err)
}
event.Source, err = strconv.Atoi(c.Source)
if err != nil {
return nil, fmt.Errorf("Error parsing Event Source: %v", err)
}
event.Value, err = strconv.Atoi(c.Value)
if err != nil {
return nil, fmt.Errorf("Error parsing Event Source: %v", err)
}
event.ValueChanged = (c.ValueChanged == "1")
// map hosts
event.Hosts, err = c.Hosts.Hosts()
if err != nil {
return nil, err
}
return event, nil
}