-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutilities.go
367 lines (330 loc) · 12.2 KB
/
utilities.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
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
package harper
import (
"encoding/json"
"errors"
"strings"
"time"
)
const (
SearchBySQL = "sql"
SearchByHash = "search_by_hash"
SearchByValue = "search_by_value"
)
type ImportFromS3Response struct {
MessageResponse
JobId string `json:"job_id"`
}
type DeleteRecordsBeforeResponse struct {
MessageResponse
JobId string `json:"job_id"`
}
type SearchOperation struct {
Operation string `json:"operation,omitempty"`
SQL string `json:"sql,omitempty"`
}
type S3Credentials struct {
AWSAccessKeyID string `json:"aws_access_key_id,omitempty"`
AWSSecretAccessKey string `json:"aws_secret_access_key,omitempty"`
Bucket string `json:"bucket,omitempty"`
Key string `json:"filename,omitempty"`
Region string `json:"region,omitempty"`
}
type SysInfo struct {
System struct {
Platform string `json:"platform,omitempty"`
Distro string `json:"distro,omitempty"`
Release string `json:"release,omitempty"`
Codename string `json:"codename,omitempty"`
Kernel string `json:"kernel,omitempty"`
Arch string `json:"arch,omitempty"`
Hostname string `json:"hostname,omitempty"`
NodeVersion string `json:"node_version,omitempty"`
NPMVersion string `json:"npm_version,omitempty"`
} `json:"system,omitempty"`
Time struct {
Current Timestamp `json:"current,omitempty"`
Uptime float64 `json:"uptime,omitempty"`
Timezone string `json:"timezone,omitempty"`
TimezoneName string `json:"timezoneName,omitempty"`
} `json:"time,omitempty"`
CPU struct {
Manufacturer string `json:"manufacturer,omitempty"`
Brand string `json:"brand,omitempty"`
Vendor string `json:"vendor,omitempty"`
Speed float64 `json:"speed,omitempty"`
Cores int `json:"cores,omitempty"`
PhysicalCores int `json:"physicalCores,omitempty"`
Processors int `json:"processors,omitempty"`
CPUSpeed struct {
Min float64 `json:"min,omitempty"`
Max float64 `json:"max,omitempty"`
Avg float64 `json:"avg,omitempty"`
Cores []float64 `json:"cores,omitempty"`
} `json:"cpu_speed,omitempty"`
CurrentLoad struct {
AvgLoad float64 `json:"avgload,omitempty"`
CurrentLoad float64 `json:"currentload,omitempty"`
CurrentLoadUser float64 `json:"currentload_user,omitempty"`
CurrentLoadSystem float64 `json:"currentload_system,omitempty"`
CurrentLoadNice float64 `json:"currentload_nice,omitempty"`
CurrentLoadIdle float64 `json:"currentload_idle,omitempty"`
CurrentLoadIRQ float64 `json:"currentload_irq,omitempty"`
} `json:"current_load,omitempty"`
CPUs []CPULoad `json:"cpus,omitempty"`
} `json:"cpu,omitempty"`
Memory struct {
Total int64 `json:"total,omitempty"`
Free int64 `json:"free,omitempty"`
Used int64 `json:"used,omitempty"`
Active int64 `json:"active,omitempty"`
Available int64 `json:"available,omitempty"`
SwapTotal int64 `json:"swaptotal,omitempty"`
SwapUsed int64 `json:"swapused,omitempty"`
SwapFree int64 `json:"swapfree,omitempty"`
} `json:"memory,omitempty"`
Disk struct {
IO struct {
RIO int64 `json:"rIO,omitempty"`
WIO int64 `json:"wIO,omitempty"`
TIO int64 `json:"tIO,omitempty"`
} `json:"io,omitempty"`
ReadWrite struct {
RX int64 `json:"rx,omitempty"`
WX int64 `json:"wx,omitempty"`
TX int64 `json:"tx,omitempty"`
MS int64 `json:"ms,omitempty"`
} `json:"read_write,omitempty"`
Size []DiskSize `json:"size,omitempty"`
} `json:"disk,omitempty"`
Network struct {
DefaultInterface string `json:"default_interface,omitempty"`
Latency struct {
URL string `json:"url,omitempty"`
Ok bool `json:"ok,omitempty"`
Status int64 `json:"status,omitempty"`
MS int64 `json:"ms,omitempty"`
} `json:"latency,omitempty"`
Interfaces []NetworkInterface `json:"interfaces,omitempty"`
Stats []NetworkStats `json:"stats,omitempty"`
Connections []NetworkConnection `json:"connections,omitempty"`
} `json:"network,omitempty"`
HarperDBProcesses struct {
Core []HDBProcess `json:"core,omitempty"`
Clustering []HDBProcess `json:"clustering,omitempty"`
} `json:"harperdb_processes,omitempty"`
TableSize []TableSize `json:"table_size,omitempty"`
Replication []NATSStreamInfo `json:"replication,omitempty"`
Threads []Thread `json:"threads,omitempty"`
}
type CPULoad struct {
Load float64 `json:"load,omitempty"`
LoadUser float64 `json:"load_user,omitempty"`
LoadSystem float64 `json:"load_system,omitempty"`
LoadNice float64 `json:"load_nice,omitempty"`
LoadIdle float64 `json:"load_idle,omitempty"`
LoadIRQ float64 `json:"load_irq,omitempty"`
}
type DiskSize struct {
FS string `json:"fs,omitempty"`
Type string `json:"overlay,omitempty"`
Size int64 `json:"size,omitempty"`
Used int64 `json:"used,omitempty"`
Use float64 `json:"use,omitempty"`
Mount string `json:"mount,omitempty"`
}
type NetworkInterface struct {
Iface string `json:"iface,omitempty"`
IfaceName string `json:"ifaceName,omitempty"`
IP4 string `json:"ip4,omitempty"`
IP6 string `json:"ip6,omitempty"`
Mac string `json:"mac,omitempty"`
OperState string `json:"operstate,omitempty"`
Type string `json:"virtual,omitempty"`
Duplex string `json:"duplex,omitempty"`
Speed float64 `json:"speed,omitempty"`
CarrierChanges int64 `json:"carrierChanges,omitempty"`
}
type NetworkStats struct {
Iface string `json:"iface,omitempty"`
OperState string `json:"operstate,omitempty"`
RxBytes int64 `json:"rx_bytes,omitempty"`
RxDropped int64 `json:"rx_dropped,omitempty"`
RxErrors int64 `json:"rx_errors,omitempty"`
TxBytes int64 `json:"tx_bytes,omitempty"`
TxDropped int64 `json:"tx_dropped,omitempty"`
TxErrors int64 `json:"tx_errors,omitempty"`
}
type NetworkConnection struct {
Protocol string `json:"protocol,omitempty"`
LocalAddress string `json:"localaddress,omitempty"`
LocalPort string `json:"localport,omitempty"`
PeerAddress string `json:"peeraddress,omitempty"`
PeerPort string `json:"peerport,omitempty"`
State string `json:"state,omitempty"`
PID int64 `json:"pid,omitempty"`
Process string `json:"node,omitempty"`
}
type HDBProcess struct {
PID int64 `json:"pid,omitempty"`
ParentPID int64 `json:"parentPid,omitempty"`
Name string `json:"name,omitempty"`
CPU float64 `json:"cpu,omitempty"`
CPUUser float64 `json:"cpuu,omitempty"`
CPUSystem float64 `json:"cpus,omitempty"`
Memory float64 `json:"mem,omitempty"`
Priority int64 `json:"priority,omitempty"`
MemVsz int64 `json:"memVsz,omitempty"`
MemRSS int64 `json:"memRss,omitempty"`
Nice int64 `json:"nice,omitempty"`
Started ProcessStarted `json:"started,omitempty"`
State string `json:"state,omitempty"`
TTY string `json:"tty,omitempty"`
User string `json:"user,omitempty"`
Command string `json:"command,omitempty"`
Params string `json:"params,omitempty"`
Path string `json:"path,omitempty"`
}
type ProcessStarted time.Time
// UnmarshalJSON for ProcessStarted values parses date-times that look like
// "YYYY-MM-DD HH:mm:ss" into time.Time values
func (ps *ProcessStarted) UnmarshalJSON(b []byte) error {
s := strings.Trim(string(b), `"`)
t, err := time.Parse(time.DateTime, s)
if err != nil {
return err
}
*ps = ProcessStarted(t)
return nil
}
type TableSize struct {
Schema string `json:"schema,omitempty"`
Table string `json:"table,omitempty"`
TableSize int64 `json:"table_size,omitempty"`
RecordCount int64 `json:"record_count,omitempty"`
TransactionLogSize int64 `json:"transaction_log_size,omitempty"`
TransactionLogRecordCount int64 `json:"transaction_log_record_count,omitempty"`
}
type NATSStreamInfo struct {
StreamName string `json:"stream_name,omitempty"`
Database string `json:"database,omitempty"`
Table string `json:"table,omitempty"`
State string `json:"state,omitempty"`
Consumers []Consumer `json:"consumers,omitempty"`
}
type Consumer struct {
Name string `json:"name,omitempty"`
Created time.Time `json:"created,omitempty"`
NumAckPending int64 `json:"num_ack_pending,omitempty"`
NumRedelivered int64 `json:"num_redelivered,omitempty"`
NumWaiting int64 `json:"num_waiting,omitempty"`
NumPending int64 `json:"num_pending,omitempty"`
}
type Thread struct {
ThreadID int64 `json:"threadId,omitempty"`
Name string `json:"name,omitempty"`
HeapTotal int64 `json:"heapTotal,omitempty"`
HeapUsed int64 `json:"heapUsed,omitempty"`
ExternalMemory int64 `json:"externalMemory,omitempty"`
ArrayBuffers int64 `json:"arrayBuffers,omitempty"`
SinceLastUpdate int64 `json:"sinceLastUpdate,omitempty"`
Idle float64 `json:"idle,omitempty"`
Active float64 `json:"active,omitempty"`
Utilization float64 `json:"utilization,omitempty"`
}
func (c *Client) ExportLocal(format, path string, searchOperation SearchOperation) error {
return c.opRequest(operation{
Operation: OP_EXPORT_TO_S3,
Format: format,
Path: path,
SearchOperation: &searchOperation,
}, nil)
}
func (c *Client) ExportToS3(format string, s3creds S3Credentials, searchOperation SearchOperation) error {
return c.opRequest(operation{
Operation: OP_EXPORT_TO_S3,
Format: format,
S3: &s3creds,
SearchOperation: &searchOperation,
}, nil)
}
func (c *Client) ImportFromS3(action, database, table string, s3creds S3Credentials) (*ImportFromS3Response, error) {
var result ImportFromS3Response
err := c.opRequest(operation{
Operation: OP_IMPORT_FROM_S3,
Action: action,
Database: database,
Table: table,
S3: &s3creds,
}, &result)
return &result, err
}
func (c *Client) SystemInformationAll() (*SysInfo, error) {
var sysInfo SysInfo
err := c.opRequest(operation{
Operation: OP_SYSTEM_INFORMATION,
}, &sysInfo)
return &sysInfo, err
}
func (c *Client) SystemInformation(attrs []string) (*SysInfo, error) {
var sysInfo SysInfo
err := c.opRequest(operation{
Operation: OP_SYSTEM_INFORMATION,
Attributes: attrs,
}, &sysInfo)
return &sysInfo, err
}
func (c *Client) Restart() (*MessageResponse, error) {
var response MessageResponse
err := c.opRequest(operation{
Operation: OP_RESTART,
}, &response)
return &response, err
}
func (c *Client) RestartService(service string) (*MessageResponse, error) {
var response MessageResponse
err := c.opRequest(operation{
Operation: OP_RESTART_SERVICE,
Service: service,
}, &response)
return &response, err
}
func (c *Client) DeleteRecordsBefore(date time.Time, schema, table string) (*DeleteRecordsBeforeResponse, error) {
var response DeleteRecordsBeforeResponse
err := c.opRequest(operation{
Operation: OP_DELETE_RECORDS_BEFORE,
Date: date.UTC().Format(time.RFC3339),
Schema: schema,
Table: table,
}, &response)
return &response, err
}
func (c *Client) InstallNodeModules(projects []string, dryRun bool) (*MessageResponse, error) {
var response MessageResponse
err := c.opRequest(operation{
Operation: OP_INSTALL_NODE_MODULES,
Projects: projects,
DryRun: dryRun,
}, &response)
return &response, err
}
func (c *Client) GetConfiguration() (map[string]interface{}, error) {
var response map[string]interface{}
err := c.opRequest(operation{
Operation: OP_GET_CONFIGURATION,
}, &response)
return response, err
}
func (c *Client) SetConfiguration(configuration interface{}) (*MessageResponse, error) {
var response MessageResponse
data, err := json.Marshal(configuration)
if err != nil {
return &MessageResponse{"unable to marshal struct into json"}, errors.New("unable to marshal struct into json")
}
v2 := map[string]interface{}{}
if err := json.Unmarshal(data, &v2); err != nil {
return &MessageResponse{"unable to unmarhsal struct into map"}, errors.New("unable to unmarhsal struct into map")
}
v2["operation"] = "set_configuration"
err = c.SetConfigurationRequest(v2, &response)
return &response, err
}