forked from neogan74/go-zabbix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathevent_json.go
79 lines (63 loc) · 2.04 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
73
74
75
76
77
78
79
package zabbix
import (
"fmt"
"strconv"
"strings"
"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"`
Name string `json:"name"`
Severity string `json:"severity"`
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)
}
loc, _ := time.LoadLocation("UTC")
now := time.Now().In(loc)
event.Timestamp = time.Unix(sec, nsec)
tmp := strings.Split(fmt.Sprint(now.Sub(time.Unix(sec, nsec))), ".")
event.Age = tmp[0] + "s"
// event.ObjectType, _ = 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, _ = 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")
event.Name = c.Name
event.Severity = c.Severity
return event, nil
}