-
Notifications
You must be signed in to change notification settings - Fork 93
FreeBSD support #40
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
FreeBSD support #40
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| // Licensed to Elasticsearch B.V. under one or more contributor | ||
| // license agreements. See the NOTICE file distributed with | ||
| // this work for additional information regarding copyright | ||
| // ownership. Elasticsearch B.V. licenses this file to you 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. | ||
|
|
||
| package freebsd | ||
|
|
||
| import ( | ||
| "syscall" | ||
|
|
||
| "github.com/pkg/errors" | ||
| ) | ||
|
|
||
| const hardwareMIB = "hw.machine" | ||
|
|
||
| func Architecture() (string, error) { | ||
| arch, err := syscall.Sysctl(hardwareMIB) | ||
| if err != nil { | ||
| return "", errors.Wrap(err, "failed to get architecture") | ||
| } | ||
|
|
||
| return arch, nil | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| // Licensed to Elasticsearch B.V. under one or more contributor | ||
| // license agreements. See the NOTICE file distributed with | ||
| // this work for additional information regarding copyright | ||
| // ownership. Elasticsearch B.V. licenses this file to you 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. | ||
|
|
||
| package freebsd | ||
|
|
||
| import ( | ||
| "syscall" | ||
| "time" | ||
|
|
||
| "github.com/pkg/errors" | ||
| ) | ||
|
|
||
| const kernBoottimeMIB = "kern.boottime" | ||
|
|
||
| func BootTime() (time.Time, error) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. exported function BootTime should have comment or be unexported |
||
| var tv syscall.Timeval | ||
| if err := sysctlByName(kernBoottimeMIB, &tv); err != nil { | ||
| return time.Time{}, errors.Wrap(err, "failed to get host uptime") | ||
| } | ||
|
|
||
| bootTime := time.Unix(int64(tv.Sec), int64(tv.Usec)*int64(time.Microsecond)) | ||
| return bootTime, nil | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| // Licensed to Elasticsearch B.V. under one or more contributor | ||
| // license agreements. See the NOTICE file distributed with | ||
| // this work for additional information regarding copyright | ||
| // ownership. Elasticsearch B.V. licenses this file to you 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. | ||
|
|
||
| // +build ignore | ||
|
|
||
| package freebsd | ||
|
|
||
| /* | ||
| #include <sys/types.h> | ||
| #include <sys/vmmeter.h> | ||
| #include <sys/time.h> | ||
| #include <kvm.h> | ||
| */ | ||
| import "C" | ||
|
|
||
| type vmTotal C.struct_vmtotal | ||
|
|
||
| type kvmSwap C.struct_kvm_swap | ||
|
|
||
| type clockInfo C.struct_clockinfo |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| // Licensed to Elasticsearch B.V. under one or more contributor | ||
| // license agreements. See the NOTICE file distributed with | ||
| // this work for additional information regarding copyright | ||
| // ownership. Elasticsearch B.V. licenses this file to you 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. | ||
|
|
||
| // Package freebsd implements the HostProvider and ProcessProvider interfaces | ||
| // for providing information about FreeBSD | ||
| package freebsd |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,229 @@ | ||
| // Licensed to Elasticsearch B.V. under one or more contributor | ||
| // license agreements. See the NOTICE file distributed with | ||
| // this work for additional information regarding copyright | ||
| // ownership. Elasticsearch B.V. licenses this file to you 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. | ||
|
|
||
| // +build,freebsd,cgo | ||
|
|
||
| package freebsd | ||
|
|
||
| // #cgo LDFLAGS: -lkvm | ||
| //#include <kvm.h> | ||
| //#include <sys/vmmeter.h> | ||
| import "C" | ||
|
|
||
| import ( | ||
| "os" | ||
| "time" | ||
|
|
||
| "github.com/joeshaw/multierror" | ||
| "github.com/pkg/errors" | ||
|
|
||
| "github.com/elastic/go-sysinfo/internal/registry" | ||
| "github.com/elastic/go-sysinfo/providers/shared" | ||
| "github.com/elastic/go-sysinfo/types" | ||
| ) | ||
|
|
||
| func init() { | ||
| registry.Register(freebsdSystem{}) | ||
| } | ||
|
|
||
| type freebsdSystem struct{} | ||
|
|
||
| func (s freebsdSystem) Host() (types.Host, error) { | ||
| return newHost() | ||
| } | ||
|
|
||
| type host struct { | ||
| info types.HostInfo | ||
| } | ||
|
|
||
| func (h *host) Info() types.HostInfo { | ||
| return h.info | ||
| } | ||
|
|
||
| func (h *host) CPUTime() (types.CPUTimes, error) { | ||
| cpu := types.CPUTimes{} | ||
| r := &reader{} | ||
| r.cpuTime(&cpu) | ||
|
|
||
| return cpu, nil | ||
| } | ||
|
|
||
| func (h *host) Memory() (*types.HostMemoryInfo, error) { | ||
| m := &types.HostMemoryInfo{} | ||
| r := &reader{} | ||
| r.memInfo(m) | ||
| return m, r.Err() | ||
| } | ||
|
|
||
| func newHost() (*host, error) { | ||
| h := &host{} | ||
| r := &reader{} | ||
| r.architecture(h) | ||
| r.bootTime(h) | ||
| r.hostname(h) | ||
| r.network(h) | ||
| r.kernelVersion(h) | ||
| r.os(h) | ||
| r.time(h) | ||
| r.uniqueID(h) | ||
| return h, r.Err() | ||
| } | ||
|
|
||
| type reader struct { | ||
| errs []error | ||
| } | ||
|
|
||
| func (r *reader) addErr(err error) bool { | ||
| if err != nil { | ||
| if errors.Cause(err) != types.ErrNotImplemented { | ||
| r.errs = append(r.errs, err) | ||
| } | ||
| return true | ||
| } | ||
| return false | ||
| } | ||
|
|
||
| func (r *reader) Err() error { | ||
| if len(r.errs) > 0 { | ||
| return &multierror.MultiError{Errors: r.errs} | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| func (r *reader) cpuTime(cpu *types.CPUTimes) { | ||
| cptime, err := Cptime() | ||
|
|
||
| if r.addErr(err) { | ||
| return | ||
| } | ||
|
|
||
| cpu.User = time.Duration(cptime["User"]) | ||
| cpu.System = time.Duration(cptime["System"]) | ||
| cpu.Idle = time.Duration(cptime["Idle"]) | ||
| cpu.Nice = time.Duration(cptime["Nice"]) | ||
| cpu.IRQ = time.Duration(cptime["IRQ"]) | ||
| } | ||
|
|
||
| func (r *reader) memInfo(m *types.HostMemoryInfo) { | ||
| pageSize, err := PageSize() | ||
|
|
||
| if r.addErr(err) { | ||
| return | ||
| } | ||
|
|
||
| totalMemory, err := TotalMemory() | ||
| if r.addErr(err) { | ||
| return | ||
| } | ||
|
|
||
| m.Total = totalMemory | ||
|
|
||
| vm, err := VmTotal() | ||
| if r.addErr(err) { | ||
| return | ||
| } | ||
|
|
||
| m.Free = uint64(vm.Free) * uint64(pageSize) | ||
| m.Used = m.Total - m.Free | ||
|
|
||
| numFreeBuffers, err := NumFreeBuffers() | ||
| if r.addErr(err) { | ||
| return | ||
| } | ||
|
|
||
| m.Available = m.Free + (uint64(numFreeBuffers) * uint64(pageSize)) | ||
|
|
||
| swap, err := KvmGetSwapInfo() | ||
| if r.addErr(err) { | ||
| return | ||
| } | ||
|
|
||
| swapMaxPages, err := SwapMaxPages() | ||
| if r.addErr(err) { | ||
| return | ||
| } | ||
|
|
||
| if swap.Total > swapMaxPages { | ||
| swap.Total = swapMaxPages | ||
| } | ||
|
|
||
| m.VirtualTotal = uint64(swap.Total) * uint64(pageSize) | ||
| m.VirtualUsed = uint64(swap.Used) * uint64(pageSize) | ||
| m.VirtualFree = m.VirtualTotal - m.VirtualUsed | ||
| } | ||
|
|
||
| func (r *reader) architecture(h *host) { | ||
| v, err := Architecture() | ||
| if r.addErr(err) { | ||
| return | ||
| } | ||
| h.info.Architecture = v | ||
| } | ||
|
|
||
| func (r *reader) bootTime(h *host) { | ||
| v, err := BootTime() | ||
| if r.addErr(err) { | ||
| return | ||
| } | ||
|
|
||
| h.info.BootTime = v | ||
| } | ||
|
|
||
| func (r *reader) hostname(h *host) { | ||
| v, err := os.Hostname() | ||
| if r.addErr(err) { | ||
| return | ||
| } | ||
| h.info.Hostname = v | ||
| } | ||
|
|
||
| func (r *reader) network(h *host) { | ||
| ips, macs, err := shared.Network() | ||
| if r.addErr(err) { | ||
| return | ||
| } | ||
| h.info.IPs = ips | ||
| h.info.MACs = macs | ||
| } | ||
|
|
||
| func (r *reader) kernelVersion(h *host) { | ||
| v, err := KernelVersion() | ||
| if r.addErr(err) { | ||
| return | ||
| } | ||
| h.info.KernelVersion = v | ||
| } | ||
|
|
||
| func (r *reader) os(h *host) { | ||
| v, err := OperatingSystem() | ||
| if r.addErr(err) { | ||
| return | ||
| } | ||
| h.info.OS = v | ||
| } | ||
|
|
||
| func (r *reader) time(h *host) { | ||
| h.info.Timezone, h.info.TimezoneOffsetSec = time.Now().Zone() | ||
| } | ||
|
|
||
| func (r *reader) uniqueID(h *host) { | ||
| v, err := MachineID() | ||
| if r.addErr(err) { | ||
| return | ||
| } | ||
| h.info.UniqueID = v | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| // Licensed to Elasticsearch B.V. under one or more contributor | ||
| // license agreements. See the NOTICE file distributed with | ||
| // this work for additional information regarding copyright | ||
| // ownership. Elasticsearch B.V. licenses this file to you 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. | ||
|
|
||
| package freebsd | ||
|
|
||
| import ( | ||
| "syscall" | ||
|
|
||
| "github.com/pkg/errors" | ||
| ) | ||
|
|
||
| const kernelReleaseMIB = "kern.osrelease" | ||
|
|
||
| func KernelVersion() (string, error) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. exported function KernelVersion should have comment or be unexported |
||
| version, err := syscall.Sysctl(kernelReleaseMIB) | ||
| if err != nil { | ||
| return "", errors.Wrap(err, "failed to get kernel version") | ||
| } | ||
|
|
||
| return version, nil | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
exported function Architecture should have comment or be unexported