-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathrest.go
More file actions
226 lines (187 loc) · 4.99 KB
/
rest.go
File metadata and controls
226 lines (187 loc) · 4.99 KB
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
package main
import (
"errors"
"fmt"
"regexp"
"strconv"
"strings"
"github.com/imroc/req"
)
type InputDetail struct {
Name string `json:"name"`
Identifier string `json:"id"`
State string `json:"state"`
Message string `json:"message"`
}
type InputOverview []struct {
ID string `json:"id"`
}
type CloudService struct {
Name string `json:"service"`
State string `json:"state"`
}
type MQTTRestResponse struct {
Default string `json:"defaultBroker"`
Kubernetes string `json:"kubernetes"`
}
type MQTTServer struct {
Name string
State string
}
type SystemResponse struct {
FreeMemory float64 `json:"freeMemory"`
Disks []struct {
DriveLetter string `json:"disk"`
FreeSpace float64 `json:"freeSpace"`
} `json:"disks"`
}
type StatusResponse struct {
State string `json:"state"`
Message string `json:"message"`
NbrOfLoggedErrors float64 `json:"nbrOfLoggedErrors"`
RedundancyState struct {
State string `json:"state"`
Current string `json:"current"`
Configured string `json:"configured"`
} `json:"redundancyState"`
}
// CheckState compares two string values and returns either 0 oder 1
func CheckState(value string, state string) float64 {
if value == state {
return 1
} else {
return 0
}
}
// GetValue searches in the message field for a possible numerical value
func (i InputDetail) GetValue() (float64, error) {
r, _ := regexp.Compile("[0-9]*,[0-9[0-9]]*")
if v := r.FindString(i.Message); v != "" {
// Convert float from german to american notation
v = strings.ReplaceAll(v, ",", ".")
f, _ := strconv.ParseFloat(v, 64)
return f, nil
} else {
return 0, errors.New("no value in message")
}
}
// QueryInputs returns a list with information about all alarm inputs
func QueryInputs(hostname string, AccessKey string) (*[]InputDetail, error) {
authHeader := req.Header{
"Accept": "application/json",
"Authorization": AccessKey,
}
var inputDetails []InputDetail
var inputOverview InputOverview
r, err := req.Get(fmt.Sprintf("%s/rest/monitoring/input", hostname), authHeader)
if err != nil {
return nil, err
}
err = r.ToJSON(&inputOverview)
if err != nil {
return nil, err
}
for _, input := range inputOverview {
r, err := req.Get(fmt.Sprintf("%s/rest/monitoring/input/%s", hostname, input.ID), authHeader)
if err != nil {
return nil, err
}
var detail InputDetail
err = r.ToJSON(&detail)
if err != nil {
return nil, err
}
detail.Identifier = input.ID
inputDetails = append(inputDetails, detail)
}
return &inputDetails, nil
}
// QueryCloudServices returns a list of all cloudservices
func QueryCloudServices(hostname string, AccessKey string) (*[]CloudService, error) {
authHeader := req.Header{
"Accept": "application/json",
"Authorization": AccessKey,
}
var services []CloudService
r, err := req.Get(fmt.Sprintf("%s/rest/monitoring/cloud", hostname), authHeader)
if err != nil {
return nil, err
}
err = r.ToJSON(&services)
if err != nil {
return nil, err
}
return &services, nil
}
// QueryMQTTServer returns a list of all mqtt brokers
func QueryMQTTServer(hostname string, AccessKey string) (*[]MQTTServer, error) {
authHeader := req.Header{
"Accept": "application/json",
"Authorization": AccessKey,
}
var resp MQTTRestResponse
r, err := req.Get(fmt.Sprintf("%s/rest/monitoring/mqtt", hostname), authHeader)
if err != nil {
return nil, err
}
err = r.ToJSON(&resp)
if err != nil {
return nil, err
}
// Convert data structure from single object to list of objects
// to fit the structure of all other endpoints
mqttServer := []MQTTServer{
MQTTServer{
Name: "defaultBroker",
State: resp.Default,
},
MQTTServer{
Name: "kubernetes",
State: resp.Kubernetes,
},
}
return &mqttServer, nil
}
// QuerySystem returns information about memory and disc space
func QuerySystem(hostname string, AccessKey string) (*SystemResponse, error) {
authHeader := req.Header{
"Accept": "application/json",
"Authorization": AccessKey,
}
var resp SystemResponse
r, err := req.Get(fmt.Sprintf("%s/rest/monitoring/system", hostname), authHeader)
if err != nil {
return nil, err
}
err = r.ToJSON(&resp)
if err != nil {
return nil, err
}
// Convert MB back to base unit
// MB -> KB -> Byte
resp.FreeMemory *= (1024 * 1024)
for idx := range resp.Disks {
// Convert GB back to base unit
// GB -> MB -> KB -> Byte
resp.Disks[idx].FreeSpace *= (1024 * 1024 * 1024)
resp.Disks[idx].DriveLetter = strings.Split(resp.Disks[idx].DriveLetter, ":")[0]
}
return &resp, nil
}
// QueryStatus returns status information about the FE2 software
func QueryStatus(hostname string, AccessKey string) (*StatusResponse, error) {
authHeader := req.Header{
"Accept": "application/json",
"Authorization": AccessKey,
}
var resp StatusResponse
r, err := req.Get(fmt.Sprintf("%s/rest/monitoring/status", hostname), authHeader)
if err != nil {
return nil, err
}
err = r.ToJSON(&resp)
if err != nil {
return nil, err
}
return &resp, nil
}