Added
Link Here
|
1 |
diff -urN vendor.orig/github.com/elastic/go-sysinfo/internal/cgo/disabled.go vendor/github.com/elastic/go-sysinfo/internal/cgo/disabled.go |
2 |
--- vendor.orig/github.com/elastic/go-sysinfo/internal/cgo/disabled.go 1970-01-01 01:00:00.000000000 +0100 |
3 |
+++ vendor/github.com/elastic/go-sysinfo/internal/cgo/disabled.go 2024-07-13 20:28:36.060882000 +0200 |
4 |
@@ -0,0 +1,23 @@ |
5 |
+// Licensed to Elasticsearch B.V. under one or more contributor |
6 |
+// license agreements. See the NOTICE file distributed with |
7 |
+// this work for additional information regarding copyright |
8 |
+// ownership. Elasticsearch B.V. licenses this file to you under |
9 |
+// the Apache License, Version 2.0 (the "License"); you may |
10 |
+// not use this file except in compliance with the License. |
11 |
+// You may obtain a copy of the License at |
12 |
+// |
13 |
+// http://www.apache.org/licenses/LICENSE-2.0 |
14 |
+// |
15 |
+// Unless required by applicable law or agreed to in writing, |
16 |
+// software distributed under the License is distributed on an |
17 |
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
18 |
+// KIND, either express or implied. See the License for the |
19 |
+// specific language governing permissions and limitations |
20 |
+// under the License. |
21 |
+ |
22 |
+//go:build !cgo |
23 |
+ |
24 |
+package cgo |
25 |
+ |
26 |
+// Enabled is true if cgo was enabled at compile-time. |
27 |
+const Enabled = false |
28 |
diff -urN vendor.orig/github.com/elastic/go-sysinfo/internal/cgo/enabled.go vendor/github.com/elastic/go-sysinfo/internal/cgo/enabled.go |
29 |
--- vendor.orig/github.com/elastic/go-sysinfo/internal/cgo/enabled.go 1970-01-01 01:00:00.000000000 +0100 |
30 |
+++ vendor/github.com/elastic/go-sysinfo/internal/cgo/enabled.go 2024-07-13 20:28:36.060953000 +0200 |
31 |
@@ -0,0 +1,23 @@ |
32 |
+// Licensed to Elasticsearch B.V. under one or more contributor |
33 |
+// license agreements. See the NOTICE file distributed with |
34 |
+// this work for additional information regarding copyright |
35 |
+// ownership. Elasticsearch B.V. licenses this file to you under |
36 |
+// the Apache License, Version 2.0 (the "License"); you may |
37 |
+// not use this file except in compliance with the License. |
38 |
+// You may obtain a copy of the License at |
39 |
+// |
40 |
+// http://www.apache.org/licenses/LICENSE-2.0 |
41 |
+// |
42 |
+// Unless required by applicable law or agreed to in writing, |
43 |
+// software distributed under the License is distributed on an |
44 |
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
45 |
+// KIND, either express or implied. See the License for the |
46 |
+// specific language governing permissions and limitations |
47 |
+// under the License. |
48 |
+ |
49 |
+//go:build cgo |
50 |
+ |
51 |
+package cgo |
52 |
+ |
53 |
+// Enabled is true if cgo was enabled at compile-time. |
54 |
+const Enabled = true |
55 |
diff -urN vendor.orig/github.com/elastic/go-sysinfo/providers/freebsd/defs_freebsd.go vendor/github.com/elastic/go-sysinfo/providers/freebsd/defs_freebsd.go |
56 |
--- vendor.orig/github.com/elastic/go-sysinfo/providers/freebsd/defs_freebsd.go 1970-01-01 01:00:00.000000000 +0100 |
57 |
+++ vendor/github.com/elastic/go-sysinfo/providers/freebsd/defs_freebsd.go 2024-07-13 20:28:36.065106000 +0200 |
58 |
@@ -0,0 +1,33 @@ |
59 |
+// Licensed to Elasticsearch B.V. under one or more contributor |
60 |
+// license agreements. See the NOTICE file distributed with |
61 |
+// this work for additional information regarding copyright |
62 |
+// ownership. Elasticsearch B.V. licenses this file to you under |
63 |
+// the Apache License, Version 2.0 (the "License"); you may |
64 |
+// not use this file except in compliance with the License. |
65 |
+// You may obtain a copy of the License at |
66 |
+// |
67 |
+// http://www.apache.org/licenses/LICENSE-2.0 |
68 |
+// |
69 |
+// Unless required by applicable law or agreed to in writing, |
70 |
+// software distributed under the License is distributed on an |
71 |
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
72 |
+// KIND, either express or implied. See the License for the |
73 |
+// specific language governing permissions and limitations |
74 |
+// under the License. |
75 |
+ |
76 |
+//go:build ignore |
77 |
+// +build ignore |
78 |
+ |
79 |
+package freebsd |
80 |
+ |
81 |
+/* |
82 |
+#include <sys/types.h> |
83 |
+#include <sys/vmmeter.h> |
84 |
+#include <sys/time.h> |
85 |
+#include <kvm.h> |
86 |
+*/ |
87 |
+import "C" |
88 |
+ |
89 |
+type kvmSwap C.struct_kvm_swap |
90 |
+ |
91 |
+type clockInfo C.struct_clockinfo |
92 |
diff -urN vendor.orig/github.com/elastic/go-sysinfo/providers/freebsd/doc.go vendor/github.com/elastic/go-sysinfo/providers/freebsd/doc.go |
93 |
--- vendor.orig/github.com/elastic/go-sysinfo/providers/freebsd/doc.go 1970-01-01 01:00:00.000000000 +0100 |
94 |
+++ vendor/github.com/elastic/go-sysinfo/providers/freebsd/doc.go 2024-07-13 20:28:36.065203000 +0200 |
95 |
@@ -0,0 +1,22 @@ |
96 |
+// Licensed to Elasticsearch B.V. under one or more contributor |
97 |
+// license agreements. See the NOTICE file distributed with |
98 |
+// this work for additional information regarding copyright |
99 |
+// ownership. Elasticsearch B.V. licenses this file to you under |
100 |
+// the Apache License, Version 2.0 (the "License"); you may |
101 |
+// not use this file except in compliance with the License. |
102 |
+// You may obtain a copy of the License at |
103 |
+// |
104 |
+// http://www.apache.org/licenses/LICENSE-2.0 |
105 |
+// |
106 |
+// Unless required by applicable law or agreed to in writing, |
107 |
+// software distributed under the License is distributed on an |
108 |
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
109 |
+// KIND, either express or implied. See the License for the |
110 |
+// specific language governing permissions and limitations |
111 |
+// under the License. |
112 |
+ |
113 |
+// Package freebsd implements the HostProvider and ProcessProvider interfaces |
114 |
+// for providing information about FreeBSD. |
115 |
+package freebsd |
116 |
+ |
117 |
+//go:generate sh -c "go tool cgo -godefs defs_freebsd.go > ztypes_freebsd.go" |
118 |
diff -urN vendor.orig/github.com/elastic/go-sysinfo/providers/freebsd/host_freebsd_cgo.go vendor/github.com/elastic/go-sysinfo/providers/freebsd/host_freebsd_cgo.go |
119 |
--- vendor.orig/github.com/elastic/go-sysinfo/providers/freebsd/host_freebsd_cgo.go 1970-01-01 01:00:00.000000000 +0100 |
120 |
+++ vendor/github.com/elastic/go-sysinfo/providers/freebsd/host_freebsd_cgo.go 2024-07-13 20:28:36.065317000 +0200 |
121 |
@@ -0,0 +1,238 @@ |
122 |
+// Licensed to Elasticsearch B.V. under one or more contributor |
123 |
+// license agreements. See the NOTICE file distributed with |
124 |
+// this work for additional information regarding copyright |
125 |
+// ownership. Elasticsearch B.V. licenses this file to you under |
126 |
+// the Apache License, Version 2.0 (the "License"); you may |
127 |
+// not use this file except in compliance with the License. |
128 |
+// You may obtain a copy of the License at |
129 |
+// |
130 |
+// http://www.apache.org/licenses/LICENSE-2.0 |
131 |
+// |
132 |
+// Unless required by applicable law or agreed to in writing, |
133 |
+// software distributed under the License is distributed on an |
134 |
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
135 |
+// KIND, either express or implied. See the License for the |
136 |
+// specific language governing permissions and limitations |
137 |
+// under the License. |
138 |
+ |
139 |
+//go:build freebsd && cgo |
140 |
+ |
141 |
+package freebsd |
142 |
+ |
143 |
+import ( |
144 |
+ "context" |
145 |
+ "errors" |
146 |
+ "os" |
147 |
+ "path/filepath" |
148 |
+ "time" |
149 |
+ |
150 |
+ "github.com/prometheus/procfs" |
151 |
+ |
152 |
+ "github.com/elastic/go-sysinfo/internal/registry" |
153 |
+ "github.com/elastic/go-sysinfo/providers/shared" |
154 |
+ "github.com/elastic/go-sysinfo/types" |
155 |
+) |
156 |
+ |
157 |
+func init() { |
158 |
+ registry.Register(newFreeBSDSystem()) |
159 |
+} |
160 |
+ |
161 |
+type freebsdSystem struct{} |
162 |
+ |
163 |
+func newFreeBSDSystem() freebsdSystem { |
164 |
+ return freebsdSystem{} |
165 |
+} |
166 |
+ |
167 |
+func (s freebsdSystem) Host() (types.Host, error) { |
168 |
+ return newHost() |
169 |
+} |
170 |
+ |
171 |
+type host struct { |
172 |
+ procFS procFS |
173 |
+ info types.HostInfo |
174 |
+} |
175 |
+ |
176 |
+func (h *host) Info() types.HostInfo { |
177 |
+ return h.info |
178 |
+} |
179 |
+ |
180 |
+func (h *host) CPUTime() (types.CPUTimes, error) { |
181 |
+ cpu := types.CPUTimes{} |
182 |
+ r := &reader{} |
183 |
+ r.cpuTime(&cpu) |
184 |
+ return cpu, r.Err() |
185 |
+} |
186 |
+ |
187 |
+func (h *host) Memory() (*types.HostMemoryInfo, error) { |
188 |
+ m := &types.HostMemoryInfo{} |
189 |
+ r := &reader{} |
190 |
+ r.memInfo(m) |
191 |
+ return m, r.Err() |
192 |
+} |
193 |
+ |
194 |
+func (h *host) FQDNWithContext(ctx context.Context) (string, error) { |
195 |
+ return shared.FQDNWithContext(ctx) |
196 |
+} |
197 |
+ |
198 |
+func (h *host) FQDN() (string, error) { |
199 |
+ return h.FQDNWithContext(context.Background()) |
200 |
+} |
201 |
+ |
202 |
+func newHost() (*host, error) { |
203 |
+ h := &host{} |
204 |
+ r := &reader{} |
205 |
+ r.architecture(h) |
206 |
+ r.bootTime(h) |
207 |
+ r.hostname(h) |
208 |
+ r.network(h) |
209 |
+ r.kernelVersion(h) |
210 |
+ r.os(h) |
211 |
+ r.time(h) |
212 |
+ r.uniqueID(h) |
213 |
+ return h, r.Err() |
214 |
+} |
215 |
+ |
216 |
+type reader struct { |
217 |
+ errs []error |
218 |
+} |
219 |
+ |
220 |
+func (r *reader) addErr(err error) bool { |
221 |
+ if err != nil { |
222 |
+ if !errors.Is(err, types.ErrNotImplemented) { |
223 |
+ r.errs = append(r.errs, err) |
224 |
+ } |
225 |
+ return true |
226 |
+ } |
227 |
+ return false |
228 |
+} |
229 |
+ |
230 |
+func (r *reader) Err() error { |
231 |
+ return errors.Join(r.errs...) |
232 |
+} |
233 |
+ |
234 |
+func (r *reader) cpuTime(cpu *types.CPUTimes) { |
235 |
+ times, err := cpuStateTimes() |
236 |
+ if r.addErr(err) { |
237 |
+ return |
238 |
+ } |
239 |
+ *cpu = *times |
240 |
+} |
241 |
+ |
242 |
+func (r *reader) memInfo(m *types.HostMemoryInfo) { |
243 |
+ // Memory counter calculations: |
244 |
+ // total = physical memory |
245 |
+ // used = active + wired |
246 |
+ // free = free |
247 |
+ // available = buffers + inactive + cache + free |
248 |
+ |
249 |
+ pageSize, err := pageSizeBytes() |
250 |
+ if r.addErr(err) { |
251 |
+ return |
252 |
+ } |
253 |
+ |
254 |
+ m.Total = totalPhysicalMem(r) |
255 |
+ activePages := activePageCount(r) |
256 |
+ |
257 |
+ m.Metrics = make(map[string]uint64, 6) |
258 |
+ m.Metrics["active_bytes"] = activePages * pageSize |
259 |
+ |
260 |
+ wirePages := wirePageCount(r) |
261 |
+ m.Metrics["wired_bytes"] = wirePages * pageSize |
262 |
+ |
263 |
+ inactivePages := inactivePageCount(r) |
264 |
+ m.Metrics["inactive_bytes"] = inactivePages * pageSize |
265 |
+ |
266 |
+ cachePages := cachePageCount(r) |
267 |
+ m.Metrics["cache_bytes"] = cachePages * pageSize |
268 |
+ |
269 |
+ freePages := freePageCount(r) |
270 |
+ m.Metrics["free_bytes"] = freePages * pageSize |
271 |
+ |
272 |
+ buffers := buffersUsedBytes(r) |
273 |
+ m.Metrics["buffer_bytes"] = buffers |
274 |
+ |
275 |
+ m.Used = (activePages + wirePages) * pageSize |
276 |
+ m.Free = freePages * pageSize |
277 |
+ m.Available = (inactivePages+cachePages+freePages)*pageSize + buffers |
278 |
+ |
279 |
+ // Virtual (swap) Memory |
280 |
+ swap, err := kvmGetSwapInfo() |
281 |
+ if r.addErr(err) { |
282 |
+ return |
283 |
+ } |
284 |
+ |
285 |
+ m.VirtualTotal = uint64(swap.Total) * pageSize |
286 |
+ m.VirtualUsed = uint64(swap.Used) * pageSize |
287 |
+ m.VirtualFree = m.VirtualTotal - m.VirtualUsed |
288 |
+} |
289 |
+ |
290 |
+func (r *reader) architecture(h *host) { |
291 |
+ v, err := architecture() |
292 |
+ if r.addErr(err) { |
293 |
+ return |
294 |
+ } |
295 |
+ h.info.Architecture = v |
296 |
+} |
297 |
+ |
298 |
+func (r *reader) bootTime(h *host) { |
299 |
+ v, err := bootTime() |
300 |
+ if r.addErr(err) { |
301 |
+ return |
302 |
+ } |
303 |
+ h.info.BootTime = v |
304 |
+} |
305 |
+ |
306 |
+func (r *reader) hostname(h *host) { |
307 |
+ v, err := os.Hostname() |
308 |
+ if r.addErr(err) { |
309 |
+ return |
310 |
+ } |
311 |
+ h.info.Hostname = v |
312 |
+} |
313 |
+ |
314 |
+func (r *reader) network(h *host) { |
315 |
+ ips, macs, err := shared.Network() |
316 |
+ if r.addErr(err) { |
317 |
+ return |
318 |
+ } |
319 |
+ h.info.IPs = ips |
320 |
+ h.info.MACs = macs |
321 |
+} |
322 |
+ |
323 |
+func (r *reader) kernelVersion(h *host) { |
324 |
+ v, err := kernelVersion() |
325 |
+ if r.addErr(err) { |
326 |
+ return |
327 |
+ } |
328 |
+ h.info.KernelVersion = v |
329 |
+} |
330 |
+ |
331 |
+func (r *reader) os(h *host) { |
332 |
+ v, err := operatingSystem() |
333 |
+ if r.addErr(err) { |
334 |
+ return |
335 |
+ } |
336 |
+ h.info.OS = v |
337 |
+} |
338 |
+ |
339 |
+func (r *reader) time(h *host) { |
340 |
+ h.info.Timezone, h.info.TimezoneOffsetSec = time.Now().Zone() |
341 |
+} |
342 |
+ |
343 |
+func (r *reader) uniqueID(h *host) { |
344 |
+ v, err := machineID() |
345 |
+ if r.addErr(err) { |
346 |
+ return |
347 |
+ } |
348 |
+ h.info.UniqueID = v |
349 |
+} |
350 |
+ |
351 |
+type procFS struct { |
352 |
+ procfs.FS |
353 |
+ mountPoint string |
354 |
+} |
355 |
+ |
356 |
+func (fs *procFS) path(p ...string) string { |
357 |
+ elem := append([]string{fs.mountPoint}, p...) |
358 |
+ return filepath.Join(elem...) |
359 |
+} |
360 |
diff -urN vendor.orig/github.com/elastic/go-sysinfo/providers/freebsd/host_freebsd_cgo_test.go vendor/github.com/elastic/go-sysinfo/providers/freebsd/host_freebsd_cgo_test.go |
361 |
--- vendor.orig/github.com/elastic/go-sysinfo/providers/freebsd/host_freebsd_cgo_test.go 1970-01-01 01:00:00.000000000 +0100 |
362 |
+++ vendor/github.com/elastic/go-sysinfo/providers/freebsd/host_freebsd_cgo_test.go 2024-07-13 20:28:36.065385000 +0200 |
363 |
@@ -0,0 +1,53 @@ |
364 |
+// Licensed to Elasticsearch B.V. under one or more contributor |
365 |
+// license agreements. See the NOTICE file distributed with |
366 |
+// this work for additional information regarding copyright |
367 |
+// ownership. Elasticsearch B.V. licenses this file to you under |
368 |
+// the Apache License, Version 2.0 (the "License"); you may |
369 |
+// not use this file except in compliance with the License. |
370 |
+// You may obtain a copy of the License at |
371 |
+// |
372 |
+// http://www.apache.org/licenses/LICENSE-2.0 |
373 |
+// |
374 |
+// Unless required by applicable law or agreed to in writing, |
375 |
+// software distributed under the License is distributed on an |
376 |
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
377 |
+// KIND, either express or implied. See the License for the |
378 |
+// specific language governing permissions and limitations |
379 |
+// under the License. |
380 |
+ |
381 |
+//go:build freebsd && cgo |
382 |
+ |
383 |
+package freebsd |
384 |
+ |
385 |
+import ( |
386 |
+ "encoding/json" |
387 |
+ "testing" |
388 |
+ |
389 |
+ "github.com/elastic/go-sysinfo/internal/registry" |
390 |
+) |
391 |
+ |
392 |
+var _ registry.HostProvider = freebsdSystem{} |
393 |
+ |
394 |
+func TestHost(t *testing.T) { |
395 |
+ host, err := newFreeBSDSystem().Host() |
396 |
+ if err != nil { |
397 |
+ t.Fatal(err) |
398 |
+ } |
399 |
+ |
400 |
+ t.Run("Info", func(t *testing.T) { |
401 |
+ info := host.Info() |
402 |
+ |
403 |
+ data, _ := json.MarshalIndent(info, "", " ") |
404 |
+ t.Log(string(data)) |
405 |
+ }) |
406 |
+ |
407 |
+ t.Run("Memory", func(t *testing.T) { |
408 |
+ mem, err := host.Memory() |
409 |
+ if err != nil { |
410 |
+ t.Fatal(err) |
411 |
+ } |
412 |
+ |
413 |
+ data, _ := json.MarshalIndent(mem, "", " ") |
414 |
+ t.Log(string(data)) |
415 |
+ }) |
416 |
+} |
417 |
diff -urN vendor.orig/github.com/elastic/go-sysinfo/providers/freebsd/kvm_freebsd_cgo.go vendor/github.com/elastic/go-sysinfo/providers/freebsd/kvm_freebsd_cgo.go |
418 |
--- vendor.orig/github.com/elastic/go-sysinfo/providers/freebsd/kvm_freebsd_cgo.go 1970-01-01 01:00:00.000000000 +0100 |
419 |
+++ vendor/github.com/elastic/go-sysinfo/providers/freebsd/kvm_freebsd_cgo.go 2024-07-13 20:28:36.065498000 +0200 |
420 |
@@ -0,0 +1,58 @@ |
421 |
+// Licensed to Elasticsearch B.V. under one or more contributor |
422 |
+// license agreements. See the NOTICE file distributed with |
423 |
+// this work for additional information regarding copyright |
424 |
+// ownership. Elasticsearch B.V. licenses this file to you under |
425 |
+// the Apache License, Version 2.0 (the "License"); you may |
426 |
+// not use this file except in compliance with the License. |
427 |
+// You may obtain a copy of the License at |
428 |
+// |
429 |
+// http://www.apache.org/licenses/LICENSE-2.0 |
430 |
+// |
431 |
+// Unless required by applicable law or agreed to in writing, |
432 |
+// software distributed under the License is distributed on an |
433 |
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
434 |
+// KIND, either express or implied. See the License for the |
435 |
+// specific language governing permissions and limitations |
436 |
+// under the License. |
437 |
+ |
438 |
+//go:build freebsd && cgo |
439 |
+ |
440 |
+package freebsd |
441 |
+ |
442 |
+/* |
443 |
+#cgo LDFLAGS: -lkvm |
444 |
+#include <sys/cdefs.h> |
445 |
+#include <sys/types.h> |
446 |
+#include <sys/sysctl.h> |
447 |
+ |
448 |
+#include <paths.h> |
449 |
+#include <kvm.h> |
450 |
+#include <stdlib.h> |
451 |
+*/ |
452 |
+import "C" |
453 |
+ |
454 |
+import ( |
455 |
+ "fmt" |
456 |
+ "unsafe" |
457 |
+ |
458 |
+ "golang.org/x/sys/unix" |
459 |
+) |
460 |
+ |
461 |
+// kvmGetSwapInfo returns swap summary statistics for the system. It accesses |
462 |
+// the kernel virtual memory (kvm) images by using libkvm. |
463 |
+func kvmGetSwapInfo() (*kvmSwap, error) { |
464 |
+ // Obtain a KVM file descriptor. |
465 |
+ var errstr *C.char |
466 |
+ kd := C.kvm_open(nil, nil, nil, unix.O_RDONLY, errstr) |
467 |
+ if errstr != nil { |
468 |
+ return nil, fmt.Errorf("failed calling kvm_open: %s", C.GoString(errstr)) |
469 |
+ } |
470 |
+ defer C.kvm_close(kd) |
471 |
+ |
472 |
+ var swap kvmSwap |
473 |
+ if n, err := C.kvm_getswapinfo(kd, (*C.struct_kvm_swap)(unsafe.Pointer(&swap)), 1, 0); n != 0 { |
474 |
+ return nil, fmt.Errorf("failed to get kvm_getswapinfo: %w", err) |
475 |
+ } |
476 |
+ |
477 |
+ return &swap, nil |
478 |
+} |
479 |
diff -urN vendor.orig/github.com/elastic/go-sysinfo/providers/freebsd/process_freebsd_cgo.go vendor/github.com/elastic/go-sysinfo/providers/freebsd/process_freebsd_cgo.go |
480 |
--- vendor.orig/github.com/elastic/go-sysinfo/providers/freebsd/process_freebsd_cgo.go 1970-01-01 01:00:00.000000000 +0100 |
481 |
+++ vendor/github.com/elastic/go-sysinfo/providers/freebsd/process_freebsd_cgo.go 2024-07-13 20:28:36.065578000 +0200 |
482 |
@@ -0,0 +1,386 @@ |
483 |
+// Licensed to Elasticsearch B.V. under one or more contributor |
484 |
+// license agreements. See the NOTICE file distributed with |
485 |
+// this work for additional information regarding copyright |
486 |
+// ownership. Elasticsearch B.V. licenses this file to you under |
487 |
+// the Apache License, Version 2.0 (the "License"); you may |
488 |
+// not use this file except in compliance with the License. |
489 |
+// You may obtain a copy of the License at |
490 |
+// |
491 |
+// http://www.apache.org/licenses/LICENSE-2.0 |
492 |
+// |
493 |
+// Unless required by applicable law or agreed to in writing, |
494 |
+// software distributed under the License is distributed on an |
495 |
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
496 |
+// KIND, either express or implied. See the License for the |
497 |
+// specific language governing permissions and limitations |
498 |
+// under the License. |
499 |
+ |
500 |
+//go:build freebsd && cgo |
501 |
+ |
502 |
+package freebsd |
503 |
+ |
504 |
+import "C" |
505 |
+ |
506 |
+/* |
507 |
+#cgo LDFLAGS: -lprocstat |
508 |
+#include <sys/types.h> |
509 |
+#include <sys/sysctl.h> |
510 |
+#include <sys/time.h> |
511 |
+#include <sys/param.h> |
512 |
+#include <sys/queue.h> |
513 |
+#include <sys/socket.h> |
514 |
+#include <sys/user.h> |
515 |
+ |
516 |
+#include <libprocstat.h> |
517 |
+#include <string.h> |
518 |
+ |
519 |
+struct kinfo_proc getProcInfoAt(struct kinfo_proc *procs, unsigned int index) { |
520 |
+ return procs[index]; |
521 |
+} |
522 |
+ |
523 |
+unsigned int countArrayItems(char **items) { |
524 |
+ unsigned int i = 0; |
525 |
+ for (i = 0; items[i] != NULL; ++i); |
526 |
+ return i; |
527 |
+} |
528 |
+ |
529 |
+char * itemAtIndex(char **items, unsigned int index) { |
530 |
+ return items[index]; |
531 |
+} |
532 |
+ |
533 |
+unsigned int countFileStats(struct filestat_list *head) { |
534 |
+ unsigned int count = 0; |
535 |
+ struct filestat *fst; |
536 |
+ STAILQ_FOREACH(fst, head, next) { |
537 |
+ ++count; |
538 |
+ } |
539 |
+ |
540 |
+ return count; |
541 |
+} |
542 |
+ |
543 |
+void copyFileStats(struct filestat_list *head, struct filestat *out, unsigned int size) { |
544 |
+ unsigned int index = 0; |
545 |
+ struct filestat *fst; |
546 |
+ if (size == 0) { |
547 |
+ return; |
548 |
+ } |
549 |
+ STAILQ_FOREACH(fst, head, next) { |
550 |
+ memcpy(out, fst, sizeof(*fst)); |
551 |
+ ++out; |
552 |
+ --size; |
553 |
+ } |
554 |
+} |
555 |
+*/ |
556 |
+import "C" |
557 |
+ |
558 |
+import ( |
559 |
+ "errors" |
560 |
+ "fmt" |
561 |
+ "os" |
562 |
+ "strconv" |
563 |
+ "strings" |
564 |
+ "time" |
565 |
+ |
566 |
+ "github.com/elastic/go-sysinfo/types" |
567 |
+) |
568 |
+ |
569 |
+func getProcInfo(op, arg int) ([]process, error) { |
570 |
+ procstat, err := C.procstat_open_sysctl() |
571 |
+ if procstat == nil { |
572 |
+ return nil, fmt.Errorf("failed to open procstat sysctl: %w", err) |
573 |
+ } |
574 |
+ defer C.procstat_close(procstat) |
575 |
+ |
576 |
+ var count C.uint = 0 |
577 |
+ kprocs, err := C.procstat_getprocs(procstat, C.int(op), C.int(arg), &count) |
578 |
+ if kprocs == nil { |
579 |
+ return nil, fmt.Errorf("getprocs failed: %w", err) |
580 |
+ } |
581 |
+ defer C.procstat_freeprocs(procstat, kprocs) |
582 |
+ |
583 |
+ procs := make([]process, count) |
584 |
+ var index C.uint |
585 |
+ for index = 0; index < count; index++ { |
586 |
+ proc := C.getProcInfoAt(kprocs, index) |
587 |
+ procs[index].kinfo = proc |
588 |
+ procs[index].pid = int(proc.ki_pid) |
589 |
+ } |
590 |
+ |
591 |
+ return procs, nil |
592 |
+} |
593 |
+ |
594 |
+func copyArray(from **C.char) []string { |
595 |
+ if from == nil { |
596 |
+ return nil |
597 |
+ } |
598 |
+ |
599 |
+ count := C.countArrayItems(from) |
600 |
+ out := make([]string, count) |
601 |
+ |
602 |
+ for index := C.uint(0); index < count; index++ { |
603 |
+ out[index] = C.GoString(C.itemAtIndex(from, index)) |
604 |
+ } |
605 |
+ |
606 |
+ return out |
607 |
+} |
608 |
+ |
609 |
+func makeMap(from []string) map[string]string { |
610 |
+ out := make(map[string]string, len(from)) |
611 |
+ |
612 |
+ for _, env := range from { |
613 |
+ parts := strings.Split(env, "=") |
614 |
+ if len(parts) > 1 { |
615 |
+ out[parts[0]] = parts[1] |
616 |
+ } |
617 |
+ } |
618 |
+ |
619 |
+ return out |
620 |
+} |
621 |
+ |
622 |
+func getProcEnv(p *process) (map[string]string, error) { |
623 |
+ procstat, err := C.procstat_open_sysctl() |
624 |
+ |
625 |
+ if procstat == nil { |
626 |
+ return nil, fmt.Errorf("failed to open procstat sysctl: %w", err) |
627 |
+ } |
628 |
+ defer C.procstat_close(procstat) |
629 |
+ |
630 |
+ env, err := C.procstat_getenvv(procstat, &p.kinfo, 0) |
631 |
+ defer C.procstat_freeenvv(procstat) |
632 |
+ |
633 |
+ return makeMap(copyArray(env)), err |
634 |
+} |
635 |
+ |
636 |
+func getProcArgs(p *process) ([]string, error) { |
637 |
+ procstat, err := C.procstat_open_sysctl() |
638 |
+ |
639 |
+ if procstat == nil { |
640 |
+ return nil, fmt.Errorf("failed to open procstat sysctl: %w", err) |
641 |
+ } |
642 |
+ defer C.procstat_close(procstat) |
643 |
+ |
644 |
+ args, err := C.procstat_getargv(procstat, &p.kinfo, 0) |
645 |
+ defer C.procstat_freeargv(procstat) |
646 |
+ |
647 |
+ return copyArray(args), err |
648 |
+} |
649 |
+ |
650 |
+func getProcPathname(p *process) (string, error) { |
651 |
+ procstat, err := C.procstat_open_sysctl() |
652 |
+ |
653 |
+ if procstat == nil { |
654 |
+ return "", fmt.Errorf("failed to open procstat sysctl: %w", err) |
655 |
+ } |
656 |
+ defer C.procstat_close(procstat) |
657 |
+ |
658 |
+ const maxlen = uint(1024) |
659 |
+ out := make([]C.char, maxlen) |
660 |
+ if res, err := C.procstat_getpathname(procstat, &p.kinfo, &out[0], C.ulong(maxlen)); res != 0 { |
661 |
+ return "", err |
662 |
+ } |
663 |
+ return C.GoString(&out[0]), nil |
664 |
+} |
665 |
+ |
666 |
+func getFileStats(fileStats *C.struct_filestat_list) []C.struct_filestat { |
667 |
+ count := C.countFileStats(fileStats) |
668 |
+ |
669 |
+ if count < 1 { |
670 |
+ return nil |
671 |
+ } |
672 |
+ |
673 |
+ out := make([]C.struct_filestat, count) |
674 |
+ |
675 |
+ C.copyFileStats(fileStats, &out[0], count) |
676 |
+ return out |
677 |
+} |
678 |
+ |
679 |
+func getProcCWD(p *process) (string, error) { |
680 |
+ procstat, err := C.procstat_open_sysctl() |
681 |
+ |
682 |
+ if procstat == nil { |
683 |
+ return "", fmt.Errorf("failed to open procstat sysctl: %w", err) |
684 |
+ } |
685 |
+ defer C.procstat_close(procstat) |
686 |
+ |
687 |
+ fs, err := C.procstat_getfiles(procstat, &p.kinfo, 0) |
688 |
+ if fs == nil { |
689 |
+ return "", fmt.Errorf("failed to get files: %w", err) |
690 |
+ } |
691 |
+ |
692 |
+ defer C.procstat_freefiles(procstat, fs) |
693 |
+ |
694 |
+ files := getFileStats(fs) |
695 |
+ for _, f := range files { |
696 |
+ if f.fs_uflags == C.PS_FST_UFLAG_CDIR { |
697 |
+ return C.GoString(f.fs_path), nil |
698 |
+ } |
699 |
+ } |
700 |
+ |
701 |
+ return "", nil |
702 |
+} |
703 |
+ |
704 |
+type process struct { |
705 |
+ pid int |
706 |
+ kinfo C.struct_kinfo_proc |
707 |
+} |
708 |
+ |
709 |
+func timevalToDuration(tm C.struct_timeval) time.Duration { |
710 |
+ return time.Duration(tm.tv_sec)*time.Second + |
711 |
+ time.Duration(tm.tv_usec)*time.Microsecond |
712 |
+} |
713 |
+ |
714 |
+func (p *process) CPUTime() (types.CPUTimes, error) { |
715 |
+ procs, err := getProcInfo(C.KERN_PROC_PID, p.PID()) |
716 |
+ if err != nil { |
717 |
+ return types.CPUTimes{}, err |
718 |
+ } |
719 |
+ p.kinfo = procs[0].kinfo |
720 |
+ |
721 |
+ return types.CPUTimes{ |
722 |
+ User: timevalToDuration(p.kinfo.ki_rusage.ru_utime), |
723 |
+ System: timevalToDuration(p.kinfo.ki_rusage.ru_stime), |
724 |
+ }, nil |
725 |
+} |
726 |
+ |
727 |
+func (p *process) Info() (types.ProcessInfo, error) { |
728 |
+ procs, err := getProcInfo(C.KERN_PROC_PID, p.PID()) |
729 |
+ if err != nil { |
730 |
+ return types.ProcessInfo{}, err |
731 |
+ } |
732 |
+ p.kinfo = procs[0].kinfo |
733 |
+ |
734 |
+ cwd, err := getProcCWD(p) |
735 |
+ if err != nil { |
736 |
+ return types.ProcessInfo{}, err |
737 |
+ } |
738 |
+ |
739 |
+ args, err := getProcArgs(p) |
740 |
+ if err != nil { |
741 |
+ return types.ProcessInfo{}, err |
742 |
+ } |
743 |
+ |
744 |
+ exe, _ := getProcPathname(p) |
745 |
+ |
746 |
+ return types.ProcessInfo{ |
747 |
+ Name: C.GoString(&p.kinfo.ki_comm[0]), |
748 |
+ PID: int(p.kinfo.ki_pid), |
749 |
+ PPID: int(p.kinfo.ki_ppid), |
750 |
+ CWD: cwd, |
751 |
+ Exe: exe, |
752 |
+ Args: args, |
753 |
+ StartTime: time.Unix(int64(p.kinfo.ki_start.tv_sec), int64(p.kinfo.ki_start.tv_usec)*1000), |
754 |
+ }, nil |
755 |
+} |
756 |
+ |
757 |
+func (p *process) Memory() (types.MemoryInfo, error) { |
758 |
+ pageSize, err := pageSizeBytes() |
759 |
+ if err != nil { |
760 |
+ return types.MemoryInfo{}, err |
761 |
+ } |
762 |
+ |
763 |
+ procs, err := getProcInfo(C.KERN_PROC_PID, p.PID()) |
764 |
+ if err != nil { |
765 |
+ return types.MemoryInfo{}, err |
766 |
+ } |
767 |
+ p.kinfo = procs[0].kinfo |
768 |
+ |
769 |
+ return types.MemoryInfo{ |
770 |
+ Resident: uint64(p.kinfo.ki_rssize) * pageSize, |
771 |
+ Virtual: uint64(p.kinfo.ki_size), |
772 |
+ }, nil |
773 |
+} |
774 |
+ |
775 |
+func (p *process) User() (types.UserInfo, error) { |
776 |
+ procs, err := getProcInfo(C.KERN_PROC_PID, p.PID()) |
777 |
+ if err != nil { |
778 |
+ return types.UserInfo{}, err |
779 |
+ } |
780 |
+ |
781 |
+ p.kinfo = procs[0].kinfo |
782 |
+ |
783 |
+ return types.UserInfo{ |
784 |
+ UID: strconv.FormatUint(uint64(p.kinfo.ki_ruid), 10), |
785 |
+ EUID: strconv.FormatUint(uint64(p.kinfo.ki_uid), 10), |
786 |
+ SUID: strconv.FormatUint(uint64(p.kinfo.ki_svuid), 10), |
787 |
+ GID: strconv.FormatUint(uint64(p.kinfo.ki_rgid), 10), |
788 |
+ EGID: strconv.FormatUint(uint64(p.kinfo.ki_groups[0]), 10), |
789 |
+ SGID: strconv.FormatUint(uint64(p.kinfo.ki_svgid), 10), |
790 |
+ }, nil |
791 |
+} |
792 |
+ |
793 |
+func (p *process) PID() int { |
794 |
+ return p.pid |
795 |
+} |
796 |
+ |
797 |
+func (p *process) OpenHandles() ([]string, error) { |
798 |
+ procstat := C.procstat_open_sysctl() |
799 |
+ if procstat == nil { |
800 |
+ return nil, errors.New("failed to open procstat sysctl") |
801 |
+ } |
802 |
+ defer C.procstat_close(procstat) |
803 |
+ |
804 |
+ fs := C.procstat_getfiles(procstat, &p.kinfo, 0) |
805 |
+ defer C.procstat_freefiles(procstat, fs) |
806 |
+ |
807 |
+ files := getFileStats(fs) |
808 |
+ names := make([]string, 0, len(files)) |
809 |
+ |
810 |
+ for _, file := range files { |
811 |
+ if file.fs_type == 1 { |
812 |
+ if path := C.GoString(file.fs_path); path != "" { |
813 |
+ names = append(names, path) |
814 |
+ } |
815 |
+ } |
816 |
+ } |
817 |
+ |
818 |
+ return names, nil |
819 |
+} |
820 |
+ |
821 |
+func (p *process) OpenHandleCount() (int, error) { |
822 |
+ procstat := C.procstat_open_sysctl() |
823 |
+ if procstat == nil { |
824 |
+ return 0, errors.New("failed to open procstat sysctl") |
825 |
+ } |
826 |
+ defer C.procstat_close(procstat) |
827 |
+ |
828 |
+ fs := C.procstat_getfiles(procstat, &p.kinfo, 0) |
829 |
+ defer C.procstat_freefiles(procstat, fs) |
830 |
+ return int(C.countFileStats(fs)), nil |
831 |
+} |
832 |
+ |
833 |
+func (p *process) Environment() (map[string]string, error) { |
834 |
+ return getProcEnv(p) |
835 |
+} |
836 |
+ |
837 |
+func (s freebsdSystem) Processes() ([]types.Process, error) { |
838 |
+ procs, err := getProcInfo(C.KERN_PROC_PROC, 0) |
839 |
+ out := make([]types.Process, 0, len(procs)) |
840 |
+ |
841 |
+ for _, proc := range procs { |
842 |
+ out = append(out, &process{ |
843 |
+ pid: proc.pid, |
844 |
+ kinfo: proc.kinfo, |
845 |
+ }) |
846 |
+ } |
847 |
+ |
848 |
+ return out, err |
849 |
+} |
850 |
+ |
851 |
+func (s freebsdSystem) Process(pid int) (types.Process, error) { |
852 |
+ return &process{pid: pid}, nil |
853 |
+} |
854 |
+ |
855 |
+func (s freebsdSystem) Self() (types.Process, error) { |
856 |
+ return s.Process(os.Getpid()) |
857 |
+} |
858 |
+ |
859 |
+func (p *process) Parent() (types.Process, error) { |
860 |
+ info, err := p.Info() |
861 |
+ if err != nil { |
862 |
+ return nil, err |
863 |
+ } |
864 |
+ |
865 |
+ proc := process{pid: info.PPID} |
866 |
+ |
867 |
+ return &proc, nil |
868 |
+} |
869 |
diff -urN vendor.orig/github.com/elastic/go-sysinfo/providers/freebsd/sysctl_freebsd.go vendor/github.com/elastic/go-sysinfo/providers/freebsd/sysctl_freebsd.go |
870 |
--- vendor.orig/github.com/elastic/go-sysinfo/providers/freebsd/sysctl_freebsd.go 1970-01-01 01:00:00.000000000 +0100 |
871 |
+++ vendor/github.com/elastic/go-sysinfo/providers/freebsd/sysctl_freebsd.go 2024-07-13 20:28:36.065700000 +0200 |
872 |
@@ -0,0 +1,248 @@ |
873 |
+// Licensed to Elasticsearch B.V. under one or more contributor |
874 |
+// license agreements. See the NOTICE file distributed with |
875 |
+// this work for additional information regarding copyright |
876 |
+// ownership. Elasticsearch B.V. licenses this file to you under |
877 |
+// the Apache License, Version 2.0 (the "License"); you may |
878 |
+// not use this file except in compliance with the License. |
879 |
+// You may obtain a copy of the License at |
880 |
+// |
881 |
+// http://www.apache.org/licenses/LICENSE-2.0 |
882 |
+// |
883 |
+// Unless required by applicable law or agreed to in writing, |
884 |
+// software distributed under the License is distributed on an |
885 |
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
886 |
+// KIND, either express or implied. See the License for the |
887 |
+// specific language governing permissions and limitations |
888 |
+// under the License. |
889 |
+ |
890 |
+//go:build freebsd |
891 |
+ |
892 |
+package freebsd |
893 |
+ |
894 |
+import ( |
895 |
+ "fmt" |
896 |
+ "strconv" |
897 |
+ "strings" |
898 |
+ "sync" |
899 |
+ "time" |
900 |
+ "unsafe" |
901 |
+ |
902 |
+ "github.com/elastic/go-sysinfo/types" |
903 |
+ |
904 |
+ "golang.org/x/sys/unix" |
905 |
+) |
906 |
+ |
907 |
+var tickDuration = sync.OnceValues(func() (time.Duration, error) { |
908 |
+ const mib = "kern.clockrate" |
909 |
+ |
910 |
+ c, err := unix.SysctlClockinfo(mib) |
911 |
+ if err != nil { |
912 |
+ return 0, fmt.Errorf("failed to get %s: %w", mib, err) |
913 |
+ } |
914 |
+ return time.Duration(c.Tick) * time.Microsecond, nil |
915 |
+}) |
916 |
+ |
917 |
+var pageSizeBytes = sync.OnceValues(func() (uint64, error) { |
918 |
+ const mib = "vm.stats.vm.v_page_size" |
919 |
+ |
920 |
+ v, err := unix.SysctlUint32(mib) |
921 |
+ if err != nil { |
922 |
+ return 0, fmt.Errorf("failed to get %s: %w", mib, err) |
923 |
+ } |
924 |
+ |
925 |
+ return uint64(v), nil |
926 |
+}) |
927 |
+ |
928 |
+func activePageCount(r *reader) uint64 { |
929 |
+ const mib = "vm.stats.vm.v_active_count" |
930 |
+ |
931 |
+ v, err := unix.SysctlUint32(mib) |
932 |
+ if r.addErr(err) { |
933 |
+ return 0 |
934 |
+ } |
935 |
+ return uint64(v) |
936 |
+} |
937 |
+ |
938 |
+func architecture() (string, error) { |
939 |
+ const mib = "hw.machine" |
940 |
+ |
941 |
+ arch, err := unix.Sysctl(mib) |
942 |
+ if err != nil { |
943 |
+ return "", fmt.Errorf("failed to get architecture: %w", err) |
944 |
+ } |
945 |
+ |
946 |
+ return arch, nil |
947 |
+} |
948 |
+ |
949 |
+func bootTime() (time.Time, error) { |
950 |
+ const mib = "kern.boottime" |
951 |
+ |
952 |
+ tv, err := unix.SysctlTimeval(mib) |
953 |
+ if err != nil { |
954 |
+ return time.Time{}, fmt.Errorf("failed to get host uptime: %w", err) |
955 |
+ } |
956 |
+ |
957 |
+ bootTime := time.Unix(tv.Sec, tv.Usec*int64(time.Microsecond)) |
958 |
+ return bootTime, nil |
959 |
+} |
960 |
+ |
961 |
+// buffersUsedBytes returns the number memory bytes used as disk cache. |
962 |
+func buffersUsedBytes(r *reader) uint64 { |
963 |
+ const mib = "vfs.bufspace" |
964 |
+ |
965 |
+ v, err := unix.SysctlUint64(mib) |
966 |
+ if r.addErr(err) { |
967 |
+ return 0 |
968 |
+ } |
969 |
+ |
970 |
+ return v |
971 |
+} |
972 |
+ |
973 |
+func cachePageCount(r *reader) uint64 { |
974 |
+ const mib = "vm.stats.vm.v_cache_count" |
975 |
+ |
976 |
+ v, err := unix.SysctlUint32(mib) |
977 |
+ if r.addErr(err) { |
978 |
+ return 0 |
979 |
+ } |
980 |
+ |
981 |
+ return uint64(v) |
982 |
+} |
983 |
+ |
984 |
+const sizeOfUint64 = int(unsafe.Sizeof(uint64(0))) |
985 |
+ |
986 |
+// cpuStateTimes uses sysctl kern.cp_time to get the amount of time spent in |
987 |
+// different CPU states. |
988 |
+func cpuStateTimes() (*types.CPUTimes, error) { |
989 |
+ tickDuration, err := tickDuration() |
990 |
+ if err != nil { |
991 |
+ return nil, err |
992 |
+ } |
993 |
+ |
994 |
+ const mib = "kern.cp_time" |
995 |
+ buf, err := unix.SysctlRaw(mib) |
996 |
+ if err != nil { |
997 |
+ return nil, fmt.Errorf("failed to get %s: %w", mib, err) |
998 |
+ } |
999 |
+ |
1000 |
+ var clockTicks [unix.CPUSTATES]uint64 |
1001 |
+ if len(buf) < len(clockTicks)*sizeOfUint64 { |
1002 |
+ return nil, fmt.Errorf("kern.cp_time data is too short (got %d bytes)", len(buf)) |
1003 |
+ } |
1004 |
+ for i := range clockTicks { |
1005 |
+ val := *(*uint64)(unsafe.Pointer(&buf[sizeOfUint64*i])) |
1006 |
+ clockTicks[i] = val |
1007 |
+ } |
1008 |
+ |
1009 |
+ return &types.CPUTimes{ |
1010 |
+ User: time.Duration(clockTicks[unix.CP_USER]) * tickDuration, |
1011 |
+ System: time.Duration(clockTicks[unix.CP_SYS]) * tickDuration, |
1012 |
+ Idle: time.Duration(clockTicks[unix.CP_IDLE]) * tickDuration, |
1013 |
+ IRQ: time.Duration(clockTicks[unix.CP_INTR]) * tickDuration, |
1014 |
+ Nice: time.Duration(clockTicks[unix.CP_NICE]) * tickDuration, |
1015 |
+ }, nil |
1016 |
+} |
1017 |
+ |
1018 |
+func freePageCount(r *reader) uint64 { |
1019 |
+ const mib = "vm.stats.vm.v_free_count" |
1020 |
+ |
1021 |
+ v, err := unix.SysctlUint32(mib) |
1022 |
+ if r.addErr(err) { |
1023 |
+ return 0 |
1024 |
+ } |
1025 |
+ |
1026 |
+ return uint64(v) |
1027 |
+} |
1028 |
+ |
1029 |
+func inactivePageCount(r *reader) uint64 { |
1030 |
+ const mib = "vm.stats.vm.v_inactive_count" |
1031 |
+ |
1032 |
+ v, err := unix.SysctlUint32(mib) |
1033 |
+ if r.addErr(err) { |
1034 |
+ return 0 |
1035 |
+ } |
1036 |
+ |
1037 |
+ return uint64(v) |
1038 |
+} |
1039 |
+ |
1040 |
+func kernelVersion() (string, error) { |
1041 |
+ const mib = "kern.osrelease" |
1042 |
+ |
1043 |
+ version, err := unix.Sysctl(mib) |
1044 |
+ if err != nil { |
1045 |
+ return "", fmt.Errorf("failed to get kernel version: %w", err) |
1046 |
+ } |
1047 |
+ |
1048 |
+ return version, nil |
1049 |
+} |
1050 |
+ |
1051 |
+func machineID() (string, error) { |
1052 |
+ const mib = "kern.hostuuid" |
1053 |
+ |
1054 |
+ uuid, err := unix.Sysctl(mib) |
1055 |
+ if err != nil { |
1056 |
+ return "", fmt.Errorf("failed to get machine id: %w", err) |
1057 |
+ } |
1058 |
+ |
1059 |
+ return uuid, nil |
1060 |
+} |
1061 |
+ |
1062 |
+func operatingSystem() (*types.OSInfo, error) { |
1063 |
+ info := &types.OSInfo{ |
1064 |
+ Type: "freebsd", |
1065 |
+ Family: "freebsd", |
1066 |
+ Platform: "freebsd", |
1067 |
+ } |
1068 |
+ |
1069 |
+ osType, err := unix.Sysctl("kern.ostype") |
1070 |
+ if err != nil { |
1071 |
+ return info, err |
1072 |
+ } |
1073 |
+ info.Name = osType |
1074 |
+ |
1075 |
+ // Example: 13.0-RELEASE-p11 |
1076 |
+ osRelease, err := unix.Sysctl("kern.osrelease") |
1077 |
+ if err != nil { |
1078 |
+ return info, err |
1079 |
+ } |
1080 |
+ info.Version = osRelease |
1081 |
+ |
1082 |
+ releaseParts := strings.Split(osRelease, "-") |
1083 |
+ |
1084 |
+ majorMinor := strings.Split(releaseParts[0], ".") |
1085 |
+ if len(majorMinor) > 0 { |
1086 |
+ info.Major, _ = strconv.Atoi(majorMinor[0]) |
1087 |
+ } |
1088 |
+ if len(majorMinor) > 1 { |
1089 |
+ info.Minor, _ = strconv.Atoi(majorMinor[1]) |
1090 |
+ } |
1091 |
+ |
1092 |
+ if len(releaseParts) > 1 { |
1093 |
+ info.Build = releaseParts[1] |
1094 |
+ } |
1095 |
+ if len(releaseParts) > 2 { |
1096 |
+ info.Patch, _ = strconv.Atoi(strings.TrimPrefix(releaseParts[2], "p")) |
1097 |
+ } |
1098 |
+ |
1099 |
+ return info, nil |
1100 |
+} |
1101 |
+ |
1102 |
+func totalPhysicalMem(r *reader) uint64 { |
1103 |
+ const mib = "hw.physmem" |
1104 |
+ |
1105 |
+ v, err := unix.SysctlUint64(mib) |
1106 |
+ if r.addErr(err) { |
1107 |
+ return 0 |
1108 |
+ } |
1109 |
+ return v |
1110 |
+} |
1111 |
+ |
1112 |
+func wirePageCount(r *reader) uint64 { |
1113 |
+ const mib = "vm.stats.vm.v_wire_count" |
1114 |
+ |
1115 |
+ v, err := unix.SysctlUint32(mib) |
1116 |
+ if r.addErr(err) { |
1117 |
+ return 0 |
1118 |
+ } |
1119 |
+ return uint64(v) |
1120 |
+} |
1121 |
diff -urN vendor.orig/github.com/elastic/go-sysinfo/providers/freebsd/sysctl_freebsd_test.go vendor/github.com/elastic/go-sysinfo/providers/freebsd/sysctl_freebsd_test.go |
1122 |
--- vendor.orig/github.com/elastic/go-sysinfo/providers/freebsd/sysctl_freebsd_test.go 1970-01-01 01:00:00.000000000 +0100 |
1123 |
+++ vendor/github.com/elastic/go-sysinfo/providers/freebsd/sysctl_freebsd_test.go 2024-07-13 20:28:36.065808000 +0200 |
1124 |
@@ -0,0 +1,137 @@ |
1125 |
+// Licensed to Elasticsearch B.V. under one or more contributor |
1126 |
+// license agreements. See the NOTICE file distributed with |
1127 |
+// this work for additional information regarding copyright |
1128 |
+// ownership. Elasticsearch B.V. licenses this file to you under |
1129 |
+// the Apache License, Version 2.0 (the "License"); you may |
1130 |
+// not use this file except in compliance with the License. |
1131 |
+// You may obtain a copy of the License at |
1132 |
+// |
1133 |
+// http://www.apache.org/licenses/LICENSE-2.0 |
1134 |
+// |
1135 |
+// Unless required by applicable law or agreed to in writing, |
1136 |
+// software distributed under the License is distributed on an |
1137 |
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
1138 |
+// KIND, either express or implied. See the License for the |
1139 |
+// specific language governing permissions and limitations |
1140 |
+// under the License. |
1141 |
+ |
1142 |
+//go:build freebsd |
1143 |
+ |
1144 |
+package freebsd |
1145 |
+ |
1146 |
+import ( |
1147 |
+ "encoding/json" |
1148 |
+ "github.com/stretchr/testify/assert" |
1149 |
+ "github.com/stretchr/testify/require" |
1150 |
+ "os/exec" |
1151 |
+ "strings" |
1152 |
+ "testing" |
1153 |
+ "time" |
1154 |
+) |
1155 |
+ |
1156 |
+func TestArchitecture(t *testing.T) { |
1157 |
+ arch, err := architecture() |
1158 |
+ if err != nil { |
1159 |
+ t.Fatal(err) |
1160 |
+ } |
1161 |
+ |
1162 |
+ assert.NotEmpty(t, arch) |
1163 |
+ assert.Regexp(t, `(amd64|i386|powerpc|arm(64)?|riscv|mips|sparc64|pc98)`, arch) |
1164 |
+} |
1165 |
+ |
1166 |
+func TestBootTime(t *testing.T) { |
1167 |
+ bootTime, err := bootTime() |
1168 |
+ if err != nil { |
1169 |
+ t.Fatal(err) |
1170 |
+ } |
1171 |
+ |
1172 |
+ bootDiff := time.Since(bootTime) |
1173 |
+ // t.Logf("bootTime in seconds: %#v", int64(bootDiff.Seconds())) |
1174 |
+ |
1175 |
+ cmd := exec.Command("/usr/bin/uptime", "--libxo=json") |
1176 |
+ upcmd, err := cmd.Output() |
1177 |
+ |
1178 |
+ if err != nil { |
1179 |
+ t.Fatal(err) |
1180 |
+ } |
1181 |
+ |
1182 |
+ t.Logf(string(upcmd)) |
1183 |
+ |
1184 |
+ type UptimeOutput struct { |
1185 |
+ UptimeInformation struct { |
1186 |
+ Uptime int64 `json:"uptime"` |
1187 |
+ } `json:"uptime-information"` |
1188 |
+ } |
1189 |
+ |
1190 |
+ var upInfo UptimeOutput |
1191 |
+ err = json.Unmarshal(upcmd, &upInfo) |
1192 |
+ |
1193 |
+ if err != nil { |
1194 |
+ t.Fatal(err) |
1195 |
+ } |
1196 |
+ |
1197 |
+ upsec := upInfo.UptimeInformation.Uptime |
1198 |
+ uptime := time.Duration(upsec * int64(time.Second)) |
1199 |
+ // t.Logf("uptime in seconds: %#v", int64(uptime.Seconds())) |
1200 |
+ |
1201 |
+ assert.InDelta(t, uptime, bootDiff, float64(5*time.Second)) |
1202 |
+} |
1203 |
+ |
1204 |
+func TestCPUStateTimes(t *testing.T) { |
1205 |
+ times, err := cpuStateTimes() |
1206 |
+ if err != nil { |
1207 |
+ t.Fatal(err) |
1208 |
+ } |
1209 |
+ |
1210 |
+ require.NotNil(t, times) |
1211 |
+ assert.NotZero(t, *times) |
1212 |
+} |
1213 |
+ |
1214 |
+func TestKernelVersion(t *testing.T) { |
1215 |
+ kernel, err := kernelVersion() |
1216 |
+ if err != nil { |
1217 |
+ t.Fatal(err) |
1218 |
+ } |
1219 |
+ |
1220 |
+ // Retrieve currently running kernel version |
1221 |
+ cmd := exec.Command("/bin/freebsd-version", "-r") |
1222 |
+ fbsdout, err := cmd.Output() |
1223 |
+ |
1224 |
+ if err != nil { |
1225 |
+ t.Fatal(err) |
1226 |
+ } |
1227 |
+ |
1228 |
+ fbsdver := strings.TrimSuffix(string(fbsdout), "\n") |
1229 |
+ |
1230 |
+ assert.NotEmpty(t, kernel) |
1231 |
+ assert.EqualValues(t, kernel, fbsdver) |
1232 |
+} |
1233 |
+ |
1234 |
+func TestMachineID(t *testing.T) { |
1235 |
+ machineID, err := machineID() |
1236 |
+ if err != nil { |
1237 |
+ t.Fatal(err) |
1238 |
+ } |
1239 |
+ |
1240 |
+ assert.NotEmpty(t, machineID) |
1241 |
+ assert.Regexp(t, "^[a-zA-Z0-9]{8}-[a-zA-Z0-9]{4}-[a-zA-Z0-9]{4}-[a-zA-Z0-9]{4}-[a-zA-Z0-9]{12}$", machineID) |
1242 |
+} |
1243 |
+ |
1244 |
+func TestOperatingSystem(t *testing.T) { |
1245 |
+ t.Run("freebsd", func(t *testing.T) { |
1246 |
+ os, err := operatingSystem() |
1247 |
+ if err != nil { |
1248 |
+ t.Fatal(err) |
1249 |
+ } |
1250 |
+ assert.Equal(t, "freebsd", os.Type) |
1251 |
+ assert.Equal(t, "freebsd", os.Family) |
1252 |
+ assert.Equal(t, "freebsd", os.Platform) |
1253 |
+ assert.Equal(t, "FreeBSD", os.Name) |
1254 |
+ assert.Regexp(t, `\d{1,2}\.\d{1,2}-(RELEASE|STABLE|CURRENT|RC[0-9]|ALPHA(\d{0,2})|BETA(\d{0,2}))(-p\d)?`, os.Version) |
1255 |
+ assert.Regexp(t, `\d{1,2}`, os.Major) |
1256 |
+ assert.Regexp(t, `\d{1,2}`, os.Minor) |
1257 |
+ assert.Regexp(t, `\d{1,2}`, os.Patch) |
1258 |
+ assert.Regexp(t, `(RELEASE|STABLE|CURRENT|RC[0-9]|ALPHA([0-9]{0,2})|BETA([0-9]{0,2}))`, os.Build) |
1259 |
+ t.Logf("%#v", os) |
1260 |
+ }) |
1261 |
+} |
1262 |
diff -urN vendor.orig/github.com/elastic/go-sysinfo/providers/freebsd/ztypes_freebsd.go vendor/github.com/elastic/go-sysinfo/providers/freebsd/ztypes_freebsd.go |
1263 |
--- vendor.orig/github.com/elastic/go-sysinfo/providers/freebsd/ztypes_freebsd.go 1970-01-01 01:00:00.000000000 +0100 |
1264 |
+++ vendor/github.com/elastic/go-sysinfo/providers/freebsd/ztypes_freebsd.go 2024-07-13 20:28:36.065887000 +0200 |
1265 |
@@ -0,0 +1,39 @@ |
1266 |
+// Licensed to Elasticsearch B.V. under one or more contributor |
1267 |
+// license agreements. See the NOTICE file distributed with |
1268 |
+// this work for additional information regarding copyright |
1269 |
+// ownership. Elasticsearch B.V. licenses this file to you under |
1270 |
+// the Apache License, Version 2.0 (the "License"); you may |
1271 |
+// not use this file except in compliance with the License. |
1272 |
+// You may obtain a copy of the License at |
1273 |
+// |
1274 |
+// http://www.apache.org/licenses/LICENSE-2.0 |
1275 |
+// |
1276 |
+// Unless required by applicable law or agreed to in writing, |
1277 |
+// software distributed under the License is distributed on an |
1278 |
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
1279 |
+// KIND, either express or implied. See the License for the |
1280 |
+// specific language governing permissions and limitations |
1281 |
+// under the License. |
1282 |
+ |
1283 |
+// Code generated by cmd/cgo -godefs; and then patched up to fix |
1284 |
+// an alignment issue |
1285 |
+// cgo -godefs defs_freebsd.go |
1286 |
+ |
1287 |
+package freebsd |
1288 |
+ |
1289 |
+type kvmSwap struct { |
1290 |
+ Devname [32]int8 |
1291 |
+ Used uint32 |
1292 |
+ Total uint32 |
1293 |
+ Flags int32 |
1294 |
+ Reserved1 uint32 |
1295 |
+ Reserved2 uint32 |
1296 |
+} |
1297 |
+ |
1298 |
+type clockInfo struct { |
1299 |
+ Hz int32 |
1300 |
+ Tick int32 |
1301 |
+ Spare int32 |
1302 |
+ Stathz int32 |
1303 |
+ Profhz int32 |
1304 |
+} |
1305 |
diff -urN vendor.orig/github.com/elastic/go-sysinfo/providers/shared/fqdn.go vendor/github.com/elastic/go-sysinfo/providers/shared/fqdn.go |
1306 |
--- vendor.orig/github.com/elastic/go-sysinfo/providers/shared/fqdn.go 2024-07-13 21:34:29.109657000 +0200 |
1307 |
+++ vendor/github.com/elastic/go-sysinfo/providers/shared/fqdn.go 2024-07-13 20:28:36.077420000 +0200 |
1308 |
@@ -15,7 +15,7 @@ |
1309 |
// specific language governing permissions and limitations |
1310 |
// under the License. |
1311 |
|
1312 |
-//go:build linux || darwin || aix |
1313 |
+//go:build linux || darwin || aix || freebsd |
1314 |
|
1315 |
package shared |
1316 |
|
1317 |
diff -urN vendor.orig/github.com/elastic/go-sysinfo/system.go vendor/github.com/elastic/go-sysinfo/system.go |
1318 |
--- vendor.orig/github.com/elastic/go-sysinfo/system.go 2024-07-13 21:34:29.110051000 +0200 |
1319 |
+++ vendor/github.com/elastic/go-sysinfo/system.go 2024-07-13 20:28:36.079545000 +0200 |
1320 |
@@ -26,6 +26,7 @@ |
1321 |
// Register host and process providers. |
1322 |
_ "github.com/elastic/go-sysinfo/providers/aix" |
1323 |
_ "github.com/elastic/go-sysinfo/providers/darwin" |
1324 |
+ _ "github.com/elastic/go-sysinfo/providers/freebsd" |
1325 |
_ "github.com/elastic/go-sysinfo/providers/linux" |
1326 |
_ "github.com/elastic/go-sysinfo/providers/windows" |
1327 |
) |