-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
255 lines (227 loc) · 6.84 KB
/
main.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
package main
import (
"context"
"encoding/json"
"net/http"
"os"
"strings"
"time"
"github.com/prometheus/client_golang/api"
v1 "github.com/prometheus/client_golang/api/prometheus/v1"
"github.com/prometheus/common/model"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
)
type Node struct {
Instance string
Hostname string
}
var VERSION = "v0.2.0"
var PROMETHEUS_URL string
var NODES = []Node{}
var HOSTNAME string
func memAvailableBytes(node string) (float64, error) {
return queryPrometheusToValue(
`node_memory_MemAvailable_bytes{job="node-exporter", instance="` + node + `"}`,
)
}
func memTotalBytes(node string) (float64, error) {
return queryPrometheusToValue(
`node_memory_MemTotal_bytes{job="node-exporter", instance="` + node + `"}`,
)
}
func memUsagePercent(node string) (float64, error) {
return queryPrometheusToValue(
`100 -
(
avg(node_memory_MemAvailable_bytes{job="node-exporter", instance="` + node + `"}) /
avg(node_memory_MemTotal_bytes{job="node-exporter", instance="` + node + `"})
* 100
)`,
)
}
func fileSystemAvailableBytes(node string) (float64, error) {
return queryPrometheusToValue(
`node_filesystem_avail_bytes{job="node-exporter", instance="` + node + `", fstype!="", mountpoint="/"}`,
)
}
func fileSystemTotalBytes(node string) (float64, error) {
return queryPrometheusToValue(
`node_filesystem_size_bytes{job="node-exporter", instance="` + node + `", fstype!="", mountpoint="/"}`,
)
}
func fileSystemUsagePercent(node string) (float64, error) {
return queryPrometheusToValue(
`100 -
(
node_filesystem_avail_bytes{job="node-exporter", instance="` + node + `", fstype!="", mountpoint="/"} /
node_filesystem_size_bytes{job="node-exporter", instance="` + node + `", fstype!="", mountpoint="/"}
* 100
)`,
)
}
func cpuUsagePercent(node string) (float64, error) {
return queryPrometheusToValue(
`(1 - avg by (instance) (rate(node_cpu_seconds_total{mode="idle", instance="` + node + `"}[5m]))) * 100`,
)
}
func cpuCoresCount(node string) (float64, error) {
return queryPrometheusToValue(
`count(node_cpu_seconds_total{mode="idle", instance="` + node + `"})`,
)
}
type NodeMetrics struct {
Instance string `json:"instance"`
Hostname string `json:"hostname"`
MemTotalBytes float64 `json:"mem_total_bytes"`
MemAvailableBytes float64 `json:"mem_available_bytes"`
MemUsagePercent float64 `json:"mem_usage_percent"`
FileSystemAvailableBytes float64 `json:"filesystem_available_bytes"`
FileSystemTotalBytes float64 `json:"filesystem_total_bytes"`
FileSystemUsagePercent float64 `json:"filesystem_usage_percent"`
CpuUsagePercent float64 `json:"cpu_usage_percent"`
CpuCoresCount float64 `json:"cpu_cores_count"`
}
type Metadata struct {
Hostname string `json:"hostname"`
Version string `json:"version"`
}
type Response struct {
Metadata Metadata `json:"metadata"`
Status string `json:"status"`
ErrorCode string `json:"error_code"`
ErrorMessage string `json:"error_message"`
Metrics []NodeMetrics `json:"metrics"`
}
func main() {
PROMETHEUS_URL = os.Getenv("PROMETHEUS_URL")
if PROMETHEUS_URL == "" {
logFatal("environment vaiable PROMETHEUS_URL is required")
}
nodesListEnv := os.Getenv("NODES")
if nodesListEnv == "" {
logFatal("environment vaiable NODES is required")
}
nodesEnv := strings.Split(nodesListEnv, ",")
for _, nodeEnv := range nodesEnv {
nodeEnvSplit := strings.Split(nodeEnv, "=")
instance := nodeEnvSplit[0]
hostname := ""
if len(nodeEnvSplit) > 1 {
hostname = nodeEnvSplit[1]
}
NODES = append(NODES, Node{
Instance: instance,
Hostname: hostname,
})
}
zerolog.TimeFieldFormat = zerolog.TimeFormatUnix
HOSTNAME, _ = os.Hostname()
http.HandleFunc("/favicon.ico", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusNotFound)
})
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
isOK := true
metrics := []NodeMetrics{}
for _, node := range NODES {
memTotalBytes, err := memTotalBytes(node.Instance)
isOK = isOK && err == nil
memAvailableBytes, err := memAvailableBytes(node.Instance)
isOK = isOK && err == nil
memUsagePercent, err := memUsagePercent(node.Instance)
isOK = isOK && err == nil
fileSystemAvailableBytes, err := fileSystemAvailableBytes(node.Instance)
isOK = isOK && err == nil
fileSystemTotalBytes, err := fileSystemTotalBytes(node.Instance)
isOK = isOK && err == nil
fileSystemUsagePercent, err := fileSystemUsagePercent(node.Instance)
isOK = isOK && err == nil
cpuUsagePercent, err := cpuUsagePercent(node.Instance)
isOK = isOK && err == nil
cpuCoresCount, err := cpuCoresCount(node.Instance)
isOK = isOK && err == nil
metrics = append(metrics, NodeMetrics{
Instance: node.Instance,
Hostname: node.Hostname,
MemTotalBytes: memTotalBytes,
MemAvailableBytes: memAvailableBytes,
MemUsagePercent: memUsagePercent,
FileSystemAvailableBytes: fileSystemAvailableBytes,
FileSystemTotalBytes: fileSystemTotalBytes,
FileSystemUsagePercent: fileSystemUsagePercent,
CpuUsagePercent: cpuUsagePercent,
CpuCoresCount: cpuCoresCount,
})
}
status := "OK"
errorCode := ""
errorMessage := ""
if !isOK {
status = "ERR"
errorCode = "ERROR"
errorMessage = "Some metrics are not available"
}
data, _ := json.Marshal(Response{
Metadata: Metadata{
Hostname: HOSTNAME,
Version: VERSION,
},
Status: status,
ErrorCode: errorCode,
ErrorMessage: errorMessage,
Metrics: metrics,
})
w.Header().Set("Content-Type", "application/json")
w.Write(data)
if isOK {
logDebug("GET / OK")
} else {
logErrorWithCode(errorCode, "GET / ERR: "+errorMessage)
}
})
logInfo("Starting server on port 0.0.0.0:8000, http://127.0.0.1:8000")
err := http.ListenAndServe(":8000", nil)
if err != nil {
logFatal(err.Error())
}
}
func queryPrometheusToValue(query string) (float64, error) {
client, err := api.NewClient(api.Config{
Address: PROMETHEUS_URL,
})
if err != nil {
return 0, err
}
queryClient := v1.NewAPI(client)
result, err := queryClient.Query(context.Background(), query, time.Now())
if err != nil {
return 0, err
}
return float64(result.(model.Vector)[0].Value), nil
}
func logDebug(msg string) {
log.Debug().
Str("version", VERSION).
Str("hostname", HOSTNAME).
Msg(msg)
}
func logInfo(msg string) {
log.Info().
Str("version", VERSION).
Str("hostname", HOSTNAME).
Msg(msg)
}
func logErrorWithCode(code, msg string) {
log.Error().
Str("version", VERSION).
Str("hostname", HOSTNAME).
Str("error_code", code).
Msg(msg)
}
func logFatal(msg string) {
log.Fatal().
Str("version", VERSION).
Str("hostname", HOSTNAME).
Msg(msg)
os.Exit(1)
}