forked from zabbix-tools/go-zabbix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
history_json.go
84 lines (68 loc) · 2.05 KB
/
history_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
80
81
82
83
84
package zabbix
import (
"fmt"
"strconv"
)
// jHistory is a private map for the Zabbix API History object.
// See: https://www.zabbix.com/documentation/4.0/manual/api/reference/history/get
type jHistory struct {
ItemID string `json:"itemid"`
Clock string `json:"clock"`
Ns string `json:"ns"`
Value string `json:"value"`
LogEventID string `json:"logeventid,omitempty"`
Severity string `json:"severity,omitempty"`
Source string `json:"source,omitempty"`
Timestamp string `json:"timestamp,omitempty"`
}
// History returns a native Go History struct mapped from the given JSON History data.
func (c *jHistory) History() (*History, error) {
var err error
history := &History{}
history.Clock, err = strconv.Atoi(c.Clock)
if err != nil {
return nil, fmt.Errorf("Error parsing History Clock: %v", err)
}
history.ItemID, err = strconv.Atoi(c.ItemID)
if err != nil {
return nil, fmt.Errorf("Error parsing History ItemID: %v", err)
}
history.Ns, err = strconv.Atoi(c.Ns)
if err != nil {
return nil, fmt.Errorf("Error parsing History Ns: %v", err)
}
history.Value = c.Value
if c.LogEventID != "" {
history.LogEventID, err = strconv.Atoi(c.LogEventID)
if err != nil {
return nil, fmt.Errorf("Error parsing History LogEventID: %v", err)
}
}
if c.Severity != "" {
history.LogEventID, err = strconv.Atoi(c.Severity)
if err != nil {
return nil, fmt.Errorf("Error parsing History Severity: %v", err)
}
}
history.Source = c.Source
history.Timestamp = c.Timestamp
return history, err
}
// jHistories is a slice of jHistory structs.
type jHistories []jHistory
// Histories returns a native Go slice of Histories mapped from the given JSON HISTORIES
// data.
func (c jHistories) Histories() ([]History, error) {
if c != nil {
histories := make([]History, len(c))
for i, jhistory := range c {
history, err := jhistory.History()
if err != nil {
return nil, fmt.Errorf("Error unmarshalling History %d in JSON data: %v", i, err)
}
histories[i] = *history
}
return histories, nil
}
return nil, nil
}