-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmetrics.go
110 lines (86 loc) · 3.37 KB
/
metrics.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
// Copyright 2024 qbee.io
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// SPDX-License-Identifier: Apache-2.0
package client
import (
"context"
"fmt"
"net/http"
)
// FilesystemMount contains the summary of a filesystem mount.
type FilesystemMount struct {
// Path is the mount point of the filesystem.
Path string `json:"path"`
// Used is the amount of used space in percent.
Used int64 `json:"used"`
// Available is the amount of available space in kilobytes.
Available int64 `json:"available"`
}
// FilesystemSummary contains the summary of the filesystems on a device.
type FilesystemSummary struct {
// Reported contains unix timestamp of when the metric was recorded.
Reported int64 `json:"reported"`
// Mounts contains the summary of the filesystem mounts.
Mounts []FilesystemMount `json:"mounts"`
}
// CPUSummary contains the summary of the CPU metrics on a device.
type CPUSummary struct {
// Reported contains unix timestamp of when the metric was recorded.
Reported int64 `json:"reported"`
// User is the percentage of CPU time spent in the user space.
User float64 `json:"user"`
// System is the percentage of CPU time spent in the kernel space.
System float64 `json:"system"`
// IO is the percentage of CPU time spent on waiting for I/O.
IO float64 `json:"io"`
}
// LoadSummary contains the summary of the load metrics on a device.
type LoadSummary struct {
// Reported contains unix timestamp of when the metric was recorded.
Reported int64 `json:"reported"`
// Minute1 average system load over 1 minute.
Minute1 float64 `json:"1min"`
// Minute1 average system load over 5 minutes.
Minute5 float64 `json:"5min"`
// Minute1 average system load over 15 minutes.
Minute15 float64 `json:"15min"`
}
// MemorySummary contains the summary of the memory metrics on a device.
type MemorySummary struct {
// Reported contains unix timestamp of when the metric was recorded.
Reported int64 `json:"reported"`
// Used is the percentage of memory being used.
Used int64 `json:"used"`
// SwapUsed is the percentage of swap being used.
SwapUsed int64 `json:"swap_used"`
// Available is the amount of available (free) memory in kilobytes.
Available int64 `json:"available"`
}
// MetricsSummary contains the summary of the metrics on a device.
type MetricsSummary struct {
Filesystem FilesystemSummary `json:"filesystem"`
CPU CPUSummary `json:"cpu"`
Load LoadSummary `json:"load"`
Memory MemorySummary `json:"memory"`
}
// GetLatestMetrics returns the latest metrics of a device (not older than 1 hour).
func (cli *Client) GetLatestMetrics(ctx context.Context, deviceID string) (*MetricsSummary, error) {
metricsSummary := new(MetricsSummary)
path := fmt.Sprintf("/api/v2/device/%s/metrics/latest", deviceID)
if err := cli.Call(ctx, http.MethodGet, path, nil, metricsSummary); err != nil {
return nil, err
}
return metricsSummary, nil
}