Files
go-zabbix/history.go

90 lines
1.7 KiB
Go

package zabbix
import (
"net/http"
"time"
"encoding/json"
)
type historyBase struct {
Clock Time `json:"clock"`
ItemID string `json:"itemid"`
Ns int `json:"ns"`
}
type HistoryFloat struct {
Value float32 `json:"value"`
historyBase
}
type HistoryInt struct {
Value int `json:"value"`
historyBase
}
type HistoryString struct {
Value string `json:"value"`
historyBase
}
type HistoryText struct {
ID string `json:"id"`
HistoryString
}
type HistoryLog struct {
LogEventID int `json:"logeventid"`
Severity int `json:"severity"`
Source int `json:"source"`
Timestamp Time `json:"timestamp"`
HistoryText
}
type HistoryGet struct {
HistoryType int `json:"history,omitempty"`
HostIDs []int `json:"hostids,omitempty"`
ItemIDs []int `json:"itemids,omitempty"`
TimeFrom Time `json:"time_from,omitempty"`
TimeTill Time `json:"time_till,omitempty"`
SortField string `json:"sortfield,omitempty"`
commonGet
}
type historyReq struct{
JSONrpc string `json:"jsonrpc"`
Method string `json:"method"`
Params HistoryGet `json:"params"`
}
func GetHistory(c *Connection,params *HistoryGet, result *HistoryInt) {
body := historyReq{
JSONrpc: "2.0",
Method: "history.get",
Params: &params,
}
bodyreq,err := json.Marshal(body)
if err != nil {
log.Fatal(err)
}
request, err = http.NewRequest(http.MethodPost, c.URL, bytes.NewBuffer(bodyreq))
request.Header.Set("Content-Type", "application/json")
request.Header.Set("Authorization", "Bearer "+c.APIKey)
client := &http.Client{
CheckRedirect: redirectPolicyFunc,
}
resp, err := client.Do(request)
defer resp.Body.Close()
body, err := io.ReadAll()
if err != nil {
log.Fatal(err)
}
}